Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion torchvision/transforms/v2/_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"``,
Expand Down Expand Up @@ -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
Expand Down