-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval_utils.py
266 lines (221 loc) · 11.1 KB
/
eval_utils.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
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
# encoding=utf8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import importlib
importlib.reload(sys)
####
# reload(sys)
# sys.setdefaultencoding('utf8')
import os
import json
import hashlib
import pandas as pd
import time
from vist_eval.album_eval import AlbumEvaluator
import logging
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
from torch.utils.data import DataLoader
import misc.utils as utils
class CocoResFormat:
def __init__(self):
self.res = []
self.caption_dict = {}
def read_multiple_files(self, filelist, hash_img_name):
for filename in filelist:
# print('In file %s\n' % filename)
self.read_file(filename, hash_img_name)
def read_file(self, filename, hash_img_name):
count = 0
with open(filename, 'r') as opfd:
for line in opfd:
count += 1
id_sent = line.split('\t')
if len(id_sent) > 2:
id_sent = id_sent[-2:]
assert len(id_sent) == 2
sent = id_sent[1].strip()
if hash_img_name:
img_id = int(int(hashlib.sha256(id_sent[0].encode('utf8')).hexdigest(),
16) % sys.maxsize)
else:
img_id = id_sent[0]
imgid_sent = {}
if img_id in self.caption_dict:
print(img_id)
assert self.caption_dict[img_id] == sent
else:
self.caption_dict[img_id] = sent
imgid_sent['image_id'] = img_id
imgid_sent['caption'] = sent
self.res.append(imgid_sent)
def dump_json(self, outfile):
with open(outfile, 'w') as fd:
json.dump(self.res, fd, ensure_ascii=False, sort_keys=True,
indent=2, separators=(',', ': '))
class Evaluator:
def __init__(self, opt, mode='val'):
if opt.task == 'story_telling' or opt.task == 'story_telling_with_caption':
ref_json_path = "data/reference/{}_reference.json".format(mode)
else:
ref_json_path = "data/reference/{}_desc_reference.json".format(mode)
self.reference = json.load(open(ref_json_path))
print("loading file {}".format(ref_json_path))
self.save_dir = os.path.join(opt.checkpoint_path, opt.id)
self.prediction_file = os.path.join(self.save_dir, 'prediction_{}'.format(mode))
self.eval = AlbumEvaluator()
def measure(self):
json_prediction_file = '{}.json'.format(self.prediction_file)
predictions = {}
with open(self.prediction_file) as f:
for line in f:
vid, seq = line.strip().split('\t')
if vid not in predictions:
predictions[vid] = [seq]
self.eval.evaluate(self.reference, predictions)
with open(json_prediction_file, 'w') as f:
json.dump(predictions, f)
return self.eval.eval_overall
def eval_story(self, model, crit, dataset, loader, opt, side_model=None):
# Make sure in the evaluation mode
logging.info("Evaluating...")
start = time.time()
model.eval()
dataset.val()
loss_sum = 0
loss_evals = 0
predictions = {}
prediction_txt = open(self.prediction_file, 'w') # open the file to store the predictions
count = 0
for iter, batch in enumerate(loader):
iter_start = time.time()
feature_fc = Variable(batch['feature_fc'], volatile=True).cuda()
target = Variable(batch['split_story'], volatile=True).cuda()
conv_feature = Variable(batch['feature_conv'], volatile=True).cuda() if 'feature_conv' in batch else None
count += feature_fc.size(0)
if side_model is not None:
story, _ = side_model.predict(feature_fc.view(-1, feature_fc.shape[2]), 1)
story = Variable(story).cuda()
if conv_feature is not None:
output = model(feature_fc, target, story, conv_feature)
else:
output = model(feature_fc, target, story)
else:
if conv_feature is not None:
output = model(feature_fc, target, conv_feature)
else:
output = model(feature_fc, target)
loss = crit(output, target).data[0]
loss_sum += loss
loss_evals += 1
# forward the model to also get generated samples for each video
if side_model is not None:
if conv_feature is not None:
results, _ = model.decode(feature_fc, story, conv_feature, beam_size=opt.beam_size)
else:
results, _ = model.decode(feature_fc, conv_feature, beam_size=opt.beam_size)
else:
if conv_feature is not None:
results, _ = model.decode(feature_fc, conv_feature, beam_size=opt.beam_size)
else:
results, _ = model.decode(feature_fc, beam_size=opt.beam_size)
stories = utils.decode_story(dataset.get_vocab(), results)
indexes = batch['index'].numpy()
for j, story in enumerate(stories):
vid, _ = dataset.get_id(indexes[j])
if vid not in predictions: # only predict one story for an album
# write into txt file for evaluate metrics like Cider
prediction_txt.write('{}\t {}\n'.format(vid, story))
# save into predictions
predictions[vid] = story
logging.info("Evaluate iter {}/{} {:04.2f}%. Time used: {}".format(iter,
len(loader),
iter * 100.0 / len(loader),
time.time() - iter_start))
prediction_txt.close()
metrics = self.measure() # compute all the language metrics
# Switch back to training mode
model.train()
dataset.train()
logging.info("Evaluation finished. Evaluated {} samples. Time used: {}".format(count, time.time() - start))
return loss_sum / loss_evals, predictions, metrics
def test_story(self, model, dataset, loader, opt):
logging.info("Evaluating...")
start = time.time()
model.eval()
dataset.test()
predictions = {}
prediction_txt = open(self.prediction_file, 'w') # open the file to store the predictions
for iter, batch in enumerate(loader):
iter_start = time.time()
feature_fc = Variable(batch['feature_fc'], volatile=True).cuda()
feature_conv = Variable(batch['feature_conv'], volatile=True).cuda() if 'feature_conv' in batch else None
if feature_conv is not None:
results, _ = model.predict(feature_fc, feature_conv, beam_size=opt.beam_size)
else:
results, _ = model.predict(feature_fc, beam_size=opt.beam_size)
sents = utils.decode_story(dataset.get_vocab(), results)
indexes = batch['index'].numpy()
for j, story in enumerate(sents):
vid, _ = dataset.get_id(indexes[j])
if vid not in predictions: # only predict one story for an album
# write into txt file for evaluate metrics like Cider
prediction_txt.write('{}\t {}\n'.format(vid, story))
# save into predictions
predictions[vid] = story
print("Evaluate iter {}/{} {:04.2f}%. Time used: {}".format(iter,
len(loader),
iter * 100.0 / len(loader),
time.time() - iter_start))
prediction_txt.close()
metrics = self.measure() # compute all the language metrics
json.dump(metrics, open(os.path.join(self.save_dir, 'test_scores.json'), 'w'))
# Switch back to training mode
print("Test finished. Time used: {}".format(time.time() - start))
return predictions, metrics
def test_challange(self, model, dataset, loader, opt, side_model=None):
# Make sure in the evaluation mode
logging.info("Evaluating...")
start = time.time()
model.eval()
dataset.test()
predictions = {"team_name": "", "evaluation_info": {"additional_description": ""}, "output_stories": []}
prediction_txt = open(self.prediction_file, 'w') # open the file to store the predictions
count = 0
finished_flickr_ids = []
for iter, batch in enumerate(loader):
iter_start = time.time()
feature_fc = Variable(batch['feature_fc'], volatile=True).cuda()
conv_feature = Variable(batch['feature_conv'], volatile=True).cuda() if 'feature_conv' in batch else None
count += feature_fc.size(0)
if conv_feature is not None:
results, _ = model.predict(feature_fc, conv_feature, beam_size=opt.beam_size)
else:
results, _ = model.predict(feature_fc, beam_size=opt.beam_size)
stories = utils.decode_story(dataset.get_vocab(), results)
indexes = batch['index'].numpy()
for j, story in enumerate(stories):
album_id, flickr_id = dataset.get_all_id(indexes[j])
concat_flickr_id = "-".join(flickr_id)
if concat_flickr_id not in finished_flickr_ids:
# if vid not in predictions: # only predict one story for an album
# write into txt file for evaluate metrics like Cider
prediction_txt.write('{}\t {}\n'.format(album_id, story))
# save into predictions
predictions['output_stories'].append(
{'album_id': album_id, 'photo_sequence': flickr_id, 'story_text_normalized': story})
finished_flickr_ids.append(concat_flickr_id)
logging.info("Evaluate iter {}/{} {:04.2f}%. Time used: {}".format(iter,
len(loader),
iter * 100.0 / len(loader),
time.time() - iter_start))
prediction_txt.close()
json_prediction_file = os.path.join(self.save_dir, 'challenge.json')
with open(json_prediction_file, 'w') as f:
json.dump(predictions, f)
logging.info("Evaluation finished. Evaluated {} samples. Time used: {}".format(count, time.time() - start))
return predictions