From 3f09f1a9ae1f76ad0116ae425b024f411c3d209c Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Thu, 10 Jun 2021 17:52:59 +0530 Subject: [PATCH 1/5] Port test_datasets_samplers.py to pytest --- test/test_datasets_samplers.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/test_datasets_samplers.py b/test/test_datasets_samplers.py index 10d8704dbb1..535171f4ce9 100644 --- a/test/test_datasets_samplers.py +++ b/test/test_datasets_samplers.py @@ -2,7 +2,7 @@ import sys import os import torch -import unittest +import pytest from torchvision import io from torchvision.datasets.samplers import ( @@ -38,13 +38,13 @@ def get_list_of_videos(num_videos=5, sizes=None, fps=None): yield names -@unittest.skipIf(not io.video._av_available(), "this test requires av") -class Tester(unittest.TestCase): +@pytest.mark.skipif(not io.video._av_available(), reason="this test requires av") +class TestDatasetsSamplers: def test_random_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - self.assertEqual(len(sampler), 3 * 3) + assert_equal(len(sampler), 3 * 3) indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) @@ -55,10 +55,10 @@ def test_random_clip_sampler_unequal(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - self.assertEqual(len(sampler), 2 + 3 + 3) + assert_equal(len(sampler), 2 + 3 + 3) indices = list(iter(sampler)) - self.assertIn(0, indices) - self.assertIn(1, indices) + assert 0 in indices + assert 1 in indices # remove elements of the first video, to simplify testing indices.remove(0) indices.remove(1) @@ -72,7 +72,7 @@ def test_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - self.assertEqual(len(sampler), 3 * 3) + assert_equal(len(sampler), 3 * 3) indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) @@ -84,7 +84,7 @@ def test_uniform_clip_sampler_insufficient_clips(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - self.assertEqual(len(sampler), 3 * 3) + assert_equal(len(sampler), 3 * 3) indices = torch.tensor(list(iter(sampler))) assert_equal(indices, torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11])) @@ -100,7 +100,7 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank0))) - self.assertEqual(len(distributed_sampler_rank0), 6) + assert_equal(len(distributed_sampler_rank0), 6) assert_equal(indices, torch.tensor([0, 2, 4, 10, 12, 14])) distributed_sampler_rank1 = DistributedSampler( @@ -110,9 +110,9 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank1))) - self.assertEqual(len(distributed_sampler_rank1), 6) + assert_equal(len(distributed_sampler_rank1), 6) assert_equal(indices, torch.tensor([5, 7, 9, 0, 2, 4])) if __name__ == '__main__': - unittest.main() + pytest.main([__file__]) From 2958a462119095a508359f7b7efd13ebe0da2e5d Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Thu, 10 Jun 2021 18:16:19 +0530 Subject: [PATCH 2/5] simplified assert_equal(a, b) to assert a == b --- test/test_datasets_samplers.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/test/test_datasets_samplers.py b/test/test_datasets_samplers.py index 535171f4ce9..d0daaa55fd1 100644 --- a/test/test_datasets_samplers.py +++ b/test/test_datasets_samplers.py @@ -14,7 +14,6 @@ from torchvision import get_video_backend from common_utils import get_tmp_dir -from _assert_utils import assert_equal @contextlib.contextmanager @@ -44,18 +43,18 @@ def test_random_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - assert_equal(len(sampler), 3 * 3) + assert len(sampler) == 3 * 3 indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert_equal(v_idxs, torch.tensor([0, 1, 2])) - assert_equal(count, torch.tensor([3, 3, 3])) + assert v_idxs == torch.tensor([0, 1, 2]) + assert count == torch.tensor([3, 3, 3]) def test_random_clip_sampler_unequal(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - assert_equal(len(sampler), 2 + 3 + 3) + assert len(sampler) == 2 + 3 + 3 indices = list(iter(sampler)) assert 0 in indices assert 1 in indices @@ -65,28 +64,28 @@ def test_random_clip_sampler_unequal(self): indices = torch.tensor(indices) - 2 videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert_equal(v_idxs, torch.tensor([0, 1])) - assert_equal(count, torch.tensor([3, 3])) + assert v_idxs == torch.tensor([0, 1]) + assert count == torch.tensor([3, 3]) def test_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - assert_equal(len(sampler), 3 * 3) + assert len(sampler) == 3 * 3 indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert_equal(v_idxs, torch.tensor([0, 1, 2])) - assert_equal(count, torch.tensor([3, 3, 3])) - assert_equal(indices, torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14])) + assert v_idxs == torch.tensor([0, 1, 2]) + assert count == torch.tensor([3, 3, 3]) + assert indices == torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14]) def test_uniform_clip_sampler_insufficient_clips(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - assert_equal(len(sampler), 3 * 3) + assert len(sampler) == 3 * 3 indices = torch.tensor(list(iter(sampler))) - assert_equal(indices, torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11])) + assert indices == torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11]) def test_distributed_sampler_and_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: @@ -100,8 +99,8 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank0))) - assert_equal(len(distributed_sampler_rank0), 6) - assert_equal(indices, torch.tensor([0, 2, 4, 10, 12, 14])) + assert len(distributed_sampler_rank0) == 6 + assert indices == torch.tensor([0, 2, 4, 10, 12, 14]) distributed_sampler_rank1 = DistributedSampler( clip_sampler, @@ -110,8 +109,8 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank1))) - assert_equal(len(distributed_sampler_rank1), 6) - assert_equal(indices, torch.tensor([5, 7, 9, 0, 2, 4])) + assert len(distributed_sampler_rank1) == 6 + assert indices == torch.tensor([5, 7, 9, 0, 2, 4]) if __name__ == '__main__': From f1483af391bf36fe4d76922e34f83856e5abeb72 Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Thu, 10 Jun 2021 18:40:08 +0530 Subject: [PATCH 3/5] reverted last changes --- test/test_datasets_samplers.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/test/test_datasets_samplers.py b/test/test_datasets_samplers.py index d0daaa55fd1..535171f4ce9 100644 --- a/test/test_datasets_samplers.py +++ b/test/test_datasets_samplers.py @@ -14,6 +14,7 @@ from torchvision import get_video_backend from common_utils import get_tmp_dir +from _assert_utils import assert_equal @contextlib.contextmanager @@ -43,18 +44,18 @@ def test_random_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - assert len(sampler) == 3 * 3 + assert_equal(len(sampler), 3 * 3) indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert v_idxs == torch.tensor([0, 1, 2]) - assert count == torch.tensor([3, 3, 3]) + assert_equal(v_idxs, torch.tensor([0, 1, 2])) + assert_equal(count, torch.tensor([3, 3, 3])) def test_random_clip_sampler_unequal(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - assert len(sampler) == 2 + 3 + 3 + assert_equal(len(sampler), 2 + 3 + 3) indices = list(iter(sampler)) assert 0 in indices assert 1 in indices @@ -64,28 +65,28 @@ def test_random_clip_sampler_unequal(self): indices = torch.tensor(indices) - 2 videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert v_idxs == torch.tensor([0, 1]) - assert count == torch.tensor([3, 3]) + assert_equal(v_idxs, torch.tensor([0, 1])) + assert_equal(count, torch.tensor([3, 3])) def test_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - assert len(sampler) == 3 * 3 + assert_equal(len(sampler), 3 * 3) indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert v_idxs == torch.tensor([0, 1, 2]) - assert count == torch.tensor([3, 3, 3]) - assert indices == torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14]) + assert_equal(v_idxs, torch.tensor([0, 1, 2])) + assert_equal(count, torch.tensor([3, 3, 3])) + assert_equal(indices, torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14])) def test_uniform_clip_sampler_insufficient_clips(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - assert len(sampler) == 3 * 3 + assert_equal(len(sampler), 3 * 3) indices = torch.tensor(list(iter(sampler))) - assert indices == torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11]) + assert_equal(indices, torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11])) def test_distributed_sampler_and_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: @@ -99,8 +100,8 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank0))) - assert len(distributed_sampler_rank0) == 6 - assert indices == torch.tensor([0, 2, 4, 10, 12, 14]) + assert_equal(len(distributed_sampler_rank0), 6) + assert_equal(indices, torch.tensor([0, 2, 4, 10, 12, 14])) distributed_sampler_rank1 = DistributedSampler( clip_sampler, @@ -109,8 +110,8 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank1))) - assert len(distributed_sampler_rank1) == 6 - assert indices == torch.tensor([5, 7, 9, 0, 2, 4]) + assert_equal(len(distributed_sampler_rank1), 6) + assert_equal(indices, torch.tensor([5, 7, 9, 0, 2, 4])) if __name__ == '__main__': From 5d4d75762ccd92cf6bb6b873b976ae43fe69c6ad Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Thu, 10 Jun 2021 22:25:04 +0530 Subject: [PATCH 4/5] discarded last changes --- test/test_datasets_samplers.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/test/test_datasets_samplers.py b/test/test_datasets_samplers.py index 535171f4ce9..d0daaa55fd1 100644 --- a/test/test_datasets_samplers.py +++ b/test/test_datasets_samplers.py @@ -14,7 +14,6 @@ from torchvision import get_video_backend from common_utils import get_tmp_dir -from _assert_utils import assert_equal @contextlib.contextmanager @@ -44,18 +43,18 @@ def test_random_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - assert_equal(len(sampler), 3 * 3) + assert len(sampler) == 3 * 3 indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert_equal(v_idxs, torch.tensor([0, 1, 2])) - assert_equal(count, torch.tensor([3, 3, 3])) + assert v_idxs == torch.tensor([0, 1, 2]) + assert count == torch.tensor([3, 3, 3]) def test_random_clip_sampler_unequal(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = RandomClipSampler(video_clips, 3) - assert_equal(len(sampler), 2 + 3 + 3) + assert len(sampler) == 2 + 3 + 3 indices = list(iter(sampler)) assert 0 in indices assert 1 in indices @@ -65,28 +64,28 @@ def test_random_clip_sampler_unequal(self): indices = torch.tensor(indices) - 2 videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert_equal(v_idxs, torch.tensor([0, 1])) - assert_equal(count, torch.tensor([3, 3])) + assert v_idxs == torch.tensor([0, 1]) + assert count == torch.tensor([3, 3]) def test_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - assert_equal(len(sampler), 3 * 3) + assert len(sampler) == 3 * 3 indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert_equal(v_idxs, torch.tensor([0, 1, 2])) - assert_equal(count, torch.tensor([3, 3, 3])) - assert_equal(indices, torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14])) + assert v_idxs == torch.tensor([0, 1, 2]) + assert count == torch.tensor([3, 3, 3]) + assert indices == torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14]) def test_uniform_clip_sampler_insufficient_clips(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: video_clips = VideoClips(video_list, 5, 5) sampler = UniformClipSampler(video_clips, 3) - assert_equal(len(sampler), 3 * 3) + assert len(sampler) == 3 * 3 indices = torch.tensor(list(iter(sampler))) - assert_equal(indices, torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11])) + assert indices == torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11]) def test_distributed_sampler_and_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: @@ -100,8 +99,8 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank0))) - assert_equal(len(distributed_sampler_rank0), 6) - assert_equal(indices, torch.tensor([0, 2, 4, 10, 12, 14])) + assert len(distributed_sampler_rank0) == 6 + assert indices == torch.tensor([0, 2, 4, 10, 12, 14]) distributed_sampler_rank1 = DistributedSampler( clip_sampler, @@ -110,8 +109,8 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): group_size=3, ) indices = torch.tensor(list(iter(distributed_sampler_rank1))) - assert_equal(len(distributed_sampler_rank1), 6) - assert_equal(indices, torch.tensor([5, 7, 9, 0, 2, 4])) + assert len(distributed_sampler_rank1) == 6 + assert indices == torch.tensor([5, 7, 9, 0, 2, 4]) if __name__ == '__main__': From 7e60ce5f2e3f1021d8e8a17a2e214549d07f5ab2 Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Thu, 10 Jun 2021 22:50:03 +0530 Subject: [PATCH 5/5] minor fixes on assert_equal --- test/test_datasets_samplers.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/test_datasets_samplers.py b/test/test_datasets_samplers.py index d0daaa55fd1..be2fab8e0dd 100644 --- a/test/test_datasets_samplers.py +++ b/test/test_datasets_samplers.py @@ -14,6 +14,7 @@ from torchvision import get_video_backend from common_utils import get_tmp_dir +from _assert_utils import assert_equal @contextlib.contextmanager @@ -47,8 +48,8 @@ def test_random_clip_sampler(self): indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert v_idxs == torch.tensor([0, 1, 2]) - assert count == torch.tensor([3, 3, 3]) + assert_equal(v_idxs, torch.tensor([0, 1, 2])) + assert_equal(count, torch.tensor([3, 3, 3])) def test_random_clip_sampler_unequal(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: @@ -64,8 +65,8 @@ def test_random_clip_sampler_unequal(self): indices = torch.tensor(indices) - 2 videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert v_idxs == torch.tensor([0, 1]) - assert count == torch.tensor([3, 3]) + assert_equal(v_idxs, torch.tensor([0, 1])) + assert_equal(count, torch.tensor([3, 3])) def test_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: @@ -75,9 +76,9 @@ def test_uniform_clip_sampler(self): indices = torch.tensor(list(iter(sampler))) videos = torch.div(indices, 5, rounding_mode='floor') v_idxs, count = torch.unique(videos, return_counts=True) - assert v_idxs == torch.tensor([0, 1, 2]) - assert count == torch.tensor([3, 3, 3]) - assert indices == torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14]) + assert_equal(v_idxs, torch.tensor([0, 1, 2])) + assert_equal(count, torch.tensor([3, 3, 3])) + assert_equal(indices, torch.tensor([0, 2, 4, 5, 7, 9, 10, 12, 14])) def test_uniform_clip_sampler_insufficient_clips(self): with get_list_of_videos(num_videos=3, sizes=[10, 25, 25]) as video_list: @@ -85,7 +86,7 @@ def test_uniform_clip_sampler_insufficient_clips(self): sampler = UniformClipSampler(video_clips, 3) assert len(sampler) == 3 * 3 indices = torch.tensor(list(iter(sampler))) - assert indices == torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11]) + assert_equal(indices, torch.tensor([0, 0, 1, 2, 4, 6, 7, 9, 11])) def test_distributed_sampler_and_uniform_clip_sampler(self): with get_list_of_videos(num_videos=3, sizes=[25, 25, 25]) as video_list: @@ -100,7 +101,7 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): ) indices = torch.tensor(list(iter(distributed_sampler_rank0))) assert len(distributed_sampler_rank0) == 6 - assert indices == torch.tensor([0, 2, 4, 10, 12, 14]) + assert_equal(indices, torch.tensor([0, 2, 4, 10, 12, 14])) distributed_sampler_rank1 = DistributedSampler( clip_sampler, @@ -110,7 +111,7 @@ def test_distributed_sampler_and_uniform_clip_sampler(self): ) indices = torch.tensor(list(iter(distributed_sampler_rank1))) assert len(distributed_sampler_rank1) == 6 - assert indices == torch.tensor([5, 7, 9, 0, 2, 4]) + assert_equal(indices, torch.tensor([5, 7, 9, 0, 2, 4])) if __name__ == '__main__':