Fix batched_nms for ndarray inputs#8901
Conversation
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>
📝 WalkthroughWalkthroughThe PR fixes a tensor consistency issue in Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/data/test_box_utils.py (1)
273-281: 💤 Low valueConsider 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
📒 Files selected for processing (2)
monai/data/box_utils.pytests/data/test_box_utils.py
Description
batched_nmsis documented to accept an Nx4/Nx6 torch tensor or ndarray, but it computedboxes_for_nms = boxes + offsets[:, None]using the originalboxesargument instead of the converted tensorboxes_t. Sinceoffsetsis a torch tensor derived fromboxes_t, passing an ndarray added a numpy array to a torch tensor and raisedTypeError, breakingbatched_nmsfor all ndarray inputs.The offset is now added to the converted tensor:
boxes_for_nms = boxes_t + offsets[:, None]. Everything else downstream already operates onboxes_t, and the result is converted back to the input type, so the torch path is unchanged.Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.