From 2c67cee720b39cbc26d5b1143aedb572e889d493 Mon Sep 17 00:00:00 2001 From: Balthasar Date: Thu, 17 Jul 2025 09:57:55 +0200 Subject: [PATCH] fix: tests and adjust comment --- supervision/metrics/mean_average_precision.py | 2 +- test/metrics/test_mean_average_precision.py | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/metrics/test_mean_average_precision.py diff --git a/supervision/metrics/mean_average_precision.py b/supervision/metrics/mean_average_precision.py index e22eb322..f5fa3159 100644 --- a/supervision/metrics/mean_average_precision.py +++ b/supervision/metrics/mean_average_precision.py @@ -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 diff --git a/test/metrics/test_mean_average_precision.py b/test/metrics/test_mean_average_precision.py new file mode 100644 index 00000000..b3b68249 --- /dev/null +++ b/test/metrics/test_mean_average_precision.py @@ -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 \ No newline at end of file