Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor tversky loss and add gpu test

edgarriba62fcfe591b68
diff --git a/kornia/losses/tversky.py b/kornia/losses/tversky.py
index 2f1d246b6..3a181e0e5 100644
--- a/kornia/losses/tversky.py
+++ b/kornia/losses/tversky.py
@@ -10,6 +10,49 @@ from kornia.utils import one_hot
# https://github.com/kevinzakka/pytorch-goodies/blob/master/losses.py
+def tversky_loss(input: torch.Tensor, target: 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.
+ """
+ if not torch.is_tensor(input):
+ raise TypeError("Input type is not a torch.Tensor. Got {}"
+ .format(type(input)))
+
+ if not len(input.shape) == 4:
+ raise ValueError("Invalid input shape, we expect BxNxHxW. Got: {}"
+ .format(input.shape))
+
+ if not input.shape[-2:] == target.shape[-2:]:
+ raise ValueError("input and target shapes must be the same. Got: {}"
+ .format(input.shape, input.shape))
+
+ if not input.device == target.device:
+ raise ValueError(
+ "input and target must be in the same device. Got: {}" .format(
+ input.device, target.device))
+
+ # compute softmax over the classes axis
+ input_soft: torch.Tensor = F.softmax(input, dim=1)
+
+ # create the labels one hot tensor
+ target_one_hot: torch.Tensor = one_hot(
+ target, num_classes=input.shape[1],
+ device=input.device, dtype=input.dtype)
+
+ # 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)
+
+ numerator = intersection
+ denominator = intersection + alpha * fps + beta * fns
+ tversky_loss = numerator / (denominator + eps)
+ return torch.mean(1. - tversky_loss)
+
+
class TverskyLoss(nn.Module):
r"""Criterion that computes Tversky Coeficient loss.
@@ -48,60 +91,14 @@ class TverskyLoss(nn.Module):
[1]: https://arxiv.org/abs/1706.05721
"""
- def __init__(self, alpha: float, beta: float) -> None:
+ def __init__(self, alpha: float, beta: float, eps: float = 1e-8) -> None:
super(TverskyLoss, self).__init__()
self.alpha: float = alpha
self.beta: float = beta
- self.eps: float = 1e-6
+ self.eps: float = eps
def forward( # type: ignore
self,
input: torch.Tensor,
target: torch.Tensor) -> torch.Tensor:
- if not torch.is_tensor(input):
- raise TypeError("Input type is not a torch.Tensor. Got {}"
- .format(type(input)))
- if not len(input.shape) == 4:
- raise ValueError("Invalid input shape, we expect BxNxHxW. Got: {}"
- .format(input.shape))
- if not input.shape[-2:] == target.shape[-2:]:
- raise ValueError("input and target shapes must be the same. Got: {}"
- .format(input.shape, input.shape))
- if not input.device == target.device:
- raise ValueError(
- "input and target must be in the same device. Got: {}" .format(
- input.device, target.device))
- # compute softmax over the classes axis
- input_soft = F.softmax(input, dim=1)
-
- # create the labels one hot tensor
- target_one_hot = one_hot(target, num_classes=input.shape[1],
- device=input.device, dtype=input.dtype)
-
- # compute the actual dice score
- dims = (1, 2, 3)
- intersection = torch.sum(input_soft * target_one_hot, dims)
- fps = torch.sum(input_soft * (torch.tensor(1.) - target_one_hot), dims)
- fns = torch.sum((torch.tensor(1.) - input_soft) * target_one_hot, dims)
-
- numerator = intersection
- denominator = intersection + self.alpha * fps + self.beta * fns
- tversky_loss = numerator / (denominator + self.eps)
- return torch.mean(torch.tensor(1.) - tversky_loss)
-
-
-######################
-# functional interface
-######################
-
-
-def tversky_loss(
- input: torch.Tensor,
- target: torch.Tensor,
- alpha: float,
- beta: float) -> torch.Tensor:
- r"""Function that computes Tversky loss.
-
- See :class:`~kornia.losses.TverskyLoss` for details.
- """
- return TverskyLoss(alpha, beta)(input, target)
+ return tversky_loss(input, target, self.alpha, self.beta, self.eps)
diff --git a/test/test_losses.py b/test/test_losses.py
index e0527a4d8..97cac9893 100644
--- a/test/test_losses.py
+++ b/test/test_losses.py
@@ -65,37 +65,37 @@ class TestFocalLoss:
class TestTverskyLoss:
- def test_smoke(self):
+ def test_smoke(self, device):
num_classes = 3
- logits = torch.rand(2, num_classes, 3, 2)
+ logits = torch.rand(2, num_classes, 3, 2).to(device)
labels = torch.rand(2, 3, 2) * num_classes
- labels = labels.long()
+ labels = labels.to(device).long()
criterion = kornia.losses.TverskyLoss(alpha=0.5, beta=0.5)
loss = criterion(logits, labels)
- def test_all_zeros(self):
+ def test_all_zeros(self, device):
num_classes = 3
- logits = torch.zeros(2, num_classes, 1, 2)
+ logits = torch.zeros(2, num_classes, 1, 2).to(device)
logits[:, 0] = 10.0
logits[:, 1] = 1.0
logits[:, 2] = 1.0
- labels = torch.zeros(2, 1, 2, dtype=torch.int64)
+ labels = torch.zeros(2, 1, 2, dtype=torch.int64).to(device)
criterion = kornia.losses.TverskyLoss(alpha=0.5, beta=0.5)
loss = criterion(logits, labels)
assert pytest.approx(loss.item(), 0.0)
# TODO: implement me
- def test_jit(self):
+ def test_jit(self, device):
pass
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
num_classes = 3
alpha, beta = 0.5, 0.5 # for tversky loss
- logits = torch.rand(2, num_classes, 3, 2)
+ logits = torch.rand(2, num_classes, 3, 2).to(device)
labels = torch.rand(2, 3, 2) * num_classes
- labels = labels.long()
+ labels = labels.to(device).long()
logits = utils.tensor_to_gradcheck_var(logits) # to var
assert gradcheck(