-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
397 lines (321 loc) · 12.3 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
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
import csv
import torch
# from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report, confusion_matrix
import shutil
import numpy as np
import torch.utils.data as data
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision import datasets, models, transforms
from spatial_transforms import *
import json
import os
import time
import os
import copy
from collections import defaultdict
from functools import partial
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class Logger(object):
def __init__(self, path, header):
self.log_file = open(path, 'w')
self.logger = csv.writer(self.log_file, delimiter='\t')
self.logger.writerow(header)
self.header = header
def __del(self):
self.log_file.close()
def log(self, values):
write_values = []
for col in self.header:
assert col in values
write_values.append(values[col])
self.logger.writerow(write_values)
self.log_file.flush()
class Queue:
# Constructor creates a list
def __init__(self, max_size, n_classes):
self.queue = list(np.zeros((max_size, n_classes), dtype=float).tolist())
self.max_size = max_size
self.median = None
self.ma = None
self.ewma = None
# Adding elements to queue
def enqueue(self, data):
self.queue.insert(0, data)
self.median = self._median()
self.ma = self._ma()
self.ewma = self._ewma()
return True
# Removing the last element from the queue
def dequeue(self):
if len(self.queue) > 0:
return self.queue.pop()
return ("Queue Empty!")
# Getting the size of the queue
def size(self):
return len(self.queue)
# printing the elements of the queue
def printQueue(self):
return self.queue
# Average
def _ma(self):
return np.array(self.queue[:self.max_size]).mean(axis=0)
# Median
def _median(self):
return np.median(np.array(self.queue[:self.max_size]), axis=0)
# Exponential average
def _ewma(self):
weights = np.exp(np.linspace(-1., 0., self.max_size))
weights /= weights.sum()
average = weights.reshape(1, self.max_size).dot(np.array(self.queue[:self.max_size]))
return average.reshape(average.shape[1], )
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
def get_class_labels(data):
class_labels_map = {}
index = 0
for class_label in data['labels']:
class_labels_map[class_label] = index
index += 1
return class_labels_map
def get_video_names_and_annotations(data, subset):
subset='validation'
video_names = []
annotations = []
for key, value in data['database'].items():
this_subset = value['subset']
if this_subset == subset:
label = value['annotations']['label']
#video_names.append('{}/{}'.format(label, key))
video_names.append(key)
annotations.append(value['annotations'])
return video_names, annotations
def load_value_file(file_path):
with open(file_path, 'r') as input_file:
value = float(input_file.read().rstrip('\n\r'))
return value
def load_annotation_data(data_file_path):
with open(data_file_path, 'r') as data_file:
return json.load(data_file)
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video,
sample_duration):
data = load_annotation_data(annotation_path)
video_names, annotations = get_video_names_and_annotations(data, subset)
class_to_idx = get_class_labels(data)
idx_to_class = {}
for name, label in class_to_idx.items():
idx_to_class[label] = name
dataset = []
for i in range(len(video_names)):
if i % 1000 == 0:
print('dataset loading [{}/{}]'.format(i, len(video_names)))
video_path = os.path.join(root_path, video_names[i])
if not os.path.exists(video_path):
print(video_path)
continue
n_frames_file_path = os.path.join(video_path, 'n_frames')
n_frames = int(load_value_file(n_frames_file_path))
if n_frames <= 0:
continue
begin_t = 1
end_t = n_frames
sample = {
'video': video_path,
'segment': [begin_t, end_t],
'n_frames': n_frames,
#'video_id': video_names[i].split('/')[1]
'video_id': video_names[i]
}
if len(annotations) != 0:
sample['label'] = class_to_idx[annotations[i]['label']]
else:
sample['label'] = -1
if n_samples_for_each_video == 1:
sample['frame_indices'] = list(range(1, n_frames + 1))
dataset.append(sample)
else:
if n_samples_for_each_video > 1:
step = max(1,
math.ceil((n_frames - 1 - sample_duration) /
(n_samples_for_each_video - 1)))
else:
step = sample_duration
for j in range(1, n_frames, step):
sample_j = copy.deepcopy(sample)
sample_j['frame_indices'] = list(
range(j, min(n_frames + 1, j + sample_duration)))
dataset.append(sample_j)
return dataset, idx_to_class
def get_default_video_loader():
image_loader = get_default_image_loader()
return partial(video_loader, image_loader=image_loader)
def video_loader(video_dir_path, frame_indices, sample_duration, image_loader):
video = []
for i in frame_indices:
image_path = os.path.join(video_dir_path, '{:05d}.jpg'.format(i))
if os.path.exists(image_path):
video.append(image_loader(image_path))
else:
return video
return video
def get_default_image_loader():
from torchvision import get_image_backend
if get_image_backend() == 'accimage':
return accimage_loader
else:
return pil_loader
def LevenshteinDistance(a, b):
# This is a straightforward implementation of a well-known algorithm, and thus
# probably shouldn't be covered by copyright to begin with. But in case it is,
# the author (Magnus Lie Hetland) has, to the extent possible under law,
# dedicated all copyright and related and neighboring rights to this software
# to the public domain worldwide, by distributing it under the CC0 license,
# version 1.0. This software is distributed without any warranty. For more
# information, see <http://creativecommons.org/publicdomain/zero/1.0>
"Calculates the Levenshtein distance between a and b."
n, m = len(a), len(b)
if n > m:
# Make sure n <= m, to use O(min(n,m)) space
a, b = b, a
n, m = m, n
current = range(n + 1)
for i in range(1, m + 1):
previous, current = current, [i] + [0] * n
for j in range(1, n + 1):
add, delete = previous[j] + 1, current[j - 1] + 1
change = previous[j - 1]
if a[j - 1] != b[i - 1]:
change = change + 1
current[j] = min(add, delete, change)
if current[n]<0:
return 0
else:
return current[n]
def load_value_file(file_path):
with open(file_path, 'r') as input_file:
value = float(input_file.read().rstrip('\n\r'))
return value
def calculate_accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def calculate_precision(outputs, targets):
_, pred = outputs.topk(1, 1, True)
pred = pred.t()
return precision_score(targets.view(-1), pred.view(-1), average = 'macro')
def calculate_recall(outputs, targets):
_, pred = outputs.topk(1, 1, True)
pred = pred.t()
return recall_score(targets.view(-1), pred.view(-1), average = 'macro')
def save_checkpoint(state, is_best, opt):
torch.save(state, '%s/%s_checkpoint.pth' % (opt.result_path, opt.store_name))
if is_best:
shutil.copyfile('%s/%s_checkpoint.pth' % (opt.result_path, opt.store_name),'%s/%s_best.pth' % (opt.result_path, opt.store_name))
def adjust_learning_rate(optimizer, epoch, opt):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
lr_new = opt.learning_rate * (0.1 ** (sum(epoch >= np.array(opt.lr_steps))))
for param_group in optimizer.param_groups:
param_group['lr'] = lr_new
#param_group['lr'] = opt.learning_rate
class Jester(data.Dataset):
"""
Args:
root (string): Root directory path.
spatial_transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
temporal_transform (callable, optional): A function/transform that takes in a list of frame indices
and returns a transformed version
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
loader (callable, optional): A function to load an video given its path and frame indices.
Attributes:
classes (list): List of the class names.
class_to_idx (dict): Dict with items (class_name, class_index).
imgs (list): List of (image path, class_index) tuples
"""
def __init__(self,
root_path,
annotation_path,
subset,
n_samples_for_each_video=1,
spatial_transform= Compose([
#Scale(opt.sample_size),
Scale(112),
CenterCrop(112),
ToTensor(1), Normalize([0, 0, 0], [1, 1, 1]) ]),
temporal_transform= TemporalCenterCrop(16,1),
target_transform=ClassLabel()
,
sample_duration=16,
get_loader=get_default_video_loader):
self.data, self.class_names = make_dataset(
root_path, annotation_path, subset, n_samples_for_each_video,
sample_duration)
self.spatial_transform = spatial_transform
self.temporal_transform = temporal_transform
self.target_transform = target_transform
self.sample_duration = sample_duration
self.loader = get_loader()
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is class_index of the target class.
"""
path = self.data[index]['video']
frame_indices = self.data[index]['frame_indices']
if self.temporal_transform is not None:
frame_indices = self.temporal_transform(frame_indices)
clip = self.loader(path, frame_indices, self.sample_duration)
if self.spatial_transform is not None:
self.spatial_transform.randomize_parameters()
clip = [self.spatial_transform(img) for img in clip]
im_dim = clip[0].size()[-2:]
clip = torch.stack(clip, 0).permute(1, 0, 2, 3)
target = self.data[index]
if self.target_transform is not None:
target = self.target_transform(target)
return clip, target
def __len__(self):
return len(self.data)