@@ -57,26 +57,36 @@ def __init__(self, opt, subset=None, feature_dirs=[], fps=30, image_dir=None, im
57
57
def _get_data (self ):
58
58
print (self .video_info_path )
59
59
anno_df = pd .read_csv (self .video_info_path )
60
- video_name_list = sorted (list (set (anno_df .video .values [:])))
61
-
60
+ video_name_list = sorted (list (set (anno_df .video .values [:])))
61
+
62
+ extra_feature_path = 'features.' if self .feature_dirs else ''
62
63
video_info_dir = '/' .join (self .video_info_path .split ('/' )[:- 1 ])
63
64
if 'gymnastics' in self .video_info_path :
64
- saved_data_path = os .path .join (video_info_dir , 'saved.%s.nf%d.sf%d.num%d.exgymthresh.pkl ' % (
65
+ saved_data_path = os .path .join (video_info_dir , 'saved.%s.nf%d.sf%d.num%d.exgymthresh.%spkl ' % (
65
66
self .subset , self .num_videoframes , self .skip_videoframes ,
66
- len (video_name_list ))
67
- )
67
+ len (video_name_list ), extra_feature_path
68
+ ))
68
69
else :
69
- saved_data_path = os .path .join (video_info_dir , 'saved.%s.nf%d.sf%d.num%d.pkl ' % (
70
+ saved_data_path = os .path .join (video_info_dir , 'saved.%s.nf%d.sf%d.num%d.%spkl ' % (
70
71
self .subset , self .num_videoframes , self .skip_videoframes ,
71
- len (video_name_list ))
72
- )
72
+ len (video_name_list ), extra_feature_path
73
+ ))
73
74
74
75
print (saved_data_path )
75
76
if os .path .exists (saved_data_path ):
76
77
print ('Got saved data.' )
77
78
with open (saved_data_path , 'rb' ) as f :
78
79
self .data , self .durations = pickle .load (f )
79
- print ('Size of data: ' , len (self .data ['video_names' ]), flush = True )
80
+ print ('Size of data: ' , len (self .data ['video_names' ]), flush = True )
81
+ if self .feature_dirs :
82
+ # Pare away all of the dumb shit.
83
+ valid_indices = [
84
+ num for num , k in enumerate (self .data ['video_data' ]) \
85
+ if k .shape == (100 , 2048 )
86
+ ]
87
+ print ('Filtered size of data: ' , len (valid_indices ))
88
+ self .data = {k : [v [num ] for num in valid_indices ]
89
+ for k , v in self .data .items ()}
80
90
return
81
91
82
92
if self .feature_dirs :
@@ -104,19 +114,50 @@ def _get_data(self):
104
114
105
115
# NOTE: num_snippet is the number of snippets in this video.
106
116
if self .image_dir :
117
+ print ('Doing imagedir...' )
107
118
image_dir = self ._get_image_dir (video_name )
108
119
num_snippet = len (os .listdir (image_dir ))
109
120
self .durations [video_name ] = num_snippet
110
121
num_snippet = int ((num_snippet - start_snippet ) / skip_videoframes )
111
122
elif self .feature_dirs :
112
- feature_dfs = [
113
- pd .read_csv (os .path .join (feature_dir , '%s.csv' % video_name ))
114
- for feature_dir in self .feature_dirs
115
- ]
116
- num_snippet = min ([len (df ) for df in feature_dfs ])
117
- df_data = np .concatenate ([df .values [:num_snippet , :]
118
- for df in feature_dfs ],
119
- axis = 1 )
123
+ print ('Doing feature dir ..' )
124
+ if 'gymnastics' in self .video_info_path :
125
+ # Assuming that rgbs is the first feature_df... -_-
126
+ rgb_path = os .path .join (self .feature_dirs [0 ], video_name )
127
+ rgb_files = os .listdir (rgb_path )
128
+ orig_rgb_len = len (rgb_files )
129
+ if len (self .feature_dirs ) > 1 :
130
+ flow_path = os .path .join (self .feature_dirs [1 ], video_name )
131
+ flow_files = os .listdir (flow_path )
132
+ orig_flow_len = len (flow_files )
133
+ converted_flow_files = [
134
+ '%010.4f.npy' % (int (k [:- 4 ]) / 12 )
135
+ for k in flow_files
136
+ ]
137
+ flow_indices = [num for num , flow in enumerate (converted_flow_files ) \
138
+ if flow in rgb_files ]
139
+ rgb_indices = [num for num , rgb in enumerate (rgb_files ) \
140
+ if rgb in converted_flow_files ]
141
+ flow_files = [flow_files [num ] for num in flow_indices ]
142
+ rgb_files = [rgb_files [num ] for num in rgb_indices ]
143
+ print (video_name , ' rgb/flow: ' , len (rgb_files ), len (flow_files ), ' orig: ' , orig_rgb_len , orig_flow_len )
144
+ num_snippet = min (len (flow_files ), len (rgb_files ))
145
+ rgb_data = np .array ([np .load (os .path .join (rgb_path , rgb_file )) for rgb_file in rgb_files ])
146
+ flow_data = np .array ([np .load (os .path .join (flow_path , flow_file )) for flow_file in flow_files ])
147
+ df_data = np .concatenate ([rgb_data , flow_data ], axis = 1 )
148
+ else :
149
+ rgb_data = np .array ([np .load (os .path .join (rgb_path , rgb_file )) for rgb_file in rgb_files ])
150
+ df_data = rgb_data
151
+ num_snippet = len (rgb_files )
152
+ else :
153
+ feature_dfs = [
154
+ pd .read_csv (os .path .join (feature_dir , '%s.csv' % video_name ))
155
+ for feature_dir in self .feature_dirs
156
+ ]
157
+ num_snippet = min ([len (df ) for df in feature_dfs ])
158
+ df_data = np .concatenate ([df .values [:num_snippet , :]
159
+ for df in feature_dfs ],
160
+ axis = 1 )
120
161
121
162
df_snippet = [start_snippet + skip_videoframes * i for i in range (num_snippet )]
122
163
num_windows = int ((num_snippet + stride - num_videoframes ) / stride )
@@ -125,8 +166,11 @@ def _get_data(self):
125
166
windows_start = [0 ]
126
167
if self .feature_dirs :
127
168
# Add on a bunch of zero data if there aren't enough windows.
128
- tmp_data = np .zeros ((num_videoframes - num_snippet , 400 ))
129
- df_data = np .concatenate ((df_data , tmp_data ), axis = 0 )
169
+ if 'gymnastics' in self .video_info_path :
170
+ pass
171
+ else :
172
+ tmp_data = np .zeros ((num_videoframes - num_snippet , 400 ))
173
+ df_data = np .concatenate ((df_data , tmp_data ), axis = 0 )
130
174
df_snippet .extend ([
131
175
df_snippet [- 1 ] + skip_videoframes * (i + 1 )
132
176
for i in range (num_videoframes - num_snippet )
@@ -248,21 +292,34 @@ def __len__(self):
248
292
class TEMImages (TEMDataset ):
249
293
def __init__ (self , opt , subset = None , fps = 30 , image_dir = None , img_loading_func = None , video_info_path = None ):
250
294
self .do_augment = opt ['do_augment' ] and subset == 'train'
295
+ self .ext = 'npy'
296
+ if '240x426' in opt ['gym_image_dir' ]:
297
+ self .ext = 'png'
251
298
super (TEMImages , self ).__init__ (opt , subset , feature_dirs = None , fps = fps , image_dir = image_dir , img_loading_func = img_loading_func , video_info_path = video_info_path )
252
299
253
300
def _get_video_data (self , data , index ):
254
301
indices = data ['indices' ][index ]
255
302
name = data ['video_names' ][index ]
256
303
path = os .path .join (self .image_dir , name )
257
304
path = Path (path )
258
- paths = [path / ('%010.4f.npy ' % (i / self .fps )) for i in indices ]
305
+ paths = [path / ('%010.4f.%s ' % (( i / self .fps ), self . ext )) for i in indices ]
259
306
imgs = [self .img_loading_func (p .absolute (), do_augment = self .do_augment )
260
307
for p in paths if p .exists ()]
261
- if type (imgs [0 ]) == np .array :
262
- video_data = np .array (imgs )
263
- video_data = torch .Tensor (video_data )
264
- elif type (imgs [0 ]) == torch .Tensor :
265
- video_data = torch .stack (imgs )
308
+ try :
309
+ if type (imgs [0 ]) == np .array :
310
+ video_data = np .array (imgs )
311
+ video_data = torch .Tensor (video_data )
312
+ elif type (imgs [0 ]) == torch .Tensor :
313
+ video_data = torch .stack (imgs )
314
+ elif type (imgs [0 ]) == np .ndarray :
315
+ # This is for TSN
316
+ video_data = np .array (imgs )
317
+ video_data = torch .from_numpy (video_data )
318
+ video_data = video_data .type (torch .FloatTensor )
319
+ except Exception as e :
320
+ print (paths )
321
+ print ([p .exists () for p in paths ])
322
+ raise
266
323
267
324
if len (video_data ) < self .num_videoframes :
268
325
shape = [self .num_videoframes - len (video_data )]
@@ -352,6 +409,15 @@ def _get_image_dir(self, video_name):
352
409
return os .path .join (self .image_dir , target_dir )
353
410
354
411
412
+ class GymnasticsFeatures (TEMDataset ):
413
+
414
+ def __init__ (self , opt , subset = None , feature_dirs = [], video_info_path = None ):
415
+ super (GymnasticsFeatures , self ).__init__ (opt , subset , feature_dirs , fps = None , image_dir = None , img_loading_func = None , video_info_path = video_info_path )
416
+
417
+ def _get_video_data (self , data , index ):
418
+ return data ['video_data' ][index ]
419
+
420
+
355
421
class VideoDataset (data .Dataset ):
356
422
def __init__ (self , opt , transforms , subset , fraction = 1. ):
357
423
"""file_list is a list of [/path/to/mp4 key-to-df]"""
@@ -368,8 +434,9 @@ def __init__(self, opt, transforms, subset, fraction=1.):
368
434
self .anno_df = pd .read_csv (self .video_info_path )
369
435
print (self .anno_df )
370
436
print (subset , subset_translate .get (subset ))
371
- self .anno_df = self .anno_df [self .anno_df .subset == subset_translate [subset ]]
372
- print (self .anno_df )
437
+ if subset != 'full' :
438
+ self .anno_df = self .anno_df [self .anno_df .subset == subset_translate [subset ]]
439
+ print (self .anno_df )
373
440
374
441
file_loc = opt ['%s_video_file_list' % subset ]
375
442
with open (file_loc , 'r' ) as f :
@@ -421,7 +488,10 @@ def _subset_dataset(self, fraction):
421
488
print (sorted (self .datum_indices )[- 10 :])
422
489
423
490
def __len__ (self ):
424
- return len (self .datum_indices )
491
+ if self .mode == 'train' :
492
+ return len (self .datum_indices )
493
+ else :
494
+ return self .video_clips .num_clips ()
425
495
426
496
def _retrieve_valid_datums (self ):
427
497
video_info_dir = '/' .join (self .video_info_path .split ('/' )[:- 1 ])
@@ -463,10 +533,15 @@ def __getitem__(self, index):
463
533
if self .mode == "train" :
464
534
datum_index = self .datum_indices [index ]
465
535
flat_index , anchor_xmin , anchor_xmax , gt_bbox = self .datums [datum_index ]
536
+ else :
537
+ flat_index = index
538
+
466
539
video , _ , _ , video_idx = self .video_clips .get_clip (flat_index )
467
540
468
541
video_data = video [0 ::self .skip_videoframes ]
542
+ print ('Bef transform: ' , video_data , type (video_data ))
469
543
video_data = self .transforms (video_data )
544
+ print ('AFt transform: ' , video_data , type (video_data ))
470
545
video_data = torch .transpose (video_data , 0 , 1 )
471
546
472
547
_ , clip_idx = self .video_clips .get_clip_location (index )
@@ -477,7 +552,10 @@ def __getitem__(self, index):
477
552
match_score_action , match_score_start , match_score_end = self ._get_train_label (gt_bbox , anchor_xmin , anchor_xmax )
478
553
return video_data , match_score_action , match_score_start , match_score_end
479
554
else :
480
- video_name = self .keys_list [video_idx ]
555
+ try :
556
+ video_name = self .keys_list [video_idx ]
557
+ except Exception as e :
558
+ print ('Whoops: VideoReader ...' , video_idx , len (self .keys_list ), index , flat_index )
481
559
return flat_index , video_data , video_name , snippets
482
560
483
561
def _get_training_anchors (self , snippets , key ):
@@ -620,7 +698,7 @@ def _exists(self, video_name):
620
698
def _getDatasetDict (self ):
621
699
anno_df = pd .read_csv (self .video_info_path )
622
700
anno_database = load_json (self .video_anno_path )
623
- print (self .video_anno_path , self .video_info_path )
701
+ print (self .subset , self . video_anno_path , self .video_info_path )
624
702
self .video_dict = {}
625
703
for i in range (len (anno_df )):
626
704
video_name = anno_df .video .values [i ]
@@ -636,7 +714,9 @@ def _getDatasetDict(self):
636
714
if self .subset in video_subset :
637
715
self .video_dict [video_name ] = video_info
638
716
self .video_list = sorted (self .video_dict .keys ())
717
+ print ('Init size of video_list: ' , len (self .video_list ))
639
718
self .video_list = [k for k in self .video_list if self ._exists (k )]
719
+ print ('Exists size of video_list: ' , len (self .video_list ))
640
720
641
721
if self .opt ['pem_do_index' ]:
642
722
self .features = {}
@@ -645,8 +725,11 @@ def _getDatasetDict(self):
645
725
for video_name in self .video_list :
646
726
pgm_proposals_path = os .path .join (self .opt ['pgm_proposals_dir' ], '%s.proposals.csv' % video_name )
647
727
pgm_features_path = os .path .join (self .opt ['pgm_features_dir' ], '%s.features.npy' % video_name )
648
- pdf = pd .read_csv (pgm_proposals_path )
728
+ pdf = pd .read_csv (pgm_proposals_path )
649
729
video_feature = np .load (pgm_features_path )
730
+ if not len (pdf ) and self .mode == "train" :
731
+ continue
732
+
650
733
pre_count = len (pdf )
651
734
if self .top_K > 0 :
652
735
try :
@@ -655,13 +738,17 @@ def _getDatasetDict(self):
655
738
pdf ['score' ] = pdf .xmin_score * pdf .xmax_score
656
739
pdf = pdf .sort_values (by = "score" , ascending = False )
657
740
pdf = pdf [:self .top_K ]
658
- video_feature = video_feature [pdf .index ]
741
+ try :
742
+ video_feature = video_feature [pdf .index ]
743
+ except Exception as e :
744
+ print ('WAT IS HTIS: ' , pgm_proposals_path , pgm_features_path )
745
+ raise
659
746
660
747
# print(video_name, pre_count, len(pdf), video_feature.shape, pgm_proposals_path, pgm_features_path)
661
748
self .proposals [video_name ] = pdf
662
749
self .features [video_name ] = video_feature
663
750
self .indices .extend ([(video_name , i ) for i in range (len (pdf ))])
664
- print ('Num indices: ' , len (self .indices ))
751
+ print ('Num indices: ' , len (self .indices ), len ( self . proposals ), len ( self . features ) )
665
752
666
753
def __len__ (self ):
667
754
if self .opt ['pem_do_index' ] > 0 :
0 commit comments