forked from batra-mlp-lab/visdial-challenge-starter-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
175 lines (146 loc) · 6.43 KB
/
evaluate.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
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
import argparse
import datetime
import gc
import json
import math
import os
from tqdm import tqdm
import torch
from torch.autograd import Variable
from torch.utils.data import DataLoader
from dataloader import VisDialDataset
from encoders import Encoder
from decoders import Decoder
from utils import process_ranks, scores_to_ranks, get_gt_ranks
parser = argparse.ArgumentParser()
VisDialDataset.add_cmdline_args(parser)
parser.add_argument_group('Evaluation related arguments')
parser.add_argument('-load_path', default='checkpoints/model.pth',
help='Checkpoint to load path from')
parser.add_argument('-split', default='val', choices=['val', 'test'],
help='Split to evaluate on')
parser.add_argument('-use_gt', action='store_true',
help='Whether to use ground truth for retrieving ranks')
parser.add_argument('-batch_size', default=12, type=int, help='Batch size')
parser.add_argument('-gpuid', default=0, type=int, help='GPU id to use')
parser.add_argument('-overfit', action='store_true',
help='Use a batch of only 5 examples, useful for debugging')
parser.add_argument_group('Submission related arguments')
parser.add_argument('-save_ranks', action='store_true',
help='Whether to save retrieved ranks')
parser.add_argument('-save_path', default='logs/ranks.json',
help='Path of json file to save ranks')
# ----------------------------------------------------------------------------
# input arguments and options
# ----------------------------------------------------------------------------
args = parser.parse_args()
if args.use_gt:
if args.split == 'test':
print("Warning: No ground truth for test split, changing use_gt to False.")
args.use_gt = False
elif args.split == 'val' and args.save_ranks:
print("Warning: Cannot generate submission json if use_gt is True.")
args.save_ranks = False
# seed for reproducibility
torch.manual_seed(1234)
# set device and default tensor type
if args.gpuid >= 0:
torch.cuda.manual_seed_all(1234)
torch.cuda.set_device(args.gpuid)
# ----------------------------------------------------------------------------
# read saved model and args
# ----------------------------------------------------------------------------
components = torch.load(args.load_path)
model_args = components['model_args']
model_args.gpuid = args.gpuid
model_args.batch_size = args.batch_size
# this is required by dataloader
args.img_norm = model_args.img_norm
# set this because only late fusion encoder is supported yet
args.concat_history = True
for arg in vars(args):
print('{:<20}: {}'.format(arg, getattr(args, arg)))
# ----------------------------------------------------------------------------
# loading dataset wrapping with a dataloader
# ----------------------------------------------------------------------------
dataset = VisDialDataset(args, [args.split])
dataloader = DataLoader(dataset,
batch_size=args.batch_size,
shuffle=False,
collate_fn=dataset.collate_fn)
# iterations per epoch
setattr(args, 'iter_per_epoch',
math.ceil(dataset.num_data_points[args.split] / args.batch_size))
print("{} iter per epoch.".format(args.iter_per_epoch))
# ----------------------------------------------------------------------------
# setup the model
# ----------------------------------------------------------------------------
encoder = Encoder(model_args)
encoder.load_state_dict(components['encoder'])
decoder = Decoder(model_args, encoder)
decoder.load_state_dict(components['decoder'])
print("Loaded model from {}".format(args.load_path))
if args.gpuid >= 0:
encoder = encoder.cuda()
decoder = decoder.cuda()
# ----------------------------------------------------------------------------
# evaluation
# ----------------------------------------------------------------------------
print("Evaluation start time: {}".format(
datetime.datetime.strftime(datetime.datetime.utcnow(), '%d-%b-%Y-%H:%M:%S')))
encoder.eval()
decoder.eval()
if args.use_gt:
# ------------------------------------------------------------------------
# calculate automatic metrics and finish
# ------------------------------------------------------------------------
all_ranks = []
for i, batch in enumerate(tqdm(dataloader)):
for key in batch:
if not isinstance(batch[key], list):
batch[key] = Variable(batch[key], volatile=True)
if args.gpuid >= 0:
batch[key] = batch[key].cuda()
enc_out = encoder(batch)
dec_out = decoder(enc_out, batch)
ranks = scores_to_ranks(dec_out.data)
gt_ranks = get_gt_ranks(ranks, batch['ans_ind'].data)
all_ranks.append(gt_ranks)
all_ranks = torch.cat(all_ranks, 0)
process_ranks(all_ranks)
gc.collect()
else:
# ------------------------------------------------------------------------
# prepare json for submission
# ------------------------------------------------------------------------
ranks_json = []
for i, batch in enumerate(tqdm(dataloader)):
for key in batch:
if not isinstance(batch[key], list):
batch[key] = Variable(batch[key], volatile=True)
if args.gpuid >= 0:
batch[key] = batch[key].cuda()
enc_out = encoder(batch)
dec_out = decoder(enc_out, batch)
ranks = scores_to_ranks(dec_out.data)
ranks = ranks.view(-1, 10, 100)
for i in range(len(batch['img_fnames'])):
# cast into types explicitly to ensure no errors in schema
if args.split == 'test':
ranks_json.append({
'image_id': int(batch['img_fnames'][i][-16:-4]),
'round_id': int(batch['num_rounds'][i]),
'ranks': list(ranks[i][batch['num_rounds'][i] - 1])
})
else:
for j in range(batch['num_rounds'][i]):
ranks_json.append({
'image_id': int(batch['img_fnames'][i][-16:-4]),
'round_id': int(j + 1),
'ranks': list(ranks[i][j])
})
gc.collect()
if args.save_ranks:
print("Writing ranks to {}".format(args.save_path))
os.makedirs(os.path.dirname(args.save_path), exist_ok=True)
json.dump(ranks_json, open(args.save_path, 'w'))