-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluation.py
213 lines (173 loc) · 6.21 KB
/
evaluation.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
from __future__ import print_function
import os
import pickle
import numpy
from activity_net.data import get_test_loader
import time
import numpy as np
from anet_vocab import Vocabulary # NOQA
import torch
from model import VSE
from collections import OrderedDict
from IPython import embed
import torch.nn.functional as F
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=0):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / (.0001 + self.count)
def __str__(self):
"""String representation for logging
"""
# for values that should be recorded exactly e.g. iteration number
if self.count == 0:
return str(self.val)
# for stats
return '%.4f (%.4f)' % (self.val, self.avg)
class LogCollector(object):
"""A collection of logging objects that can change from train to val"""
def __init__(self):
# to keep the order of logged variables deterministic
self.meters = OrderedDict()
def update(self, k, v, n=0):
# create a new meter if previously not recorded
if k not in self.meters:
self.meters[k] = AverageMeter()
self.meters[k].update(v, n)
def __str__(self):
"""Concatenate the meters in one log line
"""
s = ''
for i, (k, v) in enumerate(self.meters.iteritems()):
if i > 0:
s += ' '
s += k + ' ' + str(v)
return s
def tb_log(self, tb_logger, prefix='', step=None):
"""Log using tensorboard
"""
for k, v in self.meters.iteritems():
tb_logger.log_value(prefix + k, v.val, step=step)
def LogReporter(tb_logger, result, epoch, name):
for key in result:
tb_logger.log_value(name+key, result[key], step=epoch)
return
def encode_data(opt, model, data_loader, log_step=10, logging=print, contextual_model=True):
"""Encode all images and captions loadable by `data_loader`
"""
batch_time = AverageMeter()
val_logger = LogCollector()
# switch to evaluate mode
model.val_start(opt)
end = time.time()
# numpy array to keep all the embeddings
clip_embs, cap_embs = [], []
vid_embs, para_embs = [], []
vid_contexts, para_contexts = [], []
num_clips_total = []
cur_vid_total = []
for i, (clips, captions, videos, paragraphs, lengths_clip, lengths_cap, lengths_video, lengths_paragraph, num_clips, num_caps, ind, cur_vid) in enumerate(data_loader):
# make sure val logger is used
model.logger = val_logger
num_clips_total.extend(num_clips)
# compute the embeddings
clip_emb, cap_emb = model.forward_emb(clips, captions, lengths_clip, lengths_cap)
vid_context, para_context = model.forward_emb(videos, paragraphs, lengths_video, lengths_paragraph)
if contextual_model:
vid_emb, para_emb = model.structure_emb(clip_emb, cap_emb, num_clips, num_caps, vid_context, para_context)
else:
vid_emb, para_emb = model.structure_emb(clip_emb, cap_emb, num_clips, num_caps)
clip_emb = F.normalize(clip_emb)
cap_emb = F.normalize(cap_emb)
vid_emb = F.normalize(vid_emb)
para_emb = F.normalize(para_emb)
vid_context = F.normalize(vid_context)
para_context = F.normalize(para_context)
# initialize the numpy arrays given the size of the embeddings
clip_embs.extend(clip_emb.data.cpu())
cap_embs.extend(cap_emb.data.cpu())
vid_embs.extend(vid_emb.data.cpu())
para_embs.extend(para_emb.data.cpu())
vid_contexts.extend(vid_context.data.cpu())
para_contexts.extend(para_context.data.cpu())
cur_vid_total.extend(cur_vid)
# measure accuracy and record loss
model.forward_loss(vid_emb, para_emb, 'test')
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % log_step == 0:
logging('Test: [{0}/{1}]\t'
'{e_log}\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
.format(
i, len(data_loader), batch_time=batch_time,
e_log=str(model.logger)))
vid_embs = torch.stack(vid_embs, 0)
para_embs = torch.stack(para_embs, 0)
vid_embs = vid_embs.numpy()
para_embs = para_embs.numpy()
clip_embs = torch.stack(clip_embs, 0)
cap_embs = torch.stack(cap_embs, 0)
clip_embs = clip_embs.numpy()
cap_embs = cap_embs.numpy()
vid_contexts = torch.stack(vid_contexts, 0)
para_contexts = torch.stack(para_contexts, 0)
vid_contexts = vid_contexts.numpy()
para_contexts = para_contexts.numpy()
return vid_embs, para_embs, clip_embs, cap_embs, vid_contexts, para_contexts, num_clips_total, cur_vid_total
def i2t(images, captions, npts=None, measure='cosine'):
npts = images.shape[0]
ranks = numpy.zeros(npts)
top1 = numpy.zeros(npts)
d = numpy.dot(images, captions.T)
for index in range(npts):
inds = numpy.argsort(d[index])[::-1]
rank = numpy.where(inds == index)[0][0]
ranks[index] = rank
top1[index] = inds[0]
r1 = 100.0 * len(numpy.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(numpy.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(numpy.where(ranks < 50)[0]) / len(ranks)
medr = numpy.floor(numpy.median(ranks)) + 1
meanr = ranks.mean() + 1
report_dict = dict()
report_dict['r1'] = r1
report_dict['r5'] = r5
report_dict['r10'] = r10
report_dict['medr'] = medr
report_dict['meanr'] = meanr
report_dict['sum'] = r1+r5+r10
return report_dict, top1, ranks
def t2i(images, captions, npts=None, measure='cosine'):
npts = captions.shape[0]
ranks = numpy.zeros(npts)
top1 = numpy.zeros(npts)
d = numpy.dot(captions, images.T)
for index in range(npts):
inds = numpy.argsort(d[index])[::-1]
rank = numpy.where(inds == index)[0][0]
ranks[index] = rank
top1[index] = inds[0]
r1 = 100.0 * len(numpy.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(numpy.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(numpy.where(ranks < 50)[0]) / len(ranks)
medr = numpy.floor(numpy.median(ranks)) + 1
meanr = ranks.mean() + 1
report_dict = dict()
report_dict['r1'] = r1
report_dict['r5'] = r5
report_dict['r10'] = r10
report_dict['medr'] = medr
report_dict['meanr'] = meanr
report_dict['sum'] = r1+r5+r10
return report_dict, top1, ranks