perf(detection): vectorize box_iou_batch_with_jaccard#2359
Open
RubenHaisma wants to merge 1 commit into
Open
Conversation
`box_iou_batch_with_jaccard` computed COCO-style Jaccard IoU with a double Python `for` loop calling a scalar `_jaccard` helper once per (detection, ground-truth) pair — an O(N*M) per-element pattern in otherwise pure-NumPy code. It is the inner IoU of `COCOEvaluator._compute_iou`, called once per (image, category) during mAP evaluation, and is also public API (`sv.box_iou_batch_with_jaccard`). Replace the loop with a broadcasted NumPy implementation and drop the now unused scalar `_jaccard`. The far corners are built as `x2 = x + w` and the union is associated as `(area_det + area_gt - area_inter) + eps` so the result is bit-identical to the previous per-pair output (verified to `max|diff| = 0` over 4000 randomized trials including zero/negative-width degenerate boxes and crowd flags). Crowd semantics are preserved: a crowd ground truth uses the detection area as the union. Speedup scales with batch size — ~1.6x at 5x5, ~27x at 15x60, ~66x at 50x100 — and is faster even at the smallest sizes, so there is no regime where it regresses. End-to-end COCO mAP results are unchanged (the existing metrics suite passes without modification). Adds `TestBoxIouBatchWithJaccard`: parity against an independent per-pair reference across empty / single / busy / degenerate+crowd batches, the crowd union semantics, the empty-input contract, and the `is_crowd` length guard.
03f5695 to
10fef50
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. ❌ Your project check has failed because the head coverage (82%) is below the target coverage (95%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## develop #2359 +/- ##
=======================================
Coverage 82% 82%
=======================================
Files 68 68
Lines 9560 9556 -4
=======================================
- Hits 7881 7878 -3
+ Misses 1679 1678 -1 🚀 New features to boost your workflow:
|
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
box_iou_batch_with_jaccardcomputed COCO-style Jaccard IoU with a double Pythonforloop calling a scalar_jaccardhelper once per (detection, ground-truth) pair — an O(N·M) per-element pattern in otherwise pure-NumPy code, againstAGENTS.md's "vectorized throughout — no Python loops in hot paths" guideline. It is the inner IoU ofCOCOEvaluator._compute_iou(called once per(image, category)during mAP evaluation) and is also public API (sv.box_iou_batch_with_jaccard).This replaces the loop with a broadcasted NumPy implementation and drops the now-unused scalar
_jaccard.Correctness (bit-identical)
The far corners are built as
x2 = x + wand the union associated as(area_det + area_gt - area_inter) + eps, so the output is bit-identical to the previous per-pair result — verified tomax|diff| = 0.0over 4000 randomized trials including zero/negative-width degenerate boxes and crowd flags. Crowd semantics are preserved (a crowd ground truth uses the detection area as the union). End-to-end COCO mAP results are unchanged: the existingtests/metrics/suite passes without modification, and the docstring doctest output is unchanged.Performance
Speedup scales with batch size and is faster even at the smallest sizes (no regression regime):
Tests
Adds
TestBoxIouBatchWithJaccard: parity against an independent per-pair reference across empty / single / busy / degenerate+crowd batches, the crowd union semantics, the empty-input contract, and theis_crowdlength guard.ruff check/formatclean.