Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

optimized a couple loss functions (#3272)

Siddf9d8fe714b21
diff --git a/kornia/losses/cauchy.py b/kornia/losses/cauchy.py
index 8c5303c39..b69e806a0 100644
--- a/kornia/losses/cauchy.py
+++ b/kornia/losses/cauchy.py
@@ -17,6 +17,7 @@
from __future__ import annotations
+import torch
from torch import Tensor
from kornia.core import Module
@@ -72,7 +73,7 @@ def cauchy_loss(img1: Tensor, img2: Tensor, reduction: str = "none") -> Tensor:
)
# compute loss
- loss = (0.5 * (img1 - img2) ** 2 + 1.0).log()
+ loss = torch.log1p(0.5 * torch.square(img1 - img2))
# perform reduction
if reduction == "mean":
diff --git a/kornia/losses/geman_mcclure.py b/kornia/losses/geman_mcclure.py
index 2bed1d658..011e76566 100644
--- a/kornia/losses/geman_mcclure.py
+++ b/kornia/losses/geman_mcclure.py
@@ -17,6 +17,7 @@
from __future__ import annotations
+import torch
from torch import Tensor
from kornia.core import Module
@@ -72,7 +73,9 @@ def geman_mcclure_loss(img1: Tensor, img2: Tensor, reduction: str = "none") -> T
)
# compute loss
- loss = 2.0 * (img1 - img2) ** 2 / ((img1 - img2) ** 2 + 4.0)
+ diff = img1 - img2
+ diff2 = torch.square(diff)
+ loss = 2.0 * diff2 / (diff2 + 4.0)
# perform reduction
if reduction == "mean":
diff --git a/kornia/losses/tversky.py b/kornia/losses/tversky.py
index c4688b5a0..687f4b4da 100644
--- a/kornia/losses/tversky.py
+++ b/kornia/losses/tversky.py
@@ -24,7 +24,6 @@ import torch.nn.functional as F
from torch import nn
from kornia.losses._utils import mask_ignore_pixels
-from kornia.utils.one_hot import one_hot
# based on:
# https://github.com/kevinzakka/pytorch-goodies/blob/master/losses.py
@@ -91,30 +90,31 @@ def tversky_loss(
raise ValueError(f"pred and target must be in the same device. Got: {pred.device} and {target.device}")
# compute softmax over the classes axis
- pred_soft: torch.Tensor = F.softmax(pred, dim=1)
-
+ pred_soft = F.softmax(pred, dim=1)
target, target_mask = mask_ignore_pixels(target, ignore_index)
- # create the labels one hot tensor
- target_one_hot: torch.Tensor = one_hot(target, num_classes=pred.shape[1], device=pred.device, dtype=pred.dtype)
+ p_true = pred_soft.gather(1, target.unsqueeze(1)) # (B,1,H,W)
- # mask ignore pixels
if target_mask is not None:
- target_mask.unsqueeze_(1)
- target_one_hot = target_one_hot * target_mask
- pred_soft = pred_soft * target_mask
-
- # compute the actual dice score
- dims = (1, 2, 3)
- intersection = torch.sum(pred_soft * target_one_hot, dims)
- fps = torch.sum(pred_soft * (-target_one_hot + 1.0), dims)
- fns = torch.sum((-pred_soft + 1.0) * target_one_hot, dims)
-
- numerator = intersection
- denominator = intersection + alpha * fps + beta * fns
- tversky_loss = numerator / (denominator + eps)
-
- return torch.mean(-tversky_loss + 1.0)
+ m = target_mask.unsqueeze(1).to(dtype=pred.dtype)
+ p_true = p_true * m
+ total = m.sum((1, 2, 3))
+ else:
+ B, _, H, W = pred.shape
+ total = torch.full((B,), H * W, dtype=pred.dtype, device=pred.device)
+
+ intersection = p_true.sum((1, 2, 3))
+ # denominator = intersection + (alpha + beta) * (total - intersection) + eps
+ # instead of multiple ops, do it in one fused step:
+ denominator = torch.addcmul(
+ intersection, # base
+ total - intersection, # tensor1
+ torch.full_like(total, alpha + beta), # tensor2 (scalar as tensor)
+ value=1.0, # (intersection) + 1 * (tensor1*tensor2)
+ ).add_(eps) # in-place add eps
+ score = intersection.div(denominator)
+
+ return 1.0 - score.mean()
class TverskyLoss(nn.Module):