From ca374b30e06a58f845cdff28087ad3ab83ffaf28 Mon Sep 17 00:00:00 2001 From: Pranjal Gulati Date: Thu, 3 Jun 2021 19:24:14 +0530 Subject: [PATCH 1/4] Port test to tensor in test_transforms to pytest --- test/test_transforms.py | 161 ++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 81 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 33d81c657da..7f6ab0c4ed4 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -505,54 +505,6 @@ def test_random_order(self): # Checking if RandomOrder can be printed as string random_order_transform.__repr__() - def test_to_tensor(self): - test_channels = [1, 3, 4] - height, width = 4, 4 - trans = transforms.ToTensor() - - with self.assertRaises(TypeError): - trans(np.random.rand(1, height, width).tolist()) - - with self.assertRaises(ValueError): - trans(np.random.rand(height)) - trans(np.random.rand(1, 1, height, width)) - - for channels in test_channels: - input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) - img = transforms.ToPILImage()(input_data) - output = trans(img) - torch.testing.assert_close(output, input_data, check_stride=False) - - ndarray = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) - output = trans(ndarray) - expected_output = ndarray.transpose((2, 0, 1)) / 255.0 - torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) - - ndarray = np.random.rand(height, width, channels).astype(np.float32) - output = trans(ndarray) - expected_output = ndarray.transpose((2, 0, 1)) - torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) - - # separate test for mode '1' PIL images - input_data = torch.ByteTensor(1, height, width).bernoulli_() - img = transforms.ToPILImage()(input_data.mul(255)).convert('1') - output = trans(img) - torch.testing.assert_close(input_data, output, check_dtype=False, check_stride=False) - - def test_to_tensor_with_other_default_dtypes(self): - current_def_dtype = torch.get_default_dtype() - - t = transforms.ToTensor() - np_arr = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8) - img = Image.fromarray(np_arr) - - for dtype in [torch.float16, torch.float, torch.double]: - torch.set_default_dtype(dtype) - res = t(img) - self.assertTrue(res.dtype == dtype, msg=f"{res.dtype} vs {dtype}") - - torch.set_default_dtype(current_def_dtype) - def test_max_value(self): for dtype in int_dtypes(): self.assertEqual(F_t._max_value(dtype), torch.iinfo(dtype).max) @@ -690,39 +642,6 @@ def test_accimage_to_tensor(self): torch.testing.assert_close(output, expected_output) - def test_pil_to_tensor(self): - test_channels = [1, 3, 4] - height, width = 4, 4 - trans = transforms.PILToTensor() - - with self.assertRaises(TypeError): - trans(np.random.rand(1, height, width).tolist()) - trans(np.random.rand(1, height, width)) - - for channels in test_channels: - input_data = torch.ByteTensor(channels, height, width).random_(0, 255) - img = transforms.ToPILImage()(input_data) - output = trans(img) - torch.testing.assert_close(input_data, output, check_stride=False) - - input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) - img = transforms.ToPILImage()(input_data) - output = trans(img) - expected_output = input_data.transpose((2, 0, 1)) - torch.testing.assert_close(output.numpy(), expected_output) - - input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) - img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() - output = trans(img) # HWC -> CHW - expected_output = (input_data * 255).byte() - torch.testing.assert_close(output, expected_output, check_stride=False) - - # separate test for mode '1' PIL images - input_data = torch.ByteTensor(1, height, width).bernoulli_() - img = transforms.ToPILImage()(input_data.mul(255)).convert('1') - output = trans(img).view(torch.uint8).bool().to(torch.uint8) - torch.testing.assert_close(input_data, output, check_stride=False) - @unittest.skipIf(accimage is None, 'accimage not available') def test_accimage_pil_to_tensor(self): trans = transforms.PILToTensor() @@ -1656,6 +1575,86 @@ def test_random_erasing(self): # Checking if RandomErasing can be printed as string t.__repr__() +def test_pil_to_tensor(): + test_channels = [1, 3, 4] + height, width = 4, 4 + trans = transforms.PILToTensor() + + with pytest.raises(TypeError): + trans(np.random.rand(1, height, width).tolist()) + trans(np.random.rand(1, height, width)) + + for channels in test_channels: + input_data = torch.ByteTensor(channels, height, width).random_(0, 255) + img = transforms.ToPILImage()(input_data) + output = trans(img) + torch.testing.assert_close(input_data, output, check_stride=False) + + input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + img = transforms.ToPILImage()(input_data) + output = trans(img) + expected_output = input_data.transpose((2, 0, 1)) + torch.testing.assert_close(output.numpy(), expected_output) + + input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) + img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() + output = trans(img) # HWC -> CHW + expected_output = (input_data * 255).byte() + torch.testing.assert_close(output, expected_output, check_stride=False) + + # separate test for mode '1' PIL images + input_data = torch.ByteTensor(1, height, width).bernoulli_() + img = transforms.ToPILImage()(input_data.mul(255)).convert('1') + output = trans(img).view(torch.uint8).bool().to(torch.uint8) + torch.testing.assert_close(input_data, output, check_stride=False) + +def test_to_tensor(): + test_channels = [1, 3, 4] + height, width = 4, 4 + trans = transforms.ToTensor() + + with pytest.raises(TypeError): + trans(np.random.rand(1, height, width).tolist()) + + with pytest.raises(ValueError): + trans(np.random.rand(height)) + trans(np.random.rand(1, 1, height, width)) + + for channels in test_channels: + input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) + img = transforms.ToPILImage()(input_data) + output = trans(img) + torch.testing.assert_close(output, input_data, check_stride=False) + + ndarray = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + output = trans(ndarray) + expected_output = ndarray.transpose((2, 0, 1)) / 255.0 + torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) + + ndarray = np.random.rand(height, width, channels).astype(np.float32) + output = trans(ndarray) + expected_output = ndarray.transpose((2, 0, 1)) + torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) + + # separate test for mode '1' PIL images + input_data = torch.ByteTensor(1, height, width).bernoulli_() + img = transforms.ToPILImage()(input_data.mul(255)).convert('1') + output = trans(img) + torch.testing.assert_close(input_data, output, check_dtype=False, check_stride=False) + +def test_to_tensor_with_other_default_dtypes(): + current_def_dtype = torch.get_default_dtype() + + t = transforms.ToTensor() + np_arr = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8) + img = Image.fromarray(np_arr) + + for dtype in [torch.float16, torch.float, torch.double]: + torch.set_default_dtype(dtype) + res = t(img) + assert res.dtype == dtype, f"{res.dtype} vs {dtype}" + + torch.set_default_dtype(current_def_dtype) class TestPad: From f2d52b4ddeb0b20b591314b2fdee76675cbbfec4 Mon Sep 17 00:00:00 2001 From: Pranjal Gulati Date: Fri, 4 Jun 2021 08:29:24 +0530 Subject: [PATCH 2/4] Port test to tensor in test_transforms to pytest with parametrize --- test/test_transforms.py | 80 ++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 7f6ab0c4ed4..a530d7435c2 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1575,8 +1575,8 @@ def test_random_erasing(self): # Checking if RandomErasing can be printed as string t.__repr__() -def test_pil_to_tensor(): - test_channels = [1, 3, 4] +@pytest.mark.parametrize('channels', [1, 3, 4]) +def test_pil_to_tensor(channels): height, width = 4, 4 trans = transforms.PILToTensor() @@ -1584,23 +1584,22 @@ def test_pil_to_tensor(): trans(np.random.rand(1, height, width).tolist()) trans(np.random.rand(1, height, width)) - for channels in test_channels: - input_data = torch.ByteTensor(channels, height, width).random_(0, 255) - img = transforms.ToPILImage()(input_data) - output = trans(img) - torch.testing.assert_close(input_data, output, check_stride=False) - - input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) - img = transforms.ToPILImage()(input_data) - output = trans(img) - expected_output = input_data.transpose((2, 0, 1)) - torch.testing.assert_close(output.numpy(), expected_output) - - input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) - img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() - output = trans(img) # HWC -> CHW - expected_output = (input_data * 255).byte() - torch.testing.assert_close(output, expected_output, check_stride=False) + input_data = torch.ByteTensor(channels, height, width).random_(0, 255) + img = transforms.ToPILImage()(input_data) + output = trans(img) + torch.testing.assert_close(input_data, output, check_stride=False) + + input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + img = transforms.ToPILImage()(input_data) + output = trans(img) + expected_output = input_data.transpose((2, 0, 1)) + torch.testing.assert_close(output.numpy(), expected_output) + + input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) + img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() + output = trans(img) # HWC -> CHW + expected_output = (input_data * 255).byte() + torch.testing.assert_close(output, expected_output, check_stride=False) # separate test for mode '1' PIL images input_data = torch.ByteTensor(1, height, width).bernoulli_() @@ -1608,8 +1607,8 @@ def test_pil_to_tensor(): output = trans(img).view(torch.uint8).bool().to(torch.uint8) torch.testing.assert_close(input_data, output, check_stride=False) -def test_to_tensor(): - test_channels = [1, 3, 4] +@pytest.mark.parametrize('channels', [1, 3, 4]) +def test_to_tensor(channels): height, width = 4, 4 trans = transforms.ToTensor() @@ -1620,21 +1619,20 @@ def test_to_tensor(): trans(np.random.rand(height)) trans(np.random.rand(1, 1, height, width)) - for channels in test_channels: - input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) - img = transforms.ToPILImage()(input_data) - output = trans(img) - torch.testing.assert_close(output, input_data, check_stride=False) - - ndarray = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) - output = trans(ndarray) - expected_output = ndarray.transpose((2, 0, 1)) / 255.0 - torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) - - ndarray = np.random.rand(height, width, channels).astype(np.float32) - output = trans(ndarray) - expected_output = ndarray.transpose((2, 0, 1)) - torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) + input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) + img = transforms.ToPILImage()(input_data) + output = trans(img) + torch.testing.assert_close(output, input_data, check_stride=False) + + ndarray = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + output = trans(ndarray) + expected_output = ndarray.transpose((2, 0, 1)) / 255.0 + torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) + + ndarray = np.random.rand(height, width, channels).astype(np.float32) + output = trans(ndarray) + expected_output = ndarray.transpose((2, 0, 1)) + torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) # separate test for mode '1' PIL images input_data = torch.ByteTensor(1, height, width).bernoulli_() @@ -1642,17 +1640,17 @@ def test_to_tensor(): output = trans(img) torch.testing.assert_close(input_data, output, check_dtype=False, check_stride=False) -def test_to_tensor_with_other_default_dtypes(): +@pytest.mark.parametrize('dtype', [torch.float16, torch.float, torch.double]) +def test_to_tensor_with_other_default_dtypes(dtype): current_def_dtype = torch.get_default_dtype() t = transforms.ToTensor() np_arr = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8) img = Image.fromarray(np_arr) - for dtype in [torch.float16, torch.float, torch.double]: - torch.set_default_dtype(dtype) - res = t(img) - assert res.dtype == dtype, f"{res.dtype} vs {dtype}" + torch.set_default_dtype(dtype) + res = t(img) + assert res.dtype == dtype, f"{res.dtype} vs {dtype}" torch.set_default_dtype(current_def_dtype) From 654aa9b055482e2f341aba2b168866ddaace4388 Mon Sep 17 00:00:00 2001 From: Pranjal Gulati Date: Fri, 4 Jun 2021 16:46:01 +0530 Subject: [PATCH 3/4] Add code formatting --- test/test_transforms.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index a530d7435c2..6a4bb43465b 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1575,6 +1575,7 @@ def test_random_erasing(self): # Checking if RandomErasing can be printed as string t.__repr__() + @pytest.mark.parametrize('channels', [1, 3, 4]) def test_pil_to_tensor(channels): height, width = 4, 4 @@ -1606,7 +1607,8 @@ def test_pil_to_tensor(channels): img = transforms.ToPILImage()(input_data.mul(255)).convert('1') output = trans(img).view(torch.uint8).bool().to(torch.uint8) torch.testing.assert_close(input_data, output, check_stride=False) - + + @pytest.mark.parametrize('channels', [1, 3, 4]) def test_to_tensor(channels): height, width = 4, 4 @@ -1614,11 +1616,11 @@ def test_to_tensor(channels): with pytest.raises(TypeError): trans(np.random.rand(1, height, width).tolist()) - + with pytest.raises(ValueError): trans(np.random.rand(height)) trans(np.random.rand(1, 1, height, width)) - + input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) img = transforms.ToPILImage()(input_data) output = trans(img) @@ -1639,7 +1641,8 @@ def test_to_tensor(channels): img = transforms.ToPILImage()(input_data.mul(255)).convert('1') output = trans(img) torch.testing.assert_close(input_data, output, check_dtype=False, check_stride=False) - + + @pytest.mark.parametrize('dtype', [torch.float16, torch.float, torch.double]) def test_to_tensor_with_other_default_dtypes(dtype): current_def_dtype = torch.get_default_dtype() @@ -1654,6 +1657,7 @@ def test_to_tensor_with_other_default_dtypes(dtype): torch.set_default_dtype(current_def_dtype) + class TestPad: def test_pad(self): From ed424607f900546e173a92148736a02dc20764f4 Mon Sep 17 00:00:00 2001 From: Pranjal Gulati Date: Sat, 5 Jun 2021 09:37:17 +0530 Subject: [PATCH 4/4] Fix issue: Extract errors into seperate functions --- test/test_transforms.py | 94 +++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index 6a4bb43465b..8d70e4edb0d 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -1576,51 +1576,11 @@ def test_random_erasing(self): t.__repr__() -@pytest.mark.parametrize('channels', [1, 3, 4]) -def test_pil_to_tensor(channels): - height, width = 4, 4 - trans = transforms.PILToTensor() - - with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) - trans(np.random.rand(1, height, width)) - - input_data = torch.ByteTensor(channels, height, width).random_(0, 255) - img = transforms.ToPILImage()(input_data) - output = trans(img) - torch.testing.assert_close(input_data, output, check_stride=False) - - input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) - img = transforms.ToPILImage()(input_data) - output = trans(img) - expected_output = input_data.transpose((2, 0, 1)) - torch.testing.assert_close(output.numpy(), expected_output) - - input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) - img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() - output = trans(img) # HWC -> CHW - expected_output = (input_data * 255).byte() - torch.testing.assert_close(output, expected_output, check_stride=False) - - # separate test for mode '1' PIL images - input_data = torch.ByteTensor(1, height, width).bernoulli_() - img = transforms.ToPILImage()(input_data.mul(255)).convert('1') - output = trans(img).view(torch.uint8).bool().to(torch.uint8) - torch.testing.assert_close(input_data, output, check_stride=False) - - @pytest.mark.parametrize('channels', [1, 3, 4]) def test_to_tensor(channels): height, width = 4, 4 trans = transforms.ToTensor() - with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) - - with pytest.raises(ValueError): - trans(np.random.rand(height)) - trans(np.random.rand(1, 1, height, width)) - input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) img = transforms.ToPILImage()(input_data) output = trans(img) @@ -1643,6 +1603,20 @@ def test_to_tensor(channels): torch.testing.assert_close(input_data, output, check_dtype=False, check_stride=False) +def test_to_tensor_errors(): + height, width = 4, 4 + trans = transforms.ToTensor() + + with pytest.raises(TypeError): + trans(np.random.rand(1, height, width).tolist()) + + with pytest.raises(ValueError): + trans(np.random.rand(height)) + + with pytest.raises(ValueError): + trans(np.random.rand(1, 1, height, width)) + + @pytest.mark.parametrize('dtype', [torch.float16, torch.float, torch.double]) def test_to_tensor_with_other_default_dtypes(dtype): current_def_dtype = torch.get_default_dtype() @@ -1658,6 +1632,46 @@ def test_to_tensor_with_other_default_dtypes(dtype): torch.set_default_dtype(current_def_dtype) +@pytest.mark.parametrize('channels', [1, 3, 4]) +def test_pil_to_tensor(channels): + height, width = 4, 4 + trans = transforms.PILToTensor() + + input_data = torch.ByteTensor(channels, height, width).random_(0, 255) + img = transforms.ToPILImage()(input_data) + output = trans(img) + torch.testing.assert_close(input_data, output, check_stride=False) + + input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + img = transforms.ToPILImage()(input_data) + output = trans(img) + expected_output = input_data.transpose((2, 0, 1)) + torch.testing.assert_close(output.numpy(), expected_output) + + input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) + img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() + output = trans(img) # HWC -> CHW + expected_output = (input_data * 255).byte() + torch.testing.assert_close(output, expected_output, check_stride=False) + + # separate test for mode '1' PIL images + input_data = torch.ByteTensor(1, height, width).bernoulli_() + img = transforms.ToPILImage()(input_data.mul(255)).convert('1') + output = trans(img).view(torch.uint8).bool().to(torch.uint8) + torch.testing.assert_close(input_data, output, check_stride=False) + + +def test_pil_to_tensor_errors(): + height, width = 4, 4 + trans = transforms.PILToTensor() + + with pytest.raises(TypeError): + trans(np.random.rand(1, height, width).tolist()) + + with pytest.raises(TypeError): + trans(np.random.rand(1, height, width)) + + class TestPad: def test_pad(self):