Skip to content

fix(metrics): use the COCO crowd IoU for oriented-box mAP - #2941

Merged
Borda merged 5 commits into
roboflow:developfrom
pmxlr8:fix/obb-map-crowd-iou
Nov 11, 2026
Merged

Borda merged 5 commits into
roboflow:developfrom
pmxlr8:fix/obb-map-crowd-iou

Conversation

@pmxlr8

@pmxlr8 pmxlr8 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Description

sv.metrics.MeanAveragePrecision with MetricTarget.ORIENTED_BOUNDING_BOXES uses the standard IoU for crowd targets (data["iscrowd"]). A code comment says crowd regions are not supported for oriented boxes. Boxes and masks use the COCO crowd IoU: the intersection over the prediction area. So a prediction inside a crowd region is ignored for boxes and masks, but it is a false positive for oriented boxes. The same data then gets a lower mAP as oriented boxes than as boxes.

Repro: axis-aligned oriented boxes, so the boxes and pycocotools give the expected value.

import numpy as np

import supervision as sv
from supervision.config import ORIENTED_BOX_COORDINATES
from supervision.metrics import MeanAveragePrecision, MetricTarget


def corners(xyxy):
    return np.array([[[a, b], [c, b], [c, d], [a, d]] for a, b, c, d in xyxy], dtype=float)


target_xyxy = [[100, 100, 140, 140], [0, 0, 64, 64]]
prediction_xyxy = [[8, 8, 40, 40], [40, 40, 72, 72], [100, 100, 140, 140]]
targets = sv.Detections(
    xyxy=np.array(target_xyxy, dtype=float),
    class_id=np.array([0, 0]),
    data={ORIENTED_BOX_COORDINATES: corners(target_xyxy), "iscrowd": np.array([0, 1])},
)
predictions = sv.Detections(
    xyxy=np.array(prediction_xyxy, dtype=float),
    class_id=np.array([0, 0, 0]),
    confidence=np.array([0.9, 0.85, 0.8]),
    data={ORIENTED_BOX_COORDINATES: corners(prediction_xyxy)},
)
for target in (MetricTarget.BOXES, MetricTarget.ORIENTED_BOUNDING_BOXES):
    result = MeanAveragePrecision(metric_target=target).update(predictions, targets).compute()
    print(target.value, round(result.map50_95, 4), round(result.map50, 4))
# develop:
# boxes 0.6 1.0
# obb 0.3333 0.3333
# this PR:
# boxes 0.6 1.0
# obb 0.6 1.0

pycocotools gives 0.6 and 1.0 for these boxes.

Motivation and Context

Crowd targets come from COCO-style data: sv.DetectionDataset.from_coco stores data["iscrowd"]. The same rule must hold for all metric targets, or oriented-box scores cannot be compared with box scores. MeanAverageRecall, Precision, Recall and F1Score already give the same scores for axis-aligned oriented boxes and for boxes when crowd targets are present.

I found this with a random-scene check: axis-aligned oriented boxes must score exactly like their boxes. There is no issue for it.

Changes Made

  • _oriented_box_iou_with_jaccard (new, private, in mean_average_precision.py): the oriented-box counterpart of _mask_iou_with_jaccard. For crowd targets it divides the exact intersection by the prediction area plus np.spacing(1), as box_iou_batch_with_jaccard does. It clips to [0, 1], as oriented_box_iou_batch does, because intersections use float32-rounded polygons. Other targets keep the oriented_box_iou_batch IoU.
  • _oriented_box_pair_intersections (new, private, in iou_and_nms.py): the per-pair intersection loop of oriented_box_iou_batch, moved out without changes. It returns the candidate pairs and their intersection areas, not a dense matrix, so NMS memory does not grow. oriented_box_iou_batch now calculates its IoU or IoS from these pairs.
  • Tests: the crowd IoU of one partial overlap (0.5 for crowd, 1/3 for standard), a pycocotools parity test for axis-aligned oriented boxes with a crowd target (all 6 AP values), and a rotated crowd target with a prediction inside it.
  • Changelog entry under Unreleased.

Testing

  • New tests on develop: 3 failed. With this change: 3 passed.

  • oriented_box_iou_batch output is bit-identical to develop (np.array_equal) on 1,600 matrices: IoU and IoS, self and cross comparison, float32, float64 and int64 corners, origins up to 1e6, 0 to 11 boxes per side.

  • Speed, 3,000 rotated boxes, best of 7 runs, 2 alternating rounds: self comparison 0.67 to 0.72 s, cross comparison 1.10 to 1.13 s. develop: 0.66 to 0.67 s and 1.11 to 1.21 s. No change beyond noise.

  • pytest (full suite and doctests, NumPy 2.5.4, NumPy backend without OpenCV): 6652 passed, 279 skipped.

  • pre-commit run on the changed files: all hooks pass, mypy included.

  • Random scenes, not added to the suite: 300 box scenes with 15% crowd targets, tied scores and sides near 32 and 96. For each scene, MeanAveragePrecision, MeanAverageRecall, Precision, Recall and F1Score ran on the boxes and on the same boxes as oriented boxes. On develop, mAP differs in 60 of 300 scenes, and 0 differ without crowd targets. With this change, 0 of 300 scenes differ for all 5 metrics.

  • Added/updated tests, and the full suite passes locally

  • Updated docs (docstrings / mkdocs entry) for new or changed public API (no API change)

  • Added a changelog entry in docs/changelog.md under Unreleased

Additional Notes

🤖 Generated with Claude Code

Oriented-box mAP used the standard IoU for crowd targets. Boxes and
masks divide the intersection by the prediction area, so a prediction
inside a crowd region is ignored there but was a false positive for
oriented boxes. Axis-aligned oriented boxes now score like their boxes.

The intersection loop of oriented_box_iou_batch moves into
_oriented_box_pair_intersections without changes, so the crowd path can
use the exact intersection. oriented_box_iou_batch output is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pmxlr8
pmxlr8 requested a review from SkalskiP as a code owner November 11, 2026 04:57
@Borda
Borda requested a balanced review from Copilot November 11, 2026 11:04
@Borda Borda added the bug Something isn't working label Nov 11, 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.

🟢 Approval recommended

No active review issues remain for the oriented-box crowd-handling update.

No open issues 🎉

What changed in this PR

This PR aligns oriented-box mean average precision with COCO crowd handling in Supervision. Predictions inside crowd targets are ignored rather than counted as false positives, matching box and mask behavior.

Changes:

  • Use intersection over prediction area for oriented-box crowd targets while preserving standard IoU for other targets.
  • Extract shared oriented-box intersection logic for reuse by the metric.
  • Add crowd-overlap, rotated-box, and pycocotools parity tests, plus a changelog entry.
File Description
tests/​metrics/​test_mean_average_precision.py Adds three regression tests for oriented-box crowd handling.
src/​supervision/​metrics/​mean_average_precision.py Applies COCO crowd IoU to oriented-box evaluation.
src/​supervision/​detection/​utils/​iou_and_nms.py Extracts pairwise intersection computation for reuse.
docs/​changelog.md Documents the crowd-handling correction and links the PR.

Review effort: Balanced

Borda and others added 3 commits November 11, 2026 16:30
Changes:
- Integrate target updates for mAP area buckets, OpenCV fallback arithmetic, CSV column validation, and their tests.
- Preserve both target and oriented-box crowd changelog entries.

Impact:
- Retains the PR’s crowd matching while incorporating current target behavior and tests.

Verification:
- mAP test module: 127 passed using the NumPy fallback.
- Merge index: no unresolved paths; git diff --cached --check passed.

Residual limits:
- OpenCV-specific tests were not run during merge integration.

---

Co-authored-by: Codex <[email protected]>
Changes:
- Divide oriented crowd intersections by positive detection areas without epsilon and guard zero-area detections.
- Add exact-threshold, tiny-area, and collinear zero-area regressions; clarify the changelog’s parity example.

Impact:
- Crowd detections at matching thresholds and very small scales no longer become false positives because of denominator perturbation.

Verification:
- Configured pre-commit hooks and targeted format and type checks passed.
- Full test suite: 6,669 passed, 281 skipped; 94% coverage.
- Independent closure challenge completed; 446 targeted metric tests passed.

Residual limits:
- OpenCV-dependent tests were skipped in the clean test environment.

---

Co-authored-by: Codex <[email protected]>
@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.30508% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95%. Comparing base (ba33330) to head (bda417a).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2941   +/-   ##
=======================================
- Coverage       95%     95%   -0%     
=======================================
  Files           83      83           
  Lines        14443   14461   +18     
=======================================
+ Hits         13688   13705   +17     
- Misses         755     756    +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda merged commit f2c5758 into roboflow:develop Nov 11, 2026
40 checks passed
Borda added a commit that referenced this pull request Nov 11, 2026
Oriented-box mAP used the standard IoU for crowd targets. Boxes and
masks divide the intersection by the prediction area, so a prediction
inside a crowd region is ignored there but was a false positive for
oriented boxes. Axis-aligned oriented boxes now score like their boxes.

The intersection loop of oriented_box_iou_batch moves into
_oriented_box_pair_intersections without changes, so the crowd path can
use the exact intersection. oriented_box_iou_batch output is unchanged.

* docs(changelog): note oriented-box mAP crowd fix
* fix(metrics): preserve oriented crowd overlap thresholds

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Codex <[email protected]>
avanish-garg added a commit to avanish-garg/supervision that referenced this pull request Nov 13, 2026
A zero-dimensional float32 array skipped the NumPy-scalar branch and was
widened through float(), so np.array(0.7, dtype=np.float32) on 90 items
still gave 62 train items instead of 63. Unwrap it to its scalar first and
cover it with a regression case.

Also link the changelog entry to this PR and drop the roboflow#2941 entry that the
merge of develop re-added under Unreleased although it is already released
in 0.31.7.
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.

3 participants