Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/supervision/metrics/mean_average_recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from copy import deepcopy
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, cast

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -376,7 +376,7 @@ def _compute(
stats.append(
(
np.zeros((0, iou_thresholds.size), dtype=bool),
np.zeros((0,), dtype=np.float32),
np.zeros((0,), dtype=int),
np.zeros((0,), dtype=int),
targets.class_id,
)
Expand Down Expand Up @@ -406,12 +406,18 @@ def _compute(
iou,
iou_thresholds,
)

sorted_indices = np.argsort(
-cast(npt.NDArray[np.float32], predictions.confidence)
)
stats.append(
(
matches,
predictions.confidence,
predictions.class_id,
targets.class_id,
matches[sorted_indices],
np.arange(len(predictions)),
cast(npt.NDArray[np.int32], predictions.class_id)[
sorted_indices
],
cast(npt.NDArray[np.int32], targets.class_id),
)
)

Expand Down Expand Up @@ -448,28 +454,24 @@ def _compute(
def _compute_average_recall_for_classes(
self,
matches: npt.NDArray[np.bool_],
prediction_confidence: npt.NDArray[np.float32],
prediction_indices: npt.NDArray[np.int32],
prediction_class_ids: npt.NDArray[np.int32],
true_class_ids: npt.NDArray[np.int32],
) -> tuple[
npt.NDArray[np.float64],
npt.NDArray[np.float64],
npt.NDArray[np.int32],
]:
sorted_indices = np.argsort(-prediction_confidence)
matches = matches[sorted_indices]
prediction_class_ids = prediction_class_ids[sorted_indices]
unique_classes, class_counts = np.unique(true_class_ids, return_counts=True)

recalls_at_k = []
for max_detections in self.max_detections:
# Shape: PxTh,P,C,C -> CxThx3
confusion_matrix = self._compute_confusion_matrix(
matches,
prediction_class_ids,
matches[prediction_indices < max_detections],
prediction_class_ids[prediction_indices < max_detections],
unique_classes,
class_counts,
max_detections=max_detections,
)

# Shape: CxThx3 -> CxTh
Expand Down Expand Up @@ -522,7 +524,6 @@ def _compute_confusion_matrix(
sorted_prediction_class_ids: npt.NDArray[np.int32],
unique_classes: npt.NDArray[np.int32],
class_counts: npt.NDArray[np.int32],
max_detections: int | None = None,
) -> npt.NDArray[np.float64]:
"""
Compute the confusion matrix for each class and IoU threshold.
Expand Down Expand Up @@ -567,7 +568,7 @@ class ids.
false_positives = np.full(num_thresholds, num_predictions)
false_negatives = np.zeros(num_thresholds)
else:
limited_matches = sorted_matches[is_class][slice(max_detections)]
limited_matches = sorted_matches[is_class]
true_positives = limited_matches.sum(0)

false_positives = (1 - limited_matches).sum(0)
Expand Down Expand Up @@ -641,8 +642,6 @@ def _make_empty_content(self) -> npt.NDArray[Any]:

raise ValueError(f"Invalid metric target: {self._metric_target}")

raise ValueError(f"Invalid metric target: {self._metric_target}")

def _filter_detections_by_size(
self, detections: Detections, size_category: ObjectSizeCategory
) -> Detections:
Expand Down
Loading
Loading