π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): reject non-finite scalar magnitudes in _range_bound (#4625)
yi111eb0af1e8e553
diff --git a/changelog.d/4625.breaking.md b/changelog.d/4625.breaking.mdnew file mode 100644index 000000000..4e1c30929--- /dev/null+++ b/changelog.d/4625.breaking.md@@ -0,0 +1,9 @@+A scalar augmentation magnitude of `inf` β or a finite magnitude too large to represent in the+working dtype, which converts to `inf` β now raises `ValueError: If <name> is a single number, it+must be finite` at construction. Previously `RandomSharpness`, `RandomContrast`, `RandomSaturation`,+`ColorJitter` (`contrast`, `saturation`) and `ColorJiggle` (`contrast`, `saturation`) accepted such a+magnitude and the resulting infinite draw range produced `NaN` pixels at forward time. A scalar `nan`+was already rejected by the generic range check on every reachable path and now gets the same named+message instead. `-inf` still reports "must be non negative" from the earlier check, and the explicit+two-element range form (e.g. `RandomSharpness((0.0, float("inf")))`) is unchanged and still accepted β+tracked in #4635. Fixes #4621.diff --git a/kornia/augmentation/utils/param_validation.py b/kornia/augmentation/utils/param_validation.pyindex 1ee6fc76d..408bc2425 100644--- a/kornia/augmentation/utils/param_validation.py+++ b/kornia/augmentation/utils/param_validation.py@@ -75,8 +75,10 @@ def _range_bound(A scalar ``factor`` is a magnitude around ``center``: the range is ``[center - factor, center + factor]``with its lower end floored at ``bounds[0]`` (a non-negative parameter reads ``contrast=1.5`` as``[0, 2.5]``, the way torchvision does), and an upper end past ``bounds[1]`` raises the same- ``ValueError`` the explicit range would. A pair is taken as the range itself and checked against- ``bounds`` with ``check``.+ ``ValueError`` the explicit range would. The scalar must be finite: ``inf``, ``nan``, and a finite+ magnitude 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``."""if device is None:device = torch.device("cpu")@@ -89,6 +91,8 @@ def _range_bound(if factor.dim() == 0:if factor < 0:raise ValueError(f"If {name} is a single number, it must be non negative. Got {factor}.")+ if not torch.isfinite(factor):+ raise ValueError(f"If {name} is a single number, it must be finite. Got {factor}.")if center is None or bounds is None:raise ValueError(f"`center` and `bounds` cannot be None for single number. Got {center}, {bounds}.")factor_bound = factor.repeat(2) * torch.tensor([-1.0, 1.0], device=factor.device, dtype=factor.dtype) + centerdiff --git a/tests/augmentation/test_param_validation.py b/tests/augmentation/test_param_validation.pyindex 8c92dd635..c1d32577f 100644--- a/tests/augmentation/test_param_validation.py+++ b/tests/augmentation/test_param_validation.py@@ -138,6 +138,11 @@ class TestParamValidation:(10.0, 0, (-5, 5), "singular", ValueError, r"param out of bounds\. .*got tensor\(\[-10\., 10\.\]\)"),(0.6, 0, (-0.5, 0.5), "joint", ValueError, r"param out of bounds\. .*got tensor\(\[-0\.6000, 0\.6000\]\)"),(3.0, 1.0, (0, 2), "joint", ValueError, r"param out of bounds\. .*got tensor\(\[-2\., 4\.\]\)"),+ # -inf hits the earlier "non negative" guard, not the finiteness check: the row pins that order.+ (float("-inf"), 0, (0, float("inf")), "joint", ValueError, "must be non negative"),+ (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"),],)def test_range_bound_errors(self, factor, center, bounds, check, expected_exception, match_msg):