Skip to content

⚡️ Speed up method MeanAveragePrecision._detections_content by 14% #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all 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
27 changes: 14 additions & 13 deletions supervision/metrics/mean_average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,29 +366,30 @@ def _average_precisions_per_class(

def _detections_content(self, detections: Detections) -> np.ndarray:
"""Return boxes, masks or oriented bounding boxes from detections."""
if self._metric_target == MetricTarget.BOXES:
mt = self._metric_target # cache for speed
if mt is MetricTarget.BOXES:
return detections.xyxy
if self._metric_target == MetricTarget.MASKS:
elif mt is MetricTarget.MASKS:
mask_content = detections.mask
return (
detections.mask
if detections.mask is not None
else self._make_empty_content()
mask_content if mask_content is not None else self._make_empty_content()
)
if self._metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
elif mt is MetricTarget.ORIENTED_BOUNDING_BOXES:
obb = detections.data.get(ORIENTED_BOX_COORDINATES)
if obb is not None and len(obb) > 0:
return np.array(obb, dtype=np.float32)
if obb: # check if obb is not None and non-empty
return np.asarray(obb, dtype=np.float32)
return self._make_empty_content()
raise ValueError(f"Invalid metric target: {self._metric_target}")
raise ValueError(f"Invalid metric target: {mt}")

def _make_empty_content(self) -> np.ndarray:
if self._metric_target == MetricTarget.BOXES:
mt = self._metric_target # use local cached attribute
if mt is MetricTarget.BOXES:
return np.empty((0, 4), dtype=np.float32)
if self._metric_target == MetricTarget.MASKS:
elif mt is MetricTarget.MASKS:
return np.empty((0, 0, 0), dtype=bool)
if self._metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
elif mt is MetricTarget.ORIENTED_BOUNDING_BOXES:
return np.empty((0, 4, 2), dtype=np.float32)
raise ValueError(f"Invalid metric target: {self._metric_target}")
raise ValueError(f"Invalid metric target: {mt}")

def _filter_detections_by_size(
self, detections: Detections, size_category: ObjectSizeCategory
Expand Down