Skip to content

fix(detection): stop NMM from merging detections that do not overlap at threshold 0 - #2972

Merged
Borda merged 7 commits into
roboflow:developfrom
avanish-garg:fix/nmm-zero-threshold
Nov 24, 2026
Merged

Borda merged 7 commits into
roboflow:developfrom
avanish-garg:fix/nmm-zero-threshold

Conversation

@avanish-garg

Copy link
Copy Markdown
Contributor

Description

Detections.with_nmm(threshold=0.0) merges detections that do not overlap at all. _validate_iou_threshold accepts 0, but the non-max-merge grouping compares ious >= 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, while with_nms(threshold=0.0) keeps all three.

import numpy as np
import supervision as sv

det = sv.Detections(
    xyxy=np.array([[0, 0, 10, 10], [100, 100, 110, 110], [200, 200, 210, 210]], dtype=float),
    confidence=np.array([0.9, 0.8, 0.7]),
    class_id=np.array([0, 0, 0]),
)
len(det.with_nms(threshold=0.0))  # 3
len(det.with_nmm(threshold=0.0))  # 1 on develop, 3 with this change

box_non_max_merge, mask_non_max_merge (dense and CompactMask) and oriented_box_non_max_merge group the same way.

Motivation and Context

At threshold 0, "merge anything that overlaps" is the only sensible reading, and with_nms already behaves that way (it uses a strict >). No existing test covered threshold 0 for NMM.

Closes #2971

Changes Made

  • Add _is_mergeable_overlap, which requires overlaps >= iou_threshold and overlaps > 0, and use it at the four NMM grouping sites (dense mask, CompactMask, box, oriented box).
  • For any threshold above 0 the result is identical to the old >= comparison. Only IoU == 0 pairs at threshold 0 change.
  • Tests for box, dense mask, CompactMask and oriented box at iou_threshold=0, and for Detections.with_nmm(threshold=0.0).
  • Changelog entry under Unreleased.
  • Out of scope: match_detections has the same iou >= iou_threshold at 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"

    • on develop (the fix reverted): 16 failed, 4 passed (the 4 are the one-pixel-overlap regression guards)
    • with this change: 20 passed
  • pytest tests/detection tests/dataset: 3341 passed, 3 skipped

  • Differential check outside the repo: grouping on develop vs this branch for random boxes, masks with holes, CompactMask and 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-commit hooks on the changed files (ruff, ruff-format, mypy, codespell, mdformat, docformatter): pass.

  • Added/updated tests. tests/detection and tests/dataset pass locally; the full suite was not run because tests/metrics needs the faster_coco_eval/pycocotools extras, 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.md under Unreleased

Additional Notes

  • refine(detection): count dense mask overlaps on bit-packed masks #2886 adds _group_overlapping_packed_masks, which has its own ious.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.
  • Edge-touching boxes and masks (IoU 0) now stay separate at threshold 0, consistent with NMS.
  • with_nmm public docstrings do not specify the boundary, so I did not change them.

…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.
@Borda
Borda requested a balanced review from Copilot November 13, 2026 06:59
@Borda Borda added the bug Something isn't working label Nov 13, 2026

Copilot AI 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.

🟡 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_nmm regression 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

Comment thread docs/changelog.md Outdated

### 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

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (65f01d7) to head (da8be26).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…eshold

# Conflicts:
#	docs/changelog.md
#	tests/detection/utils/test_iou_and_nms.py
Borda and others added 2 commits November 24, 2026 11:55
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
Borda merged commit 06ac43f into roboflow:develop Nov 24, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: with_nmm(threshold=0.0) merges detections that do not overlap at all, while with_nms(threshold=0.0) keeps them

3 participants