kornia.losses

dice_loss(input: torch.Tensor, target: torch.Tensor) → torch.Tensor[source]

Function that computes Sørensen-Dice Coefficient loss.

See DiceLoss for details.

total_variation(img: torch.Tensor) → torch.Tensor[source]

Function that computes Total Variation.

See TotalVariation for details.

psnr_loss(input: torch.Tensor, target: torch.Tensor, max_val: float) → torch.Tensor[source]

Function that computes PSNR

See PSNR for details.

tversky_loss(input: torch.Tensor, target: torch.Tensor, alpha: float, beta: float) → torch.Tensor[source]

Function that computes Tversky loss.

See TverskyLoss for details.

focal_loss(input: torch.Tensor, target: torch.Tensor, alpha: float, gamma: Optional[float] = 2.0, reduction: Optional[str] = 'none') → torch.Tensor[source]

Function that computes Focal loss.

See FocalLoss for details.

ssim(img1: torch.Tensor, img2: torch.Tensor, window_size: int, reduction: str = 'none', max_val: float = 1.0) → torch.Tensor[source]

Function that measures the Structural Similarity (SSIM) index between each element in the input x and target y.

See SSIM for details.

inverse_depth_smoothness_loss(idepth: torch.Tensor, image: torch.Tensor) → torch.Tensor[source]

Computes image-aware inverse depth smoothness loss.

See InvDepthSmoothnessLoss for details.

class DiceLoss[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\) expects to be the scores of each class.

  • \(Y\) expects to be the one-hot tensor with the class labels.

the loss, is finally computed as:

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

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

Shape:
  • Input: \((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
>>> loss = kornia.losses.DiceLoss()
>>> input = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = loss(input, target)
>>> output.backward()
class TverskyLoss(alpha: float, beta: float)[source]

Criterion that computes Tversky Coeficient loss.

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

\[\text{S}(P, G, \alpha; \beta) = \frac{|PG|}{|PG| + \alpha |P \ G| + \beta |G \ P|}\]
where:
  • \(P\) and \(G\) are the predicted and ground truth binary labels.

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

Notes

  • \(\alpha = \beta = 0.5\) => dice coeff

  • \(\alpha = \beta = 1\) => tanimoto coeff

  • \(\alpha + \beta = 1\) => F beta coeff

Shape:
  • Input: \((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
>>> loss = kornia.losses.TverskyLoss(alpha=0.5, beta=0.5)
>>> input = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = loss(input, target)
>>> output.backward()

References

[1]: https://arxiv.org/abs/1706.05721

class FocalLoss(alpha: float, gamma: Optional[float] = 2.0, reduction: Optional[str] = 'none')[source]

Criterion that computes Focal loss.

According to [1], 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 model’s estimated probability for each class.

Parameters
  • alpha (float) – Weighting factor \(\alpha \in [0, 1]\).

  • gamma (float) – Focusing parameter \(\gamma >= 0\).

  • reduction (Optional[str]) – 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’.

Shape:
  • Input: \((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
>>> args = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'}
>>> loss = kornia.losses.FocalLoss(*args)
>>> input = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = loss(input, target)
>>> output.backward()

References

[1] https://arxiv.org/abs/1708.02002

class SSIM(window_size: int, reduction: str = 'none', max_val: float = 1.0)[source]

Creates a criterion that measures the Structural Similarity (SSIM) index between each element in the input x and target y.

The index can be described as:

\[\text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)} {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}\]
where:
  • \(c_1=(k_1 L)^2\) and \(c_2=(k_2 L)^2\) are two variables to stabilize the division with weak denominator.

  • \(L\) is the dynamic range of the pixel-values (typically this is \(2^{\#\text{bits per pixel}}-1\)).

the loss, or the Structural dissimilarity (DSSIM) can be finally described as:

\[\text{loss}(x, y) = \frac{1 - \text{SSIM}(x, y)}{2}\]
Parameters
  • window_size (int) – the size of the kernel.

  • max_val (float) – the dynamic range of the images. Default: 1.

  • 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’.

Returns

the ssim index.

Return type

Tensor

Shape:
  • Input: \((B, C, H, W)\)

  • Target \((B, C, H, W)\)

  • Output: scale, if reduction is ‘none’, then \((B, C, H, W)\)

Examples:

>>> input1 = torch.rand(1, 4, 5, 5)
>>> input2 = torch.rand(1, 4, 5, 5)
>>> ssim = kornia.losses.SSIM(5, reduction='none')
>>> loss = ssim(input1, input2)  # 1x4x5x5
class InverseDepthSmoothnessLoss[source]

Criterion that computes image-aware inverse depth smoothness loss.

\[\text{loss} = \left | \partial_x d_{ij} \right | e^{-\left \| \partial_x I_{ij} \right \|} + \left | \partial_y d_{ij} \right | e^{-\left \| \partial_y I_{ij} \right \|}\]
Shape:
  • Inverse Depth: \((N, 1, H, W)\)

  • Image: \((N, 3, H, W)\)

  • Output: scalar

Examples:

>>> idepth = torch.rand(1, 1, 4, 5)
>>> image = torch.rand(1, 3, 4, 5)
>>> smooth = kornia.losses.DepthSmoothnessLoss()
>>> loss = smooth(idepth, image)
class TotalVariation[source]

Computes the Total Variation according to [1] https://en.wikipedia.org/wiki/Total_variation Shape:

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

  • Output: \((N,)\) or \(()\)

Examples

>>> kornia.losses.total_variation(torch.ones(3,4,4)) # tensor(0.)
>>> tv = kornia.losses.TotalVariation()
>>> output = tv(torch.ones(2,3,4,4)) # tensor([0., 0.])
>>> output.backward()
class PSNRLoss(max_val: float)[source]

Creates a criterion that calculates the PSNR between 2 images. Given an m x n image, .. math:: text{MSE}(I,T) = frac{1}{m,n}sum_{i=0}^{m-1}sum_{j=0}^{n-1} [I(i,j) - T(i,j)]^2

Parameters

max_val (float) – Maximum value of input

Shape:
  • input: \((*)\)

  • approximation: \((*)\) same shape as input

  • output: \(()\) a scalar

Examples

>>> kornia.losses.psnr(torch.ones(1), 1.2*torch.ones(1), 2)
tensor(20.0000) # 10 * log(4/((1.2-1)**2)) / log(10)
reference:

https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio#Definition