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
labelsof shape(N, *)become(N, C, *): the class axis is dim 1, whereone_hot()puts it last. With the defaulteps=0.0the result isF.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 withepsand keeps the ones, which needs a floating pointdtype. Default:0.0
- Return type:
- 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
predis an independent binary logit:predis(B, C, *)with at least two dimensions andtargethas its shape, with values in[0, 1]. The defaultreduction='none'returns that shape;'mean'and'sum'reduce it as infocal_loss().alpha(default0.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_weightandweightare(C,)vectors applied along dim 1:pos_weight[c]scales the positive term of channelcandweight[c]its whole loss.A target entry equal to
ignore_index(default-100) contributes 0 and still counts in the'mean'denominator, as infocal_loss().Losses and metrics maps this loss onto torchvision’s
sigmoid_focal_lossand torch’spos_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.25gamma (
float, optional) – Focusing parameter \(\gamma >= 0\). Default:2.0reduction (
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:Noneweight (
Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default:Noneignore_index (
Optional[int], optional) – target entries with this value contribute 0 to the loss but stay in the'mean'denominator. Default:-100
- Return type:
- 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:
predholds logits(B, C, *)and the softmax over dim 1 is taken inside.targetholds int64 class indices(B, *)in[0, C); its batch and spatial sizes must equal those ofpred.Class 0 is the background:
alphaweights it by \(1 - \alpha\) and classes \(1, \dots, C - 1\) by \(\alpha\), so a relabelling that moves class 0 changes the loss.alpha=Noneweights every class by 1. WithC = 2the target slice equalsbinary_focal_loss_with_logits()of the logit differencepred[:, 1:] - pred[:, :1]and the targettarget[:, None], with the samealphaandgamma: 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,Ctimes the pixel count, so withgamma=0andalpha=Noneit is the mean cross entropy divided byC.weightmultiplies the slice of classcbyweight[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, wheredice_loss()andtversky_loss()drop it. Losses and metrics portscross_entropy()to this loss.alphaoutside[0, 1]and a negativegammaare not validated.On the CPU,
'mean'and'sum'withoutweight, with a finitegamma >= 0andalphaNoneor 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-sizelog_softmax, unlesstorch.exportortorch.jit.tracecaptures the call, a functorch transform or forward-mode AD runs, or autocast is on. It agrees withlog_softmaxto 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\).Noneweights every class by 1.gamma (
float, optional) – Focusing parameter \(\gamma >= 0\). Default:2.0reduction (
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:Noneignore_index (
Optional[int], optional) – labels with this value contribute 0 to the loss but stay in the'mean'denominator. Default:-100
- Return type:
- 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
predover the classes.\(Y\) is the one-hot encoding of
targetbyone_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:
predandtargetare those offocal_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-channelpredhas 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,weightscaling the terms of classcin 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 noreduction.epsis 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, ateps=0as well.On the CPU, float32 or float64 logits with at least \(2^{15}\) values and a
weightthat isNoneor of their dtype skip the one-hot target, unlesstorch.exportortorch.jit.tracecaptures 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 notignore_indexraises the sameRuntimeErrorasone_hot, also undertorch.compile. Some torch releases (2.5.1 and 2.9.1 among them) report atorch.compilecapture as an export: a compiled call keeps the one-hot target there, and its compiledone_hotdoes 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-8weight (
Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\); see the Convention block for how eachaverageapplies them. Default:Noneignore_index (
Optional[int], optional) – labels with this value are ignored in the loss computation. Default:-100
- Return type:
- 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()withaverage='macro'and twice theeps.\(\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,targetandignore_indexbehave as indice_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 noweight,averageorreduction.
- 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-8ignore_index (
Optional[int], optional) – labels with this value are ignored in the loss computation. Default:-100
- Return type:
- 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.
Note
This loss function only supports binary labels. For multi-class labels please use the Lovasz-Softmax loss.
- Convention:
predholds one logit channel(B, 1, H, W)andtargetthe 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 noreduction.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.
targetis not validated and there is noignore_index: a label of 2 is accepted, and-100, theignore_indexoffocal_loss()anddice_loss(), can make the loss negative.
- Parameters:
- Return type:
- Returns:
a scalar with the computed loss, in the dtype of a floating-point
predand in float32 for an integer or boolpred. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16pred.
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
predover 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:
Note
This loss function only supports multi-class (C > 1) labels. For binary labels please use the Lovasz-Hinge loss.
- Convention:
predholds logits(B, C, H, W)withC >= 2and the softmax over dim 1 is taken inside;targetholds class labels(B, H, W)in[0, C)with the spatial size ofpred. The batch sizes match, orpredholds one image, which is scored against every target. There is noignore_indexand the labels are not validated: a label of-100is 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 allCclasses: a class absent from the image enters it with that image’s largest probability for the class.weightmultiplies the term of each class and the mean still divides byC, so a uniform weight of 2 doubles the loss, wheredice_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:
- Returns:
a scalar with the computed loss, in the dtype of
predpromoted with the dtype ofweight. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16pred.
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,alphahas 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.0reduction (
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:Noneweight (
Optional[Tensor], optional) – weights for classes with shape \((num\_of\_classes,)\). Default:Noneignore_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-8weight (
Optional[Tensor], optional) – Optional class-weight tensor of shape \((C,)\). Default:Noneignore_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()withaverage='macro'and twice theeps.\(\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-8ignore_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\).Noneweights every class by 1.gamma (
float, optional) – Focusing parameter \(\gamma >= 0\). Default:2.0reduction (
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:Noneignore_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.
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
predand in float32 for an integer or boolpred. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16pred.
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
predover 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:
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:
- Returns:
a scalar with the computed loss, in the dtype of
predpromoted with the dtype ofweight. The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16pred.
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()