Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix NMS handling of NaN IoUs (#6479)

Polly Labsbfa30b45ff31
diff --git a/changelog.d/6479.fixed.md b/changelog.d/6479.fixed.md
new file mode 100644
index 000000000..77eb00d26
--- /dev/null
+++ b/changelog.d/6479.fixed.md
@@ -0,0 +1,3 @@
+`kornia.geometry.bbox.nms` no longer suppresses a box when its IoU is NaN, as in `torchvision.ops.nms`. Before, a kept
+box with a NaN coordinate dropped every lower-scored box, and a box with a NaN coordinate was dropped by any
+higher-scored kept box.
diff --git a/kornia/geometry/bbox.py b/kornia/geometry/bbox.py
index f0fc3b904..c10ee3cb2 100644
--- a/kornia/geometry/bbox.py
+++ b/kornia/geometry/bbox.py
@@ -773,7 +773,8 @@ def nms(boxes: torch.Tensor, scores: torch.Tensor, iou_threshold: float) -> torc
Note:
Two boxes whose union is empty, such as two zero-width boxes, have an IoU of ``0``, so neither suppresses the
- other, as in ``torchvision.ops.nms``.
+ other, as in ``torchvision.ops.nms``. A box with a NaN coordinate has a NaN IoU with every other box, which
+ is not above ``iou_threshold``: it neither suppresses nor is suppressed, as in ``torchvision.ops.nms``.
.. warning::
This differs from the inclusive coordinate arithmetic used by several other
@@ -823,12 +824,14 @@ def nms(boxes: torch.Tensor, scores: torch.Tensor, iou_threshold: float) -> torc
h = torch.clamp(yy2 - yy1, min=0.0)
inter = w * h
union = areas[i] + areas[order[1:]] - inter
- # Two zero-area boxes have a zero union, and their IoU is 0 rather than 0 / 0: a NaN would compare as an
- # overlap below and suppress a disjoint box. torchvision keeps such a box too. The intersection is 0 whenever
- # the union is not positive, so dividing it by 1 there gives 0, and a NaN coordinate still gives a NaN IoU.
+ # Two zero-area boxes have a zero union, and their IoU is 0 rather than 0 / 0, so they compare with
+ # iou_threshold like disjoint boxes. The intersection is 0 whenever the union is not positive, so dividing it
+ # by 1 there gives 0, and a NaN coordinate still gives a NaN IoU.
ovr = inter / torch.where(union > 0, union, torch.ones_like(union))
- inds = torch.where(ovr <= iou_threshold)[0]
+ # Suppress only when IoU is strictly above the threshold. In particular, NaN IoUs compare false here, so a
+ # box with NaN coordinates does not suppress the remaining boxes.
+ inds = torch.where(~(ovr > iou_threshold))[0]
order = order[inds + 1]
if len(keep) > 0:
diff --git a/tests/geometry/test_bbox.py b/tests/geometry/test_bbox.py
index 6e622594d..7b9fca172 100644
--- a/tests/geometry/test_bbox.py
+++ b/tests/geometry/test_bbox.py
@@ -1039,12 +1039,20 @@ class TestNMS(BaseTester):
self.assert_close(nms(boxes.to(torch.int32), scores, iou_threshold), expected, atol=0, rtol=0)
@pytest.mark.parametrize("iou_threshold", [0.0, 0.5])
- def test_nan_box_is_still_suppressed(self, iou_threshold, device, dtype):
- # A box with a NaN coordinate has a NaN IoU with the kept box, which does not compare as at most the
- # threshold, so it is suppressed. The zero-union rule must not turn that NaN into 0.
+ def test_lower_scored_nan_box_does_not_suppress_or_get_suppressed(self, iou_threshold, device, dtype):
+ # A NaN IoU is not above the threshold, so the lower-scored NaN box is kept. It does not suppress any box.
boxes = torch.tensor([[0, 0, 10, 10], [100, 100, float("nan"), 110]], device=device, dtype=dtype)
scores = torch.tensor([0.875, 0.75], device=device, dtype=dtype)
- expected = torch.tensor([0], device=device, dtype=torch.long)
+ expected = torch.tensor([0, 1], device=device, dtype=torch.long)
+ self.assert_close(nms(boxes, scores, iou_threshold), expected, atol=0, rtol=0)
+
+ @pytest.mark.parametrize("iou_threshold", [0.0, 0.5])
+ def test_highest_scored_nan_box_does_not_suppress_valid_boxes(self, iou_threshold, device, dtype):
+ boxes = torch.tensor(
+ [[100, 100, float("nan"), 110], [0, 0, 10, 10], [200, 200, 210, 210]], device=device, dtype=dtype
+ )
+ scores = torch.tensor([0.875, 0.75, 0.625], device=device, dtype=dtype)
+ expected = torch.tensor([0, 1, 2], device=device, dtype=torch.long)
self.assert_close(nms(boxes, scores, iou_threshold), expected, atol=0, rtol=0)
def test_zero_area_box_next_to_a_box(self, device, dtype):