Segmentation#

kornia.metrics.confusion_matrix(pred, target, num_classes, normalized=False)[source]#

Compute confusion matrix to evaluate the accuracy of a classification.

Parameters:
  • pred (Tensor) – tensor with estimated targets returned by a classifier. The shape can be \((B, *)\) and must contain integer values between 0 and K-1.

  • target (Tensor) – tensor with ground truth (correct) target values. The shape can be \((B, *)\) and must contain integer values between 0 and K-1, where targets are assumed to be provided as one-hot vectors.

  • num_classes (int) – total possible number of classes in target.

  • normalized (bool, optional) – whether to return the confusion matrix normalized. Default: False

Return type:

Tensor

Returns:

a tensor containing the confusion matrix with shape \((B, K, K)\) where K is the number of classes.

Example

>>> logits = torch.tensor([[0, 1, 0]])
>>> target = torch.tensor([[0, 1, 0]])
>>> confusion_matrix(logits, target, num_classes=3)
tensor([[[2., 0., 0.],
         [0., 1., 0.],
         [0., 0., 0.]]])
kornia.metrics.mean_iou(pred, target, num_classes, eps=1e-6)[source]#

Calculate mean Intersection-Over-Union (mIOU).

The function internally computes the confusion matrix.

Parameters:
  • pred (Tensor) – tensor with estimated targets returned by a classifier. The shape can be \((B, *)\) and must contain integer values between 0 and K-1.

  • target (Tensor) – tensor with ground truth (correct) target values. The shape can be \((B, *)\) and must contain integer values between 0 and K-1, where targets are assumed to be provided as one-hot vectors.

  • num_classes (int) – total possible number of classes in target.

  • eps (float, optional) – epsilon for numerical stability. Default: 1e-6

Return type:

Tensor

Returns:

a tensor representing the mean intersection-over union with shape \((B, K)\) where K is the number of classes.

Example

>>> logits = torch.tensor([[0, 1, 0]])
>>> target = torch.tensor([[0, 1, 0]])
>>> mean_iou(logits, target, num_classes=3)
tensor([[1., 1., 1.]])