π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement multiclass dice loss
edgarriba641f9e43e3a5
diff --git a/mypy_files.txt b/mypy_files.txtindex 4d6ba7aa7..451337d22 100644--- a/mypy_files.txt+++ b/mypy_files.txt@@ -1,4 +1,5 @@torchgeometry/image/gaussian.pytorchgeometry/losses/ssim.py+torchgeometry/losses/dice.pytorchgeometry/losses/depth_smooth.pytorchgeometry/homography_warper.pydiff --git a/test/test_losses.py b/test/test_losses.pyindex fbfa01802..1a8b5a642 100644--- a/test/test_losses.py+++ b/test/test_losses.py@@ -8,6 +8,65 @@ import utilsfrom common import TEST_DEVICES+class TestDiceLoss:+ def test_make_one_hot(self):+ num_classes = 4+ labels = torch.zeros(2, 2, 1, dtype=torch.int64)+ labels[0, 0, 0] = 0+ labels[0, 1, 0] = 1+ labels[1, 0, 0] = 2+ labels[1, 1, 0] = 3++ # convert labels to one hot tensor+ one_hot = tgm.losses.DiceLoss.make_one_hot(labels, num_classes)++ assert pytest.approx(one_hot[0, labels[0, 0, 0], 0, 0].item(), 1.0)+ assert pytest.approx(one_hot[0, labels[0, 1, 0], 1, 0].item(), 1.0)+ assert pytest.approx(one_hot[1, labels[1, 0, 0], 0, 0].item(), 1.0)+ assert pytest.approx(one_hot[1, labels[1, 1, 0], 1, 0].item(), 1.0)++ def _test_smoke(self):+ num_classes = 3+ logits = torch.rand(2, num_classes, 3, 2)+ labels = torch.rand(2, 3, 2) * num_classes+ labels = labels.long()++ criterion = tgm.losses.DiceLoss(reduction='none')+ loss = criterion(logits, labels)++ # TODO: implement me+ def _test_all_zeros(self):+ num_classes = 3+ logits = torch.zeros(2, num_classes, 1, 2)+ logits[:, 0] = 10.0+ logits[:, 1] = 1.0+ logits[:, 2] = 1.0+ labels = torch.zeros(2, 1, 2, dtype=torch.int64)++ criterion = tgm.losses.DiceLoss(reduction='mean')+ loss = criterion(logits, labels)+ assert pytest.approx(loss.item(), 0.0)++ # TODO: implement me+ def _test_jit(self):+ pass++ def _test_gradcheck(self):+ num_classes = 3+ logits = torch.rand(2, num_classes, 3, 2)+ labels = torch.rand(2, 3, 2) * num_classes+ labels = labels.long()++ logits = utils.tensor_to_gradcheck_var(logits) # to var+ assert gradcheck(tgm.losses.dice_loss,+ (logits, labels,), raise_exception=True)++ def test_run_all(self):+ self._test_smoke()+ self._test_all_zeros()+ self._test_gradcheck()++class TestDepthSmoothnessLoss:def _test_smoke(self):image = self.image.clone()diff --git a/torchgeometry/losses/__init__.py b/torchgeometry/losses/__init__.pyindex 18c9574a6..104770755 100644--- a/torchgeometry/losses/__init__.py+++ b/torchgeometry/losses/__init__.py@@ -1,2 +1,3 @@from .ssim import SSIM, ssim+from .dice import DiceLoss, dice_lossfrom .depth_smooth import DepthSmoothnessLoss, depth_smoothness_lossdiff --git a/torchgeometry/losses/dice.py b/torchgeometry/losses/dice.pynew file mode 100644index 000000000..44091cd62--- /dev/null+++ b/torchgeometry/losses/dice.py@@ -0,0 +1,103 @@+from typing import Optional++import torch+import torch.nn as nn+import torch.nn.functional as F++# Based on+# https://github.com/tensorflow/models/blob/master/research/struct2depth/model.py#L625-L641+++class DiceLoss(nn.Module):+ r"""Criterion that computes Dice Coefficient loss.+ """+ def __init__(self,+ smooth: Optional[float] = 1.0,+ reduction: Optional[str] = 'none') -> None:+ super(DiceLoss, self).__init__()+ self.smooth: float = smooth+ self.reduction: str = reduction+ self.eps: float = 1e-6++ @staticmethod+ def make_one_hot(labels: torch.Tensor, num_classes: int) -> torch.Tensor:+ r"""Converts an integer label tensor a one-hot labels tensor.+ Args:+ labels (torch.Tensor) : tensor with labels of shape :math:`(N, H, W)`,+ where N is batch siz. Each value is an integer+ representing correct classification.+ num_classes (int): number of classes in labels.+ Returns:+ torch.Tensor: the labels in one hot tensor.+ """+ if not torch.is_tensor(labels):+ raise TypeError("Input labels type is not a torch.Tensor. Got {}"+ .format(type(labels)))+ if not len(labels.shape) == 3:+ raise ValueError("Invalid depth shape, we expect BxHxW. Got: {}"+ .format(labels.shape))+ if not labels.dtype == torch.int64:+ raise ValueError(+ "labels must be of the same dtype torch.int64. Got: {}" .format(+ labels.dtype))+ if num_classes < 1:+ raise ValueError("The number of classes must be bigger than one. Got: {}"+ .format(num_classes))+ batch_size, height, width = labels.shape+ one_hot = torch.zeros(+ batch_size, num_classes, height, width, device=labels.device)+ return one_hot.scatter_(1, labels.unsqueeze(1), 1.0)++ def forward(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, shape.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 = self.make_one_hot(target, num_classes=input.shape[1])+ target_one_hot = target_one_hot.to(input.dtype)++ # compute the actual dice score+ numerator = torch.sum(input_soft * target_one_hot, dim=(1, 2, 3))+ denominator = torch.sum(input_soft + target_one_hot, dim=(1, 2, 3))++ dice_score_tmp = (2. * numerator + self.smooth) / \+ (denominator + self.smooth + self.eps)++ # reducte loss+ dice_score = -1+ if self.reduction == 'none':+ dice_score = dice_score_tmp+ elif self.reduction == 'mean':+ dice_score = torch.mean(dice_score_tmp)+ elif self.reduction == 'sum':+ dice_score = torch.sum(dice_score_tmp)+ else:+ raise NotImplementedError("Invalid reduction type: {}."+ .format(self.reduction))+ return 1. - dice_score+++######################+# functional interface+######################+++def dice_loss(input: torch.Tensor, target: torch.Tensor, smooth: Optional[float] = 1.0, reduction: Optional[str] = 'none') -> torch.Tensor:+ r"""Computes image-aware depth smoothness loss.++ See :class:`~torchgeometry.losses.DiceLoss` for details.+ """+ return DiceLoss()(input, target)