From c4b457dfd63222fd3e23621824092da03d5f1239 Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Tue, 8 Jun 2021 12:40:08 +0530 Subject: [PATCH 1/4] Port random affine, rotate, perspective and to_grayscale to pytest --- test/test_transforms_tensor.py | 213 ++++++++++++++++++--------------- 1 file changed, 119 insertions(+), 94 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 63b25fb7f11..df671845aa4 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -7,9 +7,11 @@ import numpy as np import unittest +import pytest from typing import Sequence from common_utils import ( + cpu_and_gpu, get_tmp_dir, int_dtypes, float_dtypes, @@ -418,100 +420,6 @@ def test_resized_crop(self): with get_tmp_dir() as tmp_dir: s_transform.save(os.path.join(tmp_dir, "t_resized_crop.pt")) - def test_random_affine(self): - tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=self.device) - batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=self.device) - - def _test(**kwargs): - transform = T.RandomAffine(**kwargs) - s_transform = torch.jit.script(transform) - - _test_transform_vs_scripted(transform, s_transform, tensor) - _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) - - return s_transform - - for interpolation in [NEAREST, BILINEAR]: - for shear in [15, 10.0, (5.0, 10.0), [-15, 15], [-10.0, 10.0, -11.0, 11.0]]: - _test(degrees=0.0, interpolation=interpolation, shear=shear) - - for scale in [(0.7, 1.2), [0.7, 1.2]]: - _test(degrees=0.0, interpolation=interpolation, scale=scale) - - for translate in [(0.1, 0.2), [0.2, 0.1]]: - _test(degrees=0.0, interpolation=interpolation, translate=translate) - - for degrees in [45, 35.0, (-45, 45), [-90.0, 90.0]]: - _test(degrees=degrees, interpolation=interpolation) - - for fill in [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]: - _test(degrees=0.0, interpolation=interpolation, fill=fill) - - s_transform = _test(degrees=0.0) - with get_tmp_dir() as tmp_dir: - s_transform.save(os.path.join(tmp_dir, "t_random_affine.pt")) - - def test_random_rotate(self): - tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=self.device) - batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=self.device) - - for center in [(0, 0), [10, 10], None, (56, 44)]: - for expand in [True, False]: - for degrees in [45, 35.0, (-45, 45), [-90.0, 90.0]]: - for interpolation in [NEAREST, BILINEAR]: - for fill in [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]: - transform = T.RandomRotation( - degrees=degrees, interpolation=interpolation, expand=expand, center=center, fill=fill - ) - s_transform = torch.jit.script(transform) - - _test_transform_vs_scripted(transform, s_transform, tensor) - _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) - - with get_tmp_dir() as tmp_dir: - s_transform.save(os.path.join(tmp_dir, "t_random_rotate.pt")) - - def test_random_perspective(self): - tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=self.device) - batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=self.device) - - for distortion_scale in np.linspace(0.1, 1.0, num=20): - for interpolation in [NEAREST, BILINEAR]: - for fill in [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]: - transform = T.RandomPerspective( - distortion_scale=distortion_scale, - interpolation=interpolation, - fill=fill - ) - s_transform = torch.jit.script(transform) - - _test_transform_vs_scripted(transform, s_transform, tensor) - _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) - - with get_tmp_dir() as tmp_dir: - s_transform.save(os.path.join(tmp_dir, "t_perspective.pt")) - - def test_to_grayscale(self): - - meth_kwargs = {"num_output_channels": 1} - tol = 1.0 + 1e-10 - _test_class_op( - T.Grayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=self.device, - tol=tol, agg_method="max" - ) - - meth_kwargs = {"num_output_channels": 3} - _test_class_op( - T.Grayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=self.device, - tol=tol, agg_method="max" - ) - - meth_kwargs = {} - _test_class_op( - T.RandomGrayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=self.device, - tol=tol, agg_method="max" - ) - def test_normalize(self): fn = T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) tensor, _ = _create_data(26, 34, device=self.device) @@ -718,5 +626,122 @@ def setUp(self): self.device = "cuda" +def _test(device, **kwargs): + tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) + batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=device) + transform = T.RandomAffine(**kwargs) + s_transform = torch.jit.script(transform) + + _test_transform_vs_scripted(transform, s_transform, tensor) + _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) + + return s_transform + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +def test_random_affine(device): + s_transform = _test(device, degrees=0.0) + with get_tmp_dir() as tmp_dir: + s_transform.save(os.path.join(tmp_dir, "t_random_affine.pt")) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) +@pytest.mark.parametrize('shear', [15, 10.0, (5.0, 10.0), [-15, 15], [-10.0, 10.0, -11.0, 11.0]]) +def test_random_affine_shear(device, interpolation, shear): + _test(device, degrees=0.0, interpolation=interpolation, shear=shear) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) +@pytest.mark.parametrize('scale', [(0.7, 1.2), [0.7, 1.2]]) +def test_random_affine_scale(device, interpolation, scale): + _test(device, degrees=0.0, interpolation=interpolation, scale=scale) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) +@pytest.mark.parametrize('translate', [(0.1, 0.2), [0.2, 0.1]]) +def test_random_affine_translate(device, interpolation, translate): + _test(device, degrees=0.0, interpolation=interpolation, translate=translate) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) +@pytest.mark.parametrize('degrees', [45, 35.0, (-45, 45), [-90.0, 90.0]]) +def test_random_affine_degrees(device, interpolation, degrees): + _test(device, degrees=degrees, interpolation=interpolation) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) +@pytest.mark.parametrize('fill', [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) +def test_random_affine_fill(device, interpolation, fill): + _test(device, degrees=0.0, interpolation=interpolation, fill=fill) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('center', [(0, 0), [10, 10], None, (56, 44)]) +@pytest.mark.parametrize('expand', [True, False]) +@pytest.mark.parametrize('degrees', [45, 35.0, (-45, 45), [-90.0, 90.0]]) +@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) +@pytest.mark.parametrize('fill', [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) +def test_random_rotate(device, center, expand, degrees, interpolation, fill): + tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) + batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=device) + + transform = T.RandomRotation( + degrees=degrees, interpolation=interpolation, expand=expand, center=center, fill=fill + ) + s_transform = torch.jit.script(transform) + + _test_transform_vs_scripted(transform, s_transform, tensor) + _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) + + with get_tmp_dir() as tmp_dir: + s_transform.save(os.path.join(tmp_dir, "t_random_rotate.pt")) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('distortion_scale', np.linspace(0.1, 1.0, num=20)) +@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) +@pytest.mark.parametrize('fill', [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) +def test_random_perspective(device, distortion_scale, interpolation, fill): + tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) + batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=device) + + transform = T.RandomPerspective( + distortion_scale=distortion_scale, + interpolation=interpolation, + fill=fill + ) + s_transform = torch.jit.script(transform) + + _test_transform_vs_scripted(transform, s_transform, tensor) + _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) + + with get_tmp_dir() as tmp_dir: + s_transform.save(os.path.join(tmp_dir, "t_perspective.pt")) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('tol', [1.0 + 1e-10]) +@pytest.mark.parametrize('num_output_channels', [0, 1, 3]) +def test_to_grayscale(device, num_output_channels, tol): + + if num_output_channels == 0: + meth_kwargs = {} + _test_class_op( + T.RandomGrayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, + tol=tol, agg_method="max" + ) + else: + meth_kwargs = {"num_output_channels": num_output_channels} + _test_class_op( + T.Grayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, + tol=tol, agg_method="max" + ) + + if __name__ == '__main__': unittest.main() From 19047cb7b0c1afb8f0d78860e5e419de3308f40b Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Wed, 9 Jun 2021 08:36:31 +0530 Subject: [PATCH 2/4] Defined separate save function --- test/test_transforms_tensor.py | 47 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index df671845aa4..2d232a25bbf 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -626,7 +626,7 @@ def setUp(self): self.device = "cuda" -def _test(device, **kwargs): +def _test_random_affine_helper(device, **kwargs): tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=device) transform = T.RandomAffine(**kwargs) @@ -635,12 +635,11 @@ def _test(device, **kwargs): _test_transform_vs_scripted(transform, s_transform, tensor) _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) - return s_transform - @pytest.mark.parametrize('device', cpu_and_gpu()) def test_random_affine(device): - s_transform = _test(device, degrees=0.0) + transform = T.RandomAffine(degrees=45.0) + s_transform = torch.jit.script(transform) with get_tmp_dir() as tmp_dir: s_transform.save(os.path.join(tmp_dir, "t_random_affine.pt")) @@ -649,35 +648,35 @@ def test_random_affine(device): @pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) @pytest.mark.parametrize('shear', [15, 10.0, (5.0, 10.0), [-15, 15], [-10.0, 10.0, -11.0, 11.0]]) def test_random_affine_shear(device, interpolation, shear): - _test(device, degrees=0.0, interpolation=interpolation, shear=shear) + _test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, shear=shear) @pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) @pytest.mark.parametrize('scale', [(0.7, 1.2), [0.7, 1.2]]) def test_random_affine_scale(device, interpolation, scale): - _test(device, degrees=0.0, interpolation=interpolation, scale=scale) + _test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, scale=scale) @pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) @pytest.mark.parametrize('translate', [(0.1, 0.2), [0.2, 0.1]]) def test_random_affine_translate(device, interpolation, translate): - _test(device, degrees=0.0, interpolation=interpolation, translate=translate) + _test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, translate=translate) @pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) @pytest.mark.parametrize('degrees', [45, 35.0, (-45, 45), [-90.0, 90.0]]) def test_random_affine_degrees(device, interpolation, degrees): - _test(device, degrees=degrees, interpolation=interpolation) + _test_random_affine_helper(device, degrees=degrees, interpolation=interpolation) @pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) @pytest.mark.parametrize('fill', [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) def test_random_affine_fill(device, interpolation, fill): - _test(device, degrees=0.0, interpolation=interpolation, fill=fill) + _test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, fill=fill) @pytest.mark.parametrize('device', cpu_and_gpu()) @@ -698,6 +697,10 @@ def test_random_rotate(device, center, expand, degrees, interpolation, fill): _test_transform_vs_scripted(transform, s_transform, tensor) _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) + +def test_random_rotate_save(): + transform = T.RandomRotation(degrees=45.0) + s_transform = torch.jit.script(transform) with get_tmp_dir() as tmp_dir: s_transform.save(os.path.join(tmp_dir, "t_random_rotate.pt")) @@ -720,27 +723,23 @@ def test_random_perspective(device, distortion_scale, interpolation, fill): _test_transform_vs_scripted(transform, s_transform, tensor) _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) + +def test_random_perspective_save(): + transform = T.RandomPerspective() + s_transform = torch.jit.script(transform) with get_tmp_dir() as tmp_dir: s_transform.save(os.path.join(tmp_dir, "t_perspective.pt")) @pytest.mark.parametrize('device', cpu_and_gpu()) -@pytest.mark.parametrize('tol', [1.0 + 1e-10]) -@pytest.mark.parametrize('num_output_channels', [0, 1, 3]) -def test_to_grayscale(device, num_output_channels, tol): +@pytest.mark.parametrize('Klass, meth_kwargs', [(T.Grayscale, {"num_output_channels": 1}), (T.Grayscale, {"num_output_channels": 3}), (T.RandomGrayscale, {})]) +def test_to_grayscale(device, Klass, meth_kwargs): - if num_output_channels == 0: - meth_kwargs = {} - _test_class_op( - T.RandomGrayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, - tol=tol, agg_method="max" - ) - else: - meth_kwargs = {"num_output_channels": num_output_channels} - _test_class_op( - T.Grayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, - tol=tol, agg_method="max" - ) + tol = 1.0 + 1e-10 + _test_class_op( + Klass, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, + tol=tol, agg_method="max" + ) if __name__ == '__main__': From c1141f4377786acca8fb29dc6f5dec436ab7139e Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 9 Jun 2021 11:27:30 +0100 Subject: [PATCH 3/4] pep8 --- test/test_transforms_tensor.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index a8d10324f30..e02b20863c2 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -749,7 +749,11 @@ def test_random_perspective_save(): @pytest.mark.parametrize('device', cpu_and_gpu()) -@pytest.mark.parametrize('Klass, meth_kwargs', [(T.Grayscale, {"num_output_channels": 1}), (T.Grayscale, {"num_output_channels": 3}), (T.RandomGrayscale, {})]) +@pytest.mark.parametrize('Klass, meth_kwargs', [ + (T.Grayscale, {"num_output_channels": 1}), + (T.Grayscale, {"num_output_channels": 3}), + (T.RandomGrayscale, {}) +]) def test_to_grayscale(device, Klass, meth_kwargs): tol = 1.0 + 1e-10 From aa4a1ae9a75c2250ea3b4b239ba9e82e8ec86b3a Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 9 Jun 2021 11:39:21 +0100 Subject: [PATCH 4/4] again --- test/test_transforms_tensor.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index e02b20863c2..4e167d64b6a 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -11,7 +11,6 @@ from typing import Sequence from common_utils import ( - cpu_and_gpu, get_tmp_dir, int_dtypes, float_dtypes, @@ -19,7 +18,7 @@ _create_data_batch, _assert_equal_tensor_to_pil, _assert_approx_equal_tensor_to_pil, - cpu_and_gpu + cpu_and_gpu, ) from _assert_utils import assert_equal