This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvideo_dataset.py
1219 lines (1063 loc) · 53.5 KB
/
video_dataset.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
import os
import six
from typing import Union
import numpy as np
import torch
from PIL import Image
import torch.utils.data as data
try:
import lmdb
import pyarrow as pa
_HAS_LMDB = True
except ImportError as e:
_HAS_LMDB = False
_LMDB_ERROR_MSG = e
try:
import av
_HAS_PYAV = True
except ImportError as e:
_HAS_PYAV = False
_PYAV_ERROR_MSG = e
def random_clip(video_frames, sampling_rate, frames_per_clip, fixed_offset=False, start_frame_idx=0, end_frame_idx=None):
"""
Args:
video_frames (int): total frame number of a video
sampling_rate (int): sampling rate for clip, pick one every k frames
frames_per_clip (int): number of frames of a clip
fixed_offset (bool): used with sample offset to decide the offset value deterministically.
Returns:
list[int]: frame indices (started from zero)
"""
new_sampling_rate = sampling_rate
highest_idx = video_frames - new_sampling_rate * frames_per_clip if end_frame_idx is None else end_frame_idx
if highest_idx <= 0:
random_offset = 0
else:
if fixed_offset:
random_offset = (video_frames - new_sampling_rate * frames_per_clip) // 2
else:
random_offset = int(np.random.randint(start_frame_idx, highest_idx, 1))
# print(start_frame_idx, highest_idx, random_offset)
frame_idx = [int(random_offset + i * sampling_rate) % video_frames for i in range(frames_per_clip)]
return frame_idx
def compute_img_diff(image_1, image_2, bound=255.0):
image_diff = np.asarray(image_1, dtype=np.float) - np.asarray(image_2, dtype=np.float)
image_diff += bound
image_diff *= (255.0 / float(2 * bound))
image_diff = image_diff.astype(np.uint8)
image_diff = Image.fromarray(image_diff)
return image_diff
def load_image(root_path, directory, image_tmpl, idx, modality):
"""
:param root_path:
:param directory:
:param image_tmpl:
:param idx: if it is a list, load a batch of images
:param modality:
:return:
"""
def _safe_load_image(img_path):
img = None
num_try = 0
while num_try < 10:
try:
img_tmp = Image.open(img_path)
img = img_tmp.copy()
img_tmp.close()
break
except Exception as e:
print('[Will try load again] error loading image: {}, '
'error: {}'.format(img_path, str(e)))
num_try += 1
if img is None:
raise ValueError('[Fail 10 times] error loading image: {}'.format(img_path))
return img
if not isinstance(idx, list):
idx = [idx]
out = []
if modality == 'rgb':
for i in idx:
image_path_file = os.path.join(root_path, directory, image_tmpl.format(i))
out.append(_safe_load_image(image_path_file))
elif modality == 'rgbdiff':
tmp = {}
new_idx = np.unique(np.concatenate((np.asarray(idx), np.asarray(idx) + 1)))
for i in new_idx:
image_path_file = os.path.join(root_path, directory, image_tmpl.format(i))
tmp[i] = _safe_load_image(image_path_file)
for k in idx:
img_ = compute_img_diff(tmp[k + 1], tmp[k])
out.append(img_)
del tmp
elif modality == 'flow':
for i in idx:
flow_x_name = os.path.join(root_path, directory, "x_" + image_tmpl.format(i))
flow_y_name = os.path.join(root_path, directory, "y_" + image_tmpl.format(i))
out.extend([_safe_load_image(flow_x_name), _safe_load_image(flow_y_name)])
return out
def load_sound(data_dir, record, idx, fps, audio_length, resampling_rate,
window_size=10, step_size=5, eps=1e-6):
import librosa
"""idx must be the center frame of a clip"""
centre_sec = (record.start_frame + idx) / fps
left_sec = centre_sec - (audio_length / 2.0)
right_sec = centre_sec + (audio_length / 2.0)
audio_fname = os.path.join(data_dir, record.path)
# TODO: generate 0s if the audio file does not exist.
if not os.path.exists(audio_fname):
return [Image.fromarray(np.zeros((256, 256 * int(audio_length / 1.28))))]
samples, sr = librosa.core.load(audio_fname, sr=None, mono=True)
duration = samples.shape[0] / float(resampling_rate)
left_sample = int(round(left_sec * resampling_rate))
right_sample = int(round(right_sec * resampling_rate))
required_samples = int(round(resampling_rate * audio_length))
if left_sec < 0:
samples = samples[:required_samples]
elif right_sec > duration:
samples = samples[-required_samples:]
else:
samples = samples[left_sample:right_sample]
# TODO: is the size of spec is fixed if number of samples are different?
# if the samples is not long enough, repeat the waveform
if len(samples) < required_samples:
multiplies = required_samples / len(samples)
samples = np.tile(samples, int(multiplies + 0.5) + 1)
samples = samples[:required_samples]
# log sepcgram
nperseg = int(round(window_size * resampling_rate / 1e3))
noverlap = int(round(step_size * resampling_rate / 1e3))
spec = librosa.stft(samples, n_fft=511, window='hann', hop_length=noverlap,
win_length=nperseg, pad_mode='constant')
spec = np.log(np.real(spec * np.conj(spec)) + eps)
img = Image.fromarray(spec)
return [img]
def load_data_lmdb(videos, idx, modality):
def _convert_buffer_to_PIL(tmp_buf, is_flow=False):
data = six.BytesIO()
data.write(tmp_buf)
data.seek(0)
img_tmp = Image.open(data).convert('RGB' if not is_flow else 'L')
img_ = img_tmp.copy()
img_tmp.close()
return img_
img = []
if modality == 'rgb':
buf = [videos[i] for i in idx]
for x in buf:
img_ = _convert_buffer_to_PIL(x)
img.append(img_)
elif modality == 'flow':
new_idx = np.asarray(idx) * 2 - 1
buf = [[videos[i], videos[i + 1]] for i in new_idx]
for x in buf:
flow_x = _convert_buffer_to_PIL(x[0], True)
flow_y = _convert_buffer_to_PIL(x[1], True)
img.extend([flow_x, flow_y])
elif modality == 'rgbdiff':
tmp = {}
new_idx = np.unique(np.concatenate((np.asarray(idx), np.asarray(idx) + 1)))
for i in new_idx:
tmp[i] = _convert_buffer_to_PIL(videos[i])
for k in idx:
img_ = compute_img_diff(tmp[k + 1], tmp[k])
img.append(img_)
del tmp
return img
def sample_train_clip(video_length, num_consecutive_frames, num_frames, sample_freq, dense_sampling, num_clips=1):
max_frame_idx = max(1, video_length - num_consecutive_frames + 1)
if dense_sampling:
frame_idx = np.zeros((num_clips, num_frames), dtype=int)
if num_clips == 1: # backward compatibility
frame_idx[0] = np.asarray(random_clip(max_frame_idx, sample_freq, num_frames, False))
else:
max_start_frame_idx = max_frame_idx - sample_freq * num_frames
frames_per_segment = max_start_frame_idx // num_clips
for i in range(num_clips):
if frames_per_segment <= 0:
frame_idx[i] = np.asarray(random_clip(max_frame_idx, sample_freq, num_frames, False))
else:
frame_idx[i] = np.asarray(random_clip(max_frame_idx, sample_freq, num_frames, False, i * frames_per_segment, (i + 1) * frames_per_segment))
frame_idx = frame_idx.flatten()
"""
def _check_interval_overlapped(int_1, int_2):
if int_1[0] < int_2[0]:
int_l, int_r = int_1, int_2
else:
int_l, int_r = int_2, int_1
return True if int_l[-1] > int_r[0] else False
clips = 0
num_tries = 0
#all_frame_idx = np.arange(max_frame_idx - sample_freq * num_frames)
while clips < num_clips and num_tries < 1000:
curr_clips = np.asarray(random_clip(max_frame_idx, sample_freq, num_frames))
overlap = False
for i in range(clips):
overlap = _check_interval_overlapped((frame_idx[i][0], frame_idx[i][-1]), (curr_clips[0], curr_clips[-1]) )
if overlap:
break
if overlap:
num_tries += 1
continue
else:
frame_idx[clips] = curr_clips
clips += 1
for i in range(clips, num_clips):
frame_idx[i] = np.asarray(random_clip(max_frame_idx, sample_freq, num_frames))
# sort the intervals
frame_idx = frame_idx[np.argsort(frame_idx[:, 0]), ...]
frame_idx = frame_idx.flatten()
"""
else: # uniform sampling
total_frames = num_frames * sample_freq
ave_frames_per_group = max_frame_idx // num_frames
if ave_frames_per_group >= sample_freq:
# randomly sample f images per segement
frame_idx = np.arange(0, num_frames) * ave_frames_per_group
frame_idx = np.repeat(frame_idx, repeats=sample_freq)
offsets = np.random.choice(ave_frames_per_group, sample_freq, replace=False)
offsets = np.tile(offsets, num_frames)
frame_idx = frame_idx + offsets
elif max_frame_idx < total_frames:
# need to sample the same images
frame_idx = np.random.choice(max_frame_idx, total_frames)
else:
# sample cross all images
frame_idx = np.random.choice(max_frame_idx, total_frames, replace=False)
frame_idx = np.sort(frame_idx)
frame_idx = frame_idx + 1
return frame_idx
def sample_val_test_clip(video_length, num_consecutive_frames, num_frames, sample_freq, dense_sampling,
fixed_offset, num_clips, whole_video):
max_frame_idx = max(1, video_length - num_consecutive_frames + 1)
if whole_video:
return np.arange(1, max_frame_idx, step=sample_freq, dtype=int)
if dense_sampling:
if fixed_offset:
sample_pos = max(1, 1 + max_frame_idx - sample_freq * num_frames)
t_stride = sample_freq
start_list = np.linspace(0, sample_pos - 1, num=num_clips, dtype=int)
frame_idx = []
for start_idx in start_list.tolist():
frame_idx += [(idx * t_stride + start_idx) % max_frame_idx for idx in
range(num_frames)]
else:
frame_idx = []
for i in range(num_clips):
frame_idx.extend(random_clip(max_frame_idx, sample_freq, num_frames))
frame_idx = np.asarray(frame_idx) + 1
else: # uniform sampling
if fixed_offset:
frame_idices = []
sample_offsets = list(range(-num_clips // 2 + 1, num_clips // 2 + 1))
for sample_offset in sample_offsets:
if max_frame_idx > num_frames:
tick = max_frame_idx / float(num_frames)
curr_sample_offset = sample_offset
if curr_sample_offset >= tick / 2.0:
curr_sample_offset = tick / 2.0 - 1e-4
elif curr_sample_offset < -tick / 2.0:
curr_sample_offset = -tick / 2.0
frame_idx = np.array([int(tick / 2.0 + curr_sample_offset + tick * x) for x in
range(num_frames)])
else:
np.random.seed(sample_offset - (-num_clips // 2 + 1))
frame_idx = np.random.choice(max_frame_idx, num_frames)
frame_idx = np.sort(frame_idx)
frame_idices.extend(frame_idx.tolist())
else:
frame_idices = []
for i in range(num_clips):
total_frames = num_frames * sample_freq
ave_frames_per_group = max_frame_idx // num_frames
if ave_frames_per_group >= sample_freq:
# randomly sample f images per segment
frame_idx = np.arange(0, num_frames) * ave_frames_per_group
frame_idx = np.repeat(frame_idx, repeats=sample_freq)
offsets = np.random.choice(ave_frames_per_group, sample_freq,
replace=False)
offsets = np.tile(offsets, num_frames)
frame_idx = frame_idx + offsets
elif max_frame_idx < total_frames:
# need to sample the same images
np.random.seed(i)
frame_idx = np.random.choice(max_frame_idx, total_frames)
else:
# sample cross all images
np.random.seed(i)
frame_idx = np.random.choice(max_frame_idx, total_frames, replace=False)
frame_idx = np.sort(frame_idx)
frame_idices.extend(frame_idx.tolist())
frame_idx = np.asarray(frame_idices) + 1
return frame_idx
class VideoRecord(object):
def __init__(self, path, start_frame, end_frame, label, reverse=False):
self.path = path
self.video_id = os.path.basename(path)
self.start_frame = start_frame
self.end_frame = end_frame
self.label = label
self.reverse = reverse
@property
def num_frames(self):
return self.end_frame - self.start_frame + 1
def __str__(self):
return self.path
class VideoDataSet(data.Dataset):
def __init__(self, root_path, list_file, num_groups=64, frames_per_group=1, sample_offset=0, num_clips=1,
modality='rgb', dense_sampling=False, fixed_offset=True,
image_tmpl='{:05d}.jpg', transform=None, is_train=True, test_mode=False, seperator=' ',
filter_video=0, num_classes=None, whole_video=False,
fps=29.97, audio_length=1.28, resampling_rate=24000):
"""
Arguments have different meaning when dense_sampling is True:
- num_groups ==> number of frames
- frames_per_group ==> sample every K frame
- sample_offset ==> number of clips used in validation or test mode
Args:
root_path (str): the file path to the root of video folder
list_file (str): the file list, each line with folder_path, start_frame, end_frame, label_id
num_groups (int): number of frames per data sample
frames_per_group (int): number of frames within one group
sample_offset (int): used in validation/test, the offset when sampling frames from a group
modality (str): rgb or flow
dense_sampling (bool): dense sampling in I3D
fixed_offset (bool): used for generating the same videos used in TSM
image_tmpl (str): template of image ids
transform: the transformer for preprocessing
is_train (bool): shuffle the video but keep the causality
test_mode (bool): testing mode, no label
whole_video (bool): take whole video
fps (float): frame rate per second, used to localize sound when frame idx is selected.
audio_length (float): the time window to extract audio feature.
resampling_rate (int): used to resampling audio extracted from wav
"""
if modality not in ['flow', 'rgb', 'rgbdiff', 'sound']:
raise ValueError("modality should be 'flow' or 'rgb' or 'rgbdiff' or 'sound'.")
self.root_path = root_path
self.list_file = os.path.join(root_path, list_file)
self.num_groups = num_groups
self.num_frames = num_groups
self.frames_per_group = frames_per_group
self.sample_freq = frames_per_group
self.num_clips = num_clips
self.sample_offset = sample_offset
self.fixed_offset = fixed_offset
self.dense_sampling = dense_sampling
self.modality = modality.lower()
self.image_tmpl = image_tmpl
self.transform = transform
self.is_train = is_train
self.test_mode = test_mode
self.separator = seperator
self.filter_video = filter_video
self.whole_video = whole_video
self.fps = fps
self.audio_length = audio_length
self.resampling_rate = resampling_rate
self.video_length = (self.num_frames * self.sample_freq) / self.fps
if self.modality in ['flow', 'rgbdiff']:
self.num_consecutive_frames = 5
else:
self.num_consecutive_frames = 1
self.video_list, self.multi_label = self._parse_list()
self.num_classes = num_classes
def _parse_list(self):
# usually it is [video_id, num_frames, class_idx]
# or [video_id, start_frame, end_frame, list of class_idx]
tmp = []
original_video_numbers = 0
for x in open(self.list_file):
elements = x.strip().split(self.separator)
start_frame = int(elements[1])
end_frame = int(elements[2])
total_frame = end_frame - start_frame + 1
original_video_numbers += 1
if self.test_mode:
tmp.append(elements)
else:
if total_frame >= self.filter_video:
tmp.append(elements)
num = len(tmp)
print("The number of videos is {} (with more than {} frames) "
"(original: {})".format(num, self.filter_video, original_video_numbers), flush=True)
assert (num > 0)
# TODO: a better way to check if multi-label or not
multi_label = np.mean(np.asarray([len(x) for x in tmp])) > 4.0
file_list = []
for item in tmp:
if self.test_mode:
file_list.append([item[0], int(item[1]), int(item[2]), -1])
else:
labels = []
for i in range(3, len(item)):
labels.append(float(item[i]))
if not multi_label:
labels = labels[0] if len(labels) == 1 else labels
file_list.append([item[0], int(item[1]), int(item[2]), labels])
video_list = [VideoRecord(item[0], item[1], item[2], item[3]) for item in file_list]
# flow model has one frame less
if self.modality in ['rgbdiff']:
for i in range(len(video_list)):
video_list[i].end_frame -= 1
#if self.is_train:
# video_list = video_list[:50000]
return video_list, multi_label
def remove_data(self, idx):
original_video_num = len(self.video_list)
self.video_list = [v for i, v in enumerate(self.video_list) if i not in idx]
print("Original videos: {}\t remove {} videos, remaining {} videos".format(original_video_num, len(idx), len(self.video_list)))
def _sample_indices(self, record):
return sample_train_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.num_clips)
def _get_val_indices(self, record):
return sample_val_test_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.fixed_offset,
self.num_clips, self.whole_video)
def __getitem__(self, index):
"""
Returns:
torch.FloatTensor: (3xgxf)xHxW dimension, g is number of groups and f is the frames per group.
torch.FloatTensor: the label
"""
record = self.video_list[index]
# check this is a legit video folder
indices = self._sample_indices(record) if self.is_train else self._get_val_indices(record)
images = self.get_data(record, indices)
images = self.transform(images)
label = self.get_label(record)
# re-order data to targeted format.
return images, label
def get_data(self, record, indices):
images = []
if self.whole_video:
tmp = len(indices) % self.num_frames
if tmp != 0:
indices = indices[:-tmp]
num_clips = len(indices) // self.num_frames
# print(tmp, indices, self.num_frames, num_clips)
else:
num_clips = self.num_clips
if self.modality == 'sound':
new_indices = [indices[i * self.num_frames: (i + 1) * self.num_frames]
for i in range(num_clips)]
for curr_indiecs in new_indices:
center_idx = (curr_indiecs[self.num_frames // 2 - 1] + curr_indiecs[self.num_frames // 2]) // 2 \
if self.num_frames % 2 == 0 else curr_indiecs[self.num_frames // 2]
center_idx = min(record.num_frames, center_idx)
seg_imgs = load_sound(self.root_path, record, center_idx,
self.fps, self.audio_length, self.resampling_rate)
images.extend(seg_imgs)
else:
images = []
for seg_ind in indices:
new_seg_ind = [min(seg_ind + record.start_frame - 1 + i, record.num_frames)
for i in range(self.num_consecutive_frames)]
seg_imgs = load_image(self.root_path, record.path, self.image_tmpl,
new_seg_ind, self.modality)
images.extend(seg_imgs)
return images
def get_label(self, record):
if self.test_mode:
# in test mode, return the video id as label
label = record.video_id
else:
if not self.multi_label:
label = int(record.label)
else:
# create a binary vector.
label = torch.zeros(self.num_classes, dtype=torch.float)
for x in record.label:
label[int(x)] = 1.0
return label
def __len__(self):
return len(self.video_list)
class VideoDataSetLMDB(data.Dataset):
# do not support sound
def __init__(self, datadir, db_name, num_groups=16, frames_per_group=1, sample_offset=0, num_clips=1,
modality='rgb', dense_sampling=False, fixed_offset=True,
image_tmpl='{:05d}.jpg', transform=None, is_train=True, test_mode=False,
seperator=' ', filter_video=0, num_classes=None, whole_video=False,
fps=29.97, audio_length=1.28, resampling_rate=24000):
"""
Arguments have different meaning when dense_sampling is True:
- num_groups ==> number of frames
- frames_per_group ==> sample every K frame
- sample_offset ==> number of clips used in validation or test mode
Args:
db_path (str): the file path to the root of video folder
num_groups (int): number of frames per data sample
frames_per_group (int): number of frames within one group
sample_offset (int): used in validation/test, the offset when sampling frames from a group
modality (str): rgb or flow
dense_sampling (bool): dense sampling in I3D
fixed_offset (bool): used for generating the same videos used in TSM
image_tmpl (str): template of image ids
transform: the transformer for preprocessing
is_train (bool): shuffle the video but keep the causality
test_mode (bool): testing mode, no label
"""
# TODO: handle multi-label?
# TODO: flow data?
if not _HAS_LMDB:
raise ValueError(_LMDB_ERROR_MSG)
if modality not in ['flow', 'rgb', 'rgbdiff']:
raise ValueError("modality should be 'flow' or 'rgb'.")
self.db_path = os.path.join(datadir, db_name)
self.num_groups = num_groups
self.num_frames = num_groups
self.frames_per_group = frames_per_group
self.sample_freq = frames_per_group
self.num_clips = num_clips
self.sample_offset = sample_offset
self.fixed_offset = fixed_offset
self.dense_sampling = dense_sampling
self.modality = modality.lower()
self.image_tmpl = image_tmpl
self.transform = transform
self.is_train = is_train
self.test_mode = test_mode
self.seperator = seperator
self.filter_video = filter_video
self.whole_video = whole_video
self.fps = fps
self.audio_length = audio_length
self.resampling_rate = resampling_rate
self.video_length = (self.num_frames * self.sample_freq) / self.fps
if self.modality in ['flow', 'rgbdiff']:
self.num_consecutive_frames = 5
else:
self.num_consecutive_frames = 1
self.multi_label = None
self.db = None
db = lmdb.open(self.db_path, max_readers=1, subdir=os.path.isdir(self.db_path),
readonly=True, lock=False, readahead=False, meminit=False)
with db.begin(write=False) as txn:
self.length = pa.deserialize(txn.get(b'__len__'))
self.keys = pa.deserialize(txn.get(b'__keys__'))
db.close()
# TODO: a hack way to filter video
self.list_file = self.db_path.replace(".lmdb", ".txt")
valid_video_numbers = self.length
invalid_video_ids = []
if self.filter_video > 0:
valid_video_numbers = 0
invalid_video_ids = []
for x in open(self.list_file):
elements = x.strip().split(self.seperator)
start_frame = int(elements[1])
end_frame = int(elements[2])
total_frame = end_frame - start_frame + 1
if self.test_mode:
valid_video_numbers += 1
else:
if total_frame >= self.filter_video:
valid_video_numbers += 1
else:
name = u'{}'.format(elements[0].split("/")[-1]).encode('ascii')
invalid_video_ids.append(name)
print("The number of videos is {} (with more than {} frames) "
"(original: {})".format(valid_video_numbers, self.filter_video, self.length),
flush=True)
# remove keys and update length
self.length = valid_video_numbers
self.keys = [k for k in self.keys if k not in invalid_video_ids]
if self.length != len(self.keys):
raise ValueError("Do not filter video correctly.")
self.num_classes = num_classes
self.unpacked_video = None
def remove_data(self, idx):
original_video_num = self.length
self.keys = [v for i, v in enumerate(self.keys) if i not in idx]
self.length -= len(idx)
print("Original videos: {}\t remove {} videos, remaining {} videos".format(original_video_num, len(idx), self.length))
def _sample_indices(self, record):
return sample_train_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.num_clips)
def _get_val_indices(self, record):
return sample_val_test_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.fixed_offset,
self.num_clips, self.whole_video)
def __getitem__(self, index):
unpacked_video = self.maybe_open_and_get_buffer(index)
num_frames = unpacked_video[0] - 1 if self.modality == 'rgbdiff' else unpacked_video[0]
record = VideoRecord(self.keys[index].decode("utf-8"), 1, num_frames, unpacked_video[-1])
indices = self._sample_indices(record) if self.is_train else self._get_val_indices(record)
images = self.get_data(record, indices, unpacked_video)
images = self.transform(images)
label = self.get_label(record)
self.unpacked_video = None
# re-order data to targeted format.
return images, label
def maybe_open_and_get_buffer(self, index):
if self.db is None:
self.db = lmdb.open(self.db_path, max_readers=1, subdir=os.path.isdir(self.db_path),
readonly=True, lock=False, readahead=False, meminit=False)
with self.db.begin(write=False) as txn:
byteflow = txn.get(self.keys[index])
try:
unpacked_video = pa.deserialize(byteflow)
except Exception as e:
with self.db.begin(write=False) as txn:
byteflow = txn.get(self.keys[0])
unpacked_video = pa.deserialize(byteflow)
print(self.keys[index], e, flush=True)
self.unpacked_video = unpacked_video
return unpacked_video
def get_data(self, record, indices, unpacked_video):
images = []
for seg_ind in indices:
new_seg_ind = [min(seg_ind + record.start_frame - 1 + i, record.num_frames)
for i in range(self.num_consecutive_frames)]
img = load_data_lmdb(unpacked_video, new_seg_ind, self.modality)
images.extend(img)
return images
def get_label(self, record):
if self.test_mode:
# in test mode, return the video id as label
label = record.video_id
else:
if not self.multi_label:
label = int(record.label)
else:
# create a binary vector.
label = torch.zeros(self.num_classes, dtype=torch.float)
for x in record.label:
label[int(x)] = 1.0
return label
def __len__(self):
return self.length
class MultiVideoDataSet(data.Dataset):
def __init__(self, root_path, list_file, num_groups=64, frames_per_group=1, sample_offset=0, num_clips=1,
modality='rgb', dense_sampling=False, fixed_offset=True,
image_tmpl='{:05d}.jpg', transform=None, is_train=True, test_mode=False, seperator=' ',
filter_video=0, num_classes=None, whole_video=False,
fps=29.97, audio_length=1.28, resampling_rate=24000):
"""
# root_path, modality and transform become list, each for one modality
Argments have different meaning when dense_sampling is True:
- num_groups ==> number of frames
- frames_per_group ==> sample every K frame
- sample_offset ==> number of clips used in validation or test mode
Args:
root_path (str): the file path to the root of video folder
list_file (str): the file list, each line with folder_path, start_frame, end_frame, label_id
num_groups (int): number of frames per data sample
frames_per_group (int): number of frames within one group
sample_offset (int): used in validation/test, the offset when sampling frames from a group
modality (str): rgb or flow
dense_sampling (bool): dense sampling in I3D
fixed_offset (bool): used for generating the same videos used in TSM
image_tmpl (str): template of image ids
transform: the transformer for preprocessing
is_train (bool): shuffle the video but keep the causality
test_mode (bool): testing mode, no label
"""
video_datasets = []
for i in range(len(modality)):
tmp = VideoDataSet(root_path[i], os.path.join(root_path[i], list_file),
num_groups, frames_per_group, sample_offset,
num_clips, modality[i], dense_sampling, fixed_offset,
image_tmpl, transform[i], is_train, test_mode, seperator,
filter_video, num_classes, whole_video, fps, audio_length, resampling_rate)
video_datasets.append(tmp)
self.video_datasets = video_datasets
self.is_train = is_train
self.test_mode = test_mode
self.num_frames = num_groups
self.sample_freq = frames_per_group
self.dense_sampling = dense_sampling
self.num_clips = num_clips
self.fixed_offset = fixed_offset
self.modality = modality
self.num_classes = num_classes
self.whole_video = whole_video
self.video_list = video_datasets[0].video_list
self.num_consecutive_frames = max([x.num_consecutive_frames for x in self.video_datasets])
def _sample_indices(self, record):
return sample_train_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.num_clips)
def _get_val_indices(self, record):
return sample_val_test_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.fixed_offset,
self.num_clips, self.whole_video)
def remove_data(self, idx):
for i in range(len(self.video_datasets)):
self.video_datasets[i].remove_data(idx)
self.video_list = self.video_datasets[0].video_list
def __getitem__(self, index):
"""
Returns:
torch.FloatTensor: (3xgxf)xHxW dimension, g is number of groups and f is the frames per group.
torch.FloatTensor: the label
"""
record = self.video_list[index]
if self.is_train:
indices = self._sample_indices(record)
else:
indices = self._get_val_indices(record)
multi_modalities = []
for modality, video_dataset in zip(self.modality, self.video_datasets):
record = video_dataset.video_list[index]
images = video_dataset.get_data(record, indices)
images = video_dataset.transform(images)
label = video_dataset.get_label(record)
multi_modalities.append((images, label))
return [x for x, y in multi_modalities], multi_modalities[0][1]
def __len__(self):
return len(self.video_list)
class MultiVideoDataSetLMDB(data.Dataset):
def __init__(self, root_path, list_file, num_groups=64, frames_per_group=1, sample_offset=0, num_clips=1,
modality='rgb', dense_sampling=False, fixed_offset=True,
image_tmpl='{:05d}.jpg', transform=None, is_train=True, test_mode=False, seperator=' ',
filter_video=0, num_classes=None, whole_video=False,
fps=29.97, audio_length=1.28, resampling_rate=24000):
"""
# root_path, modality and transform become list, each for one modality
Argments have different meaning when dense_sampling is True:
- num_groups ==> number of frames
- frames_per_group ==> sample every K frame
- sample_offset ==> number of clips used in validation or test mode
Args:
root_path (str): the file path to the root of video folder
list_file (str): the file list, each line with folder_path, start_frame, end_frame, label_id
num_groups (int): number of frames per data sample
frames_per_group (int): number of frames within one group
sample_offset (int): used in validation/test, the offset when sampling frames from a group
modality (str): rgb or flow
dense_sampling (bool): dense sampling in I3D
fixed_offset (bool): used for generating the same videos used in TSM
image_tmpl (str): template of image ids
transform: the transformer for preprocessing
is_train (bool): shuffle the video but keep the causality
test_mode (bool): testing mode, no label
"""
video_datasets = []
for i in range(len(modality)):
if modality[i] == 'sound':
list_file_ = list_file.replace(".lmdb", ".txt")
tmp = VideoDataSet(root_path[i], os.path.join(root_path[i], list_file_),
num_groups, frames_per_group, sample_offset,
num_clips, modality[i], dense_sampling, fixed_offset,
image_tmpl, transform[i], is_train, test_mode, seperator,
filter_video, num_classes, whole_video, fps, audio_length, resampling_rate)
else:
tmp = VideoDataSetLMDB(root_path[i], list_file, num_groups, frames_per_group,
sample_offset, num_clips, modality[i], dense_sampling,
fixed_offset, image_tmpl, transform[i], is_train, test_mode,
seperator, filter_video, num_classes, whole_video, fps, audio_length,
resampling_rate)
video_datasets.append(tmp)
self.video_datasets = video_datasets
self.is_train = is_train
self.test_mode = test_mode
self.num_frames = num_groups
self.sample_freq = frames_per_group
self.dense_sampling = dense_sampling
self.num_clips = num_clips
self.fixed_offset = fixed_offset
self.modality = modality
self.num_classes = num_classes
self.whole_video = whole_video
self.num_consecutive_frames = max([x.num_consecutive_frames for x in self.video_datasets])
def _sample_indices(self, record):
return sample_train_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.num_clips)
def _get_val_indices(self, record):
return sample_val_test_clip(record.num_frames, self.num_consecutive_frames, self.num_frames,
self.sample_freq, self.dense_sampling, self.fixed_offset,
self.num_clips, self.whole_video)
def remove_data(self, idx):
for i in range(len(self.video_datasets)):
self.video_datasets[i].remove_data(idx)
def __getitem__(self, index):
"""
Returns:
torch.FloatTensor: (3xgxf)xHxW dimension, g is number of groups and f is the frames per group.
torch.FloatTensor: the label
"""
multi_modalities = []
indices = None
for modality, video_dataset in zip(self.modality, self.video_datasets):
if indices is None:
if modality == 'sound':
record = video_dataset.video_list[index]
else:
unpacked_video = video_dataset.maybe_open_and_get_buffer(index)
num_frames = unpacked_video[0] - 1 if modality == 'rgbdiff' else unpacked_video[0]
record = VideoRecord(video_dataset.keys[index].decode("utf-8"), 1, num_frames, unpacked_video[-1])
indices = video_dataset._sample_indices(record) if video_dataset.is_train else video_dataset._get_val_indices(record)
if modality == 'sound':
record = video_dataset.video_list[index]
images = video_dataset.get_data(record, indices)
else:
if video_dataset.unpacked_video is None:
video_dataset.maybe_open_and_get_buffer(index)
unpacked_video = video_dataset.unpacked_video
num_frames = unpacked_video[0] - 1 if modality == 'rgbdiff' else unpacked_video[0]
record = VideoRecord(video_dataset.keys[index].decode("utf-8"), 1, num_frames, unpacked_video[-1])
images = video_dataset.get_data(record, indices, video_dataset.unpacked_video)
video_dataset.unpacked_video = None
images = video_dataset.transform(images)
label = video_dataset.get_label(record)
multi_modalities.append((images, label))
return [x for x, y in multi_modalities], multi_modalities[0][1]
def __len__(self):
return len(self.video_datasets[0])
class VideoDataSetOnline(VideoDataSet):
def __init__(self, root_path, list_file, num_groups=8, frames_per_group=1, sample_offset=0,
num_clips=1, modality='rgb', dense_sampling=False, fixed_offset=True,
image_tmpl='{:05d}.jpg', transform=None, is_train=True, test_mode=False, seperator=' ',
filter_video=0, num_classes=None, whole_video=False,
fps=29.97, audio_length=1.28, resampling_rate=24000):
"""
Arguments have different meaning when dense_sampling is True:
- num_groups ==> number of frames
- frames_per_group ==> sample every K frame
- sample_offset ==> number of clips used in validation or test mode
Args:
root_path (str): the file path to the root of video folder
list_file (str): the file list, each line with folder_path, start_frame, end_frame, label_id
num_groups (int): number of frames per data sample
frames_per_group (int): number of frames within one group
sample_offset (int): used in validation/test, the offset when sampling frames from a group
modality (str): rgb or flow
dense_sampling (bool): dense sampling in I3D
fixed_offset (bool): used for generating the same videos used in TSM
image_tmpl (str): template of image ids
transform: the transformer for preprocessing
is_train (bool): shuffle the video but keep the causality
test_mode (bool): testing mode, no label
fps (float): frame rate per second, used to localize sound when frame idx is selected.
audio_length (float): the time window to extract audio feature.
resampling_rate (int): used to resampling audio extracted from wav
"""
if not _HAS_PYAV:
raise ValueError(_PYAV_ERROR_MSG)
if modality not in ['rgb', 'rgbdiff']:
raise ValueError("modality should be 'rgb' or 'rgbdiff'.")
super().__init__(root_path, list_file, num_groups, frames_per_group, sample_offset,
num_clips, modality, dense_sampling, fixed_offset,
image_tmpl, transform, is_train, test_mode, seperator,
filter_video, num_classes, whole_video, fps, audio_length, resampling_rate)
def remove_data(self, idx):
original_video_num = len(self.video_list)
self.video_list = [v for i, v in enumerate(self.video_list) if i not in idx]
print("Original videos: {}\t remove {} videos, remaining {} videos".format(original_video_num, len(idx), len(self.video_list)))
def get_data(self, record, indices):
indices = indices - 1
container = av.open(os.path.join(self.root_path, record.path))
container.streams.video[0].thread_type = "AUTO"
frames_length = container.streams.video[0].frames
duration = container.streams.video[0].duration
if duration is None or frames_length == 0:
# If failed to fetch the decoding information, decode the entire video.
# video_start_pts, video_end_pts = 0, math.inf
decode_all = True
else:
# Perform selective decoding.
if frames_length != record.num_frames:
# remap the index
length_ratio = frames_length / record.num_frames
indices = np.around(indices * length_ratio).astype(int)
start_idx, end_idx = min(indices), max(indices)
# if self.modality == 'rgbdiff':
# end_idx += (self.num_consecutive_frames + 1)
timebase = duration / frames_length
video_start_pts = int(start_idx * timebase)
video_end_pts = int(end_idx * timebase)
decode_all = False
def _selective_decoding(container, index, timebase):
margin = 1024
start_idx, end_idx = min(index), max(index)