Segmentation#
- kornia.metrics.confusion_matrix(pred, target, num_classes, normalized=False)[source]#
Compute confusion matrix to evaluate the accuracy of a classification.
- Convention:
predandtargetare class labels of the same shape, prediction first, inuint8or any signed integer dtype; 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: the result holds one \((K, K)\) float32 count matrix per sample, \((B, K, K)\), and is never pooled over the batch, 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.
Rows are the target and columns the prediction,
cm[b, target, pred]; swapping the two arguments transposes every count matrix. Losses and metrics maps the matrix andnormalizedonto scikit-learn.
- 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 must be that ofpred, and it must contain integer values between 0 and K-1.num_classes (
int) – total possible number of classes in target.normalized (
bool, optional) – whether to normalize each target row by its sum plus1e-6. Non-empty rows sum approximately to one; empty rows remain zero. Default:False
- Return type:
- 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 the Intersection-Over-Union (IoU) of every class in every sample.
The function internally computes the confusion matrix.
- Convention:
See the Convention block of
confusion_matrix(), which counts the labels: the same label contract, one result per sample.The result is the IoU of every class in every sample, \((B, K)\) float32, each a fraction in \([0, 1]\); despite the name, neither axis is averaged. Averaging it over \(B\) gives the mean of per-image IoUs; for the IoU of a whole dataset, sum
confusion_matrix()over all images and take the IoU of the sum.epsis added to the numerator and the denominator, so a class absent from both maps scores 1 (NaN witheps=0), and a class only predicted or only in the target scores about 0. Exclude absent classes before averaging over \(K\): a largernum_classescan only raise the average. Losses and metrics compares this absent-class rule with the losses,mean_average_precision()and scikit-learn.
- 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 must be that ofpred, and it must contain integer values between 0 and K-1.num_classes (
int) – total possible number of classes in target.eps (
float, optional) – the value added to the numerator and the denominator of every IoU. Default:1e-6
- Return type:
- Returns:
a tensor with the IoU of every class, 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.]])