Repository navigation
fix(detection): stop NMM from merging detections that do not overlap at threshold 0 - #2972
Merged
Merged
Conversation
…at threshold 0 `_validate_iou_threshold` accepts 0, but the non-max-merge grouping compared `ious >= iou_threshold`, so every pair with an IoU of exactly 0 (disjoint or edge-touching) was grouped. `Detections.with_nmm(threshold=0.0)` turned three disjoint boxes into one union box, while `with_nms(threshold=0.0)` kept all of them. Grouping now also requires a positive overlap at the box, dense-mask, compact-mask and oriented-box sites; behaviour for thresholds above 0 is unchanged.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The changelog link still contains unresolved PR_NUMBER placeholders, preventing navigation to the change and violating the concrete PR-link requirement.
1 open issue
What changed in this PR
Fixes #2971 by making non-max merging require positive overlap at threshold zero across all supported geometries. Disjoint and edge-touching detections remain separate, while behavior at positive thresholds is unchanged.
Changes:
- Adds a shared overlap check for boxes, oriented boxes, dense masks, and
CompactMask. - Adds utility and
Detections.with_nmmregression tests for disjoint, touching, and overlapping detections. - Documents the behavior change in the Unreleased changelog.
| File | Description |
|---|---|
tests/detection/utils/test_iou_and_nms.py |
Tests zero-threshold grouping across all four geometry paths. |
tests/detection/test_core.py |
Tests public API separation, field preservation, and merging. |
src/supervision/detection/utils/iou_and_nms.py |
Applies the positive-overlap guard at all four grouping sites. |
docs/changelog.md |
Describes the zero-threshold merging fix. |
Review effort: Balanced
|
|
||
| ### Unreleased <small>upcoming</small> | ||
|
|
||
| - `sv.Detections.with_nmm`, `sv.box_non_max_merge`, `sv.mask_non_max_merge` and `sv.oriented_box_non_max_merge` with `threshold=0` (`iou_threshold=0`) no longer merge detections that do not overlap. Any pair with an IoU of exactly 0 satisfied `IoU >= 0`, so every detection of a class was merged into one union box (three disjoint boxes became a single `[0, 0, 210, 210]` box), while `with_nms(threshold=0)` kept all of them. Merging now needs a positive overlap, matching non-max suppression; results for thresholds above 0 are unchanged. ([#PR_NUMBER](https://github.com/roboflow/supervision/pull/PR_NUMBER)) |
# Conflicts: # docs/changelog.md # tests/detection/utils/test_iou_and_nms.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2972 +/- ##
=======================================
Coverage 95% 95%
=======================================
Files 84 84
Lines 15603 15606 +3
=======================================
+ Hits 14892 14895 +3
Misses 711 711 🚀 New features to boost your workflow:
|
3 tasks done
…eshold # Conflicts: # docs/changelog.md # tests/detection/utils/test_iou_and_nms.py
Changes: - Integrate the current develop branch and preserve the PR's unreleased changelog entry above the 0.32.1 release notes. - Retain the target branch's image, metrics, dataset, documentation, dependency, and test updates. Impact: - The PR can be evaluated against the current target while retaining its zero-threshold merging behavior and released history. Verification: - Zero-threshold detection tests: 20 passed. - Staged merge diff check passed; the index has no unmerged paths. Residual limits: - The earlier link-check CI job failed; its broad 404 responses need a fresh CI result. --- Co-authored-by: Codex <[email protected]>
Changes: - Describe the zero-overlap comparison against the current merge candidate and distinguish index groups from Detections union boxes. Impact: - Changelog readers can distinguish candidate admission from pairwise overlap within a final group. Verification: - Pinned Markdown formatting and codespell hooks passed. - Git diff check passed; independent static challenge checked grouping semantics. Residual limits: - Hosted link-check remains unresolved; the historical job reported 50 older PR links as 404 and the updated source has no hosted run yet. --- Co-authored-by: Codex <[email protected]>
Borda
approved these changes
Nov 24, 2026
This was referenced Nov 24, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
Detections.with_nmm(threshold=0.0)merges detections that do not overlap at all._validate_iou_thresholdaccepts 0, but the non-max-merge grouping comparesious >= iou_threshold, so every pair with an IoU of exactly 0 (disjoint or only touching) is grouped. Three disjoint same-class boxes become one[0, 0, 210, 210]box, whilewith_nms(threshold=0.0)keeps all three.box_non_max_merge,mask_non_max_merge(dense andCompactMask) andoriented_box_non_max_mergegroup the same way.Motivation and Context
At threshold 0, "merge anything that overlaps" is the only sensible reading, and
with_nmsalready behaves that way (it uses a strict>). No existing test covered threshold 0 for NMM.Closes #2971
Changes Made
_is_mergeable_overlap, which requiresoverlaps >= iou_thresholdandoverlaps > 0, and use it at the four NMM grouping sites (dense mask,CompactMask, box, oriented box).>=comparison. Only IoU == 0 pairs at threshold 0 change.CompactMaskand oriented box atiou_threshold=0, and forDetections.with_nmm(threshold=0.0).match_detectionshas the sameiou >= iou_thresholdat threshold 0. It is documented as mirroring the metrics matcher, so I left it for a separate discussion.Testing
pytest tests/detection/test_core.py tests/detection/utils/test_iou_and_nms.py -k "zero_threshold or ZeroThreshold"develop(the fix reverted): 16 failed, 4 passed (the 4 are the one-pixel-overlap regression guards)pytest tests/detection tests/dataset: 3341 passed, 3 skippedDifferential check outside the repo: grouping on develop vs this branch for random boxes, masks with holes,
CompactMaskand rotated oriented boxes, thresholds in (0, 1] and both IoU and IoS: 83,200 cases, 0 differences (the same harness at threshold 0.0 shows 460 differences out of 480, so it does detect the change).pre-commithooks on the changed files (ruff, ruff-format, mypy, codespell, mdformat, docformatter): pass.Added/updated tests.
tests/detectionandtests/datasetpass locally; the full suite was not run becausetests/metricsneeds thefaster_coco_eval/pycocotoolsextras, which are not installed here.Updated docs (docstrings / mkdocs entry) for new or changed public API (no public API change)
Added a changelog entry in
docs/changelog.mdunderUnreleasedAdditional Notes
_group_overlapping_packed_masks, which has its ownious.flatten() >= iou_threshold. The two PRs merge textually, but whichever lands second needs_is_mergeable_overlap(ious.flatten(), iou_threshold)at that site, otherwise the dense-mask path returns to the old behaviour on NumPy >= 2 (checked on a merged tree: the dense-mask zero-threshold tests fail until that line is changed). Happy to carry it if refine(detection): count dense mask overlaps on bit-packed masks #2886 lands first.with_nmmpublic docstrings do not specify the boundary, so I did not change them.