π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): avoid NaN orientation weight gradients on flat patches (#6451)
Afloat41a4740864f3
diff --git a/changelog.d/6451.fixed.md b/changelog.d/6451.fixed.mdnew file mode 100644index 000000000..49da7c817--- /dev/null+++ b/changelog.d/6451.fixed.md@@ -0,0 +1 @@+`PatchDominantGradientOrientation` now gives zero smoothing-weight gradients for flat patches, preventing NaN parameter gradients when a batch includes a flat patch.diff --git a/kornia/feature/orientation.py b/kornia/feature/orientation.pyindex 73258a7dd..66306be4b 100644--- a/kornia/feature/orientation.py+++ b/kornia/feature/orientation.py@@ -189,7 +189,10 @@ class PatchDominantGradientOrientation(nn.Module):# A flat patch has an empty histogram (a zero-gradient pixel contributes no magnitude), and a# uniform one has no peak: the parabolic refinement is `0 / 0` there, so it is skipped.denom = left + right - 2.0 * center- c_subpix = torch.where(denom != 0, 0.5 * (left - right) / denom, torch.zeros_like(denom))+ has_peak = denom != 0+ # Guard the division itself: masking 0 / 0 afterwards still gives NaN smoothing-weight gradients.+ safe_denom = torch.where(has_peak, denom, torch.ones_like(denom))+ c_subpix = torch.where(has_peak, 0.5 * (left - right) / safe_denom, torch.zeros_like(denom))return -((2.0 * math.pi * (indices.to(patch.dtype) + c_subpix) / float(self.num_ang_bins)) - math.pi)diff --git a/tests/feature/test_local_features_orientation.py b/tests/feature/test_local_features_orientation.pyindex 43f6d56c3..444d77e81 100644--- a/tests/feature/test_local_features_orientation.py+++ b/tests/feature/test_local_features_orientation.py@@ -119,6 +119,51 @@ class TestPatchDominantGradientOrientation(BaseTester):expected = torch.tensor([90.0], device=device)self.assert_close(rad2deg(ang), expected)+ @pytest.mark.parametrize("value", [0.0, 0.5])+ def test_flat_patch_weight_gradient(self, device, dtype, value):+ if not supports_replicate_padding(device, dtype):+ pytest.skip(f"no replicate-pad kernel for {dtype} on {device.type}")+ patches = torch.full((2, 1, 32, 32), value, device=device, dtype=dtype, requires_grad=True)+ model = PatchDominantGradientOrientation(32).to(device, dtype)++ angles = model(patches)+ patch_grad, weight_grad = torch.autograd.grad(angles.sum(), (patches, model.angular_smooth.weight))++ # An empty histogram gives the existing fallback angle regardless of the smoothing weights.+ self.assert_close(angles, torch.full_like(angles, math.pi))+ self.assert_close(patch_grad, torch.zeros_like(patch_grad), atol=0.0, rtol=0.0)+ self.assert_close(weight_grad, torch.zeros_like(weight_grad), atol=0.0, rtol=0.0)++ def test_flat_patch_does_not_change_textured_weight_gradient(self, device, dtype):+ if not supports_replicate_padding(device, dtype):+ pytest.skip(f"no replicate-pad kernel for {dtype} on {device.type}")+ ramp = torch.linspace(0, 1, 32, device=device, dtype=dtype).expand(1, 1, 32, 32)+ model = PatchDominantGradientOrientation(32).to(device, dtype)+ expected_angle = model(ramp)+ (expected_grad,) = torch.autograd.grad(expected_angle.sum(), (model.angular_smooth.weight,))+ assert torch.isfinite(expected_grad).all()+ assert expected_grad.count_nonzero() > 0++ # Flat patches must contribute zero to the sum, without poisoning the textured sample's gradient.+ patches = torch.cat((torch.zeros_like(ramp), ramp, torch.full_like(ramp, 0.5)))+ angles = model(patches)+ (weight_grad,) = torch.autograd.grad(angles.sum(), (model.angular_smooth.weight,))++ self.assert_close(angles[1:2], expected_angle)+ self.assert_close(weight_grad, expected_grad)++ @pytest.mark.parametrize("flat", [False, True])+ def test_weight_gradcheck(self, device, flat):+ model = PatchDominantGradientOrientation(13).to(device, torch.float64)+ patches = torch.zeros(1, 1, 13, 13, device=device, dtype=torch.float64)+ if not flat:+ patches[:, :, :, :] = torch.linspace(0, 1, 13, device=device, dtype=torch.float64)++ def orientation_from_weight(weight):+ return torch.func.functional_call(model, {"angular_smooth.weight": weight}, (patches,))++ self.gradcheck(orientation_from_weight, (model.angular_smooth.weight,))+def test_gradcheck(self, device):batch_size, channels, height, width = 1, 1, 13, 13ori = PatchDominantGradientOrientation(width).to(device)