Segmentation#

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

Count predicted against target class labels, one confusion matrix per sample.

cm = confusion_matrix(pred, target, num_classes) takes two integer label tensors of shape (B, *) and returns float32 matrices with shape (B, K, K), where cm[b, target, pred] counts the positions of sample b with that target and that prediction.

Convention:
  • pred and target have the same shape, with an integer dtype: uint8 or any signed type; a bool tensor counts as the labels 0 and 1. A floating-point label tensor raises, and so does a label outside \([0, K)\), unless graph capture traces the range check (torch.export, torch.compile(fullgraph=True)), which skips it: an out-of-range label is then counted in another cell or makes the call fail.

  • The first axis is always the batch and the result is never pooled over it, so a flat (N,) label vector gives \(N\) matrices that count one label each. Sum over the first axis for the matrix of a whole batch or dataset.

  • Swapping the two arguments transposes every count matrix, but not a normalized one. Losses and metrics maps the matrix and normalized onto scikit-learn.

Parameters:
  • pred (Tensor) – predicted class labels in \([0, K)\), with shape (B, *).

  • target (Tensor) – ground-truth class labels in \([0, K)\), with the shape of pred.

  • num_classes (int) – the number of classes \(K\), at least 2.

  • normalized (bool, optional) – whether to divide each target row by its sum plus 1e-6. Non-empty rows then sum to approximately one; empty rows stay zero. Default: False

Returns:

counts, or row fractions with normalized=True.

Return type:

the matrices with shape (B, K, K)

See also

  • mean_iou(): the per-class IoU computed from this matrix.

  • accuracy(): top-k accuracy from class scores, not labels.

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]#

Compute the IoU of every class in every sample from label maps.

iou = mean_iou(pred, target, num_classes) takes class labels of shape (B, *) and returns float32 IoUs in \([0, 1]\) with shape (B, K). Despite the name, nothing is averaged.

Convention:
  • The labels follow confusion_matrix(), whose counts the IoU is computed from. To average, take the mean over \(B\) for the mean of per-image IoUs; for the IoU of a whole dataset, sum the confusion matrices over all images and take the IoU of the sum.

  • eps is added to the numerator and the denominator, so a class absent from both maps scores 1 (NaN with eps=0), and a class only predicted or only in the target scores about 0. Exclude absent classes before averaging over \(K\): a larger num_classes can only raise the average. Losses and metrics compares this absent-class rule with the losses, mean_average_precision() and scikit-learn.

Parameters:
  • pred (Tensor) – predicted class labels in \([0, K)\), with shape (B, *).

  • target (Tensor) – ground-truth class labels in \([0, K)\), with the shape of pred.

  • num_classes (int) – the number of classes \(K\), at least 2.

  • eps (float, optional) – the value added to the numerator and the denominator of every IoU. Default: 1e-6

Return type:

Tensor

Returns:

the IoU of every class, with shape (B, K).

See also

Example

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