Skip to content

Mean Average Precision

class MeanAveragePrecision

from_detections

def from_detections(cls, true_batches, detection_batches, num_classes, iou_threshold)
Calculate mean average precision (mAP) metric for selected iou_threshold based on true_batches and detection_batches.

Args
  • true_batches: List[np.ndarray] representing ground-truth objects across all images in concerned dataset. Each element of true_batches list describe single image and has shape = (N, 5) where N is number of ground-truth objects. Each row is expected to be in (x_min, y_min, x_max, y_max, class).

  • detection_batches: List[np.ndarray] representing detected objects across all images in concerned dataset. Each element of detection_batches list describe single image and has shape = (M, 6) where M is number of detected objects. Each row is expected to be in (x_min, y_min, x_max, y_max, class, conf).

  • num_classes: int number of classes detected by model.

  • iou_threshold: float detection iou threshold between 0 and 1. Detections with lower iou will be classified as FP.

Returns
  • mean_average_precision: MeanAveragePrecision object containing mAP value calculated for provided iou_threshold as well as AveragePrecision object calculated for each individual class.
Example usage
>>> import numpy as np

>>> from onemetric.cv.object_detection import MeanAveragePrecision

>>> true_batches = [
...     np.array([
...         [0.0, 0.0, 3.0, 3.0, 1],
...         [2.0, 2.0, 5.0, 5.0, 1],
...         [6.0, 1.0, 8.0, 3.0, 2],
...     ]),
...     np.array([
...         [1.0, 1.0, 2.0, 2.0, 2],
...     ]),
... ]

>>> detection_batches = [
...     np.array([
...         [0.0, 0.0, 3.0, 3.0, 1, 0.9],
...         [0.1, 0.1, 3.0, 3.0, 0, 0.9],
...         [6.0, 1.0, 8.0, 3.0, 1, 0.8],
...         [1.0, 6.0, 2.0, 7.0, 1, 0.8],
...     ]),
...     np.array([
...         [1.0, 1.0, 2.0, 2.0, 2, 0.8],
...     ]),
... ]

>>> mean_average_precision = MeanAveragePrecision.from_detections(
...     true_batches=true_batches,
...     detection_batches=detection_batches,
...     num_classes=3
... )

>>> mean_average_precision.value
... 0.4444444444444444

plot

def plot(target_path, title, class_names)
Create mean average precision plot and save it at selected location.

Args
  • target_path: str selected target location of confusion matrix plot.

  • title: Optional[str] title displayed at the top of the confusion matrix plot. Default None.

  • class_names: Optional[List[str]] list of class names detected my model. If non given class indexes will be used. Default None.