Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): validate tensor shear ranges (#5766)

Areen Joshi817330fbc310
diff --git a/changelog.d/5766.breaking.md b/changelog.d/5766.breaking.md
new file mode 100644
index 000000000..5e68f0b5d
--- /dev/null
+++ b/changelog.d/5766.breaking.md
@@ -0,0 +1,7 @@
+`RandomAffine` and `RandomShear` now check a `shear` given as a 2x2 tensor (or nested pairs
+`((x_min, x_max), (y_min, y_max))`) the way they check the flat `(x_min, x_max, y_min, y_max)` form: each
+axis range must be finite, ordered and inside `[-360, 360]`, otherwise the constructor raises `ValueError`
+(`shear-x out of bounds. Expected inside (-360, 360)`, `If shear-y is a range, it must be finite`).
+Previously the 2x2 form skipped the bound and finiteness checks: an endpoint past 360 degrees was
+accepted, and an infinite endpoint was accepted and produced NaN images at forward time. A reversed range
+was already rejected; its message now names the axis (`shear-x[0] should be smaller than shear-x[1]`).
diff --git a/kornia/augmentation/utils/param_validation.py b/kornia/augmentation/utils/param_validation.py
index 95916e265..874a36220 100644
--- a/kornia/augmentation/utils/param_validation.py
+++ b/kornia/augmentation/utils/param_validation.py
@@ -138,7 +138,10 @@ def _shear_bound(
"""
shear = torch.as_tensor(shear, device=device, dtype=dtype)
if shear.shape == torch.Size([2, 2]):
- return shear
+ shear_x = _range_bound(shear[0], "shear-x", 0, (-360, 360), device=device, dtype=dtype)
+ shear_y = _range_bound(shear[1], "shear-y", 0, (-360, 360), device=device, dtype=dtype)
+ return torch.stack([shear_x, shear_y])
+
shear_x = _range_bound(
shear if shear.dim() == 0 else shear[:2], "shear-x", 0, (-360, 360), device=device, dtype=dtype
)
diff --git a/tests/augmentation/test_param_validation.py b/tests/augmentation/test_param_validation.py
index 15d0b3876..a710e0e08 100644
--- a/tests/augmentation/test_param_validation.py
+++ b/tests/augmentation/test_param_validation.py
@@ -267,3 +267,42 @@ class TestParamValidation:
def test_3d_angles_inside_the_bound_still_construct(self, ctor):
"""The bound is inclusive, and the per-axis forms are unaffected."""
assert ctor() is not None
+
+
+class TestShearBoundTensorValidation:
+ def test_valid_tensor_shear_ranges_are_preserved(self):
+ shear = torch.tensor([[-10.0, 10.0], [-20.0, 20.0]])
+ aug = K.RandomShear(shear, p=1.0)
+ params = aug.forward_parameters((2, 3, 8, 8))
+ assert params["shear_x"].shape == (2,)
+ assert params["shear_y"].shape == (2,)
+
+ def test_tensor_shear_ranges_keep_each_axis(self):
+ params = K.RandomShear(torch.tensor([[-10.0, 10.0], [30.0, 40.0]]), p=1.0).forward_parameters((8, 3, 8, 8))
+ assert ((params["shear_x"] >= -10.0) & (params["shear_x"] <= 10.0)).all()
+ assert ((params["shear_y"] >= 30.0) & (params["shear_y"] <= 40.0)).all()
+
+ @pytest.mark.parametrize(
+ "shear",
+ [
+ [[-400.0, 10.0], [-20.0, 20.0]],
+ [[-10.0, 400.0], [-20.0, 20.0]],
+ [[float("-inf"), 10.0], [-20.0, 20.0]],
+ [[-10.0, float("inf")], [-20.0, 20.0]],
+ [[10.0, -10.0], [-20.0, 20.0]],
+ [[-10.0, 10.0], [-400.0, 20.0]],
+ [[-10.0, 10.0], [-20.0, float("inf")]],
+ ],
+ ids=[
+ "lower-bound-too-small",
+ "upper-bound-too-large",
+ "negative-infinity",
+ "positive-infinity",
+ "reversed-range",
+ "y-lower-bound-too-small",
+ "y-positive-infinity",
+ ],
+ )
+ def test_invalid_tensor_shear_ranges_raise(self, shear):
+ with pytest.raises(ValueError):
+ K.RandomShear(torch.tensor(shear), p=1.0).forward_parameters((2, 3, 8, 8))