minerva.losses.dice¶
Attributes¶
Classes¶
Base class for all neural network modules. |
|
Base class for all neural network modules. |
Module Contents¶
- minerva.losses.dice.BINARY_MODE = 'binary'¶
- class minerva.losses.dice.DiceLoss(mode, classes=None, log_loss=False, from_logits=True, smooth=0.0, ignore_index=None, eps=1e-07)[source]¶
Bases:
torch.nn.modules.loss._LossBase class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will also have their parameters converted when you call
to(), etc.Note
As per the example above, an
__init__()call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- Parameters:
mode (str)
classes (Optional[List[int]])
log_loss (bool)
from_logits (bool)
smooth (float)
ignore_index (Optional[int])
eps (float)
Initialize the DiceLoss class.
Parameters¶
- modestr
Loss mode. Valid options are ‘binary’, ‘multiclass’, or ‘multilabel’.
- classesOptional[List[int]], optional
List of classes that contribute in loss computation. By default, all channels are included. By default None
- log_lossbool, optional
If True, loss is computed as - log(dice_coeff). If False, loss is computed as 1 - dice_coeff, by default False
- from_logitsbool, optional
If True, assumes input is raw logits. If False, assumes input is probabilities., by default True
- smoothfloat, optional
Smoothness constant for dice coefficient (a), by default 0.0
- ignore_indexOptional[int], optional
Label that indicates ignored pixels (does not contribute to loss), by default None
- epsfloat, optional
A small epsilon for numerical stability to avoid zero division error (denominator will be always greater or equal to eps), by default 1e-7
Raises¶
- AssertionError
If the mode is not one of ‘binary’, ‘multiclass’, or ‘multilabel’ and classes are being masked with mode=’binary’.
- classes = None¶
- eps = 1e-07¶
- forward(y_pred, y_true)[source]¶
- Parameters:
y_pred (torch.Tensor)
y_true (torch.Tensor)
- Return type:
torch.Tensor
- from_logits = True¶
- ignore_index = None¶
- log_loss = False¶
- mode¶
- smooth = 0.0¶
- minerva.losses.dice.MULTICLASS_MODE = 'multiclass'¶
- minerva.losses.dice.MULTILABEL_MODE = 'multilabel'¶
- class minerva.losses.dice.MultiClassDiceCELoss(weight_ce=1.0, weight_dice=1.0)[source]¶
Bases:
torch.nn.ModuleBase class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will also have their parameters converted when you call
to(), etc.Note
As per the example above, an
__init__()call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- Parameters:
weight_ce (float)
weight_dice (float)
Combined Dice and Cross-Entropy loss for multi-class segmentation.
Combines Dice loss for handling class imbalance and Cross-Entropy loss for pixel-wise classification stability, improving segmentation performance.
Parameters¶
- weight_cefloat, optional
Weight for Cross-Entropy loss component (default is 1.0).
- weight_dicefloat, optional
Weight for Dice loss component (default is 1.0).
- _dice_loss(probs, targets)[source]¶
Calculate Dice loss for all classes.
Computes Dice loss as 1 - (2 * intersection) / (pred + target) averaged over all classes.
Parameters¶
- probstorch.Tensor
Softmax probabilities of shape (batch_size, num_classes, H, W).
- targetstorch.Tensor
Ground truth labels of shape (batch_size, H, W).
- Parameters:
probs (torch.Tensor)
targets (torch.Tensor)
- Return type:
torch.Tensor
- forward(inputs, targets)[source]¶
Calculate combined Dice and Cross-Entropy loss.
Parameters¶
- inputstorch.Tensor or list of torch.Tensor
Model predictions. If a list, assumes deep supervision with multiple outputs.
- targetstorch.Tensor
Ground truth segmentation masks of shape (batch_size, H, W) or (batch_size, 1, H, W).
- Parameters:
inputs (Union[torch.Tensor, List[torch.Tensor]])
targets (torch.Tensor)
- Return type:
torch.Tensor
- weight_ce = 1.0¶
- weight_dice = 1.0¶