File size: 15,368 Bytes
32408ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""Implements evaluation of trained models"""

import time
import warnings
from pathlib import Path
import pickle
import numpy as np
import torch
from torchvision import transforms
from PIL import ImageFile

from cirtorch.datasets.genericdataset import ImagesFromList

from asmk import asmk_method, kernel as kern_pkg
from ..networks import how_net
from ..utils import score_helpers, data_helpers, logging

ImageFile.LOAD_TRUNCATED_IMAGES = True
warnings.filterwarnings("ignore", r"^Possibly corrupt EXIF data", category=UserWarning)


def evaluate_demo(demo_eval, evaluation, globals):
    """Demo evaluating a trained network

    :param dict demo_eval: Demo-related options
    :param dict evaluation: Evaluation-related options
    :param dict globals: Global options
    """
    globals["device"] = torch.device("cpu")
    if demo_eval['gpu_id'] is not None:
        globals["device"] = torch.device(("cuda:%s" % demo_eval['gpu_id']))

    # Handle net_path when directory
    net_path = Path(demo_eval['exp_folder']) / demo_eval['net_path']
    if net_path.is_dir() and (net_path / "epochs/model_best.pth").exists():
        net_path = net_path / "epochs/model_best.pth"

    # Load net
    state = _convert_checkpoint(torch.load(net_path, map_location='cpu'))
    net = how_net.init_network(**state['net_params']).to(globals['device'])
    net.load_state_dict(state['state_dict'])
    globals["transform"] = transforms.Compose([transforms.ToTensor(), \
                transforms.Normalize(**dict(zip(["mean", "std"], net.runtime['mean_std'])))])

    # Eval
    if evaluation['global_descriptor']['datasets']:
        eval_global(net, evaluation['inference'], globals, **evaluation['global_descriptor'])

    if evaluation['multistep']:
        eval_asmk_multistep(net, evaluation['inference'], evaluation['multistep'], globals, **evaluation['local_descriptor'])
    elif evaluation['local_descriptor']['datasets']:
        eval_asmk(net, evaluation['inference'], globals, **evaluation['local_descriptor'])


def eval_global(net, inference, globals, *, datasets):
    """Evaluate global descriptors"""
    net.eval()
    time0 = time.time()
    logger = globals["logger"]
    logger.info("Starting global evaluation")

    results = {}
    for dataset in datasets:
        images, qimages, bbxs, gnd = data_helpers.load_dataset(dataset, data_root=globals['root_path'])
        logger.info(f"Evaluating {dataset}")

        with logging.LoggingStopwatch("extracting database images", logger.info, logger.debug):
            dset = ImagesFromList(root='', images=images, imsize=inference['image_size'], bbxs=None,
                                  transform=globals['transform'])
            vecs = how_net.extract_vectors(net, dset, globals["device"], scales=inference['scales'])
        with logging.LoggingStopwatch("extracting query images", logger.info, logger.debug):
            qdset = ImagesFromList(root='', images=qimages, imsize=inference['image_size'], bbxs=bbxs,
                                   transform=globals['transform'])
            qvecs = how_net.extract_vectors(net, qdset, globals["device"], scales=inference['scales'])

        vecs, qvecs = vecs.numpy(), qvecs.numpy()
        ranks = np.argsort(-np.dot(vecs, qvecs.T), axis=0)
        results[dataset] = score_helpers.compute_map_and_log(dataset, ranks, gnd, logger=logger)

    logger.info(f"Finished global evaluation in {int(time.time()-time0) // 60} min")
    return results


def eval_asmk(net, inference, globals, *, datasets, codebook_training, asmk):
    """Evaluate local descriptors with ASMK"""
    net.eval()
    time0 = time.time()
    logger = globals["logger"]
    logger.info("Starting asmk evaluation")

    asmk = asmk_method.ASMKMethod.initialize_untrained(asmk)
    asmk = asmk_train_codebook(net, inference, globals, logger, codebook_training=codebook_training,
                               asmk=asmk, cache_path=None)

    results = {}
    for dataset in datasets:
        dataset_name = dataset if isinstance(dataset, str) else dataset['name']
        images, qimages, bbxs, gnd = data_helpers.load_dataset(dataset, data_root=globals['root_path'])
        logger.info(f"Evaluating '{dataset_name}'")

        asmk_dataset = asmk_index_database(net, inference, globals, logger, asmk=asmk, images=images)
        asmk_query_ivf(net, inference, globals, logger, dataset=dataset, asmk_dataset=asmk_dataset,
                       qimages=qimages, bbxs=bbxs, gnd=gnd, results=results,
                       cache_path=globals["exp_path"] / "query_results.pkl")

    logger.info(f"Finished asmk evaluation in {int(time.time()-time0) // 60} min")
    return results


def eval_asmk_multistep(net, inference, multistep, globals, *, datasets, codebook_training, asmk):
    """Evaluate local descriptors with ASMK"""
    valid_steps = ["train_codebook", "aggregate_database", "build_ivf", "query_ivf", "aggregate_build_query"]
    assert multistep['step'] in valid_steps, multistep['step']

    net.eval()
    time0 = time.time()
    logger = globals["logger"]
    (globals["exp_path"] / "eval").mkdir(exist_ok=True)
    logger.info(f"Starting asmk evaluation step '{multistep['step']}'")

    # Handle partitioning
    partition = {"suffix": "", "norm_start": 0, "norm_end": 1}
    if multistep.get("partition"):
        total, index = multistep['partition']
        partition = {"suffix": f":{total}_{str(index).zfill(len(str(total-1)))}",
                     "norm_start": index / total,
                     "norm_end": (index+1) / total}
        if multistep['step'] == "aggregate_database" or multistep['step'] == "query_ivf":
            logger.info(f"Processing partition '{total}_{index}'")

    # Handle distractors
    distractors_path = None
    distractors = multistep.get("distractors")
    if distractors:
        distractors_path = globals["exp_path"] / f"eval/{distractors}.ivf.pkl"

    # Train codebook
    asmk = asmk_method.ASMKMethod.initialize_untrained(asmk)
    cdb_path = globals["exp_path"] / "eval/codebook.pkl"
    if multistep['step'] == "train_codebook":
        asmk_train_codebook(net, inference, globals, logger, codebook_training=codebook_training,
                            asmk=asmk, cache_path=cdb_path)
        return None

    asmk = asmk.train_codebook(None, cache_path=cdb_path)

    results = {}
    for dataset in datasets:
        dataset_name = database_name = dataset if isinstance(dataset, str) else dataset['name']
        if distractors and multistep['step'] != "aggregate_database":
            dataset_name = f"{distractors}_{database_name}"
        images, qimages, bbxs, gnd = data_helpers.load_dataset(dataset, data_root=globals['root_path'])
        logger.info(f"Processing dataset '{dataset_name}'")

        # Infer database
        if multistep['step'] == "aggregate_database":
            agg_path = globals["exp_path"] / f"eval/{database_name}.agg{partition['suffix']}.pkl"
            asmk_aggregate_database(net, inference, globals, logger, asmk=asmk, images=images,
                                    partition=partition, cache_path=agg_path)

        # Build ivf
        elif multistep['step'] == "build_ivf":
            ivf_path = globals["exp_path"] / f"eval/{dataset_name}.ivf.pkl"
            asmk_build_ivf(globals, logger, asmk=asmk, cache_path=ivf_path, database_name=database_name,
                           distractors=distractors, distractors_path=distractors_path)

        # Query ivf
        elif multistep['step'] == "query_ivf":
            asmk_dataset = asmk.build_ivf(None, None, cache_path=globals["exp_path"] / f"eval/{dataset_name}.ivf.pkl")
            start, end = int(len(qimages)*partition['norm_start']), int(len(qimages)*partition['norm_end'])
            bbxs = bbxs[start:end] if bbxs is not None else None
            results_path = globals["exp_path"] / f"eval/{dataset_name}.results{partition['suffix']}.pkl"
            asmk_query_ivf(net, inference, globals, logger, dataset=dataset, asmk_dataset=asmk_dataset,
                           qimages=qimages[start:end], bbxs=bbxs, gnd=gnd, results=results,
                           cache_path=results_path, imid_offset=start)

        # All 3 dataset steps
        elif multistep['step'] == "aggregate_build_query":
            if multistep.get("partition"):
                raise NotImplementedError("Partitions within step 'aggregate_build_query' are not" \
                                          " supported, use separate steps")
            results_path = globals["exp_path"] / "query_results.pkl"
            if gnd is None and results_path.exists():
                logger.debug("Step results already exist")
                continue
            asmk_dataset = asmk_index_database(net, inference, globals, logger, asmk=asmk, images=images,
                                               distractors_path=distractors_path)
            asmk_query_ivf(net, inference, globals, logger, dataset=dataset, asmk_dataset=asmk_dataset,
                           qimages=qimages, bbxs=bbxs, gnd=gnd, results=results, cache_path=results_path)

    logger.info(f"Finished asmk evaluation step '{multistep['step']}' in {int(time.time()-time0) // 60} min")
    return results

#
# Separate steps
#

def asmk_train_codebook(net, inference, globals, logger, *, codebook_training, asmk, cache_path):
    """Asmk evaluation step 'train_codebook'"""
    if cache_path and cache_path.exists():
        return asmk.train_codebook(None, cache_path=cache_path)

    images = data_helpers.load_dataset('train', data_root=globals['root_path'])[0]
    images = images[:codebook_training['images']]
    dset = ImagesFromList(root='', images=images, imsize=inference['image_size'], bbxs=None,
                          transform=globals['transform'])
    infer_opts = {"scales": codebook_training['scales'], "features_num": inference['features_num']}
    des_train = how_net.extract_vectors_local(net, dset, globals["device"], **infer_opts)[0]
    asmk = asmk.train_codebook(des_train, cache_path=cache_path)
    logger.info(f"Codebook trained in {asmk.metadata['train_codebook']['train_time']:.1f}s")
    return asmk

def asmk_aggregate_database(net, inference, globals, logger, *, asmk, images, partition, cache_path):
    """Asmk evaluation step 'aggregate_database'"""
    if cache_path.exists():
        logger.debug("Step results already exist")
        return
    codebook = asmk.codebook
    kernel = kern_pkg.ASMKKernel(codebook, **asmk.params['build_ivf']['kernel'])
    start, end = int(len(images)*partition['norm_start']), int(len(images)*partition['norm_end'])
    data_opts = {"imsize": inference['image_size'], "transform": globals['transform']}
    infer_opts = {"scales": inference['scales'], "features_num": inference['features_num']}
    # Aggregate database
    dset = ImagesFromList(root='', images=images[start:end], bbxs=None, **data_opts)
    vecs, imids, *_ = how_net.extract_vectors_local(net, dset, globals["device"], **infer_opts)
    imids += start
    quantized = codebook.quantize(vecs, imids, **asmk.params["build_ivf"]["quantize"])
    aggregated = kernel.aggregate(*quantized, **asmk.params["build_ivf"]["aggregate"])
    with cache_path.open("wb") as handle:
        pickle.dump(dict(zip(["des", "word_ids", "image_ids"], aggregated)), handle)

def asmk_build_ivf(globals, logger, *, asmk, cache_path, database_name, distractors, distractors_path):
    """Asmk evaluation step 'build_ivf'"""
    if cache_path.exists():
        logger.debug("Step results already exist")
        return asmk.build_ivf(None, None, cache_path=cache_path)
    builder = asmk.create_ivf_builder(cache_path=cache_path)
    # Build ivf
    if not builder.loaded_from_cache:
        if distractors:
            builder.initialize_with_distractors(distractors_path)
            logger.debug(f"Loaded ivf with distractors '{distractors}'")
        for path in sorted(globals["exp_path"].glob(f"eval/{database_name}.agg*.pkl")):
            with path.open("rb") as handle:
                des = pickle.load(handle)
            builder.ivf.add(des['des'], des['word_ids'], des['image_ids'])
            logger.info(f"Indexed '{path.name}'")
    asmk_dataset = asmk.add_ivf_builder(builder)
    logger.debug(f"IVF stats: {asmk_dataset.metadata['build_ivf']['ivf_stats']}")
    return asmk_dataset

def asmk_index_database(net, inference, globals, logger, *, asmk, images, distractors_path=None):
    """Asmk evaluation step 'aggregate_database' and 'build_ivf'"""
    data_opts = {"imsize": inference['image_size'], "transform": globals['transform']}
    infer_opts = {"scales": inference['scales'], "features_num": inference['features_num']}
    # Index database vectors
    dset = ImagesFromList(root='', images=images, bbxs=None, **data_opts)
    vecs, imids, *_ = how_net.extract_vectors_local(net, dset, globals["device"], **infer_opts)
    asmk_dataset = asmk.build_ivf(vecs, imids, distractors_path=distractors_path)
    logger.info(f"Indexed images in {asmk_dataset.metadata['build_ivf']['index_time']:.2f}s")
    logger.debug(f"IVF stats: {asmk_dataset.metadata['build_ivf']['ivf_stats']}")
    return asmk_dataset

def asmk_query_ivf(net, inference, globals, logger, *, dataset, asmk_dataset, qimages, bbxs, gnd,
                   results, cache_path, imid_offset=0):
    """Asmk evaluation step 'query_ivf'"""
    if gnd is None and cache_path and cache_path.exists():
        logger.debug("Step results already exist")
        return
    data_opts = {"imsize": inference['image_size'], "transform": globals['transform']}
    infer_opts = {"scales": inference['scales'], "features_num": inference['features_num']}
    # Query vectors
    qdset = ImagesFromList(root='', images=qimages, bbxs=bbxs, **data_opts)
    qvecs, qimids, *_ = how_net.extract_vectors_local(net, qdset, globals["device"], **infer_opts)
    qimids += imid_offset
    metadata, query_ids, ranks, scores = asmk_dataset.query_ivf(qvecs, qimids)
    logger.debug(f"Average query time (quant+aggr+search) is {metadata['query_avg_time']:.3f}s")
    # Evaluate
    if gnd is not None:
        results[dataset] = score_helpers.compute_map_and_log(dataset, ranks.T, gnd, logger=logger)
    with cache_path.open("wb") as handle:
        pickle.dump({"metadata": metadata, "query_ids": query_ids, "ranks": ranks, "scores": scores}, handle)

#
# Helpers
#

def _convert_checkpoint(state):
    """Enable loading checkpoints in the old format"""
    if "_version" not in state:
        # Old checkpoint format
        meta = state['meta']
        state['net_params'] = {
            "architecture": meta['architecture'],
            "pretrained": True,
            "skip_layer": meta['skip_layer'],
            "dim_reduction": {"dim": meta["dim"]},
            "smoothing": {"kernel_size": meta["feat_pool_k"]},
            "runtime": {
                "mean_std": [meta['mean'], meta['std']],
                "image_size": 1024,
                "features_num": 1000,
                "scales": [2.0, 1.414, 1.0, 0.707, 0.5, 0.353, 0.25],
                "training_scales": [1],
            },
        }

        state_dict = state['state_dict']
        state_dict['dim_reduction.weight'] = state_dict.pop("whiten.weight")
        state_dict['dim_reduction.bias'] = state_dict.pop("whiten.bias")

        state['_version'] = "how/2020"

    return state