예제 #1
0
def get_netvlad():
    checkpoint_path = Path(
        EXPER_PATH,
        'vd16_pitts30k_conv5_3_vlad_preL2_intra_white/vd16_pitts30k_conv5_3_vlad_preL2_intra_white'
    )
    config = {
        'checkpoint_path':
        checkpoint_path,
        'data': {
            'name': 'aachen',
            'load_db': False,
            'load_queries': True,
            'resize_max': 960
        },
        'model': {
            'name': 'netvlad_original',
            'local_descriptor_layer': 'conv3_3',
            'image_channels': 1
        },
        'weights':
        'vd16_pitts30k_conv5_3_vlad_preL2_intra_white/vd16_pitts30k_conv5_3_vlad_preL2_intra_white'
    }

    net = get_model(config['model']['name'])(data_shape={
        'image': [None, None, None, 3]
    },
                                             **config['model'])
    net.load(str(checkpoint_path))
    return net
def _init_graph(config, with_dataset=False):
    set_seed(config.get('seed', int.from_bytes(os.urandom(4), byteorder='big')))
    n_gpus = len(os.environ['CUDA_VISIBLE_DEVICES'].split(','))
    logging.info('Number of GPUs detected: {}'.format(n_gpus))

    dataset = get_dataset(config['data']['name'])(**config['data'])
    model = get_model(config['model']['name'])(
            data=dataset.get_tf_datasets(), n_gpus=n_gpus, **config['model'])
    model.__enter__()
    if with_dataset:
        yield model, dataset
    else:
        yield model
    model.__exit__()
    tf.reset_default_graph()
        with open(Path(EXPER_PATH, exper_name, 'config.yaml'), 'r') as f:
            config['model'] = tools.dict_update(
                yaml.load(f)['model'], config.get('model', {}))
        checkpoint_path = Path(EXPER_PATH, exper_name)
        if config.get('weights', None):
            checkpoint_path = Path(checkpoint_path, config['weights'])
    else:
        if config.get('weights', None):
            checkpoint_path = Path(DATA_PATH, 'weights', config['weights'])
        else:
            checkpoint_path = None
            logging.info('No weights provided.')
    logging.info(f'Starting export with configuration:\n{pformat(config)}')

    with get_model(config['model']['name'])(data_shape={
            'image': [None, None, None, config['model']['image_channels']]
    },
                                            **config['model']) as net:
        if checkpoint_path is not None:
            net.load(str(checkpoint_path))
        dataset = get_dataset(config['data']['name'])(**config['data'])
        test_set = dataset.get_test_set()

        for data in tqdm(test_set):
            predictions = net.predict(data, keys=keys)
            predictions['input_shape'] = data['image'].shape
            name = data['name'].decode('utf-8')
            Path(base_dir,
                 Path(name).parent).mkdir(parents=True, exist_ok=True)
            np.savez(Path(base_dir, '{}.npz'.format(name)), **predictions)