-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_MILES.py
129 lines (108 loc) · 5.06 KB
/
test_MILES.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
import argparse
import torch
import transformers
import pandas as pd
import numpy as np
from tqdm import tqdm
import data_loader.data_loader as module_data
import model.metric as module_metric
import model.model_MILES as module_arch
from parse_config import ConfigParser
from model.model import sim_matrix
from sacred import Experiment
from utils.util import state_dict_data_parallel_fix
from trainer.trainer import verbose
ex = Experiment('test')
@ex.main
def run():
# setup data_loader instances
config._config['data_loader']['args']['split'] = 'test'
config._config['data_loader']['args']['shuffle'] = False
config._config['data_loader']['args']['sliding_window_stride'] = config._config['sliding_window_stride']
data_loader = config.initialize('data_loader', module_data)
tokenizer = transformers.AutoTokenizer.from_pretrained(config['arch']['args']['text_params']['model'])
# build model architecture
model = config.initialize('arch', module_arch)
# get function handles of loss and metrics
metric_fns = [getattr(module_metric, met) for met in config['metrics']]
#logger.info('Loading checkpoint: {} ...'.format(config.resume))
if config.resume is not None:
checkpoint = torch.load(config.resume)
state_dict = checkpoint['state_dict']
new_state_dict = state_dict_data_parallel_fix(state_dict, model.state_dict())
model.load_state_dict(new_state_dict, strict=True)
else:
print('Using random weights')
if config['n_gpu'] > 1:
model = torch.nn.DataParallel(model)
# prepare model for testing
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
model.eval()
meta_arr = []
text_embed_arr = []
vid_embed_arr = []
print(len(data_loader))
with torch.no_grad():
for i, data in tqdm(tqdm(enumerate(data_loader))):
# leave this for now since not doing anything on the gpu
meta_arr.append(data['meta'])
if tokenizer is not None:
data['text'] = tokenizer(data['text'], return_tensors='pt', padding=True, truncation=True)
data['text'] = {key: val.cuda() for key, val in data['text'].items()}
if isinstance(data['video'], list):
data['video'] = [x.to(device) for x in data['video']]
else:
data['video'] = data['video'].to(device)
text_embed, vid_embed = model(data, return_embeds=True)
text_embed_arr.append(text_embed)
vid_embed_arr.append(vid_embed)
text_embeds = torch.cat(text_embed_arr)
vid_embeds = torch.cat(vid_embed_arr)
mask = None
if data_loader.dataset.sliding_window_stride != -1:
cpu_vid_embeds = vid_embeds.cpu().detach()
cpu_text_embeds = text_embeds.cpu().detach()
li_vid_embeds = [x for x in cpu_vid_embeds]
li_txt_embeds = [x for x in cpu_text_embeds]
videoids = pd.Series([x['paths'] for x in meta_arr]).explode()
raw_caps = pd.Series([x['raw_captions']] for x in meta_arr).explode().explode()
vid_df = pd.DataFrame({'videoid': videoids, 'vid_embed': li_vid_embeds, 'txt_embed': li_txt_embeds,
'captions': raw_caps})
new_vid_embeds = []
new_txt_embeds = []
for vid in vid_df['videoid'].unique():
tdf = vid_df[vid_df['videoid'] == vid]
tvembeds = torch.stack(tdf['vid_embed'].values.tolist())
tvembeds = tvembeds.mean(dim=0)
new_vid_embeds.append(tvembeds)
for cap in tdf['captions'].unique():
cdf = vid_df[vid_df['captions'] == cap]
ttembeds = torch.stack(cdf['txt_embed'].values.tolist())
new_txt_embeds.append(ttembeds[0])
vid_embeds = torch.stack(new_vid_embeds).cuda()
text_embeds = torch.stack(new_txt_embeds).cuda()
sims = sim_matrix(text_embeds, vid_embeds)
sims = sims.detach().cpu().numpy()
nested_metrics = {}
for metric in metric_fns:
metric_name = metric.__name__
res = metric(sims, query_masks=mask)
verbose(epoch=0, metrics=res, name="", mode=metric_name)
nested_metrics[metric_name] = res
if __name__ == '__main__':
args = argparse.ArgumentParser(description='PyTorch Template')
args.add_argument('-r', '--resume', default=None, type=str,
help='path to latest checkpoint (default: None)')
args.add_argument('-d', '--device', default=None, type=str,
help='indices of GPUs to enable (default: all)')
args.add_argument('-c', '--config', default=None, type=str,
help='config file path (default: None)')
args.add_argument('-s', '--sliding_window_stride', default=-1, type=int,
help='test time temporal augmentation, repeat samples with different start times.')
config = ConfigParser(args, test=True)
# hack to get sliding into config
args = args.parse_args()
config._config['sliding_window_stride'] = args.sliding_window_stride
ex.add_config(config.config)
ex.run()