Repository: Lightning-AI/torchmetrics

Issue:
Get confidence scores when using extended_summary with MeanAveragePrecision
### Discussed in https://github.com/Lightning-AI/torchmetrics/discussions/2201

<div type='discussions-op-text'>

<sup>Originally posted by **bradsteiner-sd** November  6, 2023</sup>
Hi, I am using the `extended_summary` flag with `MeanAveragePrecision()` and getting the precision values for each of the recall thresholds. However, I would like to know what confidence threshold is used at each of those recall points. In other words, how do I get the operating points for each location along the precision-recall curve? Is this possible?</div>


Discussion:
Hi! thanks for your contribution!, great first issue!

Resolving patch:
diff --git a/src/torchmetrics/detection/mean_ap.py b/src/torchmetrics/detection/mean_ap.py
--- a/src/torchmetrics/detection/mean_ap.py
+++ b/src/torchmetrics/detection/mean_ap.py
@@ -202,6 +202,9 @@ class MeanAveragePrecision(Metric):
                 - ``recall``: a tensor of shape ``(TxKxAxM)`` containing the recall values. Here ``T`` is the number of
                   IoU thresholds, ``K`` is the number of classes, ``A`` is the number of areas and ``M`` is the number
                   of max detections per image.
+                - ``scores``: a tensor of shape ``(TxRxKxAxM)`` containing the confidence scores.  Here ``T`` is the
+                  number of IoU thresholds, ``R`` is the number of recall thresholds, ``K`` is the number of classes,
+                  ``A`` is the number of areas and ``M`` is the number of max detections per image.
 
         average:
             Method for averaging scores over labels. Choose between "``"macro"`` and ``"micro"``.
@@ -531,6 +534,7 @@ def compute(self) -> dict:
                         ),
                         f"{prefix}precision": torch.tensor(coco_eval.eval["precision"]),
                         f"{prefix}recall": torch.tensor(coco_eval.eval["recall"]),
+                        f"{prefix}scores": torch.tensor(coco_eval.eval["scores"]),
                     }
                 result_dict.update(summary)
 


Test patch:
diff --git a/tests/unittests/detection/test_map.py b/tests/unittests/detection/test_map.py
--- a/tests/unittests/detection/test_map.py
+++ b/tests/unittests/detection/test_map.py
@@ -741,7 +741,7 @@ def test_warning_on_many_detections(self, iou_type, backend):
             metric.update(preds, targets)
 
     @pytest.mark.parametrize(
-        ("preds", "target", "expected_iou_len", "iou_keys", "precision_shape", "recall_shape"),
+        ("preds", "target", "expected_iou_len", "iou_keys", "precision_shape", "recall_shape", "scores_shape"),
         [
             (
                 [
@@ -758,6 +758,7 @@ def test_warning_on_many_detections(self, iou_type, backend):
                 [(0, 0)],
                 (10, 101, 1, 4, 3),
                 (10, 1, 4, 3),
+                (10, 101, 1, 4, 3),
             ),
             (
                 _inputs["preds"],
@@ -766,11 +767,12 @@ def test_warning_on_many_detections(self, iou_type, backend):
                 list(product([0, 1, 2, 3], [0, 1, 2, 3, 4, 49])),
                 (10, 101, 6, 4, 3),
                 (10, 6, 4, 3),
+                (10, 101, 6, 4, 3),
             ),
         ],
     )
     def test_for_extended_stats(
-        self, preds, target, expected_iou_len, iou_keys, precision_shape, recall_shape, backend
+        self, preds, target, expected_iou_len, iou_keys, precision_shape, recall_shape, scores_shape, backend
     ):
         """Test that extended stats are computed correctly."""
         metric = MeanAveragePrecision(extended_summary=True, backend=backend)
@@ -793,6 +795,10 @@ def test_for_extended_stats(
         assert isinstance(recall, Tensor)
         assert recall.shape == recall_shape
 
+        scores = result["scores"]
+        assert isinstance(scores, Tensor)
+        assert scores.shape == scores_shape
+
     @pytest.mark.parametrize("class_metrics", [False, True])
     def test_average_argument(self, class_metrics, backend):
         """Test that average argument works.