Detection#

kornia.metrics.mean_average_precision(pred_boxes, pred_labels, pred_scores, gt_boxes, gt_labels, n_classes, threshold=0.5)[source]#

Calculate the Mean Average Precision (mAP) of detected objects.

Code altered from sgrvinod/a-PyTorch-Tutorial-to-Object-Detection. Background class (0 index) is excluded.

Parameters:
  • pred_boxes (List[Tensor]) – a torch.Tensor list of predicted bounding boxes.

  • pred_labels (List[Tensor]) – a torch.Tensor list of predicted labels.

  • pred_scores (List[Tensor]) – a torch.Tensor list of predicted labels’ scores.

  • gt_boxes (List[Tensor]) – a torch.Tensor list of ground truth bounding boxes.

  • gt_labels (List[Tensor]) – a torch.Tensor list of ground truth labels.

  • n_classes (int) – the number of classes.

  • threshold (float, optional) – count as a positive if the overlap is greater than the threshold. Default: 0.5

Return type:

Tuple[Tensor, Dict[int, float]]

Returns:

mean average precision (mAP), list of average precisions for each class.

Examples

>>> boxes, labels, scores = torch.tensor([[100, 50, 150, 100.]]), torch.tensor([1]), torch.tensor([.7])
>>> gt_boxes, gt_labels = torch.tensor([[100, 50, 150, 100.]]), torch.tensor([1])
>>> mean_average_precision([boxes], [labels], [scores], [gt_boxes], [gt_labels], 2)
(tensor(1.), {1: 1.0})
kornia.metrics.mean_iou_bbox(boxes_1, boxes_2, box_format='xyxy')[source]#

Compute the IoU of the cartesian product of two sets of boxes.

Parameters:
  • boxes_1 (Tensor) – a tensor of bounding boxes in \((B1, 4)\).

  • boxes_2 (Tensor) – a tensor of bounding boxes in \((B2, 4)\).

  • box_format (str, optional) – the bounding box format. Supported formats are: - ‘xyxy’: (x1, y1, x2, y2) where (x1, y1) is top-left and (x2, y2) is bottom-right - ‘xywh’: (x, y, w, h) where (x, y) is top-left, w is width, h is height - ‘cxcywh’: (cx, cy, w, h) where (cx, cy) is center, w is width, h is height Default: ‘xyxy’.

Return type:

Tensor

Returns:

a tensor in dimensions \((B1, B2)\), representing the intersection of each of the boxes in set 1 with respect to each of the boxes in set 2.

Example

>>> # XYXY format
>>> boxes_1 = torch.tensor([[40, 40, 60, 60], [30, 40, 50, 60]])
>>> boxes_2 = torch.tensor([[40, 50, 60, 70], [30, 40, 40, 50]])
>>> mean_iou_bbox(boxes_1, boxes_2)
tensor([[0.3333, 0.0000],
        [0.1429, 0.2500]])
>>> # XYWH format
>>> boxes_1_xywh = torch.tensor([[40, 40, 20, 20], [30, 40, 20, 20]])
>>> boxes_2_xywh = torch.tensor([[40, 50, 20, 20], [30, 40, 10, 10]])
>>> mean_iou_bbox(boxes_1_xywh, boxes_2_xywh, box_format='xywh')
tensor([[0.3333, 0.0000],
        [0.1429, 0.2500]])
>>> # CXCYWH format
>>> boxes_1_cxcywh = torch.tensor([[50, 50, 20, 20], [40, 50, 20, 20]])
>>> boxes_2_cxcywh = torch.tensor([[50, 60, 20, 20], [35, 45, 10, 10]])
>>> mean_iou_bbox(boxes_1_cxcywh, boxes_2_cxcywh, box_format='cxcywh')
tensor([[0.3333, 0.0000],
        [0.1429, 0.2500]])