-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytorch_implementation_new_way.py
1631 lines (1291 loc) · 53.3 KB
/
pytorch_implementation_new_way.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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# In[87]:
config = {
"model_name": "3D_model",
"output_dir": "trained_models",
"input_mode": "av",
"data_folder": "D:/something-something-project/data/videos/20bn-something-something-v2/",
"json_data_train": "D:/something-something-project/data/train_data.json",
"json_data_val": "D:/something-something-project/data/validation_data.json",
"json_data_test": "D:/something-something-project/data/something-something-v2-test.json",
"json_data_labels": "D:/something-something-project/data/something-something-v2-mylabels.json",
"num_workers": 8, # for parallel processing
"num_classes": 4, # number of classes to classify
"batch_size": 30,
"clip_size": 30,
"nclips_train": 1,
"nclips_val": 1,
"upscale_factor_train": 1.4,
"upscale_factor_eval": 1.0,
"step_size_train": 1,
"step_size_val": 1,
"lr": 0.008,
"last_lr": 0.00001,
"momentum": 0.9,
"weight_decay": 0.00001,
"num_epochs": 30,
"print_freq": 1,
"conv_model": "models.model3d_1",
"input_spatial_size": 84,
"column_units": 512,
"save_features": True
}
# In[29]:
# Load and read json data and construct a list containing video sample
# (name, id, label, path)
import os
import json
from collections import namedtuple
# In[30]:
ListData = namedtuple('ListData', ['id', 'label', 'path'])
# In[31]:
# defining a class to read json labels from <...>labels.json file provided in the dataset
class BaseDataset:
"Read json data and construct a list containing video sample ids, label and path"
def __init__(self, json_input_path, json_path_labels, data_root, extension, is_test=False):
self.json_input_path = json_input_path
self.json_path_labels = json_path_labels
self.data_root = data_root
self.extension = extension
self.is_test = is_test
self.classes = self.read_json_labels()
self.classes_dict = self.get_two_way_dict(self.classes)
self.json_data = self.read_json_input()
def read_json_labels(self):
classes = []
with open(self.json_path_labels, 'rb') as jsonfile:
json_reader = json.load(jsonfile)
for elem in json_reader:
classes.append(elem)
return sorted(classes)
def clean_template(self, template):
"""Replaces instances of '[something] --> 'something' """
template = template.replace("[", "")
template = template.replace("]", "")
return template
def get_two_way_dict(self, classes):
classes_dict = {}
for i, item in enumerate(classes):
classes_dict[item] = i
classes_dict[i] = item
return classes_dict
def read_json_input(self):
json_data = []
if not self.is_test:
with open(self.json_input_path, 'rb') as jsonfile:
json_reader = json.load(jsonfile)
for elem in json_reader:
label = self.clean_template(elem['template'])
if label not in self.classes:
continue
raise ValueError("Label mismatch! Please correct")
item = ListData(elem['id'], label, os.path.join(self.data_root + elem['id'] + self.extension))
json_data.append(item)
else:
with open(self.json_input_path, 'rb') as jsonfile:
json_reader = json.load(jsonfile)
for elem in json_reader:
# add a dummy label for all test samples
item = ListData(elem['id'],
"Holding something",
os.path.join(self.data_root,
elem['id'] + self.extension)
)
json_data.append(item)
return json_data
# In[32]:
# defining class specific to webm video format and inherit the base class
class WebMDataset(BaseDataset):
def __init__(self, json_input_path, json_path_labels, data_root, is_test=False):
EXTENSION = ".webm"
super().__init__(json_input_path, json_path_labels, data_root, EXTENSION, is_test)
# In[33]:
# testing the read json class declared above and seeing how the data looks like
# webmdataset = WebMDataset("D:/something-something-project/data/something-something-v2-train.json",
# "D:/something-something-project/data/something-something-v2-labels.json",
# "D:/something-something-project/data/videos/20bn-something-something-v2/")
# webmdataset.display_data_members()
# In[34]:
# As you can see, we have created a named tuple (name, id, location) format for all the train data
# So, we just need to pass the train/validation/test.json file and we will get this list of named tuples for further processing
# In[35]:
import cv2
import torch
import numpy as np
import numbers
import collections
import random
# In[56]:
# transforming video
# we will define a class which composes several transformations together
class ComposeMix:
"""
Composes several transformations together. It takes a list of transformations,
where each element of transform is a list with two elemts.
First being the transformation function itself, second being a string indicating "img" or "vid" transform
Args:
transforms (List[Transform, "<type>"]): list of transforms to compose. <type> = "img" | "vid"
Example:
>>> transforms.ComposeMix([
[RandomCropVideo(84), "vid"],
[torchvision.transforms.ToTensor(), "img"],
[torchvision.transforms.Normalize(
mean=[0.485, 0.456, 0.406], # default values for imagenet
std=[0.229, 0.224, 0.225]
), "img"]
])
As you can see, we first randomly crop a video for 84x84 pixels,
then convert the cropped image into tensor and finally normalize the image using default values for imagenet.
"""
def __init__(self, transforms):
self.transforms = transforms
"""
What is __call__() method?
class Example:
def __init__(self):
print("Instance Created")
# Defining __call__ method
def __call__(self):
print("Instance is called via special method")
# Instance created
e = Example()
# __call__ method will be called
e()
Output:
Instance Created
Instance is called via special method
"""
def __call__(self, imgs):
for t in self.transforms:
if t[1] == "img":
for idx, img in enumerate(imgs):
imgs[idx] = t[0](img)
elif t[1] == "vid":
imgs = t[0](imgs)
else:
print ("Please specify the transform type")
raise ValueError
return imgs
class RandomCropVideo:
"""
Crop the given video frames at a random location. Crop location is the same for all the frames.
Args:
size (sequence or int): Desired output size of the crop. If size is an int instead of a sequence like
(w, h), a square crop (size, size) is made
padding: (cv2 constant): Method to be used for padding
"""
def __init__(self, size, padding=0, pad_method=cv2.BORDER_CONSTANT):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
self.padding = padding
self.pad_method = pad_method
def __call__(self, imgs):
"""
Args:
img (numpy.array): Video to be cropped
Returns:
numpy.array: Cropped Video
"""
th, tw = self.size
h, w = imgs[0].shape[:2]
# Return random integers from low (inclusive) to high (exclusive)
x1 = np.random.randint(0, w - tw)
y1 = np.random.randint(0, h - th)
for idx, img in enumerate(imgs):
if self.padding > 0:
img = cv2.copyMakeBorder(img, self.padding, self.padding,
self.padding, self.padding,
self.pad_method)
# sample crop locations if not given
# it is necessary to keep cropping same in a video
img_crop = img[y1:y1 + th, x1:x1 + tw]
imgs[idx] = img_crop
return imgs
class RandomHorizontalFlipVideo:
"""Horizontally flip the given video frames randomly with a given probability.
Args:
p (float): probability of the image being flipped. Default value is 0.5
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, imgs):
"""
Args:
imgs (numpy.array): Video to be flipped.
Returns:
numpy.array: Randomly flipped video.
"""
if random.random() < self.p:
for idx, img in enumerate(imgs):
imgs[idx] = cv2.flip(img, 1)
return imgs
def __repr__(self):
return self.__class__.__name__ + '(p={})'.format(self.p)
class RandomReverseTimeVideo(object):
"""Reverse the given video frames in time randomly with a given probability.
Args:
p (float): probability of the image being flipped. Default value is 0.5
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, imgs):
"""
Args:
imgs (numpy.array): Video to be flipped.
Returns:
numpy.array: Randomly flipped video.
"""
if random.random() < self.p:
imgs = imgs[::-1]
return imgs
def __repr__(self):
return self.__class__.__name__ + '(p={})'.format(self.p)
class RandomRotationVideo(object):
"""Rotate the given video frames randomly with a given degree.
Args:
degree (float): degrees used to rotate the video
"""
def __init__(self, degree=10):
self.degree = degree
def __call__(self, imgs):
"""
Args:
imgs (numpy.array): Video to be rotated.
Returns:
numpy.array: Randomly rotated video.
"""
h, w = imgs[0].shape[:2]
degree_sampled = np.random.choice(
np.arange(-self.degree, self.degree, 0.5))
M = cv2.getRotationMatrix2D((w / 2, h / 2), degree_sampled, 1)
for idx, img in enumerate(imgs):
imgs[idx] = cv2.warpAffine(img, M, (w, h))
return imgs
def __repr__(self):
return self.__class__.__name__ + '(degree={})'.format(self.degree_sampled)
class IdentityTransform(object):
"""
Returns same video back
"""
def __init__(self,):
pass
def __call__(self, imgs):
return imgs
class Scale(object):
r"""Rescale the input image to the given size.
Args:
size (sequence or int): Desired output size. If size is a sequence like
(w, h), output size will be matched to this. If size is an int,
smaller edge of the image will be matched to this number.
i.e, if height > width, then image will be rescaled to
(size * height / width, size)
interpolation (int, optional): Desired interpolation. Default is
``cv2.INTER_LINEAR``
"""
def __init__(self, size, interpolation=cv2.INTER_LINEAR):
assert isinstance(size, int) or (isinstance(
size, collections.Iterable) and len(size) == 2)
self.size = size
self.interpolation = interpolation
def __call__(self, img):
"""
Args:
img (numpy.array): Image to be scaled.
Returns:
numpy.array: Rescaled image.
"""
if isinstance(self.size, int):
h, w = img.shape[:2]
if (w <= h and w == self.size) or (h <= w and h == self.size):
return img
if w < h:
ow = self.size
oh = int(self.size * h / w)
if ow < w:
return cv2.resize(img, (ow, oh), cv2.INTER_AREA)
else:
return cv2.resize(img, (ow, oh))
else:
oh = self.size
ow = int(self.size * w / h)
if oh < h:
return cv2.resize(img, (ow, oh), cv2.INTER_AREA)
else:
return cv2.resize(img, (ow, oh))
else:
return cv2.resize(img, tuple(self.size))
class UnNormalize(object):
"""Unnormalize an tensor image with mean and standard deviation.
Given mean: (R, G, B) and std: (R, G, B),
will normalize each channel of the torch.*Tensor, i.e.
channel = (channel x std) + mean
Args:
mean (sequence): Sequence of means for R, G, B channels respecitvely.
std (sequence): Sequence of standard deviations for R, G, B channels
respecitvely.
"""
def __init__(self, mean, std):
self.mean = np.array(mean).astype('float32')
self.std = np.array(std).astype('float32')
def __call__(self, tensor):
"""
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
Tensor: Normalized image.
"""
if isinstance(tensor, torch.Tensor):
self.mean = torch.FloatTensor(self.mean)
self.std = torch.FloatTensor(self.std)
if (self.std.dim() != tensor.dim() or
self.mean.dim() != tensor.dim()):
for i in range(tensor.dim() - self.std.dim()):
self.std = self.std.unsqueeze(-1)
self.mean = self.mean.unsqueeze(-1)
tensor = torch.add(torch.mul(tensor, self.std), self.mean)
else:
# Relying on Numpy broadcasting abilities
tensor = tensor * self.std + self.mean
return tensor
# In[57]:
# data augmentor
class Augmentor:
def __init__(self, augmentation_mappings_json=None,
augmentation_types_todo=None,
fps_jitter_factors=[1, 0.75, 0.5]):
self.augmentation_mappings_json = augmentation_mappings_json
self.augmentation_types_todo = augmentation_types_todo
self.fps_jitter_factors = fps_jitter_factors
# read json to get the mapping dict
self.augmentation_mapping = self.read_augmentation_mapping(
self.augmentation_mappings_json)
self.augmentation_transforms = self.define_augmentation_transforms()
def __call__(self, imgs, label):
if not self.augmentation_mapping:
return imgs, label
else:
candidate_augmentations = {"same": label}
for candidate in self.augmentation_types_todo:
if candidate == "jitter_fps":
continue
if label in self.augmentation_mapping[candidate]:
if isinstance(self.augmentation_mapping[candidate], list):
candidate_augmentations[candidate] = label
elif isinstance(self.augmentation_mapping[candidate], dict):
candidate_augmentations[candidate] = self.augmentation_mapping[candidate][label]
else:
print("Something wrong with data type specified in "
"augmentation file. Please check!")
augmentation_chosen = np.random.choice(list(candidate_augmentations.keys()))
imgs = self.augmentation_transforms[augmentation_chosen](imgs)
label = candidate_augmentations[augmentation_chosen]
return imgs, label
def read_augmentation_mapping(self, path):
if path:
with open(path, "rb") as fp:
mapping = json.load(fp)
else:
mapping = None
return mapping
def define_augmentation_transforms(self, ):
augmentation_transforms = {}
augmentation_transforms["same"] = IdentityTransform()
augmentation_transforms["left/right"] = RandomHorizontalFlipVideo(1)
augmentation_transforms["left/right agnostic"] = RandomHorizontalFlipVideo(1)
augmentation_transforms["reverse time"] = RandomReverseTimeVideo(1)
augmentation_transforms["reverse time agnostic"] = RandomReverseTimeVideo(0.5)
return augmentation_transforms
def jitter_fps(self, framerate):
if self.augmentation_types_todo and "jitter_fps" in self.augmentation_types_todo:
jitter_factor = np.random.choice(self.fps_jitter_factors)
return int(jitter_factor * framerate)
else:
return framerate
# In[58]:
import os
import sys
import json
import pickle
import argparse
import torch
import shutil
import glob
import numpy as np
# In[59]:
# utility functions
def load_args():
parser = argparse.ArgumentParser(description='Smth-Smth example training')
parser.add_argument('--config', '-c', help='json config file path')
parser.add_argument('--eval_only', '-e', action='store_true',
help="evaluate trained model on validation data.")
parser.add_argument('--resume', '-r', action='store_true',
help="resume training from a given checkpoint.")
parser.add_argument('--gpus', '-g', help="GPU ids to use. Please"
" enter a comma separated list")
parser.add_argument('--use_cuda', action='store_true',
help="to use GPUs")
args = parser.parse_args()
if len(sys.argv) < 2:
parser.print_help()
sys.exit(1)
return args
def remove_module_from_checkpoint_state_dict(state_dict):
"""
Removes the prefix `module` from weight names that gets added by
torch.nn.DataParallel()
"""
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
return new_state_dict
def load_json_config(path):
""" loads a json config file"""
with open(path) as data_file:
config = json.load(data_file)
config = config_init(config)
return config
def config_init(config):
""" Some of the variables that should exist and contain default values """
if "augmentation_mappings_json" not in config:
config["augmentation_mappings_json"] = None
if "augmentation_types_todo" not in config:
config["augmentation_types_todo"] = None
return config
def setup_cuda_devices(args):
device_ids = []
device = torch.device("cuda" if args.use_cuda else "cpu")
if device.type == "cuda":
device_ids = [int(i) for i in args.gpus.split(',')]
return device, device_ids
def save_checkpoint(state, is_best, config, filename='checkpoint.pth.tar'):
checkpoint_path = os.path.join(config['output_dir'], config['model_name'], filename)
model_path = os.path.join(config['output_dir'], config['model_name'], 'model_best.pth.tar')
torch.save(state, checkpoint_path)
if is_best:
print(" > Best model found at this epoch. Saving ...")
shutil.copyfile(checkpoint_path, model_path)
def save_results(logits_matrix, features_matrix, targets_list, item_id_list,
class_to_idx, config):
"""
Saves the predicted logits matrix, true labels, sample ids and class
dictionary for further analysis of results
"""
print("Saving inference results ...")
path_to_save = os.path.join(
config['output_dir'], config['model_name'], "test_results.pkl")
with open(path_to_save, "wb") as f:
pickle.dump([logits_matrix, features_matrix, targets_list,
item_id_list, class_to_idx], f)
def save_images_for_debug(dir_img, imgs):
"""
2x3x12x224x224 --> [BS, C, seq_len, H, W]
"""
print("Saving images to {}".format(dir_img))
from matplotlib import pylab as plt
imgs = imgs.permute(0, 2, 3, 4, 1) # [BS, seq_len, H, W, C]
imgs = imgs.mul(255).numpy()
if not os.path.exists(dir_img):
os.makedirs(dir_img)
print(imgs.shape)
for batch_id, batch in enumerate(imgs):
batch_dir = os.path.join(dir_img, "batch{}".format(batch_id + 1))
if not os.path.exists(batch_dir):
os.makedirs(batch_dir)
for j, img in enumerate(batch):
plt.imsave(os.path.join(batch_dir, "frame{%04d}.png" % (j + 1)),
img.astype("uint8"))
def get_submission(logits_matrix, item_id_list, class_to_idx, config):
top5_classes_pred_list = []
for i, id in enumerate(item_id_list):
logits_sample = logits_matrix[i]
logits_sample_top5 = logits_sample.argsort()[-5:][::-1]
# top1_class_index = logits_sample.argmax()
# top1_class_label = class_to_idx[top1_class_index]
top5_classes_pred_list.append(logits_sample_top5)
path_to_save = os.path.join(
config['output_dir'], config['model_name'], "test_submission.csv")
with open(path_to_save, 'w') as fw:
for id, top5_pred in zip(item_id_list, top5_classes_pred_list):
fw.write("{}".format(id))
for elem in top5_pred:
fw.write(";{}".format(elem))
fw.write("\n")
def 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
class ExperimentalRunCleaner(object):
"""
Remove the output dir, if you exit with Ctrl+C and if there are less
then 1 file. It prevents the noise of experimental runs.
"""
def __init__(self, save_dir):
self.save_dir = save_dir
def __call__(self, signal, frame):
num_files = len(glob.glob(self.save_dir + "/*"))
if num_files < 1:
print('Removing: {}'.format(self.save_dir))
shutil.rmtree(self.save_dir)
print('You pressed Ctrl+C!')
sys.exit(0)
# In[60]:
# video parser
# PyAV is for direct and precise access to your media via containers, streams, packets, codecs, and frames.
# It exposes a few transformations of that data, and helps you get your data to/from other packages (e.g. Numpy and Pillow).
import av
import torch
import numpy as np
import torchvision
# In[61]:
class VideoFolder(torch.utils.data.Dataset):
def __init__(self, root, json_file_input, json_file_labels, clip_size,
nclips, step_size, is_val, transform_pre=None, transform_post=None,
augmentation_mappings_json=None, augmentation_types_todo=None,
get_item_id=False, is_test=False):
self.dataset_object = WebMDataset(json_file_input, json_file_labels,
root, is_test=is_test)
self.json_data = self.dataset_object.json_data
self.classes = self.dataset_object.classes
self.classes_dict = self.dataset_object.classes_dict
self.root = root
self.transform_pre = transform_pre
self.transform_post = transform_post
self.augmentor = Augmentor(augmentation_mappings_json,
augmentation_types_todo)
self.clip_size = clip_size
self.nclips = nclips
self.step_size = step_size
self.is_val = is_val
self.get_item_id = get_item_id
def __getitem__(self, index):
"""
[!] FPS jittering doesn't work with AV dataloader as of now
"""
item = self.json_data[index]
# Open video file
reader = av.open(item.path)
try:
imgs = []
imgs = [f.to_rgb().to_nd_array() for f in reader.decode(video=0)]
except (RuntimeError, ZeroDivisionError) as exception:
print('{}: WEBM reader cannot open {}. Empty '
'list returned.'.format(type(exception).__name__, item.path))
imgs = self.transform_pre(imgs)
imgs, label = self.augmentor(imgs, item.label)
imgs = self.transform_post(imgs)
num_frames = len(imgs)
target_idx = self.classes_dict[label]
if self.nclips > -1:
num_frames_necessary = self.clip_size * self.nclips * self.step_size
else:
num_frames_necessary = num_frames
offset = 0
if num_frames_necessary < num_frames:
# If there are more frames, then sample starting offset.
diff = (num_frames - num_frames_necessary)
# temporal augmentation
if not self.is_val:
offset = np.random.randint(0, diff)
imgs = imgs[offset: num_frames_necessary + offset: self.step_size]
if len(imgs) < (self.clip_size * self.nclips):
imgs.extend([imgs[-1]] *
((self.clip_size * self.nclips) - len(imgs)))
# format data to torch
data = torch.stack(imgs)
data = data.permute(1, 0, 2, 3)
if self.get_item_id:
return (data, target_idx, item.id)
else:
return (data, target_idx)
def __len__(self):
return len(self.json_data)
# In[62]:
# testing VideoFolder class
"""
upscale_size = int(84 * 1.1)
transform_pre = ComposeMix([
[Scale(upscale_size), "img"],
[RandomCropVideo(84), "vid"]
])
transform_post = ComposeMix([
[torchvision.transforms.ToTensor(), "img"]
])
loader = VideoFolder(root = "D:/something-something-project/data/videos/20bn-something-something-v2/",
json_file_input = "D:/something-something-project/data/something-something-v2-train.json",
json_file_labels = "D:/something-something-project/data/something-something-v2-labels.json",
clip_size = 36,
nclips = 1,
step_size = 1,
is_val = False,
transform_pre = transform_pre,
transform_post = transform_post)
import time
from tqdm import tqdm
# change the number of workers to 8 or something since jupyter notebook has some issues. Using 0 works fine
batch_loader = torch.utils.data.DataLoader(loader, batch_size=10, shuffle=False, num_workers=8, pin_memory=True)
start = time.time()
for i, a in enumerate(tqdm(batch_loader)):
if i > 100:
break
pass
print ("Size --> {}".format(a[0].size()))
print (time.time() - start)
"""
# In[63]:
import sys
import time
import os
import warnings
import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pylab as plt
from torch.optim.optimizer import Optimizer
###############################################################################
# TRAINING CALLBACKS
###############################################################################
class PlotLearning(object):
def __init__(self, save_path, num_classes):
self.accuracy = []
self.val_accuracy = []
self.losses = []
self.val_losses = []
self.learning_rates = []
self.save_path_loss = os.path.join(save_path, 'loss_plot.png')
self.save_path_accu = os.path.join(save_path, 'accu_plot.png')
self.save_path_lr = os.path.join(save_path, 'lr_plot.png')
self.init_loss = -np.log(1.0 / num_classes)
def plot(self, logs):
self.accuracy.append(logs.get('acc'))
self.val_accuracy.append(logs.get('val_acc'))
self.losses.append(logs.get('loss'))
self.val_losses.append(logs.get('val_loss'))
self.learning_rates.append(logs.get('learning_rate'))
best_val_acc = max(self.val_accuracy)
best_train_acc = max(self.accuracy)
best_val_epoch = self.val_accuracy.index(best_val_acc)
best_train_epoch = self.accuracy.index(best_train_acc)
plt.figure(1)
plt.gca().cla()
plt.ylim(0, 1)
plt.plot(self.accuracy, label='train')
plt.plot(self.val_accuracy, label='valid')
plt.title("best_val@{0:}-{1:.2f}, best_train@{2:}-{3:.2f}".format(
best_val_epoch, best_val_acc, best_train_epoch, best_train_acc))
plt.legend()
plt.savefig(self.save_path_accu)
best_val_loss = min(self.val_losses)
best_train_loss = min(self.losses)
best_val_epoch = self.val_losses.index(best_val_loss)
best_train_epoch = self.losses.index(best_train_loss)
plt.figure(2)
plt.gca().cla()
plt.ylim(0, self.init_loss)
plt.plot(self.losses, label='train')
plt.plot(self.val_losses, label='valid')
plt.title("best_val@{0:}-{1:.2f}, best_train@{2:}-{3:.2f}".format(
best_val_epoch, best_val_loss, best_train_epoch, best_train_loss))
plt.legend()
plt.savefig(self.save_path_loss)
min_learning_rate = min(self.learning_rates)
max_learning_rate = max(self.learning_rates)
plt.figure(2)
plt.gca().cla()
plt.ylim(0, max_learning_rate)
plt.plot(self.learning_rates)
plt.title("max_learning_rate-{0:.6f}, min_learning_rate-{1:.6f}".format(max_learning_rate, min_learning_rate))
plt.savefig(self.save_path_lr)
# Taken from keras.keras.utils.generic_utils
class Progbar(object):
"""Displays a progress bar.
# Arguments
target: Total number of steps expected.
interval: Minimum visual progress update interval (in seconds).
"""
def __init__(self, target, width=30, verbose=1, interval=0.05):
self.width = width
self.target = target
self.sum_values = {}
self.unique_values = []
self.start = time.time()
self.last_update = 0
self.interval = interval
self.total_width = 0
self.seen_so_far = 0
self.verbose = verbose
def update(self, current, values=None, force=False):
"""Updates the progress bar.
# Arguments
current: Index of current step.
values: List of tuples (name, value_for_last_step).
The progress bar will display averages for these values.
force: Whether to force visual progress update.
"""
values = values or []
for k, v in values:
if k not in self.sum_values:
self.sum_values[k] = [v * (current - self.seen_so_far),
current - self.seen_so_far]
self.unique_values.append(k)
else:
self.sum_values[k][0] += v * (current - self.seen_so_far)
self.sum_values[k][1] += (current - self.seen_so_far)
self.seen_so_far = current
now = time.time()
if self.verbose == 1:
if not force and (now - self.last_update) < self.interval:
return
prev_total_width = self.total_width
sys.stdout.write('\b' * prev_total_width)
sys.stdout.write('\r')
numdigits = int(np.floor(np.log10(self.target))) + 1
barstr = '%%%dd/%%%dd [' % (numdigits, numdigits)
bar = barstr % (current, self.target)
prog = float(current) / self.target
prog_width = int(self.width * prog)
if prog_width > 0:
bar += ('=' * (prog_width - 1))
if current < self.target:
bar += '>'
else:
bar += '='
bar += ('.' * (self.width - prog_width))
bar += ']'
sys.stdout.write(bar)
self.total_width = len(bar)
if current:
time_per_unit = (now - self.start) / current
else:
time_per_unit = 0
eta = time_per_unit * (self.target - current)
info = ''
if current < self.target:
info += ' - ETA: %ds' % eta
else:
info += ' - %ds' % (now - self.start)
for k in self.unique_values:
info += ' - %s:' % k
if isinstance(self.sum_values[k], list):
avg = self.sum_values[k][0] / max(1, self.sum_values[k][1])
if abs(avg) > 1e-3:
info += ' %.4f' % avg
else:
info += ' %.4e' % avg
else:
info += ' %s' % self.sum_values[k]
self.total_width += len(info)
if prev_total_width > self.total_width:
info += ((prev_total_width - self.total_width) * ' ')
sys.stdout.write(info)
sys.stdout.flush()
if current >= self.target:
sys.stdout.write('\n')
if self.verbose == 2:
if current >= self.target:
info = '%ds' % (now - self.start)
for k in self.unique_values:
info += ' - %s:' % k
avg = self.sum_values[k][0] / max(1, self.sum_values[k][1])