Skip to content

Commit

Permalink
Add a warning if any clip can't be obtained from a video in VideoClip…
Browse files Browse the repository at this point in the history
…s. (pytorch#2513)

* Add a warning if a clip can't be get from a video in VideoClips

* Update torchvision/datasets/video_utils.py

Co-authored-by: Philip Meier <[email protected]>

* Add a test

Co-authored-by: Philip Meier <[email protected]>
  • Loading branch information
2 people authored and vfdev-5 committed Dec 4, 2020
1 parent 6b409dc commit f8890a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/test_datasets_video_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ def test_compute_clips_for_video(self):
self.assertTrue(clips.equal(idxs))
self.assertTrue(idxs.flatten().equal(resampled_idxs))

# case 3: frames aren't enough for a clip
num_frames = 32
orig_fps = 30
new_fps = 13
with self.assertWarns(UserWarning):
clips, idxs = VideoClips.compute_clips_for_video(video_pts, num_frames, num_frames,
orig_fps, new_fps)
self.assertEqual(len(clips), 0)
self.assertEqual(len(idxs), 0)


if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions torchvision/datasets/video_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bisect
import math
import warnings
from fractions import Fraction
from typing import List

Expand Down Expand Up @@ -204,6 +205,9 @@ def compute_clips_for_video(video_pts, num_frames, step, fps, frame_rate):
)
video_pts = video_pts[idxs]
clips = unfold(video_pts, num_frames, step)
if not clips.numel():
warnings.warn("There aren't enough frames in the current video to get a clip for the given clip length and "
"frames between clips. The video (and potentially others) will be skipped.")
if isinstance(idxs, slice):
idxs = [idxs] * len(clips)
else:
Expand Down

0 comments on commit f8890a2

Please sign in to comment.