Skip to content

fix(metrics): size mAP buckets with the areas pycocotools uses - #2939

Merged
Borda merged 3 commits into
roboflow:developfrom
pmxlr8:fix/map-size-buckets-match-pycocotools
Nov 11, 2026
Merged

Borda merged 3 commits into
roboflow:developfrom
pmxlr8:fix/map-size-buckets-match-pycocotools

Conversation

@pmxlr8

@pmxlr8 pmxlr8 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Description

sv.metrics.MeanAveragePrecision puts some objects in a different size bucket than pycocotools does. This changes the small, medium and large mAP. There are 2 causes:

  1. Prediction area metadata. A prediction with data["area"] uses that value as its area. pycocotools COCO.loadRes ignores the area of a result. It calculates the area again from the result's bbox or segmentation. An unmatched prediction counts as a false positive only in the buckets that contain its area, so the metadata moves false positives between buckets.
  2. Target areas in float32. Target data["area"] is cast to float32. float32 rounds an area within about 1e-4 of 32² or 96² onto the limit. The COCO area ranges include their limits, so the target is scored in 2 buckets. pycocotools keeps the area as a Python float.

Repro, with pycocotools through tests/metrics/coco_reference.py:

import numpy as np

import supervision as sv
from supervision.metrics import MeanAveragePrecision
from tests.metrics.coco_reference import evaluate_detections

# 1. A medium false positive whose area metadata says it is small.
targets = sv.Detections(xyxy=np.array([[0, 0, 20, 20]], dtype=float), class_id=np.array([0]))
predictions = sv.Detections(
    xyxy=np.array([[0, 0, 20, 20], [60, 60, 100, 100]], dtype=float),
    class_id=np.array([0, 0]),
    confidence=np.array([0.5, 0.9]),
    data={"area": np.array([400.0, 300.0])},
)
result = MeanAveragePrecision().update(predictions, targets).compute()
reference = evaluate_detections(predictions, targets)
print(result.small_objects.map50_95, reference.stats[3])
# develop: 0.5 0.9999999999999998
# this PR:  1.0 0.9999999999999998

# 2. A target area just above 96².
targets = sv.Detections(
    xyxy=np.array([[0, 0, 96, 96]], dtype=float),
    class_id=np.array([0]),
    data={"area": np.array([9216.0004])},
)
predictions = sv.Detections(
    xyxy=np.array([[0, 0, 96, 96]], dtype=float),
    class_id=np.array([0]),
    confidence=np.array([0.9]),
)
result = MeanAveragePrecision().update(predictions, targets).compute()
reference = evaluate_detections(predictions, targets)
print(result.medium_objects.map50_95, reference.stats[4])
# develop: 1.0 -1.0
# this PR:  -1  -1.0

Motivation and Context

Size-bucket mAP must match COCO, or the small, medium and large numbers cannot be compared with published results. Detections get data["area"] from COCO files: sv.DetectionDataset.from_coco(..., use_iscrowd=True) stores it. sv.metrics.MeanAverageRecall already reads target areas as float64 (get_detection_areas).

I found both causes with a random-scene comparison against pycocotools. There is no issue for them.

Changes Made

  • MeanAveragePrecision._prepare_predictions: a prediction is always sized by its box, mask or oriented box (_content_area). Prediction data["area"] is no longer read.
  • MeanAveragePrecision._prepare_targets: target data["area"] is read as float64.
  • tests/metrics/coco_reference.py: prediction data["area"] now goes to pycocotools in the result records. So the live package, not the helper, decides what to do with it.
  • Tests: 6 parity tests against pycocotools. Prediction area metadata with boxes and with masks. Target areas just below and just above 32² and 96².
  • Changelog entry under Unreleased.
  • Out of scope: EvaluationDataset.load_predictions still keeps an area key that a caller gives it. MeanAveragePrecision now always gives it the geometry area.

Testing

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

  • pytest tests/metrics: 756 passed (750 passed on develop).

  • pytest (full suite and doctests, NumPy 2.5.4, NumPy backend without OpenCV): 6655 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 and 300 mask scenes. Each scene has 1 to 3 images, 1 to 3 classes, sides near 32 and 96, 15% crowd targets and tied scores. Half of the target areas are moved to within 3e-4 of a limit, and every prediction has random data["area"]. On develop, 241 of the 300 box scenes differ from pycocotools. With this change, 0 box scenes and 0 mask scenes differ in any of the 6 AP values. MeanAverageRecall on the same scenes: 0 differences.

  • 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

  • The random-scene comparison could become a seeded test in the suite. I left it out to keep this PR small. I can add it in a follow-up if you want it.

🤖 Generated with Claude Code

Predictions with data["area"] used that value as their area.
pycocotools loadRes ignores result areas and calculates them from the
bbox or segmentation, so the metadata moved unmatched predictions
between size buckets.

Target data["area"] was cast to float32. An area within about 1e-4 of
32² or 96² rounded onto the limit, and the target was scored in two
buckets. Read it as float64, as pycocotools does.

The COCO reference helper now passes prediction area metadata to
pycocotools, so the live package decides how it is used.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pmxlr8
pmxlr8 requested a review from SkalskiP as a code owner November 11, 2026 03:33
@Borda
Borda requested a balanced review from Copilot November 11, 2026 11:01
@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 mAP size-bucket corrections.

No open issues 🎉

What changed in this PR

Aligns MeanAveragePrecision size buckets with pycocotools, correcting small, medium, and large mAP when prediction area metadata conflicts with geometry or target areas lie near bucket boundaries.

Changes:

  • Size predictions from their evaluated geometry, ignoring prediction area metadata.
  • Preserve target area metadata as float64 to avoid rounding across bucket boundaries.
  • Add six parity cases against pycocotools and document the fixes.
File Description
tests/​metrics/​test_mean_average_precision.py Adds box/mask metadata and target-boundary parity tests.
tests/​metrics/​coco_reference.py Passes prediction area metadata through so pycocotools determines its handling.
src/​supervision/​metrics/​mean_average_precision.py Uses geometry-derived prediction areas and float64 target metadata.
docs/​changelog.md Documents both size-bucket corrections under Unreleased.

Review effort: Balanced

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (2c4deb3) to head (89ab377).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2939   +/-   ##
=======================================
- Coverage       95%     95%   -0%     
=======================================
  Files           83      83           
  Lines        14445   14440    -5     
=======================================
- Hits         13690   13685    -5     
  Misses         755     755           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Borda
Borda previously approved these changes Nov 11, 2026
@Borda
Borda merged commit 64886e2 into roboflow:develop Nov 11, 2026
40 checks passed
Borda added a commit that referenced this pull request Nov 11, 2026
Predictions with data["area"] used that value as their area.
pycocotools loadRes ignores result areas and calculates them from the
bbox or segmentation, so the metadata moved unmatched predictions
between size buckets.

Target data["area"] was cast to float32. An area within about 1e-4 of
32² or 96² rounded onto the limit, and the target was scored in two
buckets. Read it as float64, as pycocotools does.

The COCO reference helper now passes prediction area metadata to
pycocotools, so the live package decides how it is used.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Jirka Borovec <[email protected]>
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