Repository navigation
refine(detection): count dense mask overlaps on bit-packed masks - #2886
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2886 +/- ##
=======================================
Coverage 95% 95%
=======================================
Files 84 84
Lines 16343 16408 +65
=======================================
+ Hits 15601 15669 +68
+ Misses 742 739 -3 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
The chunk planner still budgets float mask copies instead of packed buffers, causing unnecessary chunking, misleading memory warnings, and repeated packing of detections in memory-limited calls.
1 open issue
What changed in this PR
This PR optimizes Supervision’s dense boolean mask overlap calculations using bit-packed masks on NumPy ≥ 2.0, addressing #2845 without changing the public API.
Changes:
- Adds packed intersection counting and keeps merge candidates packed during non-max merging.
- Preserves fallback behavior for older NumPy and non-boolean masks; leaves
CompactMaskunchanged. - Adds parity and edge-case tests plus a performance changelog entry.
| File | Description |
|---|---|
tests/detection/utils/test_iou_and_nms.py |
Tests packed/fallback parity, suppression, merging, and large-mask counts. |
src/supervision/detection/utils/iou_and_nms.py |
Implements bit-packed overlap and merge paths with shared overlap calculation. |
docs/changelog.md |
Documents the optimization, compatibility, and reported performance gains. |
Review effort: Balanced
Changes: - Incorporate target changes to annotation, mask, dataset, geometry, and image utilities and their tests. - Preserve the packed mask implementation and both sets of changelog entries. Impact: - Keep the PR compatible with the current target while retaining its dense mask optimization. Verification: - Focused NMS and compact mask IoU tests: 310 passed. - Staged merge diff check passed. Residual limits: - Full suite has not been rerun on the merge; selected review findings remain open. --- Co-authored-by: Codex <[email protected]>
Changes: - Budget packed mask buffers and reuse packed detections across chunks. - Add regressions for warning floors, noncontiguous masks, and repeated packing. Impact: - Avoid false memory warnings and repeated detection packing in memory-limited calls. Verification: - Full test suite: 6503 passed, 247 skipped. - All pre-commit hooks and diff check passed. Residual limits: - Peak memory and performance across representative workloads were not benchmarked. - The original parallel handover remains incomplete; its worktrees are preserved. --- Co-authored-by: Codex <[email protected]>
Changes: - Link the packed mask changelog entry to PR roboflow#2886. Impact: - Readers can trace the change to its pull request. Verification: - All pre-commit hooks and diff check passed. - Full test suite: 6503 passed, 247 skipped. Residual limits: - The original parallel handover remains incomplete; its worktrees are preserved. --- Co-authored-by: Codex <[email protected]>
Changes: - Retain the PR author’s packed-overlap helper and regression alongside the local chunk planner and layout-aware warning checks. - Preserve both upstream and local mask overlap tests. Impact: - Integrate the current PR head without losing detection reuse or memory-limit behavior. Verification: - Full test suite: 6504 passed, 247 skipped. - All pre-commit hooks and staged diff check passed. - Independent merge challenge found no correctness defect. Residual limits: - Peak memory and performance across representative workloads were not benchmarked. - The earlier parallel remediation lifecycle remains incomplete and its evidence is preserved. --- Co-authored-by: Codex <[email protected]>
|
Great work on this PR—leveraging Looking through the implementation and the chunking behavior, I noticed a few high-impact memory and vectorization optimizations that could make this even faster and cleaner:
@SajjadPSavoji, are you planning to incorporate these optimizations and rebase on |
|
the chunk budgeting and the develop merge are already in from Borda's commits, so I'll leave it with the maintainers from here. the other two tweaks are probably better as a separate PR |
- perf(detection): keep float matmul unless packed IoU is needed - perf(detection): reduce packed detections once per mask IoU call - fix(detection): warn on packed buffers, not float size, for bool masks - fix(detection): count pack and pad buffers in the memory floor - fix(detection): validate mask ndim in packed non-max merge - refactor(detection): share one greedy mask merging driver - style(detection): align packed mask docstrings and constants [resolve group] PR roboflow#2886 — items 3 2 9 10 11 12 16 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- test(detection): cover packed mask gate and mixed dtype pairs - test(detection): test packed masks on random layouts and chunks [resolve group] PR roboflow#2886 — items 13 14 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
[resolve group] PR roboflow#2886 — items 15 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Changes: - Merge the current develop branch and preserve packed-mask tests in the reorganized IoU test module. Impact: - The PR retains its packed-mask behavior alongside the current base and test organization. Verification: - IoU and suppression tests: 350 passed. - Staged merge diff check passed. Residual limits: - Full project checks and carried-forward remediation remain pending. --- Co-authored-by: Codex <[email protected]>
Changes: - Remove unused fallback branches from the private packed overlap helper and clarify its required inputs. - Test packed overlap parity and chunk sizing for non-contiguous true masks. Impact: - Packed overlap retains its behavior with fewer unreachable branches and a regression check for reshape-copy budgeting. Verification: - Full suite: 8546 passed, 301 skipped. - Configured Ruff, format, mypy, and scoped diff checks passed. - Independent review found no correctness defects and its test and documentation observations were addressed. Residual limits: - OpenCV was unavailable locally; the suite used the NumPy fallback. --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Codex <[email protected]>
b189596 to
a471de4
Compare

Description
Dense mask IoU and NMM cast the masks to float32 copies, which is 32x the size of the bits.
_group_overlapping_masksalso redoes that cast on every inner iteration. Now_mask_iou_batch_splitand a new_group_overlapping_packed_maskspack the masks withnp.packbits, view them asuint64and count withnp.bitwise_count. Counts are exact integers and go through the same IoU/IoS division, so results are identical.The packed path only runs on NumPy >= 2.0 with
boolmasks. Older NumPy and non-bool masks keep the original matmul andlogical_or.reducecode. Masks over 2**24 pixels still use float64 counts.CompactMaskis untouched.Motivation and Context
Fixes #2845. The approach is from @Connoravila1.
Changes Made
_pack_masks,_count_pixelsand_overlap_from_areashelpers iniou_and_nms.py_mask_iou_batch_split, skipping words that are empty in either set_group_overlapping_packed_maskskeeps the merge candidate packed between stepsOne thing I left as is: the intersection loop runs over the rows of the first set in Python and vectorizes across the second.
Apple M-series, NumPy 2.5.4,
OPENBLAS_NUM_THREADS=1, develop to this change:Testing
New
TestBitPackedMaskOverlapintests/detection/utils/test_iou_and_nms.py.test_mask_iou_batch_matches_fallbackchecks IoU and IoS against the matmul path (empty sets, all-False, all-True, pixel counts not a multiple of 8 or 64).test_mask_non_max_merge_matches_fallbackchecks NMM groups against the original loop. There are also tests for float masks in NMM, NMS keeps, Soft-NMS scores and exact counts above 2**24 pixels.I ran
pytest tests/detection/utils/test_iou_and_nms.py tests/detection/test_compact_mask_iou.pyplusruff checkandruff format --checkon the two touched files, all clean. A 400-case fuzz against develop also gavearray_equalIoU/IoS with the same dtype and identical NMM groups.docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
The IoU speedup will be smaller on many-core machines with a fast multithreaded BLAS. The memory drop and the NMM gain don't depend on BLAS.