Skip to content

Fix batched_nms for ndarray inputs#8901

Open
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:fix/batched-nms-ndarray
Open

Fix batched_nms for ndarray inputs#8901
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:fix/batched-nms-ndarray

Conversation

@aymuos15

@aymuos15 aymuos15 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Description

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. Since offsets is a torch tensor derived from boxes_t, passing an ndarray added a numpy array to a torch tensor and raised TypeError, breaking batched_nms for all ndarray inputs.

The offset is now added to the converted tensor: boxes_for_nms = boxes_t + offsets[:, None]. Everything else downstream already operates on boxes_t, and the result is converted back to the input type, so the torch path is unchanged.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.

aymuos15 added 2 commits June 7, 2026 03:26
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 <soumya_snigdha.kundu@kcl.ac.uk>
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 <soumya_snigdha.kundu@kcl.ac.uk>
@coderabbitai

coderabbitai Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The PR fixes a tensor consistency issue in batched_nms by computing the class-offset adjusted boxes from the already-converted float tensor boxes_t instead of the original input. This ensures the NMS path operates uniformly on tensor representation rather than mixing container types. A parameterized test is added to validate the function across multiple backends.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly summarizes the main fix: resolving batched_nms function to work with ndarray inputs.
Description check ✅ Passed Description explains the bug, root cause, and solution. Required sections present: description, types of changes. New tests added checkbox marked.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/data/test_box_utils.py (1)

273-281: 💤 Low value

Consider adding a docstring.

Per coding guidelines, docstrings should describe the test's purpose, parameters, and expected behavior. While existing tests in this file lack docstrings, adding one would improve maintainability.

📝 Suggested docstring
 class TestBatchedNms(unittest.TestCase):
     `@parameterized.expand`(TEST_NDARRAYS)
     def test_batched_nms_backend(self, p):
+        """Test batched_nms with different array backends (numpy/torch).
+        
+        Args:
+            p: Array constructor from TEST_NDARRAYS.
+            
+        Validates that batched_nms correctly handles ndarray inputs and 
+        performs class-aware NMS, keeping boxes [0, 2] from overlapping 
+        class-0 boxes and a separate class-1 box.
+        """
         boxes = p(np.array([[0, 0, 10, 10], [1, 1, 11, 11], [100, 100, 110, 110]], dtype=np.float32))
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/data/test_box_utils.py` around lines 273 - 281, Add a concise docstring
to the TestBatchedNms.test_batched_nms_backend test explaining its purpose,
inputs and expected outcome: state that the test constructs three boxes and
corresponding scores/labels, calls batched_nms(boxes, scores, labels,
nms_thresh=0.5) and asserts the kept indices equal [0, 2]; place this docstring
immediately under the def test_batched_nms_backend(...) line so readers can
quickly understand the test intent and expected behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/data/test_box_utils.py`:
- Around line 273-281: Add a concise docstring to the
TestBatchedNms.test_batched_nms_backend test explaining its purpose, inputs and
expected outcome: state that the test constructs three boxes and corresponding
scores/labels, calls batched_nms(boxes, scores, labels, nms_thresh=0.5) and
asserts the kept indices equal [0, 2]; place this docstring immediately under
the def test_batched_nms_backend(...) line so readers can quickly understand the
test intent and expected behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 96d1ae07-6c7b-426f-8946-a23c0a6acdd3

📥 Commits

Reviewing files that changed from the base of the PR and between 2a7d0cf and ee66c9e.

📒 Files selected for processing (2)
  • monai/data/box_utils.py
  • tests/data/test_box_utils.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant