From 80083d7a743a393ee3dc03b4842f8cce38a408c0 Mon Sep 17 00:00:00 2001 From: AayushMainali-Github Date: Tue, 25 Aug 2026 13:15:44 +0000 Subject: [PATCH] Fix ElasticTransform to reject negative sigma values --- test/test_transforms.py | 8 ++++++++ test/test_transforms_v2.py | 5 +++++ torchvision/transforms/transforms.py | 3 +++ torchvision/transforms/v2/_geometry.py | 4 +++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/test/test_transforms.py b/test/test_transforms.py index b9fa620e187..fb68427cf14 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -2208,6 +2208,14 @@ def test_elastic_transformation(): transforms.ElasticTransform(alpha=2.0, sigma=[1.0, True]) with pytest.raises(ValueError, match=r"sigma is a sequence its length should be 2"): transforms.ElasticTransform(alpha=2.0, sigma=[1.0, 0.0, 1.0]) + with pytest.raises(ValueError, match=r"sigma should have non-negative values"): + transforms.ElasticTransform(alpha=2.0, sigma=-100.0) + with pytest.raises(ValueError, match=r"sigma should have non-negative values"): + transforms.ElasticTransform(alpha=2.0, sigma=[-100.0, -100.0]) + with pytest.raises(ValueError, match=r"sigma should have non-negative values"): + transforms.ElasticTransform(alpha=2.0, sigma=[-100.0, 100.0]) + with pytest.raises(ValueError, match=r"sigma should have non-negative values"): + transforms.ElasticTransform(alpha=2.0, sigma=[100.0, -100.0]) t = transforms.transforms.ElasticTransform(alpha=2.0, sigma=2.0, interpolation=Image.BILINEAR) assert t.interpolation == transforms.InterpolationMode.BILINEAR diff --git a/test/test_transforms_v2.py b/test/test_transforms_v2.py index 6d9f9732552..c5f7d3382b0 100644 --- a/test/test_transforms_v2.py +++ b/test/test_transforms_v2.py @@ -3427,6 +3427,11 @@ def test_transform(self, make_input, size, device): check_v1_compatibility=check_v1_compatibility, ) + @pytest.mark.parametrize("sigma", [-100, -100.0, [-100, -100], [-100.0, 100.0], [100.0, -100.0]]) + def test_transform_negative_sigma(self, sigma): + with pytest.raises(ValueError, match="sigma should have non-negative values"): + transforms.ElasticTransform(sigma=sigma) + class TestToPureTensor: def test_correctness(self): diff --git a/torchvision/transforms/transforms.py b/torchvision/transforms/transforms.py index e33b3e28194..60e2d6adde9 100644 --- a/torchvision/transforms/transforms.py +++ b/torchvision/transforms/transforms.py @@ -2063,6 +2063,7 @@ class ElasticTransform(torch.nn.Module): Args: alpha (float or sequence of floats): Magnitude of displacements. Default is 50.0. sigma (float or sequence of floats): Smoothness of displacements. Default is 5.0. + Values must be non-negative. interpolation (InterpolationMode): Desired interpolation enum defined by :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``. If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported. @@ -2104,6 +2105,8 @@ def __init__(self, alpha=50.0, sigma=5.0, interpolation=InterpolationMode.BILINE sigma = [float(sigma), float(sigma)] if isinstance(sigma, (list, tuple)) and len(sigma) == 1: sigma = [sigma[0], sigma[0]] + if any(s < 0 for s in sigma): + raise ValueError(f"sigma should have non-negative values. Got {list(sigma)}") self.sigma = sigma diff --git a/torchvision/transforms/v2/_geometry.py b/torchvision/transforms/v2/_geometry.py index 278b58eae2f..dbb3fdf380c 100644 --- a/torchvision/transforms/v2/_geometry.py +++ b/torchvision/transforms/v2/_geometry.py @@ -1053,7 +1053,7 @@ class ElasticTransform(Transform): alpha (float or sequence of floats, optional): Magnitude of displacements. Default is 50.0. A single value is ``[alpha, alpha]``. sigma (float or sequence of floats, optional): Smoothness of displacements. - Default is 5.0. A single value is ``[sigma, sigma]``. + Default is 5.0. A single value is ``[sigma, sigma]``. Values must be non-negative. interpolation (str or InterpolationMode, optional): Desired interpolation enum defined by :class:`torchvision.transforms.v2.InterpolationMode`. Accepted string values are ``"nearest"``, ``"nearest-exact"``, ``"bilinear"``, ``"bicubic"``, @@ -1081,6 +1081,8 @@ def __init__( super().__init__() self.alpha = _setup_number_or_seq(alpha, "alpha") self.sigma = _setup_number_or_seq(sigma, "sigma") + if any(s < 0 for s in self.sigma): + raise ValueError(f"sigma should have non-negative values. Got {list(self.sigma)}") self.interpolation = interpolation self.fill = fill