-
Notifications
You must be signed in to change notification settings - Fork 17
/
evaluate_cub.py
64 lines (52 loc) · 1.88 KB
/
evaluate_cub.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
"""
PCME
Copyright (c) 2021-present NAVER Corp.
MIT license
"""
import fire
import torch
from logger import PythonLogger
from config import parse_config
from datasets import prepare_cub_dataloaders
from engine import TrainerEngine
from engine import CUBEvaluator
@torch.no_grad()
def evaluate(config, dataset_name, model_path, dataloader, vocab, logger):
logger.log('start evaluation')
engine = TrainerEngine()
engine.set_logger(logger)
evaluator = CUBEvaluator(eval_method=config.model.get('eval_method', 'matmul'),
verbose=True,
eval_device='cuda')
engine.create(config, vocab.word2idx, evaluator)
engine.load_models(model_path,
load_keys=['model', 'criterion'])
scores = engine.evaluate(val_loaders=dataloader)
logger.pretty_log_dict(scores)
return scores
def main(config_path,
dataset_root,
caption_root,
model_path,
dataset_name='cub',
split='val',
vocab_path='datasets/vocabs/cub_vocab.pkl',
cache_dir='/home/.cache/torch/checkpoints',
dump_to=None,
**kwargs):
config = parse_config(config_path,
strict_cast=False,
model__cache_dir=cache_dir,
**kwargs)
logger = PythonLogger()
logger.log('preparing data loaders..')
dataloaders, vocab = prepare_cub_dataloaders(config.dataloader,
dataset_name,
dataset_root,
caption_root,
vocab_path)
scores = evaluate(config, dataset_name, model_path, dataloaders[split], vocab, logger)
if dump_to:
torch.save(scores, dump_to)
if __name__ == '__main__':
fire.Fire(main)