fix: tests and adjust comment

This commit is contained in:
Balthasar
2025-07-17 09:57:55 +02:00
parent 0ddbcb2010
commit 2c67cee720
2 changed files with 67 additions and 1 deletions
@@ -1237,7 +1237,7 @@ class MeanAveragePrecision(Metric):
"image_id": image_id,
"bbox": xywh,
"category_id": category_id,
"id": len(annotations) + 1, # incrementally increase the id
"id": len(annotations) + 1, # Start IDs from 1 (0 means no match)
}
annotations.append(dict_annotation)
# Category list
@@ -0,0 +1,66 @@
"""
Tests for Mean Average Precision ID=0 bug fix
"""
import numpy as np
import pytest
from supervision.detection.core import Detections
from supervision.metrics.mean_average_precision import MeanAveragePrecision
def test_single_perfect_detection():
"""Test that single perfect detection gets 1.0 mAP (not 0.0 due to ID=0 bug)"""
# Perfect detection (identical prediction and target)
detection = Detections(
xyxy=np.array([[10, 10, 50, 50]], dtype=np.float64),
class_id=np.array([0]),
confidence=np.array([0.9])
)
metric = MeanAveragePrecision()
metric.update([detection], [detection])
result = metric.compute()
# Should be perfect 1.0 mAP, not 0.0 due to ID=0 bug
assert abs(result.map50_95 - 1.0) < 1e-6
def test_multiple_perfect_detections():
"""Test that multiple perfect detections get 1.0 mAP"""
# Multiple perfect detections in one image
detections = Detections(
xyxy=np.array([
[10, 10, 50, 50],
[100, 100, 140, 140],
[200, 200, 240, 240]
], dtype=np.float64),
class_id=np.array([0, 0, 0]),
confidence=np.array([0.9, 0.9, 0.9])
)
metric = MeanAveragePrecision()
metric.update([detections], [detections])
result = metric.compute()
# Should be perfect 1.0 mAP
assert abs(result.map50_95 - 1.0) < 1e-6
def test_batch_updates_perfect_detections():
"""Test that batch updates with perfect detections get 1.0 mAP"""
# Single perfect detection for multiple batch updates
detection = Detections(
xyxy=np.array([[10, 10, 50, 50]], dtype=np.float64),
class_id=np.array([0]),
confidence=np.array([0.9])
)
metric = MeanAveragePrecision()
# Add 3 batch updates
metric.update([detection], [detection])
metric.update([detection], [detection])
metric.update([detection], [detection])
result = metric.compute()
# Should be perfect 1.0 mAP across all batches
assert abs(result.map50_95 - 1.0) < 1e-6