-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.py
187 lines (153 loc) · 6.49 KB
/
utils.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
import os
import cv2
import torch
import argparse
import numpy as np
import pickle as pk
from sklearn.model_selection import train_test_split
def collate_student(batch):
anchors, positives, negatives, similarities = zip(*batch)
videos = anchors + positives + negatives
num = len(videos)
max_len = max([s.size(0) for s in videos])
_, r, c = anchors[0].shape
padded_videos = videos[0].data.new(*(num, max_len, r, c)).fill_(0)
masks = videos[0].data.new(*(num, max_len)).fill_(0)
for i, tensor in enumerate(videos):
length = tensor.size(0)
padded_videos[i, :length] = tensor
masks[i, :length] = 1
similarities = torch.cat(similarities, 0)
return padded_videos, masks, similarities
def collate_selector(batch):
queries, targes, similarities, labels = zip(*batch)
videos = queries + targes
num = len(videos)
max_len = max([s.size(0) for s in videos])
max_reg = max([s.size(1) for s in videos])
padded_videos = videos[0].data.new(*(num, max_len, max_reg, 512)).fill_(0)
masks = videos[0].data.new(*(num, max_len)).fill_(0)
for i, tensor in enumerate(videos):
length = tensor.size(0)
padded_videos[i, :length] = tensor
masks[i, :length] = 1
similarities = torch.cat(similarities, 0)
labels = torch.cat(labels, 0)
return padded_videos, masks, similarities, labels
def collate_eval(batch):
videos, video_ids = zip(*batch)
num = len(videos)
max_len = max([s.size(0) for s in videos])
max_reg = max([s.size(1) for s in videos])
dims = videos[0].size(2)
padded_videos = videos[0].data.new(*(num, max_len, max_reg, dims)).fill_(0)
masks = videos[0].data.new(*(num, max_len)).fill_(0)
for i, tensor in enumerate(videos):
length = tensor.size(0)
padded_videos[i, :length] = tensor
masks[i, :length] = 1
return padded_videos, masks, video_ids
def save_model(args, model, optimizer, global_step):
d = dict()
d['model'] = model.state_dict()
d['optimizer'] = optimizer.state_dict()
d['global_step'] = global_step
d['args'] = args
if not os.path.exists(args.experiment_path):
os.makedirs(args.experiment_path)
torch.save(d, os.path.join(args.experiment_path, 'model_{}.pth'.format(
model.get_network_name())))
def bool_flag(s):
FALSY_STRINGS = {"off", "false", "0"}
TRUTHY_STRINGS = {"on", "true", "1"}
if s.lower() in FALSY_STRINGS:
return False
elif s.lower() in TRUTHY_STRINGS:
return True
else:
raise argparse.ArgumentTypeError("invalid value for a boolean flag")
def pprint_args(args):
for k, v in sorted(dict(vars(args)).items()):
if 'val_' not in k:
if 'fine' in args.student_type:
if 'netvlad' not in k and 'transformer' not in k:
if args.attention and 'binar' not in k:
print('%s: %s' % (k, str(v)))
elif args.binarization and 'attention' not in k:
print('%s: %s' % (k, str(v)))
elif not args.attention and not args.binarization and \
'attention' not in k and 'binar' not in k:
print('%s: %s' % (k, str(v)))
else:
if 'binar' not in k:
print('%s: %s' % (k, str(v)))
elif args.val_hdf5 is not None:
print('%s: %s' % (k, str(v)))
def center_crop(frame, desired_size):
if frame.ndim == 3:
old_size = frame.shape[:2]
top = int(np.maximum(0, (old_size[0] - desired_size)/2))
left = int(np.maximum(0, (old_size[1] - desired_size)/2))
return frame[top: top+desired_size, left: left+desired_size, :]
else:
old_size = frame.shape[1:3]
top = int(np.maximum(0, (old_size[0] - desired_size)/2))
left = int(np.maximum(0, (old_size[1] - desired_size)/2))
return frame[:, top: top+desired_size, left: left+desired_size, :]
def resize_frame(frame, desired_size):
min_size = np.min(frame.shape[:2])
ratio = desired_size / min_size
frame = cv2.resize(frame, dsize=(0, 0), fx=ratio, fy=ratio, interpolation=cv2.INTER_CUBIC)
return frame
def load_video(video, all_frames=False, fps=1, cc_size=None, rs_size=None):
cv2.setNumThreads(1)
cap = cv2.VideoCapture(video)
fps_div = fps
fps = cap.get(cv2.CAP_PROP_FPS)
if fps > 144 or fps is None:
fps = 25
frames = []
count = 0
while cap.isOpened():
ret = cap.grab()
if int(count % round(fps / fps_div)) == 0 or all_frames:
ret, frame = cap.retrieve()
if isinstance(frame, np.ndarray):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if rs_size is not None:
frame = resize_frame(frame, rs_size)
frames.append(frame)
else:
break
count += 1
cap.release()
frames = np.array(frames)
if cc_size is not None:
frames = center_crop(frames, cc_size)
return frames
def generate_selector_dataset(threshold, val_size=0.03, coarse_student='cg_student', fine_student='fg_att_student'):
with open('data/trainset_similarities_{}_iter2.pk'.format(fine_student), 'rb') as f:
pickle_file = pk.load(f)
index = pickle_file['index']
similarities_fine = pickle_file['pairs']
with open('data/trainset_similarities_{}_iter2.pk'.format(coarse_student), 'rb') as f:
similarities_coarse = pk.load(f)['pairs']
X, y = [], []
for query, pair_pools in similarities_fine.items():
for pos in pair_pools['positives']:
sim_fine = similarities_fine[query]['positives'][pos] / 2. + 0.5
sim_coarse = similarities_coarse[query]['positives'][pos]
x = [index[query], index[pos], sim_coarse]
X.append(np.array(x))
l = 1 if np.abs(sim_fine - sim_coarse) > threshold else 0
y.append(l)
for neg in pair_pools['negatives']:
sim_fine = similarities_fine[query]['negatives'][neg] / 2. + 0.5
sim_coarse = similarities_coarse[query]['negatives'][neg]
x = [index[query], index[neg], sim_coarse]
X.append(np.array(x))
l = 1 if np.abs(sim_fine - sim_coarse) > threshold else 0
y.append(l)
X = np.array(X)
y = np.array(y, dtype=np.float32)
return train_test_split(X, y, test_size=val_size, random_state=42)