Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): avoid NaN in NMS suppressed values (#5076)

Puttala Jeevan Kumarff463cbf0840
diff --git a/changelog.d/5076.fixed.md b/changelog.d/5076.fixed.md
new file mode 100644
index 000000000..f35766315
--- /dev/null
+++ b/changelog.d/5076.fixed.md
@@ -0,0 +1,4 @@
+`nms2d`, `nms3d`, `NonMaximaSuppression2d` and `NonMaximaSuppression3d` now return 0 at every suppressed position, as
+their Returns sections state. The value output used to be `input * mask`, so a suppressed `±inf` or `NaN` came back as
+`NaN`, which `topk` ranks above every maximum, and a suppressed negative value came back as `-0.0` (now `+0.0`). The
+gradient reaching a suppressed position is 0 even when the incoming gradient there is not finite. (#5067)
diff --git a/kornia/geometry/subpix/nms.py b/kornia/geometry/subpix/nms.py
index 1d70cd323..45b706c79 100644
--- a/kornia/geometry/subpix/nms.py
+++ b/kornia/geometry/subpix/nms.py
@@ -289,7 +289,7 @@ class NonMaximaSuppression2d(nn.Module):
if mask_only:
return mask
- return x * (mask.to(x.dtype))
+ return torch.where(mask, x, 0)
class NonMaximaSuppression3d(nn.Module):
@@ -381,7 +381,7 @@ class NonMaximaSuppression3d(nn.Module):
mask[..., cd : D - bd, cy : H - by, cx : W - bx] = centre > _neighbourhood_max3d(x, kd, ky, kx)
if mask_only:
return mask
- return x * (mask.to(x.dtype))
+ return torch.where(mask, x, 0)
# functional api
diff --git a/tests/geometry/subpix/test_nms.py b/tests/geometry/subpix/test_nms.py
index 6e12c6e39..2524f210f 100644
--- a/tests/geometry/subpix/test_nms.py
+++ b/tests/geometry/subpix/test_nms.py
@@ -55,6 +55,26 @@ def _reference_nms_mask(x: torch.Tensor, kernel_size: tuple[int, ...]) -> torch.
class TestNMS2d(BaseTester):
+ @pytest.mark.parametrize("kernel_size", [(3, 3), (4, 4)])
+ def test_suppressed_nonfinite_values(self, device, dtype, kernel_size):
+ inp = torch.zeros(1, 1, 5, 7, device=device, dtype=dtype)
+ inp[0, 0, :, 0] = float("-inf")
+ inp[0, 0, 2, 2] = float("-inf")
+ inp[0, 0, 2, 3] = 1.0
+ inp.requires_grad_()
+ expected = torch.zeros_like(inp)
+ expected[0, 0, 2, 3] = 1.0
+
+ mask = kornia.geometry.subpix.nms2d(inp, kernel_size, mask_only=True)
+ self.assert_close(mask, expected.bool())
+ out = kornia.geometry.subpix.nms2d(inp, kernel_size)
+ self.assert_close(out, expected, atol=0, rtol=0)
+ assert not out.isnan().any()
+ assert out.flatten().argmax().item() == 17
+
+ out.sum().backward()
+ self.assert_close(inp.grad, mask.to(dtype), atol=0, rtol=0)
+
def test_shape(self, device):
inp = torch.ones(1, 3, 4, 4, device=device)
nms = kornia.geometry.subpix.NonMaximaSuppression2d((3, 3)).to(device)
@@ -279,6 +299,26 @@ class TestNMS2d(BaseTester):
class TestNMS3d(BaseTester):
+ @pytest.mark.parametrize("kernel_size", [(3, 3, 3), (4, 4, 4)])
+ def test_suppressed_nonfinite_values(self, device, dtype, kernel_size):
+ inp = torch.zeros(1, 2, 7, 7, 7, device=device, dtype=dtype)
+ inp[:, :, 0] = float("inf")
+ inp[0, 0, 3, 3, 3] = 2.0
+ inp[0, 1, 3, 3, 3] = float("inf")
+ inp.requires_grad_()
+ expected = torch.zeros_like(inp)
+ expected[0, 0, 3, 3, 3] = 2.0
+ expected[0, 1, 3, 3, 3] = float("inf")
+
+ mask = kornia.geometry.subpix.nms3d(inp, kernel_size, mask_only=True)
+ self.assert_close(mask, expected.bool())
+ out = kornia.geometry.subpix.nms3d(inp, kernel_size)
+ self.assert_close(out, expected, atol=0, rtol=0)
+ assert not out.isnan().any()
+
+ out.sum().backward()
+ self.assert_close(inp.grad, mask.to(dtype), atol=0, rtol=0)
+
def test_shape(self, device):
inp = torch.ones(1, 1, 3, 4, 4, device=device)
nms = kornia.geometry.subpix.NonMaximaSuppression3d((3, 3, 3)).to(device)