Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): give zero-area box pairs an IoU of 0 in bbox.nms (#6362)

Saksham Sharma9623fca792b2
diff --git a/changelog.d/6362.fixed.md b/changelog.d/6362.fixed.md
new file mode 100644
index 000000000..133167d8a
--- /dev/null
+++ b/changelog.d/6362.fixed.md
@@ -0,0 +1 @@
+`kornia.geometry.bbox.nms` no longer suppresses a box that does not overlap the kept box when both boxes have zero area. Their union is empty, so the IoU was `0 / 0 = NaN`, which compared as an overlap and dropped the lower-scored box however far away it was: two zero-width boxes or two points kept only one. The IoU of an empty union is now `0`, as in `torchvision.ops.nms`.
diff --git a/kornia/geometry/bbox.py b/kornia/geometry/bbox.py
index 045bee835..05d4dc6ae 100644
--- a/kornia/geometry/bbox.py
+++ b/kornia/geometry/bbox.py
@@ -771,6 +771,10 @@ def nms(boxes: torch.Tensor, scores: torch.Tensor, iou_threshold: float) -> torc
the size of the output depend on the box values. ``fullgraph=True`` raises, and a default ``torch.compile``
runs it eagerly, so it is outside the :ref:`torch.compile promise <export-support-promise>`.
+ 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``.
+
.. warning::
This differs from the inclusive coordinate arithmetic used by several other
:mod:`kornia.geometry.bbox` operations and is documented in
@@ -818,7 +822,11 @@ def nms(boxes: torch.Tensor, scores: torch.Tensor, iou_threshold: float) -> torc
w = torch.clamp(xx2 - xx1, min=0.0)
h = torch.clamp(yy2 - yy1, min=0.0)
inter = w * h
- ovr = inter / (areas[i] + areas[order[1:]] - inter)
+ 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.
+ ovr = inter / torch.where(union > 0, union, torch.ones_like(union))
inds = torch.where(ovr <= iou_threshold)[0]
order = order[inds + 1]
diff --git a/tests/geometry/test_bbox.py b/tests/geometry/test_bbox.py
index 455ac70eb..6e622594d 100644
--- a/tests/geometry/test_bbox.py
+++ b/tests/geometry/test_bbox.py
@@ -1018,6 +1018,46 @@ class TestNMS(BaseTester):
expected = torch.tensor([0], device=device, dtype=torch.long)
self.assert_close(nms(boxes, scores, 0.5), expected, atol=0, rtol=0)
+ @pytest.mark.parametrize(
+ "boxes",
+ [
+ [[10, 10, 10, 20], [100, 100, 100, 200]], # zero width, far apart
+ [[10, 10, 20, 10], [10, 100, 20, 100]], # zero height, far apart
+ [[10, 10, 10, 10], [50, 50, 50, 50]], # two points
+ [[10, 10, 10, 20], [10, 10, 10, 20]], # identical zero-width boxes
+ ],
+ )
+ @pytest.mark.parametrize("iou_threshold", [0.0, 0.5])
+ def test_zero_area_boxes_have_zero_iou(self, boxes, iou_threshold, device, dtype):
+ # The intersection and the union of two zero-area boxes are both 0. Their IoU is 0, as in torchvision, not
+ # 0 / 0 = NaN, which compared as an overlap and suppressed the lower-scored box. At a threshold of 0, any
+ # positive IoU would suppress it too.
+ boxes = torch.tensor(boxes, device=device, dtype=dtype)
+ scores = torch.tensor([0.875, 0.75], device=device, dtype=dtype)
+ expected = torch.tensor([0, 1], device=device, dtype=torch.long)
+ self.assert_close(nms(boxes, scores, iou_threshold), expected, atol=0, rtol=0)
+ 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.
+ 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)
+ 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):
+ # A zero-width box inside a box with a positive area has IoU 0 with it, and a box with a positive area still
+ # suppresses its duplicate. The last box, a point, is then compared with the zero-width box: their union is
+ # empty, so their IoU is 0 and the point is kept (before #6361 the 0 / 0 suppressed it).
+ boxes = torch.tensor(
+ [[0, 0, 20, 20], [10, 5, 10, 15], [0, 0, 20, 20], [40, 40, 40, 40]], device=device, dtype=dtype
+ )
+ scores = torch.tensor([0.875, 0.75, 0.625, 0.5], device=device, dtype=dtype)
+ expected = torch.tensor([0, 1, 3], device=device, dtype=torch.long)
+ self.assert_close(nms(boxes, scores, 0.5), expected, atol=0, rtol=0)
+
def test_convention_nms_uses_exclusive_xyxy_iou_4008(self, device, dtype):
# The boxes overlap by 1 / 4 under exclusive xyxy arithmetic. At 0.3, NMS
# keeps both; inclusive corner arithmetic would compute 4 / 9 and suppress