Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix mypy error

edgarriba082310f4ed38
diff --git a/kornia/losses/dice.py b/kornia/losses/dice.py
index 9463dc8a2..e9619d44d 100644
--- a/kornia/losses/dice.py
+++ b/kornia/losses/dice.py
@@ -46,7 +46,7 @@ def dice_loss(input: torch.Tensor, target: torch.Tensor, eps: float = 1e-8) -> t
cardinality = torch.sum(input_soft + target_one_hot, dims)
dice_score = 2. * intersection / (cardinality + eps)
- return torch.mean(1. - dice_score)
+ return torch.mean(-dice_score + 1.)
class DiceLoss(nn.Module):
@@ -92,4 +92,4 @@ class DiceLoss(nn.Module):
self,
input: torch.Tensor,
target: torch.Tensor) -> torch.Tensor:
- return dice_loss(input, target)
+ return dice_loss(input, target, self.eps)
diff --git a/kornia/losses/focal.py b/kornia/losses/focal.py
index dc7a713b0..eadf06590 100644
--- a/kornia/losses/focal.py
+++ b/kornia/losses/focal.py
@@ -47,7 +47,7 @@ def focal_loss(
device=input.device, dtype=input.dtype)
# compute the actual focal loss
- weight = torch.pow(1. - input_soft, gamma)
+ weight = torch.pow(-input_soft + 1., gamma)
focal = -alpha * weight * torch.log(input_soft)
loss_tmp = torch.sum(target_one_hot * focal, dim=1)
diff --git a/kornia/losses/tversky.py b/kornia/losses/tversky.py
index 3a181e0e5..e3233951c 100644
--- a/kornia/losses/tversky.py
+++ b/kornia/losses/tversky.py
@@ -11,7 +11,7 @@ from kornia.utils import one_hot
def tversky_loss(input: torch.Tensor, target: torch.Tensor,
- alpha: float, beta: float, eps: float = 1e-8) -> torch.Tensor:
+ alpha: float, beta: float, eps: float = 1e-8) -> torch.Tensor:
r"""Function that computes Tversky loss.
See :class:`~kornia.losses.TverskyLoss` for details.
@@ -44,13 +44,13 @@ def tversky_loss(input: torch.Tensor, target: torch.Tensor,
# compute the actual dice score
dims = (1, 2, 3)
intersection = torch.sum(input_soft * target_one_hot, dims)
- fps = torch.sum(input_soft * (1. - target_one_hot), dims)
- fns = torch.sum((1. - input_soft) * target_one_hot, dims)
+ fps = torch.sum(input_soft * (-target_one_hot + 1.), dims)
+ fns = torch.sum((-input_soft + 1.) * target_one_hot, dims)
numerator = intersection
denominator = intersection + alpha * fps + beta * fns
tversky_loss = numerator / (denominator + eps)
- return torch.mean(1. - tversky_loss)
+ return torch.mean(-tversky_loss + 1.)
class TverskyLoss(nn.Module):