π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(metrics): use exact tenths as the mean_average_precision recall thresholds (#5101)
εΊζ’η2050e941395b
diff --git a/changelog.d/5101.breaking.md b/changelog.d/5101.breaking.mdnew file mode 100644index 000000000..ed6d388c1--- /dev/null+++ b/changelog.d/5101.breaking.md@@ -0,0 +1,8 @@+`mean_average_precision` now compares float64 recalls against the recall thresholds `i / 10`, so a recall that lands+exactly on a tenth reaches it. Previously the thresholds were the float32 values of `torch.arange(0, 1.1, 0.1)` widened+to Python floats, and `0.10000000149`, `0.20000000298`, `0.30000001192`, `0.40000000596`, `0.60000002384` and+`0.80000001192` lie above the tenths they stand for. A float64 recall of exactly 0.1, 0.2, 0.3, 0.4, 0.6 or 0.8 then+skipped that threshold, and the class AP and the mAP came out lower than in float32: 10 objects with detections ranked+TP, FP, then 9 TP scored 0.9174 (111 / 121) instead of 0.9256 (112 / 121). float64 now gives the float32 values.+float16, bfloat16 and float32 results are unchanged, because each threshold rounds to the same value in those dtypes+(#5083).diff --git a/kornia/metrics/mean_average_precision.py b/kornia/metrics/mean_average_precision.pyindex d37d8e5be..170dea209 100644--- a/kornia/metrics/mean_average_precision.py+++ b/kornia/metrics/mean_average_precision.py@@ -162,7 +162,9 @@ def mean_average_precision(cumul_recall = cumul_gt_positives / gt_class_images.size(0) # (n_class_detections)# Find the mean of the maximum of the precisions corresponding to recalls above the threshold 't'- recall_thresholds = torch.arange(start=0, end=1.1, step=0.1).tolist() # (11)+ # Exact tenths as Python floats: each comparison below casts them to the recall dtype. A float32 arange widened+ # to Python floats gives 0.10000000149..., which a float64 recall of exactly 1/10 never reaches (#5083).+ recall_thresholds = [i / 10 for i in range(11)] # (11)precisions = torch.zeros((len(recall_thresholds)), device=_gt_boxes.device, dtype=_gt_boxes.dtype) # (11)for i, t in enumerate(recall_thresholds):recalls_above_t = cumul_recall >= tdiff --git a/tests/metrics/test_map.py b/tests/metrics/test_map.pyindex cbad615da..a7ee58b44 100644--- a/tests/metrics/test_map.py+++ b/tests/metrics/test_map.py@@ -94,3 +94,38 @@ class TestMeanAveragePrecision(BaseTester):with pytest.raises(AssertionError):_ = kornia.metrics.mean_average_precision(boxes[0], [labels], [scores], [gt_boxes], [gt_labels], 2)++ def test_recall_on_an_exact_tenth_reaches_its_threshold_5083(self, device, dtype):+ # 10 objects, ranked detections TP, FP, then 9 TP: recall passes through every tenth, precision drops at the FP.+ gt_boxes = torch.tensor([[i * 20.0, 0.0, i * 20.0 + 10.0, 10.0] for i in range(10)], device=device, dtype=dtype)+ gt_labels = torch.ones(10, device=device, dtype=torch.long)+ boxes = torch.cat(+ [gt_boxes[:1], torch.tensor([[500.0, 500.0, 510.0, 510.0]], device=device, dtype=dtype), gt_boxes[1:]]+ )+ labels = torch.ones(11, device=device, dtype=torch.long)+ scores = torch.linspace(1.0, 0.5, 11, device=device, dtype=dtype)++ mean_ap, ap = kornia.metrics.mean_average_precision([boxes], [labels], [scores], [gt_boxes], [gt_labels], 2)++ # Precision 1 at the recall thresholds 0 and 0.1, then 10/11 at the nine others: (2 + 9 * 10 / 11) / 11+ expected = torch.tensor(112.0 / 121.0, device=device, dtype=dtype)+ self.assert_close(mean_ap, expected)+ self.assert_close(torch.tensor(ap[1], device=device, dtype=dtype), expected)++ def test_recall_on_every_tenth_reaches_its_threshold_5083(self, device, dtype):+ # 20 objects, ranked detections TP, then (FP, TP) 19 times: the j-th TP has recall j / 20 and precision+ # j / (2j - 1), above every later precision. Recall 2i / 20 is the first to reach the threshold i / 10, exactly,+ # so each of the 11 thresholds sets its own term, and a threshold it misses takes the next TP's lower precision.+ gt_boxes = torch.tensor([[i * 20.0, 0.0, i * 20.0 + 10.0, 10.0] for i in range(20)], device=device, dtype=dtype)+ gt_labels = torch.ones(20, device=device, dtype=torch.long)+ fp_box = torch.tensor([[500.0, 500.0, 510.0, 510.0]], device=device, dtype=dtype)+ boxes = torch.cat([gt_boxes[:1]] + [torch.cat([fp_box, gt_boxes[j : j + 1]]) for j in range(1, 20)])+ labels = torch.ones(39, device=device, dtype=torch.long)+ scores = torch.linspace(1.0, 0.5, 39, device=device, dtype=dtype)++ mean_ap, ap = kornia.metrics.mean_average_precision([boxes], [labels], [scores], [gt_boxes], [gt_labels], 2)++ # Precision 1 at the threshold 0, then 2i / (4i - 1) at the threshold i / 10.+ expected = torch.tensor((1.0 + sum(2 * i / (4 * i - 1) for i in range(1, 11))) / 11, device=device, dtype=dtype)+ self.assert_close(mean_ap, expected)+ self.assert_close(torch.tensor(ap[1], device=device, dtype=dtype), expected)