From 4f22341ce9f141e6fa064d973a75bff9bd7c1b8d Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Wed, 9 Jun 2021 18:11:29 +0530 Subject: [PATCH 1/3] Port normalize, linear_transformation, compose, random_apply and gaussian_blur to pytest --- test/test_transforms_tensor.py | 247 ++++++++++++++++----------------- 1 file changed, 120 insertions(+), 127 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 4e167d64b6a..22cb15b75e8 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -286,133 +286,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_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) - - with self.assertRaisesRegex(TypeError, r"Input tensor should be a float tensor"): - fn(tensor) - - batch_tensors = torch.rand(4, 3, 44, 56, device=self.device) - tensor = tensor.to(dtype=torch.float32) / 255.0 - # test for class interface - 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) - - with get_tmp_dir() as tmp_dir: - scripted_fn.save(os.path.join(tmp_dir, "t_norm.pt")) - - def test_linear_transformation(self): - c, h, w = 3, 24, 32 - - tensor, _ = _create_data(h, w, channels=c, device=self.device) - - matrix = torch.rand(c * h * w, c * h * w, device=self.device) - mean_vector = torch.rand(c * h * w, device=self.device) - - fn = T.LinearTransformation(matrix, mean_vector) - scripted_fn = torch.jit.script(fn) - - _test_transform_vs_scripted(fn, scripted_fn, tensor) - - batch_tensors = torch.rand(4, c, h, w, device=self.device) - # We skip some tests from _test_transform_vs_scripted_on_batch as - # results for scripted and non-scripted transformations are not exactly the same - torch.manual_seed(12) - transformed_batch = fn(batch_tensors) - torch.manual_seed(12) - s_transformed_batch = scripted_fn(batch_tensors) - assert_equal(transformed_batch, s_transformed_batch) - - with get_tmp_dir() as tmp_dir: - scripted_fn.save(os.path.join(tmp_dir, "t_norm.pt")) - - def test_compose(self): - tensor, _ = _create_data(26, 34, device=self.device) - tensor = tensor.to(dtype=torch.float32) / 255.0 - - transforms = T.Compose([ - T.CenterCrop(10), - T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), - ]) - s_transforms = torch.nn.Sequential(*transforms.transforms) - - scripted_fn = torch.jit.script(s_transforms) - torch.manual_seed(12) - transformed_tensor = transforms(tensor) - torch.manual_seed(12) - transformed_tensor_script = scripted_fn(tensor) - assert_equal(transformed_tensor, transformed_tensor_script, msg="{}".format(transforms)) - - t = T.Compose([ - lambda x: x, - ]) - with self.assertRaisesRegex(RuntimeError, r"Could not get name of python class object"): - torch.jit.script(t) - - def test_random_apply(self): - tensor, _ = _create_data(26, 34, device=self.device) - tensor = tensor.to(dtype=torch.float32) / 255.0 - - transforms = T.RandomApply([ - T.RandomHorizontalFlip(), - T.ColorJitter(), - ], p=0.4) - s_transforms = T.RandomApply(torch.nn.ModuleList([ - T.RandomHorizontalFlip(), - T.ColorJitter(), - ]), p=0.4) - - scripted_fn = torch.jit.script(s_transforms) - torch.manual_seed(12) - transformed_tensor = transforms(tensor) - torch.manual_seed(12) - transformed_tensor_script = scripted_fn(tensor) - assert_equal(transformed_tensor, transformed_tensor_script, msg="{}".format(transforms)) - - if torch.device(self.device).type == "cpu": - # Can't check this twice, otherwise - # "Can't redefine method: forward on class: __torch__.torchvision.transforms.transforms.RandomApply" - transforms = T.RandomApply([ - T.ColorJitter(), - ], p=0.3) - with self.assertRaisesRegex(RuntimeError, r"Module 'RandomApply' has no attribute 'transforms'"): - torch.jit.script(transforms) - - def test_gaussian_blur(self): - tol = 1.0 + 1e-10 - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": 3, "sigma": 0.75}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": 23, "sigma": [0.1, 2.0]}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": 23, "sigma": (0.1, 2.0)}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": [3, 3], "sigma": (1.0, 1.0)}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": (3, 3), "sigma": (0.1, 2.0)}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": [23], "sigma": 0.75}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - def test_random_erasing(self): img = torch.rand(3, 60, 60) @@ -762,5 +635,125 @@ def test_to_grayscale(device, Klass, meth_kwargs): ) +@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)) + tensor, _ = _create_data(26, 34, device=device) + + with pytest.raises(TypeError, match="Input tensor should be a float tensor"): + fn(tensor) + + batch_tensors = torch.rand(4, 3, 44, 56, device=device) + tensor = tensor.to(dtype=torch.float32) / 255.0 + # test for class interface + 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) + + with get_tmp_dir() as tmp_dir: + scripted_fn.save(os.path.join(tmp_dir, "t_norm.pt")) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +def test_linear_transformation(device): + c, h, w = 3, 24, 32 + + tensor, _ = _create_data(h, w, channels=c, device=device) + + matrix = torch.rand(c * h * w, c * h * w, device=device) + mean_vector = torch.rand(c * h * w, device=device) + + fn = T.LinearTransformation(matrix, mean_vector) + scripted_fn = torch.jit.script(fn) + + _test_transform_vs_scripted(fn, scripted_fn, tensor) + + batch_tensors = torch.rand(4, c, h, w, device=device) + # We skip some tests from _test_transform_vs_scripted_on_batch as + # results for scripted and non-scripted transformations are not exactly the same + torch.manual_seed(12) + transformed_batch = fn(batch_tensors) + torch.manual_seed(12) + s_transformed_batch = scripted_fn(batch_tensors) + assert_equal(transformed_batch, s_transformed_batch) + + with get_tmp_dir() as tmp_dir: + scripted_fn.save(os.path.join(tmp_dir, "t_norm.pt")) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +def test_compose(device): + tensor, _ = _create_data(26, 34, device=device) + tensor = tensor.to(dtype=torch.float32) / 255.0 + + transforms = T.Compose([ + T.CenterCrop(10), + T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), + ]) + s_transforms = torch.nn.Sequential(*transforms.transforms) + + scripted_fn = torch.jit.script(s_transforms) + torch.manual_seed(12) + transformed_tensor = transforms(tensor) + torch.manual_seed(12) + transformed_tensor_script = scripted_fn(tensor) + assert_equal(transformed_tensor, transformed_tensor_script, msg="{}".format(transforms)) + + t = T.Compose([ + lambda x: x, + ]) + with pytest.raises(RuntimeError, match="Could not get name of python class object"): + torch.jit.script(t) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +def test_random_apply(device): + tensor, _ = _create_data(26, 34, device=device) + tensor = tensor.to(dtype=torch.float32) / 255.0 + + transforms = T.RandomApply([ + T.RandomHorizontalFlip(), + T.ColorJitter(), + ], p=0.4) + s_transforms = T.RandomApply(torch.nn.ModuleList([ + T.RandomHorizontalFlip(), + T.ColorJitter(), + ]), p=0.4) + + scripted_fn = torch.jit.script(s_transforms) + torch.manual_seed(12) + transformed_tensor = transforms(tensor) + torch.manual_seed(12) + transformed_tensor_script = scripted_fn(tensor) + assert_equal(transformed_tensor, transformed_tensor_script, msg="{}".format(transforms)) + + if device == "cpu": + # Can't check this twice, otherwise + # "Can't redefine method: forward on class: __torch__.torchvision.transforms.transforms.RandomApply" + transforms = T.RandomApply([ + T.ColorJitter(), + ], p=0.3) + with pytest.raises(RuntimeError, match="Module 'RandomApply' has no attribute 'transforms'"): + torch.jit.script(transforms) + + +@pytest.mark.parametrize('device', cpu_and_gpu()) +@pytest.mark.parametrize('meth_kwargs', [ + {"kernel_size": 3, "sigma": 0.75}, + {"kernel_size": 23, "sigma": [0.1, 2.0]}, + {"kernel_size": 23, "sigma": (0.1, 2.0)}, + {"kernel_size": [3, 3], "sigma": (1.0, 1.0)}, + {"kernel_size": (3, 3), "sigma": (0.1, 2.0)}, + {"kernel_size": [23], "sigma": 0.75} +]) +def test_gaussian_blur(device, meth_kwargs): + tol = 1.0 + 1e-10 + _test_class_op( + T.GaussianBlur, meth_kwargs=meth_kwargs, + test_exact_match=False, device=device, agg_method="max", tol=tol + ) + + if __name__ == '__main__': unittest.main() From 124e95ad9bd2932d7211fb538383d9809d911dae Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Wed, 9 Jun 2021 19:00:37 +0530 Subject: [PATCH 2/3] Deleted tests from class --- test/test_transforms_tensor.py | 127 --------------------------------- 1 file changed, 127 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 7590a0c76c9..97be71d0284 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -287,133 +287,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_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) - - with self.assertRaisesRegex(TypeError, r"Input tensor should be a float tensor"): - fn(tensor) - - batch_tensors = torch.rand(4, 3, 44, 56, device=self.device) - tensor = tensor.to(dtype=torch.float32) / 255.0 - # test for class interface - 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) - - with get_tmp_dir() as tmp_dir: - scripted_fn.save(os.path.join(tmp_dir, "t_norm.pt")) - - def test_linear_transformation(self): - c, h, w = 3, 24, 32 - - tensor, _ = _create_data(h, w, channels=c, device=self.device) - - matrix = torch.rand(c * h * w, c * h * w, device=self.device) - mean_vector = torch.rand(c * h * w, device=self.device) - - fn = T.LinearTransformation(matrix, mean_vector) - scripted_fn = torch.jit.script(fn) - - _test_transform_vs_scripted(fn, scripted_fn, tensor) - - batch_tensors = torch.rand(4, c, h, w, device=self.device) - # We skip some tests from _test_transform_vs_scripted_on_batch as - # results for scripted and non-scripted transformations are not exactly the same - torch.manual_seed(12) - transformed_batch = fn(batch_tensors) - torch.manual_seed(12) - s_transformed_batch = scripted_fn(batch_tensors) - assert_equal(transformed_batch, s_transformed_batch) - - with get_tmp_dir() as tmp_dir: - scripted_fn.save(os.path.join(tmp_dir, "t_norm.pt")) - - def test_compose(self): - tensor, _ = _create_data(26, 34, device=self.device) - tensor = tensor.to(dtype=torch.float32) / 255.0 - - transforms = T.Compose([ - T.CenterCrop(10), - T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), - ]) - s_transforms = torch.nn.Sequential(*transforms.transforms) - - scripted_fn = torch.jit.script(s_transforms) - torch.manual_seed(12) - transformed_tensor = transforms(tensor) - torch.manual_seed(12) - transformed_tensor_script = scripted_fn(tensor) - assert_equal(transformed_tensor, transformed_tensor_script, msg="{}".format(transforms)) - - t = T.Compose([ - lambda x: x, - ]) - with self.assertRaisesRegex(RuntimeError, r"Could not get name of python class object"): - torch.jit.script(t) - - def test_random_apply(self): - tensor, _ = _create_data(26, 34, device=self.device) - tensor = tensor.to(dtype=torch.float32) / 255.0 - - transforms = T.RandomApply([ - T.RandomHorizontalFlip(), - T.ColorJitter(), - ], p=0.4) - s_transforms = T.RandomApply(torch.nn.ModuleList([ - T.RandomHorizontalFlip(), - T.ColorJitter(), - ]), p=0.4) - - scripted_fn = torch.jit.script(s_transforms) - torch.manual_seed(12) - transformed_tensor = transforms(tensor) - torch.manual_seed(12) - transformed_tensor_script = scripted_fn(tensor) - assert_equal(transformed_tensor, transformed_tensor_script, msg="{}".format(transforms)) - - if torch.device(self.device).type == "cpu": - # Can't check this twice, otherwise - # "Can't redefine method: forward on class: __torch__.torchvision.transforms.transforms.RandomApply" - transforms = T.RandomApply([ - T.ColorJitter(), - ], p=0.3) - with self.assertRaisesRegex(RuntimeError, r"Module 'RandomApply' has no attribute 'transforms'"): - torch.jit.script(transforms) - - def test_gaussian_blur(self): - tol = 1.0 + 1e-10 - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": 3, "sigma": 0.75}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": 23, "sigma": [0.1, 2.0]}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": 23, "sigma": (0.1, 2.0)}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": [3, 3], "sigma": (1.0, 1.0)}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": (3, 3), "sigma": (0.1, 2.0)}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - - _test_class_op( - T.GaussianBlur, meth_kwargs={"kernel_size": [23], "sigma": 0.75}, - test_exact_match=False, device=self.device, agg_method="max", tol=tol - ) - def test_random_erasing(self): img = torch.rand(3, 60, 60) From 90337cf53b09c0c371e34ebb159f60fbe2f10f26 Mon Sep 17 00:00:00 2001 From: vivekkumar7089 Date: Wed, 9 Jun 2021 19:46:46 +0530 Subject: [PATCH 3/3] removed tests ported from another PR --- test/test_transforms_tensor.py | 144 --------------------------------- 1 file changed, 144 deletions(-) diff --git a/test/test_transforms_tensor.py b/test/test_transforms_tensor.py index 97be71d0284..b365c3f3421 100644 --- a/test/test_transforms_tensor.py +++ b/test/test_transforms_tensor.py @@ -143,150 +143,6 @@ def test_random_autocontrast(self): def test_random_equalize(self): _test_op(F.equalize, T.RandomEqualize, device=self.device) - def _test_op_list_output(self, func, method, out_length, fn_kwargs=None, meth_kwargs=None): - if fn_kwargs is None: - fn_kwargs = {} - if meth_kwargs is None: - meth_kwargs = {} - - fn = getattr(F, func) - scripted_fn = torch.jit.script(fn) - - tensor, pil_img = _create_data(height=20, width=20, device=self.device) - transformed_t_list = fn(tensor, **fn_kwargs) - transformed_p_list = fn(pil_img, **fn_kwargs) - self.assertEqual(len(transformed_t_list), len(transformed_p_list)) - self.assertEqual(len(transformed_t_list), out_length) - for transformed_tensor, transformed_pil_img in zip(transformed_t_list, transformed_p_list): - _assert_equal_tensor_to_pil(transformed_tensor, transformed_pil_img) - - transformed_t_list_script = scripted_fn(tensor.detach().clone(), **fn_kwargs) - self.assertEqual(len(transformed_t_list), len(transformed_t_list_script)) - self.assertEqual(len(transformed_t_list_script), out_length) - for transformed_tensor, transformed_tensor_script in zip(transformed_t_list, transformed_t_list_script): - assert_equal( - transformed_tensor, - transformed_tensor_script, - msg="{} vs {}".format(transformed_tensor, transformed_tensor_script), - ) - - # test for class interface - fn = getattr(T, method)(**meth_kwargs) - scripted_fn = torch.jit.script(fn) - output = scripted_fn(tensor) - self.assertEqual(len(output), len(transformed_t_list_script)) - - # test on batch of tensors - batch_tensors = _create_data_batch(height=23, width=34, channels=3, num_samples=4, device=self.device) - torch.manual_seed(12) - transformed_batch_list = fn(batch_tensors) - - for i in range(len(batch_tensors)): - img_tensor = batch_tensors[i, ...] - torch.manual_seed(12) - transformed_img_list = fn(img_tensor) - for transformed_img, transformed_batch in zip(transformed_img_list, transformed_batch_list): - assert_equal( - transformed_img, - transformed_batch[i, ...], - msg="{} vs {}".format(transformed_img, transformed_batch[i, ...]), - ) - - with get_tmp_dir() as tmp_dir: - scripted_fn.save(os.path.join(tmp_dir, "t_op_list_{}.pt".format(method))) - - def test_five_crop(self): - fn_kwargs = meth_kwargs = {"size": (5,)} - self._test_op_list_output( - "five_crop", "FiveCrop", out_length=5, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - fn_kwargs = meth_kwargs = {"size": [5, ]} - self._test_op_list_output( - "five_crop", "FiveCrop", out_length=5, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - fn_kwargs = meth_kwargs = {"size": (4, 5)} - self._test_op_list_output( - "five_crop", "FiveCrop", out_length=5, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - fn_kwargs = meth_kwargs = {"size": [4, 5]} - self._test_op_list_output( - "five_crop", "FiveCrop", out_length=5, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - - def test_ten_crop(self): - fn_kwargs = meth_kwargs = {"size": (5,)} - self._test_op_list_output( - "ten_crop", "TenCrop", out_length=10, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - fn_kwargs = meth_kwargs = {"size": [5, ]} - self._test_op_list_output( - "ten_crop", "TenCrop", out_length=10, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - fn_kwargs = meth_kwargs = {"size": (4, 5)} - self._test_op_list_output( - "ten_crop", "TenCrop", out_length=10, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - fn_kwargs = meth_kwargs = {"size": [4, 5]} - self._test_op_list_output( - "ten_crop", "TenCrop", out_length=10, fn_kwargs=fn_kwargs, meth_kwargs=meth_kwargs - ) - - def test_resize(self): - - # TODO: Minimal check for bug-fix, improve this later - x = torch.rand(3, 32, 46) - t = T.Resize(size=38) - y = t(x) - # If size is an int, smaller edge of the image will be matched to this number. - # i.e, if height > width, then image will be rescaled to (size * height / width, size). - self.assertTrue(isinstance(y, torch.Tensor)) - self.assertEqual(y.shape[1], 38) - self.assertEqual(y.shape[2], int(38 * 46 / 32)) - - tensor, _ = _create_data(height=34, width=36, device=self.device) - batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=self.device) - - for dt in [None, torch.float32, torch.float64]: - if dt is not None: - # This is a trivial cast to float of uint8 data to test all cases - tensor = tensor.to(dt) - for size in [32, 34, [32, ], [32, 32], (32, 32), [34, 35]]: - for max_size in (None, 35, 1000): - if max_size is not None and isinstance(size, Sequence) and len(size) != 1: - continue # Not supported - for interpolation in [BILINEAR, BICUBIC, NEAREST]: - - if isinstance(size, int): - script_size = [size, ] - else: - script_size = size - - transform = T.Resize(size=script_size, interpolation=interpolation, max_size=max_size) - 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_resize.pt")) - - def test_resized_crop(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 scale in [(0.7, 1.2), [0.7, 1.2]]: - for ratio in [(0.75, 1.333), [0.75, 1.333]]: - for size in [(32, ), [44, ], [32, ], [32, 32], (32, 32), [44, 55]]: - for interpolation in [NEAREST, BILINEAR, BICUBIC]: - transform = T.RandomResizedCrop( - size=size, scale=scale, ratio=ratio, interpolation=interpolation - ) - 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_resized_crop.pt")) - def test_random_erasing(self): img = torch.rand(3, 60, 60)