-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTtimesV_V3C1_evaluation.py
847 lines (698 loc) · 32.7 KB
/
TtimesV_V3C1_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
from __future__ import print_function
import os
import sys
import time
import torch
from TxV_models.model_TtimesV import get_model, get_we_parameter
from util.text2vec import get_text_encoder
import logging
import json
import numpy as np
import pickle
import argparse
from basic.util import read_dict
from basic.constant import ROOT_PATH
from basic.bigfile import BigFile
from basic.common import makedirsforfile, checkToSkip
from scipy.spatial import distance
from util.vocab import clean_str
from util.vocab import Vocabulary
from pytorch_transformers import BertTokenizer
import itertools
import AVS_datasetload as dataload
import random
import clip
import random
random.seed(10)
from scipy.special import softmax
VIDEO_MAX_LEN = 64
def dual_softmax(sim_matrix):
m = softmax(sim_matrix, axis=0)
n = softmax(sim_matrix, axis=1)
return(np.multiply(m, n))
def dual_softmax_pt(sim_matrix):
ss = torch.nn.Softmax(dim=0)
m = ss(torch.tensor(sim_matrix))
ss = torch.nn.Softmax(dim=1)
n = ss(torch.tensor(sim_matrix))
c = m * n
# return (np.multiply(m.data.cpu().numpy().copy(), n.data.cpu().numpy().copy()))
return c.data.numpy()
def do_L2_norm(vec):
L2_norm = np.linalg.norm(vec, 2)
if L2_norm == 0.0:
L2_norm = np.finfo(float).eps
return 1.0 * np.array(vec) / L2_norm
def l2norm(X):
"""L2-normalize columns of X
"""
norm = np.linalg.norm(X, axis=1, keepdims=True)
return 1.0 * X / norm
def cosine_calculate(matrix_a, matrix_b):
result = distance.cdist(matrix_a, matrix_b, 'cosine')
# return result.tolist()
return result
def groupc3(listtest):
for x, y in itertools.groupby(enumerate(listtest), lambda a_b: a_b[0] - a_b[1]):
y = list(y)
yield y[0][1], y[-1][1]
def text2Berttext(caption_text, tokenizer):
tokenized_text = tokenizer.tokenize(caption_text)
retuned_tokenized_text = tokenized_text[:]
# print caption_text
# print tokenized_text
res = [coun for coun, ele in enumerate(tokenized_text) if ('##' in ele)]
res2 = list(groupc3(res))
# print res
# print (str(res2))
for ree in res2:
start = ree[0] - 1
end_ = ree[1]
tmp_token = ''
for i in range(start, end_ + 1):
# print tokenized_text[i].replace('##', '')
tmp_token = tmp_token + tokenized_text[i].replace('##', '')
# print tmp_token
for i in range(start, end_ + 1):
retuned_tokenized_text[i] = tmp_token
# print tokenized_text
# print retuned_tokenized_text
return ' '.join(retuned_tokenized_text)
def error_calulator_v2(videos, captions, errtype='sum'):
""" Captions vectors are numpy arrays instead of pytorch tensors
Video vectors are numpy arrays instead of pytorch tensors
"""
if errtype == 'sum':
errors = np.zeros((len(videos[0][0]), len(captions[0][0])))
for i in range(len(captions)):
for j in range(len(videos)):
capt = l2norm(captions[i][j])
vid = l2norm(videos[j][i])
errors += cosine_calculate(vid, capt)
return errors
elif errtype == 'max':
capt = l2norm(captions[0][0])
vid = l2norm(videos[0][0])
errors = cosine_calculate(vid, capt).tolist()
for i in range(1, len(captions)):
capt = l2norm(captions[i])
vid = l2norm(videos[i])
err = cosine_calculate(vid, capt).tolist()
cc = []
for aa, bb in zip(errors, err):
cc.append([max(els) for els in zip(aa, bb)])
errors = cc.copy()
return errors
elif errtype == 'min':
capt = l2norm(captions[0])
vid = l2norm(videos[0])
errors = cosine_calculate(vid, capt).tolist()
for i in range(1, len(captions)):
capt = l2norm(captions[i])
vid = l2norm(videos[i])
err = cosine_calculate(vid, capt).tolist()
cc = []
for aa, bb in zip(errors, err):
cc.append([min(els) for els in zip(aa, bb)])
errors = cc.copy()
return errors
def check(resultFile, pattern):
with open(resultFile) as f:
datafile = f.readlines()
for line in datafile:
if pattern in line:
print(line.rstrip("\n\r"))
def dataLoadedVideoText_one(video2frames, video_id, visual_feats, query, bow2vec, vocab, tokenizer, options):
data = []
videos = []
frame_list = video2frames[video_id]
frame_vecs = []
frames_tensors = []
for vis_fea in visual_feats:
frame_vecs = []
for frame_id in frame_list:
# l_2 normalize
if (options.do_visual_feas_norm):
frame_vecs.append(do_L2_norm(visual_feats[vis_fea].read_one(frame_id)))
else:
frame_vecs.append(visual_feats[vis_fea].read_one(frame_id))
frames_tensor = torch.Tensor(np.array(frame_vecs))
frames_tensors.append(frames_tensor)
# for frame_id in frame_list:
# # visual_feats.read_one(frame_id)
# if options.do_visual_feas_norm:
# frame_vecs.append(do_L2_norm(visual_feats.read_one(frame_id)))
# else:
# frame_vecs.append(visual_feats.read_one(frame_id))
# videos.append(torch.Tensor(frame_vecs).unsqueeze(0))
# Text encoding
cap_tensors = []
cap_bows = []
caption_text = query[:]
caption_text = ' '.join(clean_str(caption_text))
caption_text = text2Berttext(caption_text, tokenizer)
# caption_text = caption_text.encode("utf-8")
caption_text = caption_text.encode("utf-8").decode("utf-8")
if bow2vec is not None:
cap_bow = bow2vec.mapping(caption_text)
if cap_bow is None:
cap_bow = torch.zeros(bow2vec.ndims)
else:
cap_bow = torch.Tensor(cap_bow)
else:
cap_bow = None
if vocab is not None:
tokens = clean_str(caption_text)
caption = []
caption.append(vocab('<start>'))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab('<end>'))
cap_tensor = torch.Tensor(caption)
else:
cap_tensor = None
# cap_tensors.append(cap_tensor.unsqueeze(0))
# cap_bows.append(cap_bow.unsqueeze(0))
# BERT
caption_text = query[:]
caption_text = ' '.join(clean_str(query))
marked_text = "[CLS] " + caption_text + " [SEP]"
# print marked_text
tokenized_text = tokenizer.tokenize(marked_text)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
segments_ids = [1] * len(tokenized_text)
# Convert inputs to PyTorch tensors
tokens_tensor = torch.tensor(indexed_tokens)
segments_tensors = torch.tensor(segments_ids)
caption_text = caption_text.encode("utf-8")
data.append([frames_tensors, cap_tensor, cap_bow, tokens_tensor, segments_tensors, caption_text])
return data
# return video_data, text_data
def dataLoadedText_one(query, bow2vec, vocab, tokenizer, options):
data = []
# Text encoding
cap_tensors = []
cap_bows = []
caption_text = query[:]
caption_text = ' '.join(clean_str(caption_text))
caption_text = text2Berttext(caption_text, tokenizer)
# caption_text = caption_text.encode("utf-8")
caption_text = caption_text.encode("utf-8").decode("utf-8")
if bow2vec is not None:
cap_bow = bow2vec.mapping(caption_text)
if cap_bow is None:
cap_bow = torch.zeros(bow2vec.ndims)
else:
cap_bow = torch.Tensor(cap_bow)
else:
cap_bow = None
if vocab is not None:
tokens = clean_str(caption_text)
caption = []
caption.append(vocab('<start>'))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab('<end>'))
cap_tensor = torch.Tensor(caption)
else:
cap_tensor = None
# cap_tensors.append(cap_tensor.unsqueeze(0))
# cap_bows.append(cap_bow.unsqueeze(0))
# BERT
caption_text = query[:]
caption_text = ' '.join(clean_str(query))
marked_text = "[CLS] " + caption_text + " [SEP]"
# print marked_text
tokenized_text = tokenizer.tokenize(marked_text)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
segments_ids = [1] * len(tokenized_text)
# Convert inputs to PyTorch tensors
tokens_tensor = torch.tensor(indexed_tokens)
segments_tensors = torch.tensor(segments_ids)
caption_text = caption_text.encode("utf-8")
data.append([cap_tensor, cap_bow, tokens_tensor, segments_tensors, caption_text])
return data
# return video_data, text_data
def collate_frame_gru_fn(data):
"""
Build mini-batch tensors from a list of (video, caption) tuples.
"""
# Sort a data list by caption length
if data[0][1] is not None:
data.sort(key=lambda x: len(x[1]), reverse=True)
videos, captions, cap_bows, tokens_tensor, segments_tensors, caption_text = zip(*data)
num_of_feas = len(videos[0])
vidoes_all = []
videos_origin_all = []
video_lengths_all = []
vidoes_mask_all = []
for fea in range(num_of_feas):
frame_vec_len = len(videos[0][fea][0])
video_lengths = [min(VIDEO_MAX_LEN, len(frame[0])) for frame in videos]
vidoes = torch.zeros(len(videos), max(video_lengths), frame_vec_len)
videos_origin = torch.zeros(len(videos), frame_vec_len)
vidoes_mask = torch.zeros(len(videos), max(video_lengths))
for i, frames in enumerate(videos):
end = video_lengths[i]
vidoes[i, :end, :] = frames[fea][:end, :]
# Fil the zeros of vidoes with random frames
# print(end)
# print(max(video_lengths))
if end < max(video_lengths):
try:
# num_of_filler_frames = random.sample(range(0, end), max(video_lengths)-end)
num_of_filler_frames = random.choices(list(range(0, end)), k=(max(video_lengths) - end))
# print(num_of_filler_frames)
# print()
except:
print()
vidoes[i, end:, :] = frames[fea][num_of_filler_frames, :]
videos_origin[i, :] = torch.mean(frames[fea], 0)
vidoes_mask[i, :end] = 1.0
vidoes_all.append(vidoes)
videos_origin_all.append(videos_origin)
video_lengths_all.append(video_lengths)
vidoes_mask_all.append(vidoes_mask)
if captions[0] is not None:
# Merge captions (convert tuple of 1D tensor to 2D tensor)
lengths = [len(cap) for cap in captions]
target = torch.zeros(len(captions), max(lengths)).long()
words_mask = torch.zeros(len(captions), max(lengths))
for i, cap in enumerate(captions):
end = lengths[i]
target[i, :end] = cap[:end]
words_mask[i, :end] = 1.0
else:
target = None
lengths = None
words_mask = None
# 'BERT Process'
if captions[0] is not None:
# Merge captions (convert tuple of 1D tensor to 2D tensor)
lengths_bert = [len(seg) for seg in segments_tensors]
tokens_tensor_padded = torch.zeros(len(tokens_tensor), max(lengths_bert)).long()
segments_tensors_padded = torch.zeros(len(segments_tensors), max(lengths_bert)).long()
words_mask_bert = torch.zeros(len(tokens_tensor), max(lengths_bert))
for i, cap in enumerate(tokens_tensor):
end = lengths_bert[i]
tokens_tensor_padded[i, :end] = cap[:end]
words_mask_bert[i, :end] = 1.0
for i, cap in enumerate(segments_tensors):
end = lengths_bert[i]
segments_tensors_padded[i, :end] = cap[:end]
else:
lengths_bert = None
tokens_tensor_padded = None
segments_tensors_padded = None
words_mask_bert = None
cap_bows = torch.stack(cap_bows, 0) if cap_bows[0] is not None else None
CLIP_token = torch.squeeze(clip.tokenize([a.decode("utf-8") for a in caption_text], truncate=True).clone().detach())
if CLIP_token.size().__len__() == 1:
CLIP_token = torch.unsqueeze(CLIP_token, dim=0)
video_data = (vidoes_all, videos_origin_all, video_lengths, vidoes_mask)
text_data = (
target, cap_bows, lengths, words_mask, tokens_tensor_padded, segments_tensors_padded, lengths_bert,
caption_text, CLIP_token)
return video_data, text_data
def collate_text_gru_fn(data):
"""
Build mini-batch tensors from a list of (video, caption) tuples.
"""
# Sort a data list by caption length
if data[0][1] is not None:
data.sort(key=lambda x: len(x[1]), reverse=True)
captions, cap_bows, tokens_tensor, segments_tensors, caption_text = zip(*data)
if captions[0] is not None:
# Merge captions (convert tuple of 1D tensor to 2D tensor)
lengths = [len(cap) for cap in captions]
target = torch.zeros(len(captions), max(lengths)).long()
words_mask = torch.zeros(len(captions), max(lengths))
for i, cap in enumerate(captions):
end = lengths[i]
target[i, :end] = cap[:end]
words_mask[i, :end] = 1.0
else:
target = None
lengths = None
words_mask = None
# 'BERT Process'
if captions[0] is not None:
# Merge captions (convert tuple of 1D tensor to 2D tensor)
lengths_bert = [len(seg) for seg in segments_tensors]
tokens_tensor_padded = torch.zeros(len(tokens_tensor), max(lengths_bert)).long()
segments_tensors_padded = torch.zeros(len(segments_tensors), max(lengths_bert)).long()
words_mask_bert = torch.zeros(len(tokens_tensor), max(lengths_bert))
for i, cap in enumerate(tokens_tensor):
end = lengths_bert[i]
tokens_tensor_padded[i, :end] = cap[:end]
words_mask_bert[i, :end] = 1.0
for i, cap in enumerate(segments_tensors):
end = lengths_bert[i]
segments_tensors_padded[i, :end] = cap[:end]
else:
lengths_bert = None
tokens_tensor_padded = None
segments_tensors_padded = None
words_mask_bert = None
cap_bows = torch.stack(cap_bows, 0) if cap_bows[0] is not None else None
# CLIP_token = torch.squeeze(torch.tensor(clip.tokenize([a.decode("utf-8") for a in caption_text], truncate=True)))
CLIP_token = torch.squeeze(clip.tokenize([a.decode("utf-8") for a in caption_text], truncate=True).clone().detach())
text_data = (
target, cap_bows, lengths, words_mask, tokens_tensor_padded, segments_tensors_padded, lengths_bert,
caption_text, CLIP_token)
return text_data
def parse_args():
# Hyper Parameters
parser = argparse.ArgumentParser()
parser.add_argument('testCollection', type=str, help='test collection')
parser.add_argument('--rootpath', type=str, default=ROOT_PATH, help='path to datasets. (default: %s)' % ROOT_PATH)
parser.add_argument('--evalpath', type=str, default=ROOT_PATH,
help='path to evaluation video features. (default: %s)' % ROOT_PATH)
parser.add_argument('--overwrite', type=int, default=0, choices=[0, 1], help='overwrite existed file. (default: 0)')
parser.add_argument('--log_step', default=100, type=int, help='Number of steps to print and record the log.')
parser.add_argument('--batch_size', default=128, type=int, help='Size of a training mini-batch.')
parser.add_argument('--workers', default=5, type=int, help='Number of data loader workers.')
parser.add_argument('--logger_name', default='runs', help='Path to save the model and Tensorboard log.')
parser.add_argument('--checkpoint_name', default='model_best.pth.tar', type=str,
help='name of checkpoint (default: model_best.pth.tar)')
parser.add_argument('--n_caption', type=int, default=20, help='number of captions of each image/video (default: 1)')
parser.add_argument('--errtype', type=str, default='sum', choices=['sum', 'max', 'min'],
help='overwrite existed file. (default: 0)')
args = parser.parse_args()
return args
def main():
opt = parse_args()
print(json.dumps(vars(opt), indent=2))
rootpath = opt.rootpath
evalpath = opt.evalpath
testCollection = opt.testCollection
# n_caption = opt.n_caption
resume = os.path.join(opt.logger_name, opt.checkpoint_name)
batch_size = opt.batch_size
if not os.path.exists(resume):
logging.info(resume + ' not exists.')
sys.exit(0)
opt.dual_softmax = 0
opt.dual_softmax_same_dataset = 0
saveFile_AVS19 = (opt.logger_name + '/AVS19_' + testCollection + '_doSoftmax_' + str(opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVS20 = (opt.logger_name + '/AVS20_' + testCollection + '_doSoftmax_' + str(opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVSprogress = (opt.logger_name + '/AVSprogress_' + testCollection + '_doSoftmax_' + str(opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVS21 = (opt.logger_name + '/AVS21_' + testCollection + '_doSoftmax_' + str(opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
if os.path.exists(saveFile_AVSprogress) & (opt.overwrite == 0):
sys.exit(0)
queriesFile19 = 'data/tv19.avs.topics_parsed.txt'
lineList19 = [line.rstrip('\n') for line in open(queriesFile19)]
queriesFile20 = 'data/tv20.avs.topics_parsed.txt'
lineList20 = [line.rstrip('\n') for line in open(queriesFile20)]
queriesFileprogress = 'data/trecvid.progress.avs.topics_parsed.txt'
lineListprogress = [line.rstrip('\n') for line in open(queriesFileprogress)]
queriesFileprogress = 'data/tv21.avs.topics.txt'
lineList21 = [line.rstrip('\n') for line in open(queriesFileprogress)]
lineList = lineList19 + lineList20 + lineListprogress + lineList21
droppedListfile = 'data/V3C1.dropped.shots.list'
droppedList = [line.rstrip('\n') for line in open(droppedListfile)]
checkpoint = torch.load(resume)
start_epoch = checkpoint['epoch']
best_rsum = checkpoint['best_rsum']
print("=> loaded checkpoint '{}' (epoch {}, best_rsum {})"
.format(resume, start_epoch, best_rsum))
options = checkpoint['opt']
if not hasattr(options, 'do_visual_feas_norm'):
setattr(options, "do_visual_feas_norm", 0)
if not hasattr(options, 'concate'):
setattr(options, "concate", "full")
trainCollection = options.trainCollection
output_dir = resume.replace(trainCollection, testCollection)
output_dir = output_dir.replace('/%s/' % options.cv_name, '/results/%s/' % trainCollection)
result_pred_sents = os.path.join(output_dir, 'id.sent.score.txt')
pred_error_matrix_file = os.path.join(output_dir, 'pred_errors_matrix.pth.tar')
if checkToSkip(pred_error_matrix_file, opt.overwrite):
sys.exit(0)
makedirsforfile(pred_error_matrix_file)
# data loader prepare
# caption_files = {'test': os.path.join(evalpath, testCollection, 'TextData', '%s.caption.txt' % testCollection)}
options.visual_features = options.visual_feature.split('@')
visual_feat_path = {y: os.path.join(evalpath, testCollection, 'FeatureData', y)
for y in options.visual_features}
visual_feats = {'test': {y: BigFile(visual_feat_path[y]) for y in options.visual_features}}
assert options.visual_feat_dim == [visual_feats['test'][aa].ndims for aa in visual_feats['test']]
video2frames = {'test': read_dict(
os.path.join(evalpath, testCollection, 'FeatureData', options.visual_features[0], 'video2frames.txt'))}
# video2frames = None
# set bow vocabulary and encoding
bow_vocab_file = os.path.join(rootpath, options.trainCollection, 'TextData', 'vocabulary', 'bow',
options.vocab + '.pkl')
bow_vocab = pickle.load(open(bow_vocab_file, 'rb'))
bow2vec = get_text_encoder('bow')(bow_vocab)
options.bow_vocab_size = len(bow_vocab)
# set rnn vocabulary
rnn_vocab_file = os.path.join(rootpath, options.trainCollection, 'TextData', 'vocabulary', 'rnn',
options.vocab + '.pkl')
rnn_vocab = pickle.load(open(rnn_vocab_file, 'rb'))
options.vocab_size = len(rnn_vocab)
# initialize word embedding
options.we_parameter = None
if options.word_dim == 500:
w2v_data_path = os.path.join(rootpath, "word2vec", 'flickr', 'vec500flickr30m')
options.we_parameter = get_we_parameter(rnn_vocab, w2v_data_path)
# Construct the model
model = get_model(options.model)(options)
model.load_state_dict(checkpoint['model'])
model.Eiters = checkpoint['Eiters']
# switch to evaluate mode
model.val_start()
video2frames = video2frames['test']
videoIDs = [key for key in video2frames.keys()]
# Queries embeddings
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
queryEmbeddings = []
for quer in lineList:
query = quer.split(' ')[1]
videBatch = videoIDs[0] # a dummy video
data = dataLoadedVideoText_one(video2frames, videBatch, visual_feats['test'], query, bow2vec, rnn_vocab,
tokenizer, options)
videos, captions = collate_frame_gru_fn(data)
# compute the embeddings
vid_emb, cap_emb = model.forward_emb(videos, captions, True)
if not queryEmbeddings:
queryEmbeddings = list(cap_emb)
else:
for qq in range(len(cap_emb)):
for kk in range(0, cap_emb[qq].__len__()):
queryEmbeddings[qq][kk] = torch.cat((queryEmbeddings[qq][kk], cap_emb[qq][kk]))
queryEmbeddings = tuple(queryEmbeddings)
for i in range(len(queryEmbeddings)):
for j in range(len(queryEmbeddings[i])):
queryEmbeddings[i][j] = queryEmbeddings[i][j].data.cpu().numpy().copy()
# Dummy caption data in batch size
data = []
for o in range(batch_size):
data.extend(dataLoadedText_one(query, bow2vec, rnn_vocab, tokenizer, options))
captions = collate_text_gru_fn(data)
data_loader = dataload.get_test_data_loaders(visual_feats['test'], batch_size, 5, options.do_visual_feas_norm,
video2frames=video2frames)
start = time.time()
errorlistList = []
VideoIDS = []
video_ids = []
for i, (videos, idxs, vid_ids) in enumerate(data_loader):
video_ids.extend(vid_ids)
VideoIDS.extend(vid_ids)
# compute the embeddings
vid_emb, cap_emb = model.forward_emb(videos, captions, True)
# convert vid_emb from tensor to numpy
for ii in range(len(vid_emb)):
for j in range(len(vid_emb[ii])):
vid_emb[ii][j] = vid_emb[ii][j].data.cpu().numpy().copy()
errorlistList.extend(error_calulator_v2(vid_emb, queryEmbeddings, errtype=opt.errtype))
if errorlistList.__len__() % (batch_size * 5000) == 0:
# print (i)
end = time.time()
print(str(errorlistList.__len__()) + '/' + str(len(videoIDs)) + ' in: ' + str(end - start))
start = time.time()
errorlist = np.asarray(errorlistList)
np.save(opt.logger_name + '/AVS_19_20_21_errorlist.npy', errorlist)
file_to_store = open(opt.logger_name + '/AVS_19_20_21_VideoIDS', "wb")
pickle.dump(VideoIDS, file_to_store)
query_errors_sofmax = dual_softmax_pt(errorlist)
# No dual softmax inference
opt.dual_softmax = 0
opt.dual_softmax_same_dataset = 0
opt.dual_softmax_same_dataset_otherAVS = 0
opt.dual_softmax_selected_caps = 0
saveFile_AVS19 = (opt.logger_name + '/AVS19_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVS20 = (opt.logger_name + '/AVS20_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVSprogress = (opt.logger_name + '/AVSprogress_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVS21 = (opt.logger_name + '/AVS21_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
evaluation(saveFile_AVS19, saveFile_AVS20, saveFile_AVSprogress, saveFile_AVS21, lineList, VideoIDS,
errorlist, query_errors_sofmax, droppedList, opt)
# Dual softmax inference using AVS queries
opt.dual_softmax = 1
opt.dual_softmax_same_dataset = 1
opt.dual_softmax_same_dataset_otherAVS = 0
opt.dual_softmax_selected_caps = 0
saveFile_AVS19 = (opt.logger_name + '/AVS19_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVS20 = (opt.logger_name + '/AVS20_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVSprogress = (opt.logger_name + '/AVSprogress_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
saveFile_AVS21 = (opt.logger_name + '/AVS21_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '.txt')
evaluation(saveFile_AVS19, saveFile_AVS20, saveFile_AVSprogress, saveFile_AVS21, lineList, VideoIDS,
errorlist, query_errors_sofmax, droppedList, opt)
# Dual softmax inference using AVS queries from other years
opt.dual_softmax = 1
opt.dual_softmax_same_dataset = 1
opt.dual_softmax_same_dataset_otherAVS = 1
opt.dual_softmax_selected_caps = 0
saveFile_AVS19 = (opt.logger_name + '/AVS19_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '_otherAVS_' + str(opt.dual_softmax_same_dataset_otherAVS) + '.txt')
saveFile_AVS20 = (opt.logger_name + '/AVS20_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '_otherAVS_' + str(opt.dual_softmax_same_dataset_otherAVS) + '.txt')
saveFile_AVSprogress = (opt.logger_name + '/AVSprogress_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '_otherAVS_' + str(opt.dual_softmax_same_dataset_otherAVS) + '.txt')
saveFile_AVS21 = (opt.logger_name + '/AVS21_' + testCollection + '_doSoftmax_' + str(
opt.dual_softmax) + '_same_dataset_' + str(opt.dual_softmax_same_dataset) + '_otherAVS_' + str(opt.dual_softmax_same_dataset_otherAVS) + '.txt')
evaluation(saveFile_AVS19, saveFile_AVS20, saveFile_AVSprogress, saveFile_AVS21, lineList, VideoIDS,
errorlist, query_errors_sofmax, droppedList, opt)
def evaluation(saveFile_AVS19, saveFile_AVS20, saveFile_AVSprogress, saveFile_AVS21, queriesList, VideoIDS, errorlist, query_errors_sofmax, droppedList, opt):
# AVS 2019
print('AVS 2019')
f = open(saveFile_AVS19, "w")
# print("Loading query #", end=' ')
for num, name in enumerate(queriesList[:30], start=1):
# print(num, end=' ')
queryError = errorlist[:, num - 1]
if opt.dual_softmax:
if opt.dual_softmax_same_dataset:
if not opt.dual_softmax_same_dataset_otherAVS:
query_errors = query_errors_sofmax
queryError = query_errors[:, num - 1]
else:
query_error = np.expand_dims(errorlist[:, num - 1], axis=1)
otherAVS_errors = errorlist[:, 30:]
queryError = dual_softmax_pt(np.concatenate((query_error, otherAVS_errors), axis=1))
queryError = queryError[:, 0]
scoresIndex = np.argsort(queryError)
f = open(saveFile_AVS19, "a")
c = 0
existings = []
# for ind in scoresIndex[::-1]:
for ind in scoresIndex:
imgID = VideoIDS[ind]
c = c + 1
f.write('1' + name.split(' ')[0])
f.write(' 0 ' + imgID + ' ' + str(c) + ' ' + str(10000 - c) + ' ITI-CERTH' + '\n')
if c == 1000:
break
f.close()
resultAVSFile19 = saveFile_AVS19[:-4] + '_results.txt'
command = "perl data/AVS/sample_eval.pl -q data/AVS/avs.qrels.tv19 {} > {}".format(saveFile_AVS19, resultAVSFile19)
os.system(command)
check(resultAVSFile19, 'infAP all')
# AVS 2020
print('AVS 2020')
f = open(saveFile_AVS20, "w")
# print("Loading query #", end=' ')
for num, name in enumerate(queriesList[30:50], start=31):
# print(num, end=' ')
queryError = errorlist[:, num - 1]
if opt.dual_softmax:
if opt.dual_softmax_same_dataset:
if not opt.dual_softmax_same_dataset_otherAVS:
query_errors = query_errors_sofmax
queryError = query_errors[:, num - 1]
else:
query_error = np.expand_dims(errorlist[:, num - 1], axis=1)
otherAVS_errors = np.concatenate((errorlist[:, :30], errorlist[:, 50:]), axis=1)
queryError = dual_softmax_pt(np.concatenate((query_error, otherAVS_errors), axis=1))
queryError = queryError[:, 0]
scoresIndex = np.argsort(queryError)
f = open(saveFile_AVS20, "a")
c = 0
existings = []
# for ind in scoresIndex[::-1]:
for ind in scoresIndex:
imgID = VideoIDS[ind]
if imgID in droppedList:
continue
c = c + 1
f.write('1' + name.split(' ')[0])
f.write(' 0 ' + imgID + ' ' + str(c) + ' ' + str(10000 - c) + ' ITI-CERTH' + '\n')
if c == 1000:
break
f.close()
resultAVSFile20 = saveFile_AVS20[:-4] + '_results.txt'
command = "perl data/AVS/sample_eval.pl -q data/AVS/avs.gt.main.tv20 {} > {}".format(saveFile_AVS20, resultAVSFile20)
os.system(command)
check(resultAVSFile20, 'infAP all')
# AVS progress
print('AVS progress')
f = open(saveFile_AVSprogress, "w")
for num, name in enumerate(queriesList[50:70], start=51):
queryError = errorlist[:, num - 1]
if opt.dual_softmax:
if opt.dual_softmax_same_dataset:
if not opt.dual_softmax_same_dataset_otherAVS:
query_errors = query_errors_sofmax
queryError = query_errors[:, num - 1]
else:
query_error = np.expand_dims(errorlist[:, num - 1], axis=1)
otherAVS_errors = np.concatenate((errorlist[:, :50], errorlist[:, 70:]), axis=1)
queryError = dual_softmax_pt(np.concatenate((query_error, otherAVS_errors), axis=1))
queryError = queryError[:, 0]
scoresIndex = np.argsort(queryError)
f = open(saveFile_AVSprogress, "a")
c = 0
existings = []
# for ind in scoresIndex[::-1]:
for ind in scoresIndex:
imgID = VideoIDS[ind]
if imgID in droppedList:
continue
c = c + 1
f.write('1' + name.split(' ')[0])
f.write(' 0 ' + imgID + ' ' + str(c) + ' ' + str(10000 - c) + ' ITI-CERTH' + '\n')
if c == 1000:
break
f.close()
# AVS 2021
print('AVS 2021')
f = open(saveFile_AVS21, "w")
for num, name in enumerate(queriesList[70:90], start=71):
queryError = errorlist[:, num - 1]
if opt.dual_softmax:
if opt.dual_softmax_same_dataset:
if not opt.dual_softmax_same_dataset_otherAVS:
query_errors = query_errors_sofmax
queryError = query_errors[:, num - 1]
else:
query_error = np.expand_dims(errorlist[:, num - 1], axis=1)
otherAVS_errors = errorlist[:, :70]
queryError = dual_softmax_pt(np.concatenate((query_error, otherAVS_errors), axis=1))
queryError = queryError[:, 0]
scoresIndex = np.argsort(queryError)
f = open(saveFile_AVS21, "a")
c = 0
existings = []
# for ind in scoresIndex[::-1]:
for ind in scoresIndex:
imgID = VideoIDS[ind]
if imgID in droppedList:
continue
c = c + 1
f.write('1' + name.split(' ')[0])
f.write(' 0 ' + imgID + ' ' + str(c) + ' ' + str(10000 - c) + ' ITI-CERTH' + '\n')
if c == 1000:
break
f.close()
resultAVSFile21 = saveFile_AVS21[:-4] + '_results.txt'
command = "perl data/AVS/sample_eval.pl -q data/AVS/avs.qrels.main.tv21 {} > {}".format(saveFile_AVS21, resultAVSFile21)
os.system(command)
check(resultAVSFile21, 'infAP all')
print()
if __name__ == '__main__':
main()