From 56beb8ff0637a04435d7af4b385c8650501cee2b Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Mon, 7 Jun 2021 23:20:53 +0530 Subject: [PATCH 01/11] Porting to pytest --- test/test_transforms_tensor.py | 181 +++++++++++++++------------------ 1 file changed, 84 insertions(+), 97 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 63b25fb7f11..164e104fbf5 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -5,6 +5,7 @@ from torchvision.transforms import InterpolationMode import numpy as np +import pytest import unittest from typing import Sequence @@ -16,7 +17,7 @@ _create_data, _create_data_batch, _assert_equal_tensor_to_pil, - _assert_approx_equal_tensor_to_pil, + _assert_approx_equal_tensor_to_pil, cpu_only, ) from _assert_utils import assert_equal @@ -94,52 +95,28 @@ def _test_op(func, method, device, fn_kwargs=None, meth_kwargs=None, test_exact_ _test_class_op(method, device, meth_kwargs, test_exact_match=test_exact_match, **match_kwargs) +@pytest.mark.parametrize( + 'func,method,device,fn_kwargs,match_kwargs',[ + (F.hflip,T.RandomHorizontalFlip,cpu_only(),None, {}), + (F.vflip,T.RandomVerticalFlip,cpu_only(),None,{}), + (F.invert,T.RandomInvert,cpu_only(),None,{}), + (F.posterize,T.RandomPosterize,cpu_only(),{"bits":4},{}), + (F.solarize,T.RandomSolarize,cpu_only(),{"threshold":192.0},{}), + (F.adjust_sharpness,T.RandomAdjustSharpness,cpu_only(),{"sharpness_factor":2.0},{}), + (F.autocontrast,T.RandomAutocontrast,cpu_only(),None,{'test_exact_match':False, + 'agg_method':'max', 'tol':(1 + 1e-5), 'allowed_percentage_diff':.05}), + (F.equalize,T.RandomEqualize,cpu_only(),None,{}) + ] +) +def test_random(func,method,device,fn_kwargs,match_kwargs): + _test_op(func,method,device,fn_kwargs,fn_kwargs,**match_kwargs) + + class Tester(unittest.TestCase): def setUp(self): self.device = "cpu" - def test_random_horizontal_flip(self): - _test_op(F.hflip, T.RandomHorizontalFlip, device=self.device) - - def test_random_vertical_flip(self): - _test_op(F.vflip, T.RandomVerticalFlip, device=self.device) - - def test_random_invert(self): - _test_op(F.invert, T.RandomInvert, device=self.device) - - def test_random_posterize(self): - fn_kwargs = meth_kwargs = {"bits": 4} - _test_op( - F.posterize, T.RandomPosterize, device=self.device, fn_kwargs=fn_kwargs, - meth_kwargs=meth_kwargs - ) - - def test_random_solarize(self): - fn_kwargs = meth_kwargs = {"threshold": 192.0} - _test_op( - F.solarize, T.RandomSolarize, device=self.device, fn_kwargs=fn_kwargs, - meth_kwargs=meth_kwargs - ) - - def test_random_adjust_sharpness(self): - fn_kwargs = meth_kwargs = {"sharpness_factor": 2.0} - _test_op( - F.adjust_sharpness, T.RandomAdjustSharpness, device=self.device, fn_kwargs=fn_kwargs, - meth_kwargs=meth_kwargs - ) - - def test_random_autocontrast(self): - # We check the max abs difference because on some (very rare) pixels, the actual value may be different - # between PIL and tensors due to floating approximations. - _test_op( - F.autocontrast, T.RandomAutocontrast, device=self.device, test_exact_match=False, - agg_method='max', tol=(1 + 1e-5), allowed_percentage_diff=.05 - ) - - def test_random_equalize(self): - _test_op(F.equalize, T.RandomEqualize, device=self.device) - def test_color_jitter(self): tol = 1.0 + 1e-10 @@ -639,75 +616,85 @@ def test_gaussian_blur(self): test_exact_match=False, device=self.device, agg_method="max", tol=tol ) - def test_random_erasing(self): - img = torch.rand(3, 60, 60) - # Test Set 0: invalid value - random_erasing = T.RandomErasing(value=(0.1, 0.2, 0.3, 0.4), p=1.0) - with self.assertRaises(ValueError, msg="If value is a sequence, it should have either a single value or 3"): - random_erasing(img) +@pytest.mark.xfail() +@pytest.mark.parametrize( + 'in_dtype,out_dtype',[ + int_dtypes()+float_dtypes(),int_dtypes()+float_dtypes() + ] +) +def test_convert_image_dtype(in_dtype,out_dtype): + tensor, _ = _create_data(26, 34, device=cpu_only()) + batch_tensors = torch.rand(4, 3, 44, 56, device=cpu_only()) - tensor, _ = _create_data(24, 32, channels=3, device=self.device) - batch_tensors = torch.rand(4, 3, 44, 56, device=self.device) + in_tensor = tensor.to(in_dtype) + in_batch_tensors = batch_tensors.to(in_dtype) - test_configs = [ - {"value": 0.2}, - {"value": "random"}, - {"value": (0.2, 0.2, 0.2)}, - {"value": "random", "ratio": (0.1, 0.2)}, - ] + fn = T.ConvertImageDtype(dtype=out_dtype) + scripted_fn = torch.jit.script(fn) - for config in test_configs: - fn = T.RandomErasing(**config) - scripted_fn = torch.jit.script(fn) - _test_transform_vs_scripted(fn, scripted_fn, tensor) - _test_transform_vs_scripted_on_batch(fn, scripted_fn, batch_tensors) + if (in_dtype == torch.float32 and out_dtype in (torch.int32, torch.int64)) or \ + (in_dtype == torch.float64 and out_dtype == torch.int64): + with pytest.raises(RuntimeError, match=r"cannot be performed safely"): + _test_transform_vs_scripted(fn, scripted_fn, in_tensor) + with pytest.raises(RuntimeError,match= r"cannot be performed safely"): + _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) - with get_tmp_dir() as tmp_dir: - scripted_fn.save(os.path.join(tmp_dir, "t_random_erasing.pt")) + _test_transform_vs_scripted(fn, scripted_fn, in_tensor) + _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) - def test_convert_image_dtype(self): - tensor, _ = _create_data(26, 34, device=self.device) - batch_tensors = torch.rand(4, 3, 44, 56, device=self.device) + with get_tmp_dir() as tmp_dir: + scripted_fn.save(os.path.join(tmp_dir, "t_convert_dtype.pt")) - for in_dtype in int_dtypes() + float_dtypes(): - in_tensor = tensor.to(in_dtype) - in_batch_tensors = batch_tensors.to(in_dtype) - for out_dtype in int_dtypes() + float_dtypes(): - fn = T.ConvertImageDtype(dtype=out_dtype) - scripted_fn = torch.jit.script(fn) +@pytest.mark.parametrize( + 'policy,fill',[ + (T.AutoAugmentPolicy(),[None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) + ] +) +def test_autoaugment(policy,fill): + tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=cpu_only()) + batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=cpu_only()) + + s_transform = None + transform = T.AutoAugment(policy=policy, fill=fill) + s_transform = torch.jit.script(transform) + for _ in range(25): + _test_transform_vs_scripted(transform, s_transform, tensor) + _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) + + if s_transform is not None: + with get_tmp_dir() as tmp_dir: + s_transform.save(os.path.join(tmp_dir, "t_autoaugment.pt")) - if (in_dtype == torch.float32 and out_dtype in (torch.int32, torch.int64)) or \ - (in_dtype == torch.float64 and out_dtype == torch.int64): - with self.assertRaisesRegex(RuntimeError, r"cannot be performed safely"): - _test_transform_vs_scripted(fn, scripted_fn, in_tensor) - with self.assertRaisesRegex(RuntimeError, r"cannot be performed safely"): - _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) - continue - _test_transform_vs_scripted(fn, scripted_fn, in_tensor) - _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) +@pytest.mark.parametrize( + 'config',[ + ({"value": 0.2},), + ({"value": "random"},), + ({"value": (0.2, 0.2, 0.2)},), + {"value": "random", "ratio": (0.1, 0.2)}, + ] +) +def test_random_erasing(config): + tensor, _ = _create_data(24, 32, channels=3, device=cpu_only()) + batch_tensors = torch.rand(4, 3, 44, 56, device=cpu_only()) - with get_tmp_dir() as tmp_dir: - scripted_fn.save(os.path.join(tmp_dir, "t_convert_dtype.pt")) + fn = T.RandomErasing(**config) + scripted_fn = torch.jit.script(fn) + _test_transform_vs_scripted(fn, scripted_fn, tensor) + _test_transform_vs_scripted_on_batch(fn, scripted_fn, batch_tensors) - def test_autoaugment(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) + with get_tmp_dir() as tmp_dir: + scripted_fn.save(os.path.join(tmp_dir, "t_random_erasing.pt")) - s_transform = None - for policy in T.AutoAugmentPolicy: - for fill in [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]: - transform = T.AutoAugment(policy=policy, fill=fill) - s_transform = torch.jit.script(transform) - for _ in range(25): - _test_transform_vs_scripted(transform, s_transform, tensor) - _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) - if s_transform is not None: - with get_tmp_dir() as tmp_dir: - s_transform.save(os.path.join(tmp_dir, "t_autoaugment.pt")) +def test_random_erasing_with_invalid_data(): + img = torch.rand(3, 60, 60) + # Test Set 0: invalid value + random_erasing = T.RandomErasing(value=(0.1, 0.2, 0.3, 0.4), p=1.0) + with pytest.raises(ValueError,match="If value is a sequence, it should have either a single value or 3"): + random_erasing(img) @unittest.skipIf(not torch.cuda.is_available(), reason="Skip if no CUDA device") From 867e61401d18aab07022d84e8eb954b3112396a8 Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Mon, 7 Jun 2021 23:59:14 +0530 Subject: [PATCH 02/11] Lint fixes --- test/test_transforms_tensor.py | 43 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 164e104fbf5..d85a28187ad 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -96,20 +96,21 @@ def _test_op(func, method, device, fn_kwargs=None, meth_kwargs=None, test_exact_ @pytest.mark.parametrize( - 'func,method,device,fn_kwargs,match_kwargs',[ - (F.hflip,T.RandomHorizontalFlip,cpu_only(),None, {}), - (F.vflip,T.RandomVerticalFlip,cpu_only(),None,{}), - (F.invert,T.RandomInvert,cpu_only(),None,{}), - (F.posterize,T.RandomPosterize,cpu_only(),{"bits":4},{}), - (F.solarize,T.RandomSolarize,cpu_only(),{"threshold":192.0},{}), - (F.adjust_sharpness,T.RandomAdjustSharpness,cpu_only(),{"sharpness_factor":2.0},{}), - (F.autocontrast,T.RandomAutocontrast,cpu_only(),None,{'test_exact_match':False, - 'agg_method':'max', 'tol':(1 + 1e-5), 'allowed_percentage_diff':.05}), - (F.equalize,T.RandomEqualize,cpu_only(),None,{}) + 'func,method,device,fn_kwargs,match_kwargs', [ + (F.hflip, T.RandomHorizontalFlip, cpu_only(), None, {}), + (F.vflip, T.RandomVerticalFlip, cpu_only(), None, {}), + (F.invert, T.RandomInvert, cpu_only(), None, {}), + (F.posterize, T.RandomPosterize, cpu_only(), {"bits": 4}, {}), + (F.solarize, T.RandomSolarize, cpu_only(), {"threshold": 192.0}, {}), + (F.adjust_sharpness, T.RandomAdjustSharpness, cpu_only(), {"sharpness_factor": 2.0}, {}), + (F.autocontrast, T.RandomAutocontrast, cpu_only(), None, {'test_exact_match': False, + 'agg_method': 'max', 'tol': (1 + 1e-5), + 'allowed_percentage_diff': .05}), + (F.equalize, T.RandomEqualize, cpu_only(), None, {}) ] ) -def test_random(func,method,device,fn_kwargs,match_kwargs): - _test_op(func,method,device,fn_kwargs,fn_kwargs,**match_kwargs) +def test_random(func, method, device, fn_kwargs, match_kwargs): + _test_op(func, method, device, fn_kwargs, fn_kwargs, **match_kwargs) class Tester(unittest.TestCase): @@ -619,11 +620,11 @@ def test_gaussian_blur(self): @pytest.mark.xfail() @pytest.mark.parametrize( - 'in_dtype,out_dtype',[ - int_dtypes()+float_dtypes(),int_dtypes()+float_dtypes() + 'in_dtype,out_dtype', [ + int_dtypes() + float_dtypes(), int_dtypes() + float_dtypes() ] ) -def test_convert_image_dtype(in_dtype,out_dtype): +def test_convert_image_dtype(in_dtype, out_dtype): tensor, _ = _create_data(26, 34, device=cpu_only()) batch_tensors = torch.rand(4, 3, 44, 56, device=cpu_only()) @@ -637,7 +638,7 @@ def test_convert_image_dtype(in_dtype,out_dtype): (in_dtype == torch.float64 and out_dtype == torch.int64): with pytest.raises(RuntimeError, match=r"cannot be performed safely"): _test_transform_vs_scripted(fn, scripted_fn, in_tensor) - with pytest.raises(RuntimeError,match= r"cannot be performed safely"): + with pytest.raises(RuntimeError, match=r"cannot be performed safely"): _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) _test_transform_vs_scripted(fn, scripted_fn, in_tensor) @@ -648,11 +649,11 @@ def test_convert_image_dtype(in_dtype,out_dtype): @pytest.mark.parametrize( - 'policy,fill',[ - (T.AutoAugmentPolicy(),[None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) + 'policy,fill', [ + (T.AutoAugmentPolicy(), [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) ] ) -def test_autoaugment(policy,fill): +def test_autoaugment(policy, fill): tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=cpu_only()) batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=cpu_only()) @@ -669,7 +670,7 @@ def test_autoaugment(policy,fill): @pytest.mark.parametrize( - 'config',[ + 'config', [ ({"value": 0.2},), ({"value": "random"},), ({"value": (0.2, 0.2, 0.2)},), @@ -693,7 +694,7 @@ def test_random_erasing_with_invalid_data(): img = torch.rand(3, 60, 60) # Test Set 0: invalid value random_erasing = T.RandomErasing(value=(0.1, 0.2, 0.3, 0.4), p=1.0) - with pytest.raises(ValueError,match="If value is a sequence, it should have either a single value or 3"): + with pytest.raises(ValueError, match="If value is a sequence, it should have either a single value or 3"): random_erasing(img) From c292f3574a2a1c478e73f726e55b9b38b3383483 Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Tue, 8 Jun 2021 13:12:32 +0530 Subject: [PATCH 03/11] Using decorator for cpu_only --- test/test_transforms_tensor.py | 36 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index d85a28187ad..96259c49732 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -95,18 +95,19 @@ def _test_op(func, method, device, fn_kwargs=None, meth_kwargs=None, test_exact_ _test_class_op(method, device, meth_kwargs, test_exact_match=test_exact_match, **match_kwargs) +@cpu_only @pytest.mark.parametrize( 'func,method,device,fn_kwargs,match_kwargs', [ - (F.hflip, T.RandomHorizontalFlip, cpu_only(), None, {}), - (F.vflip, T.RandomVerticalFlip, cpu_only(), None, {}), - (F.invert, T.RandomInvert, cpu_only(), None, {}), - (F.posterize, T.RandomPosterize, cpu_only(), {"bits": 4}, {}), - (F.solarize, T.RandomSolarize, cpu_only(), {"threshold": 192.0}, {}), - (F.adjust_sharpness, T.RandomAdjustSharpness, cpu_only(), {"sharpness_factor": 2.0}, {}), - (F.autocontrast, T.RandomAutocontrast, cpu_only(), None, {'test_exact_match': False, - 'agg_method': 'max', 'tol': (1 + 1e-5), - 'allowed_percentage_diff': .05}), - (F.equalize, T.RandomEqualize, cpu_only(), None, {}) + (F.hflip, T.RandomHorizontalFlip, "cpu", None, {}), + (F.vflip, T.RandomVerticalFlip, "cpu", None, {}), + (F.invert, T.RandomInvert, "cpu", None, {}), + (F.posterize, T.RandomPosterize, "cpu", {"bits": 4}, {}), + (F.solarize, T.RandomSolarize, "cpu", {"threshold": 192.0}, {}), + (F.adjust_sharpness, T.RandomAdjustSharpness, "cpu", {"sharpness_factor": 2.0}, {}), + (F.autocontrast, T.RandomAutocontrast, "cpu", None, {'test_exact_match': False, + 'agg_method': 'max', 'tol': (1 + 1e-5), + 'allowed_percentage_diff': .05}), + (F.equalize, T.RandomEqualize, "cpu", None, {}) ] ) def test_random(func, method, device, fn_kwargs, match_kwargs): @@ -618,6 +619,7 @@ def test_gaussian_blur(self): ) +@cpu_only @pytest.mark.xfail() @pytest.mark.parametrize( 'in_dtype,out_dtype', [ @@ -625,8 +627,8 @@ def test_gaussian_blur(self): ] ) def test_convert_image_dtype(in_dtype, out_dtype): - tensor, _ = _create_data(26, 34, device=cpu_only()) - batch_tensors = torch.rand(4, 3, 44, 56, device=cpu_only()) + tensor, _ = _create_data(26, 34, device="cpu") + batch_tensors = torch.rand(4, 3, 44, 56, device="cpu") in_tensor = tensor.to(in_dtype) in_batch_tensors = batch_tensors.to(in_dtype) @@ -648,14 +650,15 @@ def test_convert_image_dtype(in_dtype, out_dtype): scripted_fn.save(os.path.join(tmp_dir, "t_convert_dtype.pt")) +@cpu_only @pytest.mark.parametrize( 'policy,fill', [ (T.AutoAugmentPolicy(), [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) ] ) def test_autoaugment(policy, fill): - tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=cpu_only()) - batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=cpu_only()) + tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device="cpu") + batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device="cpu") s_transform = None transform = T.AutoAugment(policy=policy, fill=fill) @@ -669,6 +672,7 @@ def test_autoaugment(policy, fill): s_transform.save(os.path.join(tmp_dir, "t_autoaugment.pt")) +@cpu_only @pytest.mark.parametrize( 'config', [ ({"value": 0.2},), @@ -678,8 +682,8 @@ def test_autoaugment(policy, fill): ] ) def test_random_erasing(config): - tensor, _ = _create_data(24, 32, channels=3, device=cpu_only()) - batch_tensors = torch.rand(4, 3, 44, 56, device=cpu_only()) + tensor, _ = _create_data(24, 32, channels=3, device="cpu") + batch_tensors = torch.rand(4, 3, 44, 56, device="cpu") fn = T.RandomErasing(**config) scripted_fn = torch.jit.script(fn) From 1710947624f386794eba2b421e56d2ff8af2b0ea Mon Sep 17 00:00:00 2001 From: tanvimoharir <74228962+tanvimoharir@users.noreply.github.com> Date: Tue, 8 Jun 2021 18:47:47 +0530 Subject: [PATCH 04/11] Update T.AutoAugmentPolicy call --- test/test_transforms_tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 96259c49732..fa3f94e581b 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -653,7 +653,7 @@ def test_convert_image_dtype(in_dtype, out_dtype): @cpu_only @pytest.mark.parametrize( 'policy,fill', [ - (T.AutoAugmentPolicy(), [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) + (T.AutoAugmentPolicy, [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) ] ) def test_autoaugment(policy, fill): From bb2184f6255832d842321c264a6d6b7a41e36cee Mon Sep 17 00:00:00 2001 From: tanvimoharir <74228962+tanvimoharir@users.noreply.github.com> Date: Wed, 9 Jun 2021 21:23:06 +0530 Subject: [PATCH 05/11] Adding missing paranthesis --- test/test_transforms_tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index fa3f94e581b..9c5153bdeaf 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -623,7 +623,7 @@ def test_gaussian_blur(self): @pytest.mark.xfail() @pytest.mark.parametrize( 'in_dtype,out_dtype', [ - int_dtypes() + float_dtypes(), int_dtypes() + float_dtypes() + (int_dtypes() + float_dtypes(), int_dtypes() + float_dtypes()) ] ) def test_convert_image_dtype(in_dtype, out_dtype): From 43b1e335596e7fa3374d3346ee1e13359b6b3f56 Mon Sep 17 00:00:00 2001 From: tanvimoharir <74228962+tanvimoharir@users.noreply.github.com> Date: Wed, 9 Jun 2021 22:39:15 +0530 Subject: [PATCH 06/11] Changes for failed tests --- test/test_transforms_tensor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 9c5153bdeaf..2e363500b18 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -653,7 +653,7 @@ def test_convert_image_dtype(in_dtype, out_dtype): @cpu_only @pytest.mark.parametrize( 'policy,fill', [ - (T.AutoAugmentPolicy, [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) + (T.AutoAugmentPolicy.name, [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) ] ) def test_autoaugment(policy, fill): @@ -675,10 +675,10 @@ def test_autoaugment(policy, fill): @cpu_only @pytest.mark.parametrize( 'config', [ - ({"value": 0.2},), - ({"value": "random"},), - ({"value": (0.2, 0.2, 0.2)},), - {"value": "random", "ratio": (0.1, 0.2)}, + {"value": 0.2}, + {"value": "random"}, + {"value": (0.2, 0.2, 0.2)}, + {"value": "random", "ratio": (0.1, 0.2)} ] ) def test_random_erasing(config): From 5c5773b8b3cf4b051b36768a31450a842d1dd5cc Mon Sep 17 00:00:00 2001 From: tanvimoharir <74228962+tanvimoharir@users.noreply.github.com> Date: Wed, 9 Jun 2021 23:42:49 +0530 Subject: [PATCH 07/11] Lint fixes --- test/test_transforms_tensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 5beca05a5d6..5a3e8f860be 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -8,7 +8,6 @@ import pytest import unittest -import pytest from typing import Sequence from common_utils import ( @@ -384,7 +383,7 @@ def test_resized_crop_save(self): @unittest.skipIf(not torch.cuda.is_available(), reason="Skip if no CUDA device") -class CUDATester(Tester): +class CUDATester(unittest.TestCase): def setUp(self): torch.set_deterministic(False) @@ -593,6 +592,7 @@ def test_random_erasing_with_invalid_data(): with pytest.raises(ValueError, match="If value is a sequence, it should have either a single value or 3"): random_erasing(img) + @pytest.mark.parametrize('device', cpu_and_gpu()) def test_normalize(device): fn = T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) From df626527d28b9c38100d2b2aeceb94033c7995bb Mon Sep 17 00:00:00 2001 From: tanvimoharir <74228962+tanvimoharir@users.noreply.github.com> Date: Wed, 9 Jun 2021 23:58:59 +0530 Subject: [PATCH 08/11] Changes --- test/test_transforms_tensor.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 5a3e8f860be..58f300283b0 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -542,11 +542,8 @@ def test_convert_image_dtype(in_dtype, out_dtype): @cpu_only -@pytest.mark.parametrize( - 'policy,fill', [ - ([policy for policy in T.AutoAugmentPolicy], [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) - ] -) +@pytest.mark.parametrize('policy', [policy for policy in T.AutoAugmentPolicy]) +@pytest.mark.parametrize('fill', [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) def test_autoaugment(policy, fill): tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device="cpu") batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device="cpu") @@ -592,7 +589,7 @@ def test_random_erasing_with_invalid_data(): with pytest.raises(ValueError, match="If value is a sequence, it should have either a single value or 3"): random_erasing(img) - + @pytest.mark.parametrize('device', cpu_and_gpu()) def test_normalize(device): fn = T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) From ed48ad28ea2e977ca2be85a57f7272dcf41e016f Mon Sep 17 00:00:00 2001 From: tanvimoharir <74228962+tanvimoharir@users.noreply.github.com> Date: Thu, 10 Jun 2021 19:23:03 +0530 Subject: [PATCH 09/11] Changes from review comments --- test/test_transforms_tensor.py | 100 +++++++++++++++++---------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 58f300283b0..b0403748aa9 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -7,7 +7,6 @@ import numpy as np import pytest -import unittest from typing import Sequence from common_utils import ( @@ -23,7 +22,6 @@ ) from _assert_utils import assert_equal - NEAREST, BILINEAR, BICUBIC = InterpolationMode.NEAREST, InterpolationMode.BILINEAR, InterpolationMode.BICUBIC @@ -97,19 +95,19 @@ def _test_op(func, method, device, fn_kwargs=None, meth_kwargs=None, test_exact_ _test_class_op(method, device, meth_kwargs, test_exact_match=test_exact_match, **match_kwargs) -@cpu_only +@pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize( - 'func,method,device,fn_kwargs,match_kwargs', [ - (F.hflip, T.RandomHorizontalFlip, "cpu", None, {}), - (F.vflip, T.RandomVerticalFlip, "cpu", None, {}), - (F.invert, T.RandomInvert, "cpu", None, {}), - (F.posterize, T.RandomPosterize, "cpu", {"bits": 4}, {}), - (F.solarize, T.RandomSolarize, "cpu", {"threshold": 192.0}, {}), - (F.adjust_sharpness, T.RandomAdjustSharpness, "cpu", {"sharpness_factor": 2.0}, {}), - (F.autocontrast, T.RandomAutocontrast, "cpu", None, {'test_exact_match': False, - 'agg_method': 'max', 'tol': (1 + 1e-5), - 'allowed_percentage_diff': .05}), - (F.equalize, T.RandomEqualize, "cpu", None, {}) + 'func,method,fn_kwargs,match_kwargs', [ + (F.hflip, T.RandomHorizontalFlip, None, {}), + (F.vflip, T.RandomVerticalFlip, None, {}), + (F.invert, T.RandomInvert, None, {}), + (F.posterize, T.RandomPosterize, {"bits": 4}, {}), + (F.solarize, T.RandomSolarize, {"threshold": 192.0}, {}), + (F.adjust_sharpness, T.RandomAdjustSharpness, {"sharpness_factor": 2.0}, {}), + (F.autocontrast, T.RandomAutocontrast, None, {'test_exact_match': False, + 'agg_method': 'max', 'tol': (1 + 1e-5), + 'allowed_percentage_diff': .05}), + (F.equalize, T.RandomEqualize, None, {}) ] ) def test_random(func, method, device, fn_kwargs, match_kwargs): @@ -241,7 +239,7 @@ def test_center_crop(device): meth_kwargs=meth_kwargs ) fn_kwargs = {"output_size": (5,)} - meth_kwargs = {"size": (5, )} + meth_kwargs = {"size": (5,)} _test_op( F.center_crop, T.CenterCrop, device=device, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs @@ -273,7 +271,7 @@ def test_center_crop(device): # test_ten_crop (F.ten_crop, T.TenCrop, 10) ]) -@pytest.mark.parametrize('size', [(5, ), [5, ], (4, 5), [4, 5]]) +@pytest.mark.parametrize('size', [(5,), [5, ], (4, 5), [4, 5]]) def test_x_crop(fn, method, out_length, size, device): meth_kwargs = fn_kwargs = {'size': size} scripted_fn = torch.jit.script(fn) @@ -364,7 +362,7 @@ def test_resize_save(self): @pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize('scale', [(0.7, 1.2), [0.7, 1.2]]) @pytest.mark.parametrize('ratio', [(0.75, 1.333), [0.75, 1.333]]) - @pytest.mark.parametrize('size', [(32, ), [44, ], [32, ], [32, 32], (32, 32), [44, 55]]) + @pytest.mark.parametrize('size', [(32,), [44, ], [32, ], [32, 32], (32, 32), [44, 55]]) @pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR, BICUBIC]) def test_resized_crop(self, scale, ratio, size, interpolation, device): tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) @@ -382,14 +380,6 @@ def test_resized_crop_save(self): s_transform.save(os.path.join(tmp_dir, "t_resized_crop.pt")) -@unittest.skipIf(not torch.cuda.is_available(), reason="Skip if no CUDA device") -class CUDATester(unittest.TestCase): - - def setUp(self): - torch.set_deterministic(False) - self.device = "cuda" - - 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) @@ -502,7 +492,6 @@ def test_random_perspective_save(): (T.RandomGrayscale, {}) ]) def test_to_grayscale(device, Klass, meth_kwargs): - tol = 1.0 + 1e-10 _test_class_op( Klass, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, @@ -510,16 +499,12 @@ def test_to_grayscale(device, Klass, meth_kwargs): ) -@cpu_only -@pytest.mark.xfail() -@pytest.mark.parametrize( - 'in_dtype,out_dtype', [ - (int_dtypes() + float_dtypes(), int_dtypes() + float_dtypes()) - ] -) -def test_convert_image_dtype(in_dtype, out_dtype): - tensor, _ = _create_data(26, 34, device="cpu") - batch_tensors = torch.rand(4, 3, 44, 56, device="cpu") +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('in_dtype', int_dtypes() + float_dtypes()) +@pytest.mark.parametrize('out_dtype', int_dtypes() + float_dtypes()) +def test_convert_image_dtype(device, in_dtype, out_dtype): + tensor, _ = _create_data(26, 34, device=device) + batch_tensors = torch.rand(4, 3, 44, 56, device=device) in_tensor = tensor.to(in_dtype) in_batch_tensors = batch_tensors.to(in_dtype) @@ -537,16 +522,21 @@ def test_convert_image_dtype(in_dtype, out_dtype): _test_transform_vs_scripted(fn, scripted_fn, in_tensor) _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) + +@pytest.mark.parametrize('out_dtype', int_dtypes() + float_dtypes()) +def test_convert_image_dtype_save(out_dtype): + fn = T.ConvertImageDtype(dtype=out_dtype) + scripted_fn = torch.jit.script(fn) with get_tmp_dir() as tmp_dir: scripted_fn.save(os.path.join(tmp_dir, "t_convert_dtype.pt")) -@cpu_only +@pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize('policy', [policy for policy in T.AutoAugmentPolicy]) @pytest.mark.parametrize('fill', [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) -def test_autoaugment(policy, fill): - tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device="cpu") - batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device="cpu") +def test_autoaugment(device, policy, 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) s_transform = None transform = T.AutoAugment(policy=policy, fill=fill) @@ -555,12 +545,18 @@ def test_autoaugment(policy, fill): _test_transform_vs_scripted(transform, s_transform, tensor) _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) + +@pytest.mark.parametrize('policy', [policy for policy in T.AutoAugmentPolicy]) +@pytest.mark.parametrize('fill', [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) +def test_autoaugment_save(policy, fill): + transform = T.AutoAugment(policy=policy, fill=fill) + s_transform = torch.jit.script(transform) if s_transform is not None: with get_tmp_dir() as tmp_dir: s_transform.save(os.path.join(tmp_dir, "t_autoaugment.pt")) -@cpu_only +@pytest.mark.parametrize('device', cpu_and_gpu()) @pytest.mark.parametrize( 'config', [ {"value": 0.2}, @@ -569,15 +565,27 @@ def test_autoaugment(policy, fill): {"value": "random", "ratio": (0.1, 0.2)} ] ) -def test_random_erasing(config): - tensor, _ = _create_data(24, 32, channels=3, device="cpu") - batch_tensors = torch.rand(4, 3, 44, 56, device="cpu") +def test_random_erasing(device, config): + tensor, _ = _create_data(24, 32, channels=3, device=device) + batch_tensors = torch.rand(4, 3, 44, 56, device=device) fn = T.RandomErasing(**config) scripted_fn = torch.jit.script(fn) _test_transform_vs_scripted(fn, scripted_fn, tensor) _test_transform_vs_scripted_on_batch(fn, scripted_fn, batch_tensors) + +@pytest.mark.parametrize( + 'config', [ + {"value": 0.2}, + {"value": "random"}, + {"value": (0.2, 0.2, 0.2)}, + {"value": "random", "ratio": (0.1, 0.2)} + ] +) +def test_random_erasing_save(config): + fn = T.RandomErasing(**config) + scripted_fn = torch.jit.script(fn) with get_tmp_dir() as tmp_dir: scripted_fn.save(os.path.join(tmp_dir, "t_random_erasing.pt")) @@ -707,7 +715,3 @@ def test_gaussian_blur(device, meth_kwargs): T.GaussianBlur, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, agg_method="max", tol=tol ) - - -if __name__ == '__main__': - unittest.main() From 1c1e5b14cf4e069db9e27ce2db86a71ece9aae16 Mon Sep 17 00:00:00 2001 From: tanvimoharir <74228962+tanvimoharir@users.noreply.github.com> Date: Thu, 10 Jun 2021 19:48:37 +0530 Subject: [PATCH 10/11] Adding 'return' to fix failure --- test/test_transforms_tensor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index b0403748aa9..5b31daa6d95 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -518,6 +518,7 @@ def test_convert_image_dtype(device, in_dtype, out_dtype): _test_transform_vs_scripted(fn, scripted_fn, in_tensor) with pytest.raises(RuntimeError, match=r"cannot be performed safely"): _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) + return _test_transform_vs_scripted(fn, scripted_fn, in_tensor) _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) From 28b2d9b524ef0542b98724f8e04d48892392cc99 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 10 Jun 2021 16:16:36 +0100 Subject: [PATCH 11/11] remove parametrization from _save tests --- test/test_transforms_tensor.py | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 5b31daa6d95..1a4ecfbf7c7 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -524,9 +524,8 @@ def test_convert_image_dtype(device, in_dtype, out_dtype): _test_transform_vs_scripted_on_batch(fn, scripted_fn, in_batch_tensors) -@pytest.mark.parametrize('out_dtype', int_dtypes() + float_dtypes()) -def test_convert_image_dtype_save(out_dtype): - fn = T.ConvertImageDtype(dtype=out_dtype) +def test_convert_image_dtype_save(): + fn = T.ConvertImageDtype(dtype=torch.uint8) scripted_fn = torch.jit.script(fn) with get_tmp_dir() as tmp_dir: scripted_fn.save(os.path.join(tmp_dir, "t_convert_dtype.pt")) @@ -547,14 +546,11 @@ def test_autoaugment(device, policy, fill): _test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) -@pytest.mark.parametrize('policy', [policy for policy in T.AutoAugmentPolicy]) -@pytest.mark.parametrize('fill', [None, 85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) -def test_autoaugment_save(policy, fill): - transform = T.AutoAugment(policy=policy, fill=fill) +def test_autoaugment_save(): + transform = T.AutoAugment() s_transform = torch.jit.script(transform) - if s_transform is not None: - with get_tmp_dir() as tmp_dir: - s_transform.save(os.path.join(tmp_dir, "t_autoaugment.pt")) + with get_tmp_dir() as tmp_dir: + s_transform.save(os.path.join(tmp_dir, "t_autoaugment.pt")) @pytest.mark.parametrize('device', cpu_and_gpu()) @@ -576,16 +572,8 @@ def test_random_erasing(device, config): _test_transform_vs_scripted_on_batch(fn, scripted_fn, batch_tensors) -@pytest.mark.parametrize( - 'config', [ - {"value": 0.2}, - {"value": "random"}, - {"value": (0.2, 0.2, 0.2)}, - {"value": "random", "ratio": (0.1, 0.2)} - ] -) -def test_random_erasing_save(config): - fn = T.RandomErasing(**config) +def test_random_erasing_save(): + fn = T.RandomErasing(value=0.2) scripted_fn = torch.jit.script(fn) with get_tmp_dir() as tmp_dir: scripted_fn.save(os.path.join(tmp_dir, "t_random_erasing.pt"))