From 85aecc90d51c984a9b999ae27e71cd1eeccfb951 Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 4 Nov 2021 15:51:44 +0900 Subject: [PATCH 01/12] Update functional_tensor.py To resolve issue #4818 Add assert function and logic after checking bound of image --- torchvision/transforms/functional_tensor.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index 09ae726931c..5b9cd7cb062 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -15,6 +15,10 @@ def _assert_image_tensor(img: Tensor) -> None: if not _is_tensor_a_torch_image(img): raise TypeError("Tensor is not a torch image.") +def _assert_threshold(img: Tensor, threshold:int) -> None: + bound = 1 if img.is_floating_point() else 255 + if threshold >= bound: + raise TypeError("Threshold should be less than bound of img.") def get_image_size(img: Tensor) -> List[int]: # Returns (w, h) of tensor image @@ -882,6 +886,8 @@ def solarize(img: Tensor, threshold: float) -> Tensor: _assert_channels(img, [1, 3]) + _assert_threshold(img, threshold) + inverted_img = invert(img) return torch.where(img >= threshold, inverted_img, img) From a8595eec184b9e2d191d2031f1dea2ae123f04c2 Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 4 Nov 2021 21:38:56 +0900 Subject: [PATCH 02/12] Update functional_tensor.py --- torchvision/transforms/functional_tensor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index 5b9cd7cb062..dfb32a41adf 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -15,11 +15,13 @@ def _assert_image_tensor(img: Tensor) -> None: if not _is_tensor_a_torch_image(img): raise TypeError("Tensor is not a torch image.") -def _assert_threshold(img: Tensor, threshold:int) -> None: + +def _assert_threshold(img: Tensor, threshold: float) -> None: bound = 1 if img.is_floating_point() else 255 - if threshold >= bound: + if threshold > bound: raise TypeError("Threshold should be less than bound of img.") + def get_image_size(img: Tensor) -> List[int]: # Returns (w, h) of tensor image _assert_image_tensor(img) From d6991bd3dea9203e6b450919350b93a6fcf41a18 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 13 Nov 2021 22:21:04 +0900 Subject: [PATCH 03/12] Update test --- test/test_functional_tensor.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 24a7523b62a..2e2923f854c 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -795,6 +795,23 @@ def test_solarize2(device, dtype, config, channels): ) +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize("dtype", (torch.float32, torch.float64)) +@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0, 1.5]]) +def test_solarize_threshold1(config): + img = torch.rand((3,12,23)).to("cpu") + with pytest.raises(ValueError, match="If value is a sequence, it should have either a single value or 3"): + F_t.solarize(img, **config) + + +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0, 64, 128, 192, 255, 260]]) +def test_solarize_threshold2(config): + img = torch.randint(0, 256, (3,12,23)).to("cpu") + with pytest.raises(TypeError, match="If value is a sequence, it should have either a single value or 3"): + F_t.solarize(img, **config) + + @pytest.mark.parametrize("device", cpu_and_gpu()) @pytest.mark.parametrize("dtype", (None, torch.float32, torch.float64)) @pytest.mark.parametrize("config", [{"sharpness_factor": f} for f in [0.2, 0.5, 1.0, 1.5, 2.0]]) From 773c720fdbdb20e82898cb1e5269f9b1b42d6e5a Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 13 Nov 2021 22:44:05 +0900 Subject: [PATCH 04/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 2e2923f854c..1926054953b 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -799,7 +799,7 @@ def test_solarize2(device, dtype, config, channels): @pytest.mark.parametrize("dtype", (torch.float32, torch.float64)) @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0, 1.5]]) def test_solarize_threshold1(config): - img = torch.rand((3,12,23)).to("cpu") + img = torch.rand((3, 12, 23)).to("cpu") with pytest.raises(ValueError, match="If value is a sequence, it should have either a single value or 3"): F_t.solarize(img, **config) @@ -807,7 +807,7 @@ def test_solarize_threshold1(config): @pytest.mark.parametrize("device", cpu_and_gpu()) @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0, 64, 128, 192, 255, 260]]) def test_solarize_threshold2(config): - img = torch.randint(0, 256, (3,12,23)).to("cpu") + img = torch.randint(0, 256, (3, 12, 23)).to("cpu") with pytest.raises(TypeError, match="If value is a sequence, it should have either a single value or 3"): F_t.solarize(img, **config) From 64f15b8880161c1a9e60d476a7069789549c0850 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 27 Nov 2021 09:51:01 +0900 Subject: [PATCH 05/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 1926054953b..33b6859d2d4 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -796,7 +796,6 @@ def test_solarize2(device, dtype, config, channels): @pytest.mark.parametrize("device", cpu_and_gpu()) -@pytest.mark.parametrize("dtype", (torch.float32, torch.float64)) @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0, 1.5]]) def test_solarize_threshold1(config): img = torch.rand((3, 12, 23)).to("cpu") From 38e72797bf9b94f465478f42d095114d96ab72cb Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 27 Nov 2021 09:58:06 +0900 Subject: [PATCH 06/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 33b6859d2d4..0c75be8bf7a 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -795,7 +795,6 @@ def test_solarize2(device, dtype, config, channels): ) -@pytest.mark.parametrize("device", cpu_and_gpu()) @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0, 1.5]]) def test_solarize_threshold1(config): img = torch.rand((3, 12, 23)).to("cpu") From ec3bcfb5d52dfe962d6bd892c23329f087bbf85c Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 27 Nov 2021 10:02:16 +0900 Subject: [PATCH 07/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 0c75be8bf7a..5e01e74d26c 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -802,7 +802,6 @@ def test_solarize_threshold1(config): F_t.solarize(img, **config) -@pytest.mark.parametrize("device", cpu_and_gpu()) @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0, 64, 128, 192, 255, 260]]) def test_solarize_threshold2(config): img = torch.randint(0, 256, (3, 12, 23)).to("cpu") From 0e3cad903e260b9681e71aa3a44ef778692df3b9 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 27 Nov 2021 11:22:11 +0900 Subject: [PATCH 08/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 5e01e74d26c..32e41cb637c 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -795,15 +795,27 @@ def test_solarize2(device, dtype, config, channels): ) -@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0, 1.5]]) -def test_solarize_threshold1(config): +@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0,]]) +def test_solarize_threshold1_bound(config): img = torch.rand((3, 12, 23)).to("cpu") - with pytest.raises(ValueError, match="If value is a sequence, it should have either a single value or 3"): + F_t.solarize(img, **config) + + +@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [1.5]]) +def test_solarize_threshold1_upper_bound(config): + img = torch.rand((3, 12, 23)).to("cpu") + with pytest.raises(TypeError, match="If value is a sequence, it should have either a single value or 3"): F_t.solarize(img, **config) -@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0, 64, 128, 192, 255, 260]]) -def test_solarize_threshold2(config): +@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0, 64, 128, 192, 255]]) +def test_solarize_threshold2_bound(config): + img = torch.randint(0, 256, (3, 12, 23)).to("cpu") + F_t.solarize(img, **config) + + +@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [260]]) +def test_solarize_threshold2_upper_bound(config): img = torch.randint(0, 256, (3, 12, 23)).to("cpu") with pytest.raises(TypeError, match="If value is a sequence, it should have either a single value or 3"): F_t.solarize(img, **config) From 2a0ea882f8c47eba495bc78a82bf58e376aed28e Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 27 Nov 2021 11:51:17 +0900 Subject: [PATCH 09/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 32e41cb637c..4ef7066a356 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -804,7 +804,7 @@ def test_solarize_threshold1_bound(config): @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [1.5]]) def test_solarize_threshold1_upper_bound(config): img = torch.rand((3, 12, 23)).to("cpu") - with pytest.raises(TypeError, match="If value is a sequence, it should have either a single value or 3"): + with pytest.raises(TypeError, match="Threshold should be less than bound of img."): F_t.solarize(img, **config) @@ -817,7 +817,7 @@ def test_solarize_threshold2_bound(config): @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [260]]) def test_solarize_threshold2_upper_bound(config): img = torch.randint(0, 256, (3, 12, 23)).to("cpu") - with pytest.raises(TypeError, match="If value is a sequence, it should have either a single value or 3"): + with pytest.raises(TypeError, match="Threshold should be less than bound of img."): F_t.solarize(img, **config) From ba70c8595227028f739ee4ff38895271f20315c4 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sun, 28 Nov 2021 23:25:33 +0900 Subject: [PATCH 10/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 4ef7066a356..d55282256f1 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -794,11 +794,11 @@ def test_solarize2(device, dtype, config, channels): agg_method="max", ) - -@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0.0, 0.25, 0.5, 0.75, 1.0,]]) -def test_solarize_threshold1_bound(config): - img = torch.rand((3, 12, 23)).to("cpu") - F_t.solarize(img, **config) +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize("threshold", [0.0, 0.25, 0.5, 0.75, 1.0,]) +def test_solarize_threshold1_bound(threshold, device): + img = torch.rand((3, 12, 23)).to(device) + F_t.solarize(img, threshold) @pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [1.5]]) From b781b623b7ba8853852bfcfd3c62d23696e7e08c Mon Sep 17 00:00:00 2001 From: puhuk Date: Mon, 29 Nov 2021 00:27:17 +0900 Subject: [PATCH 11/12] Update test_functional_tensor.py --- test/test_functional_tensor.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index d55282256f1..d894e6fc4db 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -795,30 +795,33 @@ def test_solarize2(device, dtype, config, channels): ) @pytest.mark.parametrize("device", cpu_and_gpu()) -@pytest.mark.parametrize("threshold", [0.0, 0.25, 0.5, 0.75, 1.0,]) +@pytest.mark.parametrize("threshold", [0.0, 0.25, 0.5, 0.75, 1.0]) def test_solarize_threshold1_bound(threshold, device): img = torch.rand((3, 12, 23)).to(device) F_t.solarize(img, threshold) -@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [1.5]]) -def test_solarize_threshold1_upper_bound(config): - img = torch.rand((3, 12, 23)).to("cpu") +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize("threshold", [1.5]) +def test_solarize_threshold1_upper_bound(threshold, device): + img = torch.rand((3, 12, 23)).to(device) with pytest.raises(TypeError, match="Threshold should be less than bound of img."): - F_t.solarize(img, **config) + F_t.solarize(img, threshold) -@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [0, 64, 128, 192, 255]]) -def test_solarize_threshold2_bound(config): - img = torch.randint(0, 256, (3, 12, 23)).to("cpu") - F_t.solarize(img, **config) +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize("threshold", [0, 64, 128, 192, 255]) +def test_solarize_threshold2_bound(threshold, device): + img = torch.randint(0, 256, (3, 12, 23)).to(device) + F_t.solarize(img, threshold) -@pytest.mark.parametrize("config", [{"threshold": threshold} for threshold in [260]]) -def test_solarize_threshold2_upper_bound(config): - img = torch.randint(0, 256, (3, 12, 23)).to("cpu") +@pytest.mark.parametrize("device", cpu_and_gpu()) +@pytest.mark.parametrize("threshold", [260]) +def test_solarize_threshold2_upper_bound(threshold, device): + img = torch.randint(0, 256, (3, 12, 23)).to(device) with pytest.raises(TypeError, match="Threshold should be less than bound of img."): - F_t.solarize(img, **config) + F_t.solarize(img, threshold) @pytest.mark.parametrize("device", cpu_and_gpu()) From e876a1e899d2883e3d505a0aabf2f338dfd9028b Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Mon, 29 Nov 2021 09:14:02 +0000 Subject: [PATCH 12/12] Fix linter --- test/test_functional_tensor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index d894e6fc4db..40a51775a09 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -794,6 +794,7 @@ def test_solarize2(device, dtype, config, channels): agg_method="max", ) + @pytest.mark.parametrize("device", cpu_and_gpu()) @pytest.mark.parametrize("threshold", [0.0, 0.25, 0.5, 0.75, 1.0]) def test_solarize_threshold1_bound(threshold, device):