-
Notifications
You must be signed in to change notification settings - Fork 44
/
test.py
284 lines (243 loc) · 11.3 KB
/
test.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
import argparse
import pandas as pd
import torch
import transformers
from sacred import Experiment
from tqdm import tqdm
import glob
import data_loader.data_loader as module_data
import model.metric as module_metric
import model.model as module_arch
from model.model import compute_similarity
from parse_config import ConfigParser
from trainer.trainer import verbose
from utils.util import state_dict_data_parallel_fix
import numpy as np
import os
import copy
ex = Experiment('test')
@ex.main
def run():
# setup data_loader instances
config._config['data_loader']['args']['split'] = args.split
config._config['data_loader']['args']['tsfm_split'] = 'test' # set transform to test split to remove augmentations
config._config['data_loader']['args']['shuffle'] = False
config._config['data_loader']['args']['batch_size'] = args.batch_size
config._config['data_loader']['args']['sliding_window_stride'] = args.sliding_window_stride
# config._config['data_loader']['args']['video_params']['num_frames'] = 120
data_loader = config.initialize('data_loader', module_data)
n_samples = len(data_loader.dataset)
text_model_name = config['arch']['args']['text_params']['model']
if "openai/clip" in text_model_name:
tokenizer_builder = transformers.CLIPTokenizer
else:
tokenizer_builder = transformers.AutoTokenizer
tokenizer = tokenizer_builder.from_pretrained(
text_model_name,
model_max_length=config['arch']['args']['text_params'].get('max_length', 1e6),
TOKENIZERS_PARALLELISM=False)
# 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()
ctr = 0
save_part = None
if args.save_feats:
part_seq = [int(x.split('_')[-1].split('.')[0]) for x in
glob.glob(os.path.join(args.save_feats, "ids_test_*.csv"))]
if len(part_seq) > 0:
save_part = max() + 1
else:
save_part = 0
print(F"##### WARNING SAVE_PART STARTING AT {save_part}, MAKE SURE THIS IS THE NEWEST")
meta_arr = []
text_embed_arr = []
vid_embed_arr = []
text_mask_arr = []
vid_mask_arr = []
print(len(data_loader))
with torch.no_grad():
for i, data_og in tqdm(tqdm(enumerate(data_loader))):
# leave this for now since not doing anything on the gpu
data = copy.deepcopy(data_og)
del data_og
if tokenizer is not None:
if args.vis_token_similarity:
data['meta']['tokenized_captions'] = [tokenizer.tokenize(x) for x in data['text']]
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_embeds, vid_embeds = model(data)
text_embed_arr.append(text_embeds.cpu().detach())
vid_embed_arr.append(vid_embeds.cpu().detach())
# meta stuff
meta_arr.append(data['meta'])
ctr += len(data['video'])
# save every 1mil samples to avoid OOM
if args.save_feats is not None and ctr > 1e4:
ctr = 0
text_embeds = torch.cat(text_embed_arr)
vid_embeds = torch.cat(vid_embed_arr)
meta_arr_cat = {key: [] for key in meta_arr[0].keys()}
for meta in meta_arr:
for key, val in meta.items():
meta_arr_cat[key].append(val)
meta_arr = meta_arr_cat
for key, val in meta_arr.items():
if isinstance(val[0], list):
val = [item for sublist in val for item in sublist]
meta_arr[key] = val
elif isinstance(val[0], torch.Tensor):
meta_arr[key] = torch.cat(val)
else:
raise NotImplementedError
save_feats(vid_embeds, text_embeds, meta_arr, args.save_feats, args.save_type,
data_loader.dataset.split, save_part=save_part)
text_embed_arr = []
vid_embed_arr = []
meta_arr = []
save_part += 1
vid_embeds = torch.cat(vid_embed_arr)
meta_arr_cat = {key: [] for key in meta_arr[0].keys()}
for meta in meta_arr:
for key, val in meta.items():
meta_arr_cat[key].append(val)
meta_arr = meta_arr_cat
for key, val in meta_arr.items():
if isinstance(val[0], list):
val = [item for sublist in val for item in sublist]
meta_arr[key] = val
elif isinstance(val[0], torch.Tensor):
meta_arr[key] = torch.cat(val)
else:
raise NotImplementedError
text_embeds = torch.cat(text_embed_arr)
mask = None
if data_loader.dataset.sliding_window_stride != -1:
cpu_vid_embeds = vid_embeds
cpu_text_embeds = text_embeds
li_vid_embeds = [x for x in cpu_vid_embeds]
li_txt_embeds = [x for x in cpu_text_embeds]
videoids = pd.Series(meta_arr['paths'])
raw_caps = pd.Series(meta_arr['raw_captions'])
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)
text_embeds = torch.stack(new_txt_embeds)
if args.split != 'train': # because train is usually too big
chunk = True
if not chunk:
sims, _ = compute_similarity(text_embeds, vid_embeds, text_masks)
else:
chunk_size = 100
sim_row_arr = []
for tdx in range(0, len(text_embeds), chunk_size):
print(tdx, ' / ', len(text_embeds), ' ...')
t_embed = text_embeds[tdx:tdx + chunk_size]
sim_row = []
for vdx in range(0, len(vid_embeds), chunk_size):
v_embed = vid_embeds[vdx:vdx + chunk_size]
sim_chunk, _ = compute_similarity(t_embed, v_embed)
sim_row.append(sim_chunk)
sim_row = torch.cat(sim_row, dim=1)
sim_row_arr.append(sim_row)
sims = torch.cat(sim_row_arr, dim=0)
sims = sims.numpy()
# if not args.vis_token_similarity:
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
# else:
# visualise_text_video_sim(sims, mask, meta_arr, num_vis=10)
# if config.config['visualizer']:
# raise NotImplementedError
if args.save_feats is not None:
if save_part == 0:
save_part = None
save_feats(vid_embeds, text_embeds, meta_arr, args.save_feats, args.save_type, data_loader.dataset.split,
save_part=save_part)
# meta_arr['frame_id'] = meta_arr['frame_id'].numpy()
if save_part is None:
fn = f'meta_arr.npy'
else:
fn = f'meta_arr_{save_part}.npy'
np.save(os.path.join(args.save_feats, fn), meta_arr)
def save_feats(vid_embeds, text_embeds, meta_arr, save_feats, save_type, split, save_part=None):
vid_embeds = vid_embeds.cpu().detach().numpy()
text_embeds = text_embeds.cpu().detach().numpy()
if save_part is not None:
vid_fn = f'vid_embeds_{split}_{save_part}.npy'
txt_fn = f'txt_embeds_{split}_{save_part}.npy'
csv_fn = f'ids_{split}_{save_part}.csv'
else:
vid_fn = f'vid_embeds_{split}.npy'
txt_fn = f'txt_embeds_{split}.npy'
csv_fn = f'ids_{split}.csv'
vid_embeds_save_fp = os.path.join(save_feats, vid_fn)
txt_embeds_save_fp = os.path.join(save_feats, txt_fn)
if save_type in ['video', 'both']:
np.save(vid_embeds_save_fp, vid_embeds)
if save_type in ['text', 'both']:
np.save(txt_embeds_save_fp, text_embeds)
videoids = pd.Series(meta_arr['paths'])
# frame_ids = pd.Series(meta_arr['frame_id'].numpy())
meta_df = pd.DataFrame({'0': videoids})
meta_df.to_csv(os.path.join(save_feats, csv_fn), index=False)
if len(videoids) != len(vid_embeds):
import pdb;
pdb.set_trace()
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.')
args.add_argument('--save_feats', default=None,
help='path to store text & video feats, this is for saving embeddings if you want to do offline retrieval.')
args.add_argument('--save_type', default='both', choices=['both', 'text', 'video'],
help='Whether to save video, text or both feats. If running on inference videos, text is just a placeholder')
args.add_argument('--vis_token_similarity', action='store_true')
args.add_argument('--split', default='test', choices=['train', 'val', 'test'],
help='split to evaluate on.')
args.add_argument('--batch_size', default=16, type=int,
help='size of batch')
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()