From 5986b905d3792ac26b17a89abd2dbde57d1e6f3b Mon Sep 17 00:00:00 2001 From: Pranjal Gulati Date: Thu, 10 Jun 2021 20:17:13 +0530 Subject: [PATCH 1/3] Port tests in test_transforms_video.py to pytest --- test/test_transforms_video.py | 59 ++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/test/test_transforms_video.py b/test/test_transforms_video.py index 942bb010f71..6a249dc1c97 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -1,6 +1,6 @@ import torch from torchvision.transforms import Compose -import unittest +import pytest import random import numpy as np import warnings @@ -17,7 +17,7 @@ import torchvision.transforms._transforms_video as transforms -class TestVideoTransforms(unittest.TestCase): +class TestVideoTransforms(): def test_random_crop_video(self): numFrames = random.randint(4, 128) @@ -30,8 +30,8 @@ def test_random_crop_video(self): transforms.ToTensorVideo(), transforms.RandomCropVideo((oheight, owidth)), ])(clip) - self.assertEqual(result.size(2), oheight) - self.assertEqual(result.size(3), owidth) + assert result.size(2) == oheight + assert result.size(3) == owidth transforms.RandomCropVideo((oheight, owidth)).__repr__() @@ -46,8 +46,8 @@ def test_random_resized_crop_video(self): transforms.ToTensorVideo(), transforms.RandomResizedCropVideo((oheight, owidth)), ])(clip) - self.assertEqual(result.size(2), oheight) - self.assertEqual(result.size(3), owidth) + assert result.size(2) == oheight + assert result.size(3) == owidth transforms.RandomResizedCropVideo((oheight, owidth)).__repr__() @@ -70,7 +70,7 @@ def test_center_crop_video(self): msg = "height: " + str(height) + " width: " \ + str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth) - self.assertEqual(result.sum().item(), 0, msg) + assert result.sum().item() == 0, msg oheight += 1 owidth += 1 @@ -82,7 +82,7 @@ def test_center_crop_video(self): msg = "height: " + str(height) + " width: " \ + str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth) - self.assertEqual(sum1.item() > 1, True, msg) + assert sum1.item() > 1 == True, msg oheight += 1 owidth += 1 @@ -94,28 +94,29 @@ def test_center_crop_video(self): msg = "height: " + str(height) + " width: " \ + str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth) - self.assertTrue(sum2.item() > 1, msg) - self.assertTrue(sum2.item() > sum1.item(), msg) + assert sum2.item() > 1, msg + assert sum2.item() > sum1.item(), msg - @unittest.skipIf(stats is None, 'scipy.stats is not available') - def test_normalize_video(self): + pytest.mark.skipif(stats is None, 'scipy.stats is not available') + @pytest.mark.parametrize('channels', [1, 3]) + def test_normalize_video(self, channels): def samples_from_standard_normal(tensor): p_value = stats.kstest(list(tensor.view(-1)), 'norm', args=(0, 1)).pvalue return p_value > 0.0001 random_state = random.getstate() random.seed(42) - for channels in [1, 3]: - numFrames = random.randint(4, 128) - height = random.randint(32, 256) - width = random.randint(32, 256) - mean = random.random() - std = random.random() - clip = torch.normal(mean, std, size=(channels, numFrames, height, width)) - mean = [clip[c].mean().item() for c in range(channels)] - std = [clip[c].std().item() for c in range(channels)] - normalized = transforms.NormalizeVideo(mean, std)(clip) - self.assertTrue(samples_from_standard_normal(normalized)) + + numFrames = random.randint(4, 128) + height = random.randint(32, 256) + width = random.randint(32, 256) + mean = random.random() + std = random.random() + clip = torch.normal(mean, std, size=(channels, numFrames, height, width)) + mean = [clip[c].mean().item() for c in range(channels)] + std = [clip[c].std().item() for c in range(channels)] + normalized = transforms.NormalizeVideo(mean, std)(clip) + assert samples_from_standard_normal(normalized) random.setstate(random_state) # Checking the optional in-place behaviour @@ -129,11 +130,11 @@ def test_to_tensor_video(self): numFrames, height, width = 64, 4, 4 trans = transforms.ToTensorVideo() - with self.assertRaises(TypeError): + with pytest.raises(TypeError): trans(np.random.rand(numFrames, height, width, 1).tolist()) trans(torch.rand((numFrames, height, width, 1), dtype=torch.float)) - with self.assertRaises(ValueError): + with pytest.raises(ValueError): trans(torch.ones((3, numFrames, height, width, 3), dtype=torch.uint8)) trans(torch.ones((height, width, 3), dtype=torch.uint8)) trans(torch.ones((width, 3), dtype=torch.uint8)) @@ -141,7 +142,7 @@ def test_to_tensor_video(self): trans.__repr__() - @unittest.skipIf(stats is None, 'scipy.stats not available') + pytest.mark.skipif(stats is None, 'scipy.stats not available') def test_random_horizontal_flip_video(self): random_state = random.getstate() random.seed(42) @@ -157,7 +158,7 @@ def test_random_horizontal_flip_video(self): p_value = stats.binom_test(num_horizontal, num_samples, p=0.5) random.setstate(random_state) - self.assertGreater(p_value, 0.0001) + assert p_value > 0.0001 num_samples = 250 num_horizontal = 0 @@ -168,10 +169,10 @@ def test_random_horizontal_flip_video(self): p_value = stats.binom_test(num_horizontal, num_samples, p=0.7) random.setstate(random_state) - self.assertGreater(p_value, 0.0001) + assert p_value > 0.0001 transforms.RandomHorizontalFlipVideo().__repr__() if __name__ == '__main__': - unittest.main() + pytest.main([__file__]) From 5006659cd264a97a0785dd3bd03e8103199defaa Mon Sep 17 00:00:00 2001 From: Pranjal Gulati Date: Fri, 11 Jun 2021 08:13:42 +0530 Subject: [PATCH 2/3] Fix minor lint issues --- test/test_transforms_video.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_transforms_video.py b/test/test_transforms_video.py index 6a249dc1c97..49b49aa1337 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -82,7 +82,7 @@ def test_center_crop_video(self): msg = "height: " + str(height) + " width: " \ + str(width) + " oheight: " + str(oheight) + " owidth: " + str(owidth) - assert sum1.item() > 1 == True, msg + assert sum1.item() > 1, msg oheight += 1 owidth += 1 @@ -97,7 +97,7 @@ def test_center_crop_video(self): assert sum2.item() > 1, msg assert sum2.item() > sum1.item(), msg - pytest.mark.skipif(stats is None, 'scipy.stats is not available') + pytest.mark.skipif(stats is None, reason='scipy.stats is not available') @pytest.mark.parametrize('channels', [1, 3]) def test_normalize_video(self, channels): def samples_from_standard_normal(tensor): @@ -142,7 +142,7 @@ def test_to_tensor_video(self): trans.__repr__() - pytest.mark.skipif(stats is None, 'scipy.stats not available') + pytest.mark.skipif(stats is None, reason='scipy.stats not available') def test_random_horizontal_flip_video(self): random_state = random.getstate() random.seed(42) From 35e47e237e5d4a44d749f0f80c29f77597644e7b Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 11 Jun 2021 11:12:06 +0100 Subject: [PATCH 3/3] fix flake8 --- test/test_transforms_video.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_transforms_video.py b/test/test_transforms_video.py index 49b49aa1337..81b65ef0a6d 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -97,7 +97,7 @@ def test_center_crop_video(self): assert sum2.item() > 1, msg assert sum2.item() > sum1.item(), msg - pytest.mark.skipif(stats is None, reason='scipy.stats is not available') + @pytest.mark.skipif(stats is None, reason='scipy.stats is not available') @pytest.mark.parametrize('channels', [1, 3]) def test_normalize_video(self, channels): def samples_from_standard_normal(tensor): @@ -142,7 +142,7 @@ def test_to_tensor_video(self): trans.__repr__() - pytest.mark.skipif(stats is None, reason='scipy.stats not available') + @pytest.mark.skipif(stats is None, reason='scipy.stats not available') def test_random_horizontal_flip_video(self): random_state = random.getstate() random.seed(42)