🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add docs and example to dice_loss
edgarriba031fc7d9a777
diff --git a/torchgeometry/losses/dice.py b/torchgeometry/losses/dice.pyindex e95e32164..88cfd00e7 100644--- a/torchgeometry/losses/dice.py+++ b/torchgeometry/losses/dice.py@@ -5,17 +5,29 @@ import torch.nn as nnimport torch.nn.functional as F-def one_hot(labels: torch.Tensor, num_classes: int, device: torch.device=None, dtype: torch.dtype=None) -> torch.Tensor:- r"""Converts an integer label tensor to a one-hot tensor.+def one_hot(labels: torch.Tensor, num_classes: int, device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None) -> torch.Tensor:+ r"""Converts an integer label 2D tensor to a one-hot 3D tensor.Args:labels (torch.Tensor) : tensor with labels of shape :math:`(N, H, W)`,where N is batch siz. Each value is an integerrepresenting correct classification.num_classes (int): number of classes in labels.+ device (Optional[torch.device]): the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.+ dtype (Optional[torch.dtype]): the desired data type of returned tensor. Default: if None, infers data type from values.Returns:torch.Tensor: the labels in one hot tensor.++ Examples::+ >>> labels = torch.LongTensor([[[0, 1], [2, 0]]])+ >>> tgm.losses.one_hot(labels, num_classes=3)+ tensor([[[[1., 0.],+ [0., 1.]],+ [[0., 1.],+ [0., 0.]],+ [[0., 0.],+ [1., 0.]]]]"""if not torch.is_tensor(labels):raise TypeError("Input labels type is not a torch.Tensor. Got {}"@@ -35,6 +47,10 @@ def one_hot(labels: torch.Tensor, num_classes: int, device: torch.device=None, ddevice=device, dtype=dtype)return one_hot.scatter_(1, labels.unsqueeze(1), 1.0)++# based on:+# https://github.com/kevinzakka/pytorch-goodies/blob/master/losses.py+class DiceLoss(nn.Module):r"""Criterion that computes Sørensen-Dice Coefficient loss."""