π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(metrics): keep root_mean_squared_disparity_error's gradient finite at an exact match (#6374)
Saksham Sharma85ef2800ade7
diff --git a/changelog.d/6374.fixed.md b/changelog.d/6374.fixed.mdnew file mode 100644index 000000000..8e2864579--- /dev/null+++ b/changelog.d/6374.fixed.md@@ -0,0 +1 @@+`kornia.metrics.root_mean_squared_disparity_error` now has a finite gradient where the prediction matches the target exactly. The derivative of the square root is infinite at a zero error, so an exact match used to get a `NaN` gradient: every matching pixel under `reduction='none'`, and every pixel of a perfect prediction under `'mean'` or `'sum'`, even where a `valid_mask` excluded the differing pixels. It now gets `0`, as `aepe` does since #6299. A pixel that `valid_mask` excludes because its ground truth is `NaN` or `inf` also gets a `0` gradient instead of `NaN`. The value is unchanged.diff --git a/kornia/metrics/disparity.py b/kornia/metrics/disparity.pyindex 5d9769ec4..979e76842 100644--- a/kornia/metrics/disparity.py+++ b/kornia/metrics/disparity.py@@ -218,8 +218,19 @@ def root_mean_squared_disparity_error(# reduction happens. The square root brings the value back into range before the cast back.out_dtype = torch.promote_types(input.dtype, target.dtype)acc_dtype = torch.promote_types(out_dtype, torch.float32)- error = (input.to(acc_dtype) - target.to(acc_dtype)) ** 2- return _reduce_disparity_error(error, mask, reduction).sqrt().to(out_dtype)+ diff = input.to(acc_dtype) - target.to(acc_dtype)+ if mask is not None:+ # Zero a masked-out difference before squaring: the reduction drops its value, but the backward of the square+ # would still multiply the zero gradient by 2 * diff, which is NaN for a NaN or inf ground truth there.+ diff = torch.where(mask, diff, 0)+ squared = _reduce_disparity_error(diff**2, mask, reduction)+ # d sqrt(x)/dx is infinite at 0 and autograd multiplies it by the zero derivative of the square, so an exact match+ # would get a NaN gradient (and a mask cannot hide it: NaN * 0 is NaN). Differentiate the sqrt of a safe positive+ # argument and select 0 at zero error, the subgradient aepe and torch.linalg.vector_norm use. The value is+ # unchanged, and an empty mask still gives NaN under 'mean'.+ is_zero = squared == 0+ safe = torch.where(is_zero, torch.ones_like(squared), squared)+ return torch.where(is_zero, torch.zeros_like(squared), safe.sqrt()).to(out_dtype)def mean_bad_pixel_error(diff --git a/tests/metrics/test_disparity.py b/tests/metrics/test_disparity.pyindex adf03660f..7d25cf026 100644--- a/tests/metrics/test_disparity.py+++ b/tests/metrics/test_disparity.py@@ -326,6 +326,62 @@ class TestRootMeanSquaredDisparityError(BaseTester):actual_none = kornia.metrics.root_mean_squared_disparity_error(input, target, reduction="none")self.assert_close(actual_none, torch.full((64,), 300.0, device=device, dtype=dtype))+ @pytest.mark.parametrize("reduction", ["none", "mean", "sum"])+ def test_exact_match_has_a_finite_gradient(self, reduction, device, dtype):+ # The square root of a zero squared error has an infinite derivative, so an exact match used to get a NaN+ # gradient. It now gets 0, the subgradient aepe uses; a differing pixel keeps its gradient.+ target = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device, dtype=dtype)+ input = target.clone()+ input[1, 1] = 5.0+ # The differing pixel's error is 1: its gradient is 1 per pixel and under 'sum', and (1 / 4) / 0.5 = 0.5+ # under 'mean', where the RMSE over the 4 pixels is 0.5.+ differing = torch.zeros_like(target)+ differing[1, 1] = 0.5 if reduction == "mean" else 1.0+ for pred, expected in ((input, differing), (target.clone(), torch.zeros_like(target))):+ pred = pred.clone().requires_grad_(True)+ (grad,) = torch.autograd.grad(+ kornia.metrics.root_mean_squared_disparity_error(pred, target, reduction=reduction).sum(), pred+ )+ self.assert_close(grad, expected)++ # A mask does not hide the NaN of the matched valid pixels: they must be finite too.+ mask = torch.ones_like(target, dtype=torch.bool)+ mask[1, 1] = False+ pred = target.clone()+ pred[1, 1] = 9.0+ pred.requires_grad_(True)+ (grad,) = torch.autograd.grad(kornia.metrics.root_mean_squared_disparity_error(pred, target, mask), pred)+ self.assert_close(grad, torch.zeros_like(grad))++ @pytest.mark.parametrize("reduction", ["none", "mean", "sum"])+ def test_non_finite_error_propagates(self, reduction, device, dtype):+ # The exact-match guard selects only a zero squared error: a NaN or inf error still reaches the output.+ target = torch.zeros(4, device=device, dtype=dtype)+ for bad, is_expected in ((float("nan"), torch.isnan), (float("inf"), torch.isinf)):+ input = target.clone()+ input[0] = bad+ actual = kornia.metrics.root_mean_squared_disparity_error(input, target, reduction=reduction)+ assert is_expected(actual[0] if reduction == "none" else actual)++ @pytest.mark.parametrize("reduction", ["none", "mean", "sum"])+ @pytest.mark.parametrize("bad", [float("nan"), float("inf")])+ def test_masked_non_finite_target_has_a_finite_gradient(self, bad, reduction, device, dtype):+ # A ground truth that marks invalid pixels with NaN or inf is masked out with valid_mask. The value never+ # saw those pixels, and the gradient must not either: the square's backward multiplied 0 by 2 * diff = NaN.+ target = torch.tensor([[1.0, 2.0], [3.0, bad]], device=device, dtype=dtype)+ mask = torch.isfinite(target)+ pred = torch.tensor([[2.0, 2.0], [3.0, 7.0]], device=device, dtype=dtype, requires_grad=True)+ actual = kornia.metrics.root_mean_squared_disparity_error(pred, target, mask, reduction=reduction)+ (grad,) = torch.autograd.grad(actual.sum(), pred)+ expected = torch.zeros_like(target)+ expected[0, 0] = 1.0 / 3.0**0.5 if reduction == "mean" else 1.0+ self.assert_close(grad, expected)++ def test_gradcheck(self, device):+ target = torch.rand(2, 3, 4, device=device, dtype=torch.float64)+ input = target + torch.rand(2, 3, 4, device=device, dtype=torch.float64) + 0.1+ self.gradcheck(kornia.metrics.root_mean_squared_disparity_error, (input, target))+def test_dynamo(self, device, dtype, torch_optimizer):input, target, mask = _dynamo_inputs(device, dtype)op = kornia.metrics.root_mean_squared_disparity_error