-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathevaluate_recall_coco.py
72 lines (58 loc) · 2.13 KB
/
evaluate_recall_coco.py
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
"""
PCME
Copyright (c) 2021-present NAVER Corp.
MIT license
"""
import datetime
import fire
import torch
from logger import PythonLogger
from config import parse_config
from datasets import prepare_coco_dataloaders
from engine import TrainerEngine
from engine import COCOEvaluator
@torch.no_grad()
def evaluate(config, model_path, dataloader, vocab, logger, n_crossfolds):
logger.log('start evaluation')
engine = TrainerEngine()
engine.set_logger(logger)
config.model.img_finetune = False
config.model.txt_finetune = False
evaluator = COCOEvaluator(eval_method='matching_prob',
verbose=True,
eval_device='cuda',
n_crossfolds=n_crossfolds)
engine.create(config, vocab.word2idx, evaluator)
engine.load_models(model_path,
load_keys=['model', 'criterion'])
scores = engine.evaluate(val_loaders=dataloader,
eval_batch_size=config.dataloader.eval_batch_size)
logger.pretty_log_dict(scores)
return scores
def main(config_path,
dataset_root,
model_path,
n_crossfolds,
split='te',
vocab_path='datasets/vocabs/coco_vocab.pkl',
cache_dir='/home/.cache/torch/checkpoints',
dump_to=None,
**kwargs):
if n_crossfolds not in {-1, 5}:
raise ValueError(f'n_crossfolds should be in (-1, 5) not {n_crossfolds}')
dt = datetime.datetime.now()
config = parse_config(config_path,
strict_cast=False,
model__cache_dir=cache_dir,
**kwargs)
logger = PythonLogger()
logger.log('preparing data loaders..')
dataloaders, vocab = prepare_coco_dataloaders(config.dataloader,
dataset_root, vocab_path)
dataloader = dataloaders[split]
scores = evaluate(config, model_path, dataloader, vocab, logger, n_crossfolds)
if dump_to:
torch.save(scores, dump_to)
logger.log('takes {}'.format(datetime.datetime.now() - dt))
if __name__ == '__main__':
fire.Fire(main)