From 281edabb57071d4dbc645f58040dd17b5b607f6b Mon Sep 17 00:00:00 2001 From: Soumya Snigdha Kundu Date: Sun, 7 Jun 2026 03:26:53 +0100 Subject: [PATCH 1/2] Fix batched_nms for ndarray inputs batched_nms is documented to accept an Nx4/Nx6 torch tensor or ndarray, but it computed boxes_for_nms = boxes + offsets[:, None] using the original boxes argument instead of the converted tensor boxes_t. offsets is a torch tensor derived from boxes_t, so for ndarray boxes this added a numpy array to a torch tensor and raised TypeError, breaking batched_nms for all ndarray inputs. Use the converted tensor: boxes_for_nms = boxes_t + offsets[:, None]. Signed-off-by: Soumya Snigdha Kundu --- monai/data/box_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/data/box_utils.py b/monai/data/box_utils.py index b0c41b5d7d..2f4a1426a9 100644 --- a/monai/data/box_utils.py +++ b/monai/data/box_utils.py @@ -1200,7 +1200,7 @@ def batched_nms( # from different classes do not overlap max_coordinate = boxes_t.max() offsets = labels_t.to(boxes_t) * (max_coordinate + 1) - boxes_for_nms = boxes + offsets[:, None] + boxes_for_nms = boxes_t + offsets[:, None] keep = non_max_suppression(boxes_for_nms, scores_t, nms_thresh, max_proposals, box_overlap_metric) # convert tensor back to numpy if needed From ee66c9edc1df941fff60385ea3de72f63ae4cc48 Mon Sep 17 00:00:00 2001 From: Soumya Snigdha Kundu Date: Sun, 7 Jun 2026 03:28:10 +0100 Subject: [PATCH 2/2] Add regression test for batched_nms ndarray inputs Runs batched_nms across the numpy and torch backends via TEST_NDARRAYS; the numpy case failed before the boxes_t fix. Signed-off-by: Soumya Snigdha Kundu --- tests/data/test_box_utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/data/test_box_utils.py b/tests/data/test_box_utils.py index 05778f691b..30136d4f1b 100644 --- a/tests/data/test_box_utils.py +++ b/tests/data/test_box_utils.py @@ -23,6 +23,7 @@ CornerCornerModeTypeB, CornerCornerModeTypeC, CornerSizeMode, + batched_nms, box_area, box_centers, box_giou, @@ -269,5 +270,15 @@ def test_integer_truncation_bug(self): self.assertGreater(iou[0, 0], 0.0, "IoU should not be truncated to 0") +class TestBatchedNms(unittest.TestCase): + @parameterized.expand(TEST_NDARRAYS) + def test_batched_nms_backend(self, p): + boxes = p(np.array([[0, 0, 10, 10], [1, 1, 11, 11], [100, 100, 110, 110]], dtype=np.float32)) + scores = p(np.array([0.9, 0.8, 0.7], dtype=np.float32)) + labels = p(np.array([0, 0, 1])) + keep = batched_nms(boxes, scores, labels, nms_thresh=0.5) + assert_allclose(keep, [0, 2], type_test=False) + + if __name__ == "__main__": unittest.main()