Skip to content

Commit 1cb4629

Browse files
committed
fix guassian border mode to adhere to torch/opencv
1 parent f0b51c4 commit 1cb4629

File tree

2 files changed

+22
-39
lines changed

2 files changed

+22
-39
lines changed

test/test_transforms_v2.py

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,11 +3991,22 @@ def test_make_params(self, sigma):
39913991
((1, 26, 28), (23, 23), 1.7),
39923992
],
39933993
)
3994-
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64, torch.float16])
3994+
@pytest.mark.parametrize("dtype", [torch.uint8, torch.float32, torch.float64, torch.float16])
39953995
@pytest.mark.parametrize("device", cpu_and_cuda())
3996-
def test_functional_image_correctness(self, dimensions, kernel_size, sigma, dtype, device):
3996+
@pytest.mark.parametrize(
3997+
"input_type",
3998+
[
3999+
tv_tensors.Image,
4000+
pytest.param(
4001+
"cvcuda.Tensor", marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA not available")
4002+
),
4003+
],
4004+
)
4005+
def test_functional_image_correctness(self, dimensions, kernel_size, sigma, dtype, device, input_type):
39974006
if dtype is torch.float16 and device == "cpu":
39984007
pytest.skip("The CPU implementation of float16 on CPU differs from opencv")
4008+
if (dtype != torch.float32 and dtype != torch.uint8) and input_type == "cvcuda.Tensor":
4009+
pytest.skip("CVCUDA does not support non-float32 or uint8 dtypes for gaussian blur")
39994010

40004011
num_channels, height, width = dimensions
40014012

@@ -4015,45 +4026,17 @@ def test_functional_image_correctness(self, dimensions, kernel_size, sigma, dtyp
40154026
device=device,
40164027
)
40174028

4018-
actual = F.gaussian_blur_image(image, kernel_size=kernel_size, sigma=sigma)
4019-
4020-
torch.testing.assert_close(actual, expected, rtol=0, atol=1)
4021-
4022-
@pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="test requires CVCUDA")
4023-
@needs_cuda
4024-
@pytest.mark.parametrize(
4025-
("dimensions", "kernel_size", "sigma"),
4026-
[
4027-
((10, 12), (3, 3), 0.8),
4028-
((10, 12), (3, 3), 0.5),
4029-
((10, 12), (3, 5), 0.8),
4030-
((10, 12), (3, 5), 0.5),
4031-
((26, 28), (23, 23), 1.7),
4032-
],
4033-
)
4034-
@pytest.mark.parametrize("color_space", ["RGB", "GRAY"])
4035-
@pytest.mark.parametrize("batch_dims", [(1,), (2,), (4,)])
4036-
@pytest.mark.parametrize("dtype", [torch.uint8, torch.float32])
4037-
def test_functional_cvcuda_correctness(self, dimensions, kernel_size, sigma, color_space, batch_dims, dtype):
4038-
height, width = dimensions
4029+
if input_type == "cvcuda.Tensor":
4030+
image = image.unsqueeze(0)
4031+
image = F.to_cvcuda_tensor(image)
40394032

4040-
image_tensor = make_image(
4041-
size=(height, width), color_space=color_space, batch_dims=batch_dims, dtype=dtype, device="cuda"
4042-
)
4043-
image_cvcuda = F.to_cvcuda_tensor(image_tensor)
4033+
actual = F.gaussian_blur(image, kernel_size=kernel_size, sigma=sigma)
40444034

4045-
expected = F.gaussian_blur_image(image_tensor, kernel_size=kernel_size, sigma=sigma)
4046-
actual = F._misc._gaussian_blur_cvcuda(image_cvcuda, kernel_size=kernel_size, sigma=sigma)
4047-
actual_torch = F.cvcuda_to_tensor(actual)
4035+
if input_type == "cvcuda.Tensor":
4036+
actual = F.cvcuda_to_tensor(actual)
4037+
actual = actual.squeeze(0).to(device=device)
40484038

4049-
if dtype.is_floating_point:
4050-
# floating point precision differences between torch and cvcuda are present
4051-
# observed greatest absolute error is 0.3
4052-
# most likely stemming from different implementation
4053-
torch.testing.assert_close(actual_torch, expected, rtol=0, atol=0.3)
4054-
else:
4055-
# uint8/16 gaussians can differ by up to max-value, most likely an overflow issue
4056-
torch.testing.assert_close(actual_torch, expected, rtol=0, atol=get_max_value(dtype))
4039+
torch.testing.assert_close(actual, expected, rtol=0, atol=1)
40574040

40584041

40594042
class TestGaussianNoise:

torchvision/transforms/v2/functional/_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def _gaussian_blur_cvcuda(
208208
image,
209209
tuple(kernel_size),
210210
tuple(sigma),
211-
border=cvcuda.Border.REFLECT,
211+
border=cvcuda.Border.REFLECT101,
212212
)
213213

214214

0 commit comments

Comments
 (0)