Semantic segmentation#

Functions#

kornia.losses.one_hot(labels, num_classes, device, dtype, eps=0.0)[source]#

Convert an integer label x-D torch.Tensor to a one-hot (x+1)-D torch.Tensor.

Convention:
  • int64 labels of shape (N, *) become (N, C, *): the class axis is dim 1, where one_hot() puts it last. With the default eps=0.0 the result is F.one_hot(labels, num_classes).movedim(-1, 1).to(device, dtype). Labels outside [0, num_classes) are not validated by kornia; the result for them is undefined.

Parameters:
  • labels (Tensor) – torch.Tensor with labels of shape \((N, *)\), where N is batch size. Each value is an integer representing correct classification.

  • num_classes (int) – number of classes in labels.

  • device (device) – the desired device of returned torch.Tensor.

  • dtype (dtype) – the desired data type of returned torch.Tensor.

  • eps (float, optional) – optional smoothing floor. The default 0 returns the exact one-hot. A non-zero value replaces the zeros with eps and keeps the ones, which needs a floating point dtype. Default: 0.0

Return type:

Tensor

Returns:

the labels in one hot torch.Tensor of shape \((N, C, *)\),

Examples

>>> labels = torch.LongTensor([[[0, 1], [2, 0]]])
>>> one_hot(labels, num_classes=3, device=torch.device('cpu'), dtype=torch.float32)
tensor([[[[1., 0.],
          [0., 1.]],

         [[0., 1.],
          [0., 0.]],

         [[0., 0.],
          [1., 0.]]]])
kornia.losses.binary_focal_loss_with_logits(pred, target, alpha=0.25, gamma=2.0, reduction='none', pos_weight=None, weight=None, ignore_index=-100)[source]#

Criterion that computes Binary Focal loss.

According to [LGG+18], the Focal loss is computed as follows:

\[\text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)\]
Where, for a target of 0 or 1:
  • \(p_t\) is the sigmoid probability of the target value.

  • \(\alpha_t\) is \(\alpha\) for a target of 1 and \(1 - \alpha\) for a target of 0.

Convention:
  • Every element of pred is an independent binary logit: pred is (B, C, *) with at least two dimensions and target has its shape, with values in [0, 1]. The default reduction='none' returns that shape; 'mean' and 'sum' reduce it as in focal_loss().

  • alpha (default 0.25) weights the positive term and \(1 - \alpha\) the negative term. A fractional target \(t\) weights the two terms by \(t\) and \(1 - t\): \(t \alpha (1 - p)^\gamma (-\log p) + (1 - t) (1 - \alpha) p^\gamma (-\log (1 - p))\), where \(p\) is the sigmoid of the logit.

  • pos_weight and weight are (C,) vectors applied along dim 1: pos_weight[c] scales the positive term of channel c and weight[c] its whole loss.

  • A target entry equal to ignore_index (default -100) contributes 0 and still counts in the 'mean' denominator, as in focal_loss().

  • Losses and metrics maps this loss onto torchvision’s sigmoid_focal_loss and torch’s pos_weight.

Parameters:
  • pred (Tensor) – logits torch.Tensor with shape \((N, C, *)\) where C = number of classes.

  • target (Tensor) – labels torch.Tensor with the same shape as pred \((N, C, *)\) where each value is between 0 and 1.

  • alpha (Optional[float], optional) – Weighting factor \(\alpha \in [0, 1]\) of the positive term; the negative term is weighted by \(1 - \alpha\). Default: 0.25

  • gamma (float, optional) – Focusing parameter \(\gamma >= 0\). Default: 2.0

  • reduction (str, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Default: "none"

  • pos_weight (Optional[Tensor], optional) – a weight of the positive term of each channel, with shape \((num\_of\_classes,)\). Default: None

  • weight (Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default: None

  • ignore_index (Optional[int], optional) – target entries with this value contribute 0 to the loss but stay in the 'mean' denominator. Default: -100

Return type:

Tensor

Returns:

the computed loss.

Examples

>>> C = 3  # num_classes
>>> pred = torch.randn(1, C, 5, requires_grad=True)
>>> target = torch.randint(2, (1, C, 5))
>>> kwargs = {"alpha": 0.25, "gamma": 2.0, "reduction": 'mean'}
>>> output = binary_focal_loss_with_logits(pred, target, **kwargs)
>>> output.backward()
kornia.losses.focal_loss(pred, target, alpha, gamma=2.0, reduction='none', weight=None, ignore_index=-100)[source]#

Criterion that computes Focal loss.

According to [LGG+18], the Focal loss is computed as follows:

\[\text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)\]
Where:
  • \(p_t\) is the softmax probability of the target class.

  • \(\alpha_t\) is \(1 - \alpha\) for class 0 and \(\alpha\) for every other class.

Convention:
  • pred holds logits (B, C, *) and the softmax over dim 1 is taken inside. target holds int64 class indices (B, *) in [0, C); its batch and spatial sizes must equal those of pred.

  • Class 0 is the background: alpha weights it by \(1 - \alpha\) and classes \(1, \dots, C - 1\) by \(\alpha\), so a relabelling that moves class 0 changes the loss. alpha=None weights every class by 1. With C = 2 the target slice equals binary_focal_loss_with_logits() of the logit difference pred[:, 1:] - pred[:, :1] and the target target[:, None], with the same alpha and gamma: class 1 is the positive class.

  • The default reduction='none' returns a (B, C, *) map whose target slice holds the focal term above and whose other slices are 0, also where their log-probability overflows to -inf. 'mean' divides its sum by every element of that map, C times the pixel count, so with gamma=0 and alpha=None it is the mean cross entropy divided by C.

  • weight multiplies the slice of class c by weight[c]; 'mean' is not normalised by the weights.

  • A pixel labelled ignore_index (default -100) is 0 in every slice and still counts in the 'mean' denominator, where dice_loss() and tversky_loss() drop it. Losses and metrics ports cross_entropy() to this loss.

  • alpha outside [0, 1] and a negative gamma are not validated.

  • On the CPU, 'mean' and 'sum' without weight, with a finite gamma >= 0 and alpha None or a number in [0, 1], of float32 or float64 logits with at least \(2^{19}\) values compute the target-class log-probability with a custom autograd function, a batch chunk at a time, instead of a full-size log_softmax, unless torch.export or torch.jit.trace captures the call, a functorch transform or forward-mode AD runs, or autocast is on. It agrees with log_softmax to rounding, with the same NaN and infinity positions and a zero gradient at ignored pixels, and supports double backward.

Parameters:
  • pred (Tensor) – logits torch.Tensor with shape \((N, C, *)\) where C = number of classes.

  • target (Tensor) – labels torch.Tensor with shape \((N, *)\) where each value is an integer representing correct classification \(target[i] \in [0, C)\).

  • alpha (Optional[float]) – Weighting factor \(\alpha \in [0, 1]\) of classes \(1, \dots, C - 1\); class 0 is weighted by \(1 - \alpha\). None weights every class by 1.

  • gamma (float, optional) – Focusing parameter \(\gamma >= 0\). Default: 2.0

  • reduction (str, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Default: "none"

  • weight (Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default: None

  • ignore_index (Optional[int], optional) – labels with this value contribute 0 to the loss but stay in the 'mean' denominator. Default: -100

Return type:

Tensor

Returns:

the computed loss.

Example

>>> C = 5  # num_classes
>>> pred = torch.randn(1, C, 3, 5, requires_grad=True)
>>> target = torch.randint(C, (1, 3, 5))
>>> kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'}
>>> output = focal_loss(pred, target, **kwargs)
>>> output.backward()
kornia.losses.dice_loss(pred, target, average='micro', eps=1e-8, weight=None, ignore_index=-100)[source]#

Criterion that computes Sørensen-Dice Coefficient loss.

According to [1], we compute the Sørensen-Dice Coefficient as follows:

\[\text{Dice}(x, class) = \frac{2 |X \cap Y|}{|X| + |Y|}\]
Where:
  • \(X\) is the softmax of pred over the classes.

  • \(Y\) is the one-hot encoding of target by one_hot().

the loss, is finally computed as:

\[\text{loss}(x, class) = 1 - \text{Dice}(x, class)\]
Reference:

[1] https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient

Convention:
  • pred and target are those of focal_loss() (logits with the softmax taken inside, int64 class indices; see its Convention block), restricted to (B, C, H, W) and (B, H, W). A one-channel pred has a softmax of 1 everywhere and gives a constant loss with zero gradient: a binary task passes two channels.

  • 'micro' (default) computes one Dice per sample over all classes and pixels, weight scaling the terms of class c in both sums: \(1 - 2 \sum_c w_c |X_c \cap Y_c| / \sum_c w_c (|X_c| + |Y_c|)\). 'macro' computes one Dice per sample and class and averages them as \(\sum_c w_c \, \text{loss}_c / \sum_c w_c\) over the classes present in the sample’s non-ignored target: a class that is predicted but absent from the target is left out. Both then average over the batch and return a 0-d tensor; there is no reduction.

  • eps is added to the denominator only: \(1 - 2 |X \cap Y| / (|X| + |Y| + \epsilon)\).

  • Pixels labelled ignore_index (default -100) leave both sums of their own sample. A sample whose pixels are all ignored, or whose present classes all have weight 0, enters the batch mean as loss 1 with a zero gradient, at eps=0 as well.

  • On the CPU, float32 or float64 logits with at least \(2^{15}\) values and a weight that is None or of their dtype skip the one-hot target, unless torch.export or torch.jit.trace captures the call, a functorch transform runs or autocast is on: the intersection takes each pixel’s target-class probability, 'micro' sums the (weighted) probabilities of each pixel over the classes, and 'macro' takes the probability mass of each class from a batched matrix-vector product and adds the per-class sums into (B, C) bins, so no full-size product with a one-hot target is formed. The result agrees with the one-hot sums to rounding, with the same NaN positions; a label outside [0, C) that is not ignore_index raises the same RuntimeError as one_hot, also under torch.compile. Some torch releases (2.5.1 and 2.9.1 among them) report a torch.compile capture as an export: a compiled call keeps the one-hot target there, and its compiled one_hot does not check the labels.

Parameters:
  • pred (Tensor) – logits torch.Tensor with shape \((N, C, H, W)\) where C = number of classes.

  • target (Tensor) – labels torch.Tensor with shape \((N, H, W)\) where each value is in range \(0 ≤ targets[i] ≤ C-1\).

  • average (str, optional) –

    Reduction applied in multi-class scenario: Default: "micro"

    • 'micro' [default]: Calculate the loss across all classes.

    • 'macro': Average the class losses of each sample; see the Convention block.

  • eps (float, optional) – Scalar added to the denominator of the Dice ratio for numerical stability. Default: 1e-8

  • weight (Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\); see the Convention block for how each average applies them. Default: None

  • ignore_index (Optional[int], optional) – labels with this value are ignored in the loss computation. Default: -100

Return type:

Tensor

Returns:

One-element torch.Tensor of the computed loss.

Note

Spatial reductions use float32 for float16 and bfloat16 inputs to avoid overflow on large images. The returned loss retains the usual dtype promotion between the inputs and class weights.

Example

>>> N = 5  # num_classes
>>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = dice_loss(pred, target)
>>> output.backward()
kornia.losses.tversky_loss(pred, target, alpha, beta, eps=1e-8, ignore_index=-100)[source]#

Criterion that computes Tversky Coefficient loss.

According to [SEG17], we compute the Tversky Coefficient as follows:

\[\text{S}(P, G, \alpha; \beta) = \frac{|PG|}{|PG| + \alpha |P \setminus G| + \beta |G \setminus P|}\]
Where:
  • \(P\) and \(G\) are the softmax probabilities and exact one-hot targets for each class, restricted to non-ignored pixels.

  • \(\alpha\) and \(\beta\) control the magnitude of the penalties for FPs and FNs, respectively.

Note

  • \(\alpha = \beta = 0.5\) gives dice_loss() with average='macro' and twice the eps.

  • \(\alpha = \beta = 1\) corresponds to the per-class Tanimoto coefficient.

  • For \(\alpha + \beta = 1\) and \(\alpha > 0\), the unsmoothed per-class score is \(F_{\sqrt{\beta / \alpha}}\).

Convention:

pred, target and ignore_index behave as in dice_loss(): logits (B, C, H, W) with the softmax taken inside, int64 class indices (B, H, W) and ignored pixels removed from their own sample. The index is computed per sample and class, averaged over the classes present in the sample’s non-ignored target, then over the batch into a 0-d tensor; a sample whose pixels are all ignored enters the batch mean as loss 1. There is no weight, average or reduction.

Parameters:
  • pred (Tensor) – logits tensor with shape \((N, C, H, W)\) where C = number of classes.

  • target (Tensor) – labels tensor with shape \((N, H, W)\) where each value is in range \(0 ≤ targets[i] ≤ C-1\).

  • alpha (float) – the first coefficient in the denominator.

  • beta (float) – the second coefficient in the denominator.

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

  • ignore_index (Optional[int], optional) – labels with this value are ignored in the loss computation. Default: -100

Return type:

Tensor

Returns:

the computed loss.

Note

Softmax, spatial reductions and the ratio use float32 for float16 and bfloat16 inputs to avoid overflow in large-image sums and ratio gradients. The returned loss retains the input dtype.

Example

>>> N = 5  # num_classes
>>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = tversky_loss(pred, target, alpha=0.5, beta=0.5)
>>> output.backward()
kornia.losses.lovasz_hinge_loss(pred, target)[source]#

Criterion that computes a surrogate binary intersection-over-union (IoU) loss.

According to [2], we compute the IoU as follows:

\[\text{IoU}(x, class) = \frac{|X \cap Y|}{|X \cup Y|}\]

[1] approximates this formula with a surrogate, which is fully differentiable.

Where:
  • \(X\) is the foreground predicted by the single logit channel of pred.

  • \(Y\) is the foreground of the binary target.

the Jaccard loss is

\[\Delta_J(x, class) = 1 - \text{IoU}(x, class)\]

and the loss is its Lovász extension [1], evaluated at the hinge errors; see the Convention block.

Reference:

[1] http://proceedings.mlr.press/v37/yub15.pdf [2] https://arxiv.org/pdf/1705.08790.pdf

Note

This loss function only supports binary labels. For multi-class labels please use the Lovasz-Softmax loss.

Convention:
  • pred holds one logit channel (B, 1, H, W) and target the labels (B, H, W), 1 for the foreground and 0 for the background. The batch and spatial sizes must match. The loss is computed per image and averaged over the batch into a 0-d tensor; there is no reduction.

  • The loss is the Lovász extension of the Jaccard loss at the hinge errors \(\max(0, 1 - z (2t - 1))\) of logit \(z\) and label \(t\), not \(1 - \text{IoU}\): logits of \(\pm 1\) that are positive on the predicted foreground give \(2 (1 - \text{IoU})\) of that prediction, and correct logits of magnitude at least 1 give 0.

  • target is not validated and there is no ignore_index: a label of 2 is accepted, and -100, the ignore_index of focal_loss() and dice_loss(), can make the loss negative.

Parameters:
  • pred (Tensor) – logits tensor with shape \((N, 1, H, W)\).

  • target (Tensor) – labels tensor with shape \((N, H, W)\) with binary values.

Return type:

Tensor

Returns:

a scalar with the computed loss, in the dtype of a floating-point pred and in float32 for an integer or bool pred. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16 pred.

Example

>>> N = 1  # num_classes
>>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = lovasz_hinge_loss(pred, target)
>>> output.backward()
kornia.losses.lovasz_softmax_loss(pred, target, weight=None)[source]#

Criterion that computes a surrogate multi-class intersection-over-union (IoU) loss.

According to [1], we compute the IoU as follows:

\[\text{IoU}(x, class) = \frac{|X \cap Y|}{|X \cup Y|}\]

[1] approximates this formula with a surrogate, which is fully differentiable.

Where:
  • \(X\) is the softmax of pred over the classes.

  • \(Y\) is the one-hot encoding of the integer class labels in target.

the Jaccard loss of each class is

\[\Delta_J(x, class) = 1 - \text{IoU}(x, class)\]

and the loss is the mean over the classes of its Lovász extension, evaluated at the errors \(|X - Y|\).

Reference:

[1] https://arxiv.org/pdf/1705.08790.pdf

Note

This loss function only supports multi-class (C > 1) labels. For binary labels please use the Lovasz-Hinge loss.

Convention:
  • pred holds logits (B, C, H, W) with C >= 2 and the softmax over dim 1 is taken inside; target holds class labels (B, H, W) in [0, C) with the spatial size of pred. The batch sizes match, or pred holds one image, which is scored against every target. There is no ignore_index and the labels are not validated: a label of -100 is not ignored, and the result for it is undefined.

  • Each image is scored on its own and the losses are averaged over the batch into a 0-d tensor; there is no reduction. An image’s loss is the mean over all C classes: a class absent from the image enters it with that image’s largest probability for the class.

  • weight multiplies the term of each class and the mean still divides by C, so a uniform weight of 2 doubles the loss, where dice_loss(average='macro') divides by the summed weights of the classes it averages.

Parameters:
  • pred (Tensor) – logits tensor with shape \((N, C, H, W)\) where C = number of classes > 1.

  • target (Tensor) – labels tensor with shape \((N, H, W)\) where each value is in range \(0 ≤ targets[i] ≤ C-1\).

  • weight (Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default: None

Return type:

Tensor

Returns:

a scalar with the computed loss, in the dtype of pred promoted with the dtype of weight. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16 pred.

Example

>>> N = 5  # num_classes
>>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = lovasz_softmax_loss(pred, target)
>>> output.backward()

Modules#

class kornia.losses.BinaryFocalLossWithLogits(alpha, gamma=2.0, reduction='none', pos_weight=None, weight=None, ignore_index=-100)[source]#

Criterion that computes Focal loss.

According to [LGG+18], the Focal loss is computed as follows:

\[\text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)\]
where, for a target of 0 or 1:
  • \(p_t\) is the sigmoid probability of the target value.

  • \(\alpha_t\) is \(\alpha\) for a target of 1 and \(1 - \alpha\) for a target of 0.

Convention:

See the Convention block of binary_focal_loss_with_logits(). Unlike the function, alpha has no default here.

Parameters:
  • alpha (Optional[float]) – Weighting factor \(\alpha \in [0, 1]\) of the positive term; the negative term is weighted by \(1 - \alpha\).

  • gamma (float, optional) – Focusing parameter \(\gamma >= 0\). Default: 2.0

  • reduction (str, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Default: "none"

  • pos_weight (Optional[Tensor], optional) – a weight of the positive term of each channel, with shape \((num\_of\_classes,)\). Default: None

  • weight (Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default: None

  • ignore_index (Optional[int], optional) – target entries with this value contribute 0 to the loss but stay in the 'mean' denominator. Default: -100

Shape:
  • Pred: \((N, C, *)\) where C = number of classes.

  • Target: the same shape as Pred \((N, C, *)\) where each value is between 0 and 1.

Examples

>>> C = 3  # num_classes
>>> pred = torch.randn(1, C, 5, requires_grad=True)
>>> target = torch.randint(2, (1, C, 5))
>>> kwargs = {"alpha": 0.25, "gamma": 2.0, "reduction": 'mean'}
>>> criterion = BinaryFocalLossWithLogits(**kwargs)
>>> output = criterion(pred, target)
>>> output.backward()
class kornia.losses.DiceLoss(average='micro', eps=1e-8, weight=None, ignore_index=-100)[source]#

Criterion that computes Sørensen-Dice Coefficient loss.

Convention:

See the Convention block of dice_loss().

Parameters:
  • average (str, optional) – Reduction strategy for multi-class computation. Use “micro” to pool the classes of each sample, or “macro” to average over classes present in each sample’s non-ignored target. Default: "micro"

  • eps (float, optional) – Small constant added to the denominator for numerical stability. Default: 1e-8

  • weight (Optional[Tensor], optional) – Optional class-weight tensor of shape \((C,)\). Default: None

  • ignore_index (Optional[int], optional) – Label value to exclude from loss computation. Default: -100

Shapes:
  • pred: \((N, C, H, W)\) where C is the number of classes.

  • target: \((N, H, W)\) where each value is in the range \([0, C-1]\).

  • Output: scalar.

class kornia.losses.TverskyLoss(alpha, beta, eps=1e-8, ignore_index=-100)[source]#

Criterion that computes Tversky Coefficient loss.

According to [SEG17], we compute the Tversky Coefficient as follows:

\[\text{S}(P, G, \alpha; \beta) = \frac{|PG|}{|PG| + \alpha |P \setminus G| + \beta |G \setminus P|}\]
Where:
  • \(P\) and \(G\) are the softmax probabilities and exact one-hot targets for each class, restricted to non-ignored pixels.

  • \(\alpha\) and \(\beta\) control the magnitude of the penalties for FPs and FNs, respectively.

Note

  • \(\alpha = \beta = 0.5\) gives dice_loss() with average='macro' and twice the eps.

  • \(\alpha = \beta = 1\) corresponds to the per-class Tanimoto coefficient.

  • For \(\alpha + \beta = 1\) and \(\alpha > 0\), the unsmoothed per-class score is \(F_{\sqrt{\beta / \alpha}}\).

Convention:

See the Convention block of tversky_loss().

Parameters:
  • alpha (float) – the first coefficient in the denominator.

  • beta (float) – the second coefficient in the denominator.

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

  • ignore_index (Optional[int], optional) – labels with this value are ignored in the loss computation. Default: -100

Shape:
  • Pred: \((N, C, H, W)\) where C = number of classes.

  • Target: \((N, H, W)\) where each value is \(0 ≤ targets[i] ≤ C-1\).

Examples

>>> N = 5  # num_classes
>>> criterion = TverskyLoss(alpha=0.5, beta=0.5)
>>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = criterion(pred, target)
>>> output.backward()
class kornia.losses.FocalLoss(alpha, gamma=2.0, reduction='none', weight=None, ignore_index=-100)[source]#

Criterion that computes Focal loss.

According to [LGG+18], the Focal loss is computed as follows:

\[\text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)\]
Where:
  • \(p_t\) is the softmax probability of the target class.

  • \(\alpha_t\) is \(1 - \alpha\) for class 0 and \(\alpha\) for every other class.

Convention:

See the Convention block of focal_loss().

Parameters:
  • alpha (Optional[float]) – Weighting factor \(\alpha \in [0, 1]\) of classes \(1, \dots, C - 1\); class 0 is weighted by \(1 - \alpha\). None weights every class by 1.

  • gamma (float, optional) – Focusing parameter \(\gamma >= 0\). Default: 2.0

  • reduction (str, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Default: "none"

  • weight (Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default: None

  • ignore_index (Optional[int], optional) – labels with this value contribute 0 to the loss but stay in the 'mean' denominator. Default: -100

Shape:
  • Pred: \((N, C, *)\) where C = number of classes.

  • Target: \((N, *)\) where each value is an integer representing correct classification \(target[i] \in [0, C)\).

Example

>>> C = 5  # num_classes
>>> pred = torch.randn(1, C, 3, 5, requires_grad=True)
>>> target = torch.randint(C, (1, 3, 5))
>>> kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'}
>>> criterion = FocalLoss(**kwargs)
>>> output = criterion(pred, target)
>>> output.backward()
class kornia.losses.LovaszHingeLoss[source]#

Criterion that computes a surrogate binary intersection-over-union (IoU) loss.

According to [2], we compute the IoU as follows:

\[\text{IoU}(x, class) = \frac{|X \cap Y|}{|X \cup Y|}\]

[1] approximates this formula with a surrogate, which is fully differentiable.

Where:
  • \(X\) is the foreground predicted by the single logit channel of pred.

  • \(Y\) is the foreground of the binary target.

the Jaccard loss is

\[\Delta_J(x, class) = 1 - \text{IoU}(x, class)\]

and the loss is its Lovász extension [1], evaluated at the hinge errors.

Reference:

[1] http://proceedings.mlr.press/v37/yub15.pdf [2] https://arxiv.org/pdf/1705.08790.pdf

Note

This loss function only supports binary labels. For multi-class labels please use the Lovasz-Softmax loss.

Convention:

See the Convention block of lovasz_hinge_loss().

Parameters:
  • pred – logits tensor with shape \((N, 1, H, W)\).

  • target – labels tensor with shape \((N, H, W)\) with binary values.

Returns:

a scalar with the computed loss, in the dtype of a floating-point pred and in float32 for an integer or bool pred. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16 pred.

Example

>>> N = 1  # num_classes
>>> criterion = LovaszHingeLoss()
>>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = criterion(pred, target)
>>> output.backward()
class kornia.losses.LovaszSoftmaxLoss(weight=None)[source]#

Criterion that computes a surrogate multi-class intersection-over-union (IoU) loss.

According to [1], we compute the IoU as follows:

\[\text{IoU}(x, class) = \frac{|X \cap Y|}{|X \cup Y|}\]

[1] approximates this formula with a surrogate, which is fully differentiable.

Where:
  • \(X\) is the softmax of pred over the classes.

  • \(Y\) is the one-hot encoding of the integer class labels in target.

the Jaccard loss of each class is

\[\Delta_J(x, class) = 1 - \text{IoU}(x, class)\]

and the loss is the mean over the classes of its Lovász extension, evaluated at the errors \(|X - Y|\).

Reference:

[1] https://arxiv.org/pdf/1705.08790.pdf

Note

This loss function only supports multi-class (C > 1) labels. For binary labels please use the Lovasz-Hinge loss.

Convention:

See the Convention block of lovasz_softmax_loss().

Parameters:
  • pred – logits tensor with shape \((N, C, H, W)\) where C = number of classes > 1.

  • target – labels tensor with shape \((N, H, W)\) where each value is in range \(0 ≤ targets[i] ≤ C-1\).

  • weight (Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default: None

Returns:

a scalar with the computed loss, in the dtype of pred promoted with the dtype of weight. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16 pred.

Example

>>> N = 5  # num_classes
>>> criterion = LovaszSoftmaxLoss()
>>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = criterion(pred, target)
>>> output.backward()