🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
docs: refine shapes and clarify logits in DiceLoss docstrings
heckkkerrr39b4d797be52
diff --git a/kornia/losses/dice.py b/kornia/losses/dice.pyindex 32ec86f3c..e0c2b3e15 100644--- a/kornia/losses/dice.py+++ b/kornia/losses/dice.py@@ -156,47 +156,20 @@ def dice_loss(class DiceLoss(nn.Module):r"""Criterion that computes Sørensen-Dice Coefficient loss.- According to [1], we compute the Sørensen-Dice Coefficient as follows:-- .. math::-- \text{Dice}(x, class) = \frac{2 |X| \cap |Y|}{|X| + |Y|}-- Where:- - :math:`X` expects to be the scores of each class.- - :math:`Y` expects to be the one-hot torch.Tensor with the class labels.-- the loss, is finally computed as:-- .. math::-- \text{loss}(x, class) = 1 - \text{Dice}(x, class)-- Reference:- [1] https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient+ Dice-based objectives are common in medical and semantic segmentation because pixel classes+ are often highly imbalanced. This loss directly optimizes region-level agreement.Args:- average:- Reduction applied in multi-class scenario:- - ``'micro'`` [default]: Calculate the loss across all classes.- - ``'macro'``: Calculate the loss for each class separately and average the metrics across classes.- eps: Scalar to enforce numerical stabiliy.- weight: weights for classes with shape :math:`(num\_of\_classes,)`.- ignore_index: labels with this value are ignored in the loss computation.-- Shape:- - Pred: :math:`(N, C, H, W)` where C = number of classes.- - Target: :math:`(N, H, W)` where each value is- :math:`0 ≤ targets[i] ≤ C-1`.-- Example:- >>> N = 5 # num_classes- >>> criterion = DiceLoss()- >>> pred = torch.randn(1, N, 3, 5, requires_grad=True)- >>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)- >>> output = criterion(pred, target)- >>> output.backward()-+ average: Reduction strategy for multi-class computation. Use "micro" to aggregate+ classes globally, or "macro" to average class-wise Dice scores.+ eps: Small constant added to the denominator for numerical stability.+ weight: Optional class-weight tensor of shape :math:`(C,)`.+ ignore_index: Label value to exclude from loss computation.++ Shapes:+ - pred: :math:`(N, C, H, W)` where C is the number of classes.+ - target: :math:`(N, H, W)` where each value is in the range :math:`[0, C-1]`.+ - Output: scalar by default."""def __init__(@@ -206,7 +179,14 @@ class DiceLoss(nn.Module):weight: Optional[torch.Tensor] = None,ignore_index: Optional[int] = -100,) -> None:- """Initialize the DiceLoss module."""+ """Initialize the Dice loss module.++ Args:+ average: Reduction strategy for multi-class computation.+ eps: Small constant added for numerical stability.+ weight: Optional class-weight tensor of shape :math:`(C,)`.+ ignore_index: Label value to exclude from loss computation.+ """super().__init__()self.average = averageself.eps = eps@@ -214,13 +194,13 @@ class DiceLoss(nn.Module):self.ignore_index = ignore_indexdef forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:- """Compute the Sørensen-Dice Coefficient loss.+ """Compute Dice loss from logits and integer labels.Args:- pred: the prediction tensor.- target: the target tensor.+ pred: The input prediction logits with shape :math:`(N, C, H, W)`.+ target: The ground truth labels with shape :math:`(N, H, W)`.Returns:- The computed loss.+ The computed Sørensen-Dice loss value."""return dice_loss(pred, target, self.average, self.eps, self.weight, self.ignore_index)