π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(metrics): compute mean_average_precision recall per class (#5079)
Chi-Wei Lee7ad14c46d766
diff --git a/changelog.d/5079.breaking.md b/changelog.d/5079.breaking.mdnew file mode 100644index 000000000..f693cd0e2--- /dev/null+++ b/changelog.d/5079.breaking.md@@ -0,0 +1,5 @@+`mean_average_precision` now computes each class's recall over the ground-truth objects of that class, as the+reference implementation it is adapted from does. It used to divide by the number of ground-truth objects of every+class, so when the ground truth held more than one label no class could reach recall 1 and the per-class AP and the+mAP came out too low: two objects of different classes, each detected exactly, scored 0.5455 instead of 1. Inputs+whose ground-truth objects all share one label give the same values as before.diff --git a/kornia/metrics/mean_average_precision.py b/kornia/metrics/mean_average_precision.pyindex d75a8c9ba..d37d8e5be 100644--- a/kornia/metrics/mean_average_precision.py+++ b/kornia/metrics/mean_average_precision.py@@ -159,7 +159,7 @@ def mean_average_precision(cumul_precision = cumul_gt_positives / (cumul_gt_positives + cumul_false_positives + 1e-10) # (n_class_detections)- cumul_recall = cumul_gt_positives / _gt_boxes.size(0) # (n_class_detections)+ 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)diff --git a/tests/metrics/test_map.py b/tests/metrics/test_map.pyindex df0cd72db..cbad615da 100644--- a/tests/metrics/test_map.py+++ b/tests/metrics/test_map.py@@ -37,6 +37,53 @@ class TestMeanAveragePrecision(BaseTester):self.assert_close(mean_ap[0], torch.tensor(1.0, device=device, dtype=dtype))self.assert_close(mean_ap[1][1], 1.0)+ def test_recall_per_class(self, device, dtype):+ # Two objects of different classes in one image, each detected exactly. Recall is taken+ # over the objects of the class being scored, so both classes reach recall 1 and AP 1.+ boxes = torch.tensor([[0.0, 0.0, 10.0, 10.0], [20.0, 20.0, 30.0, 30.0]], device=device, dtype=dtype)+ labels = torch.tensor([1, 2], device=device, dtype=torch.long)+ scores = torch.tensor([0.9, 0.8], device=device, dtype=dtype)++ mean_ap, ap = kornia.metrics.mean_average_precision([boxes], [labels], [scores], [boxes], [labels], 3)++ self.assert_close(mean_ap, torch.tensor(1.0, device=device, dtype=dtype))+ self.assert_close(ap[1], 1.0)+ self.assert_close(ap[2], 1.0)++ def test_recall_per_class_over_images(self, device, dtype):+ # Two images. Class 1 has 3 objects and 2 exact detections: recall 1/3, 2/3 at precision 1, so 7 of the 11+ # recall thresholds (0 to 0.6) are reached and AP = 7/11. Class 2 has 2 objects, the higher-scored detection+ # is a false positive and the other is exact: recall 0, 1/2 at precision 0, 1/2, so AP = 6 * 0.5 / 11. Class 3+ # is predicted but has no objects: every detection is a false positive and AP = 0. The recall denominators+ # (3, 2, 0) differ from the detection counts (2, 2, 1), from the detected counts and from the total (5).+ gt_boxes = [+ torch.tensor([[0.0, 0.0, 10.0, 10.0], [20.0, 20.0, 30.0, 30.0], [40.0, 40.0, 50.0, 50.0]]),+ torch.tensor([[0.0, 0.0, 10.0, 10.0], [20.0, 20.0, 30.0, 30.0]]),+ ]+ gt_labels = [torch.tensor([1, 1, 2]), torch.tensor([1, 2])]+ boxes = [+ torch.tensor([[0.0, 0.0, 10.0, 10.0], [40.0, 40.0, 50.0, 50.0]]),+ torch.tensor([[0.0, 0.0, 10.0, 10.0], [60.0, 60.0, 70.0, 70.0], [80.0, 80.0, 90.0, 90.0]]),+ ]+ labels = [torch.tensor([1, 2]), torch.tensor([1, 2, 3])]+ scores = [torch.tensor([0.9, 0.8]), torch.tensor([0.7, 0.95, 0.6])]++ def to(tensors, dtype):+ return [t.to(device=device, dtype=dtype) for t in tensors]++ mean_ap, ap = kornia.metrics.mean_average_precision(+ to(boxes, dtype),+ to(labels, torch.long),+ to(scores, dtype),+ to(gt_boxes, dtype),+ to(gt_labels, torch.long),+ 4,+ )++ expected = torch.tensor([7 / 11, 3 / 11, 0.0], device=device, dtype=dtype)+ self.assert_close(torch.tensor([ap[1], ap[2], ap[3]], device=device, dtype=dtype), expected)+ self.assert_close(mean_ap, expected.mean())+def test_raise(self, device, dtype):boxes = torch.tensor([[100, 50, 150, 100.0]], device=device, dtype=dtype)labels = torch.tensor([1], device=device, dtype=torch.long)