@@ -266,14 +266,15 @@ def __repr__(self):
266
266
267
267
268
268
@TRANSFORMS .register_module ()
269
- class UniformSampleFrames (BaseTransform ):
270
- """Uniformly sample frames from the video.
269
+ class UniformSample (BaseTransform ):
270
+ """Uniformly sample frames from the video. Currently used for Something-
271
+ Something V2 dataset. Modified from
272
+ https://github.com/facebookresearch/SlowFast/blob/64a
273
+ bcc90ccfdcbb11cf91d6e525bed60e92a8796/slowfast/datasets/ssv2.py#L159.
271
274
272
275
To sample an n-frame clip from the video. UniformSampleFrames basically
273
276
divides the video into n segments of equal length and randomly samples one
274
- frame from each segment. To make the testing results reproducible, a
275
- random seed is set during testing, to make the sampling results
276
- deterministic.
277
+ frame from each segment.
277
278
278
279
Required keys:
279
280
@@ -292,113 +293,23 @@ class UniformSampleFrames(BaseTransform):
292
293
num_clips (int): Number of clips to be sampled. Default: 1.
293
294
test_mode (bool): Store True when building test or validation dataset.
294
295
Default: False.
295
- seed (int): The random seed used during test time. Default: 255.
296
- out_of_bound_opt (str): The way to deal with out of bounds frame
297
- indexes. Available options are 'loop', 'repeat_frame'.
298
- Default: 'loop'.
299
296
"""
300
297
301
298
def __init__ (self ,
302
299
clip_len : int ,
303
300
num_clips : int = 1 ,
304
- test_mode : bool = False ,
305
- seed : int = 255 ,
306
- out_of_bound_opt : str = 'loop' ) -> None :
301
+ test_mode : bool = False ) -> None :
307
302
308
303
self .clip_len = clip_len
309
304
self .num_clips = num_clips
310
305
self .test_mode = test_mode
311
- self .seed = seed
312
- self .out_of_bound_opt = out_of_bound_opt
313
- assert self .out_of_bound_opt in ['loop' , 'repeat_frame' ]
314
-
315
- def _get_train_clips (self , num_frames : int ):
316
- """Uniformly sample indices for training clips.
317
-
318
- Args:
319
- num_frames (int): The number of frames.
320
- """
321
-
322
- assert self .num_clips == 1
323
- if num_frames < self .clip_len :
324
- start = np .random .randint (0 , num_frames )
325
- inds = np .arange (start , start + self .clip_len )
326
- elif self .clip_len <= num_frames < 2 * self .clip_len :
327
- basic = np .arange (self .clip_len )
328
- inds = np .random .choice (
329
- self .clip_len + 1 , num_frames - self .clip_len , replace = False )
330
- offset = np .zeros (self .clip_len + 1 , dtype = np .int32 )
331
- offset [inds ] = 1
332
- offset = np .cumsum (offset )
333
- inds = basic + offset [:- 1 ]
334
- else :
335
- bids = np .array ([
336
- i * num_frames // self .clip_len
337
- for i in range (self .clip_len + 1 )
338
- ])
339
- bsize = np .diff (bids )
340
- bst = bids [:self .clip_len ]
341
- offset = np .random .randint (bsize )
342
- inds = bst + offset
343
- return inds
344
-
345
- def _get_test_clips (self , num_frames : int ):
346
- """Uniformly sample indices for testing clips.
347
306
348
- Args:
349
- num_frames (int): The number of frames.
350
- """
351
-
352
- np .random .seed (self .seed )
353
- if num_frames < self .clip_len :
354
- # Then we use a simple strategy
355
- if num_frames < self .num_clips :
356
- start_inds = list (range (self .num_clips ))
357
- else :
358
- start_inds = [
359
- i * num_frames // self .num_clips
360
- for i in range (self .num_clips )
361
- ]
362
- inds = np .concatenate (
363
- [np .arange (i , i + self .clip_len ) for i in start_inds ])
364
- elif self .clip_len <= num_frames < self .clip_len * 2 :
365
- all_inds = []
366
- for i in range (self .num_clips ):
367
- basic = np .arange (self .clip_len )
368
- inds = np .random .choice (
369
- self .clip_len + 1 ,
370
- num_frames - self .clip_len ,
371
- replace = False )
372
- offset = np .zeros (self .clip_len + 1 , dtype = np .int32 )
373
- offset [inds ] = 1
374
- offset = np .cumsum (offset )
375
- inds = basic + offset [:- 1 ]
376
- all_inds .append (inds )
377
- inds = np .concatenate (all_inds )
378
- else :
379
- bids = np .array ([
380
- i * num_frames // self .clip_len
381
- for i in range (self .clip_len + 1 )
382
- ])
383
- bsize = np .diff (bids )
384
- bst = bids [:self .clip_len ]
385
- all_inds = []
386
- for i in range (self .num_clips ):
387
- offset = np .random .randint (bsize )
388
- all_inds .append (bst + offset )
389
- inds = np .concatenate (all_inds )
390
- return inds
391
-
392
- def _get_repeat_sample_clips (self , num_frames : int ) -> np .array :
393
- """Repeat sample when video is shorter than clip_len Modified from
394
- https://github.com/facebookresearch/SlowFast/blob/64ab
395
- cc90ccfdcbb11cf91d6e525bed60e92a8796/slowfast/datasets/ssv2.py#L159.
396
-
397
- When video frames is shorter than target clip len, this strategy would
398
- repeat sample frame, rather than loop sample in 'loop' mode.
399
- In test mode, this strategy would sample the middle frame of each
400
- segment, rather than set a random seed, and therefore only support
401
- sample 1 clip.
307
+ def _get_sample_clips (self , num_frames : int ) -> np .array :
308
+ """When video frames is shorter than target clip len, this strategy
309
+ would repeat sample frame, rather than loop sample in 'loop' mode. In
310
+ test mode, this strategy would sample the middle frame of each segment,
311
+ rather than set a random seed, and therefore only support sample 1
312
+ clip.
402
313
403
314
Args:
404
315
num_frames (int): Total number of frame in the video.
@@ -421,17 +332,7 @@ def _get_repeat_sample_clips(self, num_frames: int) -> np.array:
421
332
def transform (self , results : dict ):
422
333
num_frames = results ['total_frames' ]
423
334
424
- if self .out_of_bound_opt == 'loop' :
425
- if self .test_mode :
426
- inds = self ._get_test_clips (num_frames )
427
- else :
428
- inds = self ._get_train_clips (num_frames )
429
- inds = np .mod (inds , num_frames )
430
- elif self .out_of_bound_opt == 'repeat_frame' :
431
- inds = self ._get_repeat_sample_clips (num_frames )
432
- else :
433
- raise ValueError ('Illegal out_of_bound option.' )
434
-
335
+ inds = self ._get_sample_clips (num_frames )
435
336
start_index = results ['start_index' ]
436
337
inds = inds + start_index
437
338
@@ -445,8 +346,7 @@ def __repr__(self):
445
346
repr_str = (f'{ self .__class__ .__name__ } ('
446
347
f'clip_len={ self .clip_len } , '
447
348
f'num_clips={ self .num_clips } , '
448
- f'test_mode={ self .test_mode } , '
449
- f'seed={ self .seed } )' )
349
+ f'test_mode={ self .test_mode } ' )
450
350
return repr_str
451
351
452
352
0 commit comments