cassetta.losses
Overview
Losses are modules that return a scalar tensor and must be differentiable.
| MODULE | DESCRIPTION |
|---|---|
base |
Base class for all losses |
segmentation |
Losses for semantic segmentation |
make_loss
Instantiate a loss
A loss can be:
- the name of a cassetta loss, such as
"DiceLoss"; - the fully qualified path to a model, such as
"cassetta.losses.DiceLoss", or"monai.losses.GeneralizedDiceLoss"; - a
nn.Modulesubclass, such as [DiceLoss][cassetta.losses.DiceLoss]; - an already instantiated
nn.Module, such as [DiceLoss()][cassetta.losses.DiceLoss].
| PARAMETER | DESCRIPTION |
|---|---|
loss |
Instantiated or non-instantiated loss
TYPE:
|
*args |
Positional arguments pass to the loss constructor
TYPE:
|
**kwargs |
Keyword arguments pass to the loss constructor
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
loss
|
Instantiated loss
TYPE:
|
Source code in cassetta/losses/__init__.py
cassetta.losses.base
Loss
Bases: Module
Base class for losses
| PARAMETER | DESCRIPTION |
|---|---|
reduction |
Reduction to apply across batch elements
TYPE:
|
Source code in cassetta/losses/base.py
cassetta.losses.segmentation
DiceLoss
Bases: Loss
Soft Dice Loss
By default, each class is weighted identically.
The weighted mode allows classes to be weighted by frequency.
References
- Milletari, Navab & Ahmadi, "V-Net: Fully convolutional neural networks for volumetric medical image segmentation." 3DV (2016). arxiv:1606.04797
- Sudre, Li, Vercauteren, Ourselin & Cardoso, "Generalised dice overlap as a deep learning loss function for highly unbalanced segmentations." DLMIA (2017). arxiv:1707.03237
- Tilborghs, Bertels, Robben, Vandermeulen & Maes, "The Dice loss in the context of missing or empty labels: introducing \(\Phi\) and \(\epsilon\)." MICCAI (2022). arxiv:2207.09521
| PARAMETER | DESCRIPTION |
|---|---|
square |
Square the denominator in SoftDice.
TYPE:
|
weighted |
If True, weight the Dice of each class by its frequency in the reference. If a list, use these weights for each class.
TYPE:
|
labels |
Label corresponding to each one-hot class. Only used if the reference is an integer label map.
TYPE:
|
eps |
Stabilization of the Dice loss. Optimally, should be equal to each class' expected frequency across the whole dataset. See Tilborghs et al.
TYPE:
|
reduction |
Type of reduction to apply across minibatch elements.
TYPE:
|
activation |
Activation to apply to the prediction before computing the loss
TYPE:
|
Source code in cassetta/losses/segmentation.py
forward
| PARAMETER | DESCRIPTION |
|---|---|
pred |
Predicted classes.
TYPE:
|
ref |
Reference classes (or their expectation).
TYPE:
|
mask |
Loss mask
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
loss
|
The output shape depends on the type of reduction used. If 'mean' or 'sum', this function returns a scalar tensor.
TYPE:
|
Source code in cassetta/losses/segmentation.py
CatLoss
Bases: Loss
Weighted categorical cross-entropy.
By default, each class is weighted identically.
This differs from the classical "categorical cross-entropy loss", which corresponds to the true Categorical log-likelihood and where classes are therefore weighted by frequency. The default behavior of our loss is that of a "weighted categorical cross-entropy".
With weighted=True, classes are weighted by frequency.
| PARAMETER | DESCRIPTION |
|---|---|
weighted |
If True, weight the term of each class by its frequency in the reference. If a list, use these weights for each class.
TYPE:
|
labels |
Label corresponding to each one-hot class. Only used if the reference is an integer label map.
TYPE:
|
reduction |
Type of reduction to apply across minibatch elements.
TYPE:
|
activation |
Activation to apply to the prediction before computing the loss
TYPE:
|
Source code in cassetta/losses/segmentation.py
forward
| PARAMETER | DESCRIPTION |
|---|---|
pred |
Predicted classes.
TYPE:
|
ref |
Reference classes (or their expectation).
TYPE:
|
mask |
Loss mask
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
loss
|
The output shape depends on the type of reduction used. If 'mean' or 'sum', this function returns a scalar tensor.
TYPE:
|
Source code in cassetta/losses/segmentation.py
CatMSELoss
Bases: Loss
Mean Squared Error between one-hots.
| PARAMETER | DESCRIPTION |
|---|---|
weighted |
If True, weight the Dice of each class by its size in the reference. If a list, use these weights for each class.
TYPE:
|
labels |
Label corresponding to each one-hot class. Only used if the reference is an integer label map.
TYPE:
|
reduction |
Type of reduction to apply across minibatch elements.
TYPE:
|
activation |
Activation to apply to the prediction before computing the loss
TYPE:
|
Source code in cassetta/losses/segmentation.py
forward
| PARAMETER | DESCRIPTION |
|---|---|
pred |
Predicted classes.
TYPE:
|
ref |
Reference classes (or their expectation).
TYPE:
|
mask |
Loss mask
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
loss
|
The output shape depends on the type of reduction used. If 'mean' or 'sum', this function returns a scalar tensor.
TYPE:
|
Source code in cassetta/losses/segmentation.py
LogitMSELoss
Bases: Loss
Mean Squared Error between logits and target positive/negative values.
| PARAMETER | DESCRIPTION |
|---|---|
target |
Target value when the ground truth is True.
TYPE:
|
weighted |
If True, weight the score of each class by its frequency in the reference. If 'inv', weight the score of each class by its inverse frequency in the reference. If a list, use these weights for each class.
TYPE:
|
labels |
Label corresponding to each one-hot class. Only used if the reference is an integer label map.
TYPE:
|
reduction |
Type of reduction to apply across minibatch elements.
TYPE:
|
activation |
Activation to apply to the prediction before computing the loss
TYPE:
|
Source code in cassetta/losses/segmentation.py
forward
| PARAMETER | DESCRIPTION |
|---|---|
pred |
Predicted classes.
TYPE:
|
ref |
Reference classes (or their expectation).
TYPE:
|
mask |
Loss mask
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
loss
|
The output shape depends on the type of reduction used. If 'mean' or 'sum', this function returns a scalar tensor.
TYPE:
|