-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathevaluation.py
320 lines (244 loc) · 9.47 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from __future__ import print_function
import os
import pickle
import numpy
import time
import numpy as np
from scipy.spatial import distance
import torch
from torch.autograd import Variable
from basic.metric import getScorer
from basic.util import AverageMeter, LogCollector
from basic.generic_utils import Progbar
def l2norm(X):
"""L2-normalize columns of X
"""
norm = np.linalg.norm(X, axis=1, keepdims=True)
return 1.0 * X / norm
def cal_error(videos, captions, measure='cosine'):
if measure == 'cosine':
captions = l2norm(captions)
videos = l2norm(videos)
errors = -1*numpy.dot(captions, videos.T)
elif measure == 'euclidean':
errors = distance.cdist(captions, videos, 'euclidean')
return errors
def encode_data(model, data_loader, log_step=10, logging=print, return_ids=True):
"""Encode all videos and captions loadable by `data_loader`
"""
batch_time = AverageMeter()
val_logger = LogCollector()
# switch to evaluate mode
model.val_start()
end = time.time()
# numpy array to keep all the embeddings
video_embs = None
cap_embs = None
video_ids = ['']*len(data_loader.dataset)
caption_ids = ['']*len(data_loader.dataset)
for i, (videos, captions, idxs, cap_ids, vid_ids) in enumerate(data_loader):
# make sure val logger is used
model.logger = val_logger
# compute the embeddings
vid_emb, cap_emb = model.forward_emb(videos, captions, True)
# initialize the numpy arrays given the size of the embeddings
if video_embs is None:
video_embs = np.zeros((len(data_loader.dataset), vid_emb.size(1)))
cap_embs = np.zeros((len(data_loader.dataset), cap_emb.size(1)))
# preserve the embeddings by copying from gpu and converting to numpy
video_embs[idxs] = vid_emb.data.cpu().numpy().copy()
cap_embs[idxs] = cap_emb.data.cpu().numpy().copy()
for j, idx in enumerate(idxs):
caption_ids[idx] = cap_ids[j]
video_ids[idx] = vid_ids[j]
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % log_step == 0:
logging('Test: [{0:2d}/{1:2d}]\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)))
del videos, captions
if return_ids == True:
return video_embs, cap_embs, video_ids, caption_ids
else:
return video_embs, cap_embs
def encode_text_or_vid(encoder, data_loader, return_ids=True):
"""Encode all videos and captions loadable by `data_loader`
"""
# numpy array to keep all the embeddings
embeddings = None
ids = ['']*len(data_loader.dataset)
pbar = Progbar(len(data_loader.dataset))
for i, (datas, idxs, data_ids) in enumerate(data_loader):
# compute the embeddings
emb = encoder(datas)
# initialize the numpy arrays given the size of the embeddings
if embeddings is None:
embeddings = np.zeros((len(data_loader.dataset), emb.size(1)))
# preserve the embeddings by copying from gpu and converting to numpy
embeddings[idxs] = emb.data.cpu().numpy().copy()
for j, idx in enumerate(idxs):
ids[idx] = data_ids[j]
del datas
pbar.add(len(idxs))
if return_ids == True:
return embeddings, ids,
else:
return embeddings
# recall@k, Med r, Mean r for Text-to-Video Retrieval
def t2i(c2i, vis_details=False, n_caption=5):
"""
Text->Videos (Text-to-Video Retrieval)
c2i: (5N, N) matrix of caption to video errors
vis_details: if true, return a dictionary for ROC visualization purposes
"""
# print("errors matrix shape: ", c2i.shape)
assert c2i.shape[0] / c2i.shape[1] == n_caption, c2i.shape
ranks = np.zeros(c2i.shape[0])
for i in range(len(ranks)):
d_i = c2i[i]
inds = np.argsort(d_i)
rank = np.where(inds == i/n_caption)[0][0]
ranks[i] = rank
# Compute metrics
r1 = 100.0 * len(np.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(np.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(np.where(ranks < 10)[0]) / len(ranks)
medr = np.floor(np.median(ranks)) + 1
meanr = ranks.mean() + 1
return map(float, [r1, r5, r10, medr, meanr])
# recall@k, Med r, Mean r for Video-to-Text Retrieval
def i2t(c2i, n_caption=5):
"""
Videos->Text (Video-to-Text Retrieval)
c2i: (5N, N) matrix of caption to video errors
"""
#remove duplicate videos
# print("errors matrix shape: ", c2i.shape)
assert c2i.shape[0] / c2i.shape[1] == n_caption, c2i.shape
ranks = np.zeros(c2i.shape[1])
for i in range(len(ranks)):
d_i = c2i[:, i]
inds = np.argsort(d_i)
rank = np.where(inds/n_caption == i)[0][0]
ranks[i] = rank
# Compute metrics
r1 = 100.0 * len(np.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(np.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(np.where(ranks < 10)[0]) / len(ranks)
medr = np.floor(np.median(ranks)) + 1
meanr = ranks.mean() + 1
return map(float, [r1, r5, r10, medr, meanr])
# mAP for Text-to-Video Retrieval
def t2i_map(c2i, n_caption=5):
"""
Text->Videos (Text-to-Video Retrieval)
c2i: (5N, N) matrix of caption to video errors
"""
# print("errors matrix shape: ", c2i.shape)
assert c2i.shape[0] / c2i.shape[1] == n_caption, c2i.shape
scorer = getScorer('AP')
perf_list = []
for i in range(c2i.shape[0]):
d_i = c2i[i, :]
labels = [0]*len(d_i)
labels[i/n_caption] = 1
sorted_labels = [labels[x] for x in np.argsort(d_i)]
current_score = scorer.score(sorted_labels)
perf_list.append(current_score)
return np.mean(perf_list)
# mAP for Video-to-Text Retrieval
def i2t_map(c2i, n_caption=5):
"""
Videos->Text (Video-to-Text Retrieval)
c2i: (5N, N) matrix of caption to video errors
"""
# print("errors matrix shape: ", c2i.shape)
assert c2i.shape[0] / c2i.shape[1] == n_caption, c2i.shape
scorer = getScorer('AP')
perf_list = []
for i in range(c2i.shape[1]):
d_i = c2i[:, i]
labels = [0]*len(d_i)
labels[i*n_caption:(i+1)*n_caption] = [1]*n_caption
sorted_labels = [labels[x] for x in np.argsort(d_i)]
current_score = scorer.score(sorted_labels)
perf_list.append(current_score)
return np.mean(perf_list)
def t2i_inv_rank(c2i, n_caption=1):
"""
Text->Videos (Text-to-Video Retrieval)
c2i: (5N, N) matrix of caption to video errors
n_caption: number of captions of each image/video
"""
assert c2i.shape[0] / c2i.shape[1] == n_caption, c2i.shape
inv_ranks = np.zeros(c2i.shape[0])
for i in range(len(inv_ranks)):
d_i = c2i[i,:]
inds = np.argsort(d_i)
rank = np.where(inds == i/n_caption)[0]
inv_ranks[i] = sum(1.0 / (rank +1 ))
return np.mean(inv_ranks)
def i2t_inv_rank(c2i, n_caption=1):
"""
Videos->Text (Video-to-Text Retrieval)
c2i: (5N, N) matrix of caption to video errors
n_caption: number of captions of each image/video
"""
assert c2i.shape[0] / c2i.shape[1] == n_caption, c2i.shape
inv_ranks = np.zeros(c2i.shape[1])
for i in range(len(inv_ranks)):
d_i = c2i[:, i]
inds = np.argsort(d_i)
rank = np.where(inds/n_caption == i)[0]
inv_ranks[i] = sum(1.0 / (rank +1 ))
return np.mean(inv_ranks)
def i2t_inv_rank_multi(c2i, n_caption=2):
"""
Text->videos (Image Search)
c2i: (5N, N) matrix of caption to image errors
n_caption: number of captions of each image/video
"""
# print("errors matrix shape: ", c2i.shape)
assert c2i.shape[0] / c2i.shape[1] == n_caption, c2i.shape
inv_ranks = np.zeros(c2i.shape[1])
result = []
for i in range(n_caption):
idx = range(i, c2i.shape[0], n_caption)
sub_c2i = c2i[idx, :]
score = i2t_inv_rank(sub_c2i, n_caption=1)
result.append(score)
return result
# the number of captions are various across videos
def eval_varied(label_matrix):
ranks = np.zeros(label_matrix.shape[0])
aps = np.zeros(label_matrix.shape[0])
for index in range(len(ranks)):
rank = np.where(label_matrix[index]==1)[0] + 1
ranks[index] = rank[0]
aps[index] = np.mean([(i+1.)/rank[i] for i in range(len(rank))])
r1, r5, r10 = [100.0*np.mean([x <= k for x in ranks]) for k in [1, 5, 10]]
medr = np.floor(np.median(ranks))
meanr = ranks.mean()
# mir = (1.0/ranks).mean()
mAP = aps.mean()
return (r1, r5, r10, medr, meanr, mAP)
def t2i_varied(c2i_all_errors, caption_ids, video_ids):
inds = np.argsort(-c2i_all_errors, axis=1)
label_matrix = np.zeros(inds.shape)
for index in range(inds.shape[0]):
ind = inds[index][::-1]
label_matrix[index][np.where(np.array(video_ids)[ind]==caption_ids[index].split('#')[0])[0]]=1
return eval_varied(label_matrix)
def i2t_varied(c2i_all_errors, caption_ids, video_ids):
inds = np.argsort(-c2i_all_errors.T, axis=1)
label_matrix = np.zeros(inds.shape)
caption_ids = [txt_id.split('#')[0] for txt_id in caption_ids]
for index in range(inds.shape[0]):
ind = inds[index][::-1]
label_matrix[index][np.where(np.array(caption_ids)[ind]==video_ids[index])[0]]=1
return eval_varied(label_matrix)