Mean-Average-Precision (mAP)¶
Module Interface¶
- class torchmetrics.detection.mean_ap.MeanAveragePrecision(box_format='xyxy', iou_thresholds=None, rec_thresholds=None, max_detection_thresholds=None, class_metrics=False, compute_on_step=None, **kwargs)[source]
Computes the Mean-Average-Precision (mAP) and Mean-Average-Recall (mAR) for object detection predictions. Optionally, the mAP and mAR values can be calculated per class.
Predicted boxes and targets have to be in Pascal VOC format (xmin-top left, ymin-top left, xmax-bottom right, ymax-bottom right). See the
update()method for more information about the input format to this metric.For an example on how to use this metric check the torchmetrics examples
Note
This metric is following the mAP implementation of pycocotools, a standard implementation for the mAP metric for object detection.
Note
This metric requires you to have torchvision version 0.8.0 or newer installed (with corresponding version 1.7.0 of torch or newer). Please install with
pip install torchvisionorpip install torchmetrics[detection].- Parameters
box_format¶ (
str) – Input format of given boxes. Supported formats are[`xyxy`, `xywh`, `cxcywh`].iou_thresholds¶ (
Optional[List[float]]) – IoU thresholds for evaluation. If set toNoneit corresponds to the stepped range[0.5,...,0.95]with step0.05. Else provide a list of floats.rec_thresholds¶ (
Optional[List[float]]) – Recall thresholds for evaluation. If set toNoneit corresponds to the stepped range[0,...,1]with step0.01. Else provide a list of floats.max_detection_thresholds¶ (
Optional[List[int]]) – Thresholds on max detections per image. If set to None will use thresholds[1, 10, 100]. Else, please provide a list of ints.class_metrics¶ (
bool) – Option to enable per-class metrics for mAP and mAR_100. Has a performance impact.compute_on_step¶ (
Optional[bool]) –Forward only calls
update()and returns None if this is set to False.Deprecated since version v0.8: Argument has no use anymore and will be removed v0.9.
kwargs¶ (
Dict[str,Any]) – Additional keyword arguments, see Advanced metric settings for more info.
Example
>>> import torch >>> from torchmetrics.detection.mean_ap import MeanAveragePrecision >>> preds = [ ... dict( ... boxes=torch.tensor([[258.0, 41.0, 606.0, 285.0]]), ... scores=torch.tensor([0.536]), ... labels=torch.tensor([0]), ... ) ... ] >>> target = [ ... dict( ... boxes=torch.tensor([[214.0, 41.0, 562.0, 285.0]]), ... labels=torch.tensor([0]), ... ) ... ] >>> metric = MeanAveragePrecision() >>> metric.update(preds, target) >>> from pprint import pprint >>> pprint(metric.compute()) {'map': tensor(0.6000), 'map_50': tensor(1.), 'map_75': tensor(1.), 'map_large': tensor(0.6000), 'map_medium': tensor(-1.), 'map_per_class': tensor(-1.), 'map_small': tensor(-1.), 'mar_1': tensor(0.6000), 'mar_10': tensor(0.6000), 'mar_100': tensor(0.6000), 'mar_100_per_class': tensor(-1.), 'mar_large': tensor(0.6000), 'mar_medium': tensor(-1.), 'mar_small': tensor(-1.)}
- Raises
ModuleNotFoundError – If
torchvisionis not installed or version installed is lower than 0.8.0ValueError – If
class_metricsis not a boolean
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- compute()[source]
Compute the Mean-Average-Precision (mAP) and Mean-Average-Recall (mAR) scores.
Note
mapscore is calculated with @[ IoU=self.iou_thresholds | area=all | max_dets=max_detection_thresholds ]Caution: If the initialization parameters are changed, dictionary keys for mAR can change as well. The default properties are also accessible via fields and will raise an
AttributeErrorif not available.- Return type
- Returns
dict containing
map:
torch.Tensormap_50:
torch.Tensormap_75:
torch.Tensormap_small:
torch.Tensormap_medium:
torch.Tensormap_large:
torch.Tensormar_1:
torch.Tensormar_10:
torch.Tensormar_100:
torch.Tensormar_small:
torch.Tensormar_medium:
torch.Tensormar_large:
torch.Tensormap_per_class:
torch.Tensor(-1 if class metrics are disabled)mar_100_per_class:
torch.Tensor(-1 if class metrics are disabled)
- update(preds, target)[source]
Add detections and ground truth to the metric.
- Parameters
preds¶ (
List[Dict[str,Tensor]]) –A list consisting of dictionaries each containing the key-values (each dictionary corresponds to a single image):
boxes:torch.FloatTensorof shape[num_boxes, 4]containingnum_boxesdetection boxes of the format specified in the constructor. By default, this method expects[xmin, ymin, xmax, ymax]in absolute image coordinates.scores:torch.FloatTensorof shape[num_boxes]containing detection scores for the boxes.labels:torch.IntTensorof shape[num_boxes]containing 0-indexed detection classes for the boxes.
target¶ (
List[Dict[str,Tensor]]) –A list consisting of dictionaries each containing the key-values (each dictionary corresponds to a single image):
boxes:torch.FloatTensorof shape[num_boxes, 4]containingnum_boxesground truth boxes of the format specified in the constructor. By default, this method expects[xmin, ymin, xmax, ymax]in absolute image coordinates.labels:torch.IntTensorof shape[num_boxes]containing 1-indexed ground truthclasses for the boxes.
- Raises
ValueError – If
predsis not of typeList[Dict[str, Tensor]]ValueError – If
targetis not of typeList[Dict[str, Tensor]]ValueError – If
predsandtargetare not of the same lengthValueError – If any of
preds.boxes,preds.scoresandpreds.labelsare not of the same lengthValueError – If any of
target.boxesandtarget.labelsare not of the same lengthValueError – If any box is not type float and of length 4
ValueError – If any class is not type int and of length 1
ValueError – If any score is not type float and of length 1
- Return type