π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): reject non-finite explicit ranges in _range_bound (#4668)
Pranjal Mishra0c677d30c9c7
diff --git a/changelog.d/4668.breaking.md b/changelog.d/4668.breaking.mdnew file mode 100644index 000000000..ce534e67c--- /dev/null+++ b/changelog.d/4668.breaking.md@@ -0,0 +1,11 @@+An explicit augmentation range containing `inf` or `nan` β for example+`RandomSharpness((0.0, float("inf")))`, `ColorJitter(contrast=(0.0, float("inf")))` or+`RandomAffine(0., scale=(0., float("inf")))` β now raises+`ValueError: If <name> is a range, it must be finite` at construction. Previously an `inf` endpoint+was accepted whenever the parameter's domain was itself unbounded (the default `bounds=(0, inf)`),+because the joint check then compares `inf >= inf`; the resulting infinite draw range produced `NaN`+pixels at forward time, the same defect #4621 reported for the scalar form. With a finite domain such+a range was already rejected. A `nan` endpoint was already rejected too, but reported as+"out of bounds", naming the domain rather than the endpoint, and now gets the same named message.+Finiteness is required of the range, not of `bounds`: an unbounded domain stays legal. This completes+the scalar-form fix in #4625. Fixes #4635.diff --git a/kornia/augmentation/utils/param_validation.py b/kornia/augmentation/utils/param_validation.pyindex 3d6616925..95916e265 100644--- a/kornia/augmentation/utils/param_validation.py+++ b/kornia/augmentation/utils/param_validation.py@@ -78,7 +78,9 @@ def _range_bound(``ValueError`` the explicit range would. The scalar must be finite: ``inf``, ``nan``, and a finitemagnitude too large to represent in ``dtype`` (which converts to ``inf``) all raise ``ValueError``,while a negative value reports "must be non negative" from the earlier check. A pair is taken as the- range itself and checked against ``bounds`` with ``check``.+ range itself, must likewise be finite, and is checked against ``bounds`` with ``check``. Finiteness+ is required of the range, not of ``bounds``: the domain is legitimately unbounded (the default is+ ``(0, inf)``), but a draw range that is not finite produces ``NaN`` at forward time."""if device is None:device = torch.device("cpu")@@ -103,6 +105,15 @@ def _range_bound(factor_bound = factor_bound.clamp(min=bounds[0]).to(device=device, dtype=dtype)else:factor_bound = torch.as_tensor(factor, device=device, dtype=dtype)+ # The same finiteness rule as the scalar branch above, for the same reason: this is the+ # draw range handed to UniformDistribution, and an infinite one yields `inf * 0 -> NaN`+ # pixels at forward time (#4635). `bounds` is the permitted DOMAIN and stays unbounded β+ # the default is `(0, inf)` β so the check is on the range, never on the domain.+ # Without it an `inf` endpoint slipped through exactly when `bounds[1]` was itself `inf`,+ # since the joint check then compares `inf >= inf`. A `nan` endpoint was already rejected,+ # but reported as "out of bounds", which named the domain rather than the real problem.+ if not bool(torch.isfinite(factor_bound).all()):+ raise ValueError(f"If {name} is a range, it must be finite. Got {factor_bound}.")if check is not None:if check == "joint":diff --git a/tests/augmentation/test_param_validation.py b/tests/augmentation/test_param_validation.pyindex 5c2a0c177..15d0b3876 100644--- a/tests/augmentation/test_param_validation.py+++ b/tests/augmentation/test_param_validation.py@@ -143,6 +143,21 @@ class TestParamValidation:(float("inf"), 0, (0, float("inf")), "joint", ValueError, "must be finite"),(float("nan"), 0, (0, float("inf")), "joint", ValueError, "must be finite"),(torch.tensor(float("inf")), 0, (0, float("inf")), "joint", ValueError, "must be finite"),+ # The explicit-range form of the same rule (#4635). An `inf` endpoint used to slip+ # through exactly when bounds[1] was itself `inf`, because the joint check then+ # compares `inf >= inf`; with a finite domain it was already rejected.+ ((0.0, float("inf")), 0, (0, float("inf")), "joint", ValueError, "must be finite"),+ ([0.0, float("inf")], 0, (0, float("inf")), "joint", ValueError, "must be finite"),+ (torch.tensor([0.0, float("inf")]), 0, (0, float("inf")), "joint", ValueError, "must be finite"),+ ((0.0, float("inf")), 0, (0, float("inf")), "singular", ValueError, "must be finite"),+ # nan was already rejected, but reported as "out of bounds", which named the domain+ # rather than the endpoint. Now it gets the same message as every other non-finite range.+ ((0.0, float("nan")), 0, (0, float("inf")), "joint", ValueError, "must be finite"),+ ((float("-inf"), 1.0), 0, (-10, 10), "joint", ValueError, "must be finite"),+ # `check=None` skips the bounds check, not the finiteness rule, exactly as it does+ # for a scalar: an infinite draw range is a NaN at forward time whatever the domain.+ ((0.0, float("inf")), 0, (0, float("inf")), None, ValueError, "must be finite"),+ (float("inf"), 0, (0, float("inf")), None, ValueError, "must be finite"),],)def test_range_bound_errors(self, factor, center, bounds, check, expected_exception, match_msg):