Loss functions¶
Segmentation Losses¶
DiceLoss¶
-
class
monai.losses.
DiceLoss
(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, squared_pred=False, jaccard=False, reduction=<LossReduction.MEAN: 'mean'>)[source]¶ Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks. Input logits input (BNHW[D] where N is number of classes) is compared with ground truth target (BNHW[D]). Axis N of input is expected to have logit predictions for each class rather than being image channels, while the same axis of target can be 1 or N (one-hot format). The smooth parameter is a value added to the intersection and union components of the inter-over-union calculation to smooth results and prevent divide by 0, this value should be small. The include_background class attribute can be set to False for an instance of DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background. If the non-background segmentations are small compared to the total image size they can get overwhelmed by the signal from the background so excluding it in such cases helps convergence.
Milletari, F. et. al. (2016) V-Net: Fully Convolutional Neural Networks forVolumetric Medical Image Segmentation, 3DV, 2016.
- Parameters
include_background (
bool
) – If False channel index 0 (background category) is excluded from the calculation.to_onehot_y (
bool
) – whether to convert y into the one-hot format. Defaults to False.sigmoid (
bool
) – If True, apply a sigmoid function to the prediction.softmax (
bool
) – If True, apply a softmax function to the prediction.squared_pred (
bool
) – use squared versions of targets and predictions in the denominator or not.jaccard (
bool
) – compute Jaccard Index (soft IoU) instead of dice or not.reduction (
Union
[LossReduction
,str
]) –{
"none"
,"mean"
,"sum"
} Specifies the reduction to apply to the output. Defaults to"mean"
."none"
: no reduction will be applied."mean"
: the sum of the output will be divided by the number of elements in the output."sum"
: the output will be summed.
- Raises
ValueError – reduction={reduction} is invalid. Valid options are: none, mean or sum.
ValueError – sigmoid=True and softmax=True are not compatible.
-
monai.losses.
Dice
¶ alias of
monai.losses.dice.DiceLoss
-
monai.losses.
dice
¶ alias of
monai.losses.dice.DiceLoss
MaskedDiceLoss¶
-
class
monai.losses.
MaskedDiceLoss
(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, squared_pred=False, jaccard=False, reduction=<LossReduction.MEAN: 'mean'>)[source]¶ Same as DiceLoss, but accepts a binary mask ([0,1]) indicating a region over which to compute the dice.
- Parameters
include_background (
bool
) – If False channel index 0 (background category) is excluded from the calculation.to_onehot_y (
bool
) – whether to convert y into the one-hot format. Defaults to False.sigmoid (
bool
) – If True, apply a sigmoid function to the prediction.softmax (
bool
) – If True, apply a softmax function to the prediction.squared_pred (
bool
) – use squared versions of targets and predictions in the denominator or not.jaccard (
bool
) – compute Jaccard Index (soft IoU) instead of dice or not.reduction (
Union
[LossReduction
,str
]) –{
"none"
,"mean"
,"sum"
} Specifies the reduction to apply to the output. Defaults to"mean"
."none"
: no reduction will be applied."mean"
: the sum of the output will be divided by the number of elements in the output."sum"
: the output will be summed.
- Raises
ValueError – reduction={reduction} is invalid. Valid options are: none, mean or sum.
ValueError – sigmoid=True and softmax=True are not compatible.
GeneralizedDiceLoss¶
-
class
monai.losses.
GeneralizedDiceLoss
(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, w_type=<Weight.SQUARE: 'square'>, reduction=<LossReduction.MEAN: 'mean'>)[source]¶ Compute the generalised Dice loss defined in:
Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning loss function for highly unbalanced segmentations. DLMIA 2017.
- Parameters
include_background (
bool
) – If False channel index 0 (background category) is excluded from the calculation.to_onehot_y (
bool
) – whether to convert y into the one-hot format. Defaults to False.sigmoid (
bool
) – If True, apply a sigmoid function to the prediction.softmax (
bool
) – If True, apply a softmax function to the prediction.w_type (
Union
[Weight
,str
]) – {"square"
,"simple"
,"uniform"
} Type of function to transform ground truth volume to a weight factor. Defaults to"square"
.reduction (
Union
[LossReduction
,str
]) –{
"none"
,"mean"
,"sum"
} Specifies the reduction to apply to the output. Defaults to"mean"
."none"
: no reduction will be applied."mean"
: the sum of the output will be divided by the number of elements in the output."sum"
: the output will be summed.
- Raises
ValueError – reduction={reduction} is invalid. Valid options are: none, mean or sum.
ValueError – sigmoid=True and softmax=True are not compatible.
-
monai.losses.
generalized_dice
¶ alias of
monai.losses.dice.GeneralizedDiceLoss
FocalLoss¶
-
class
monai.losses.
FocalLoss
(gamma=2.0, weight=None, reduction=<LossReduction.MEAN: 'mean'>)[source]¶ Reimplementation of the Focal Loss described in:
“Focal Loss for Dense Object Detection”, T. Lin et al., ICCV 2017
“AnatomyNet: Deep learning for fast and fully automated whole‐volume segmentation of head and neck anatomy”, Zhu et al., Medical Physics 2018
- Parameters
gamma (
float
) – value of the exponent gamma in the definition of the Focal loss.weight (tensor) – weights to apply to the voxels of each class. If None no weights are applied. This corresponds to the weights lpha in [1].
reduction (
Union
[LossReduction
,str
]) –{
"none"
,"mean"
,"sum"
} Specifies the reduction to apply to the output. Defaults to"mean"
."none"
: no reduction will be applied."mean"
: the sum of the output will be divided by the number of elements in the output."sum"
: the output will be summed.
Example
import torch from monai.losses import FocalLoss pred = torch.tensor([[1, 0], [0, 1], [1, 0]], dtype=torch.float32) grnd = torch.tensor([[0], [1], [0]], dtype=torch.int64) fl = FocalLoss() fl(pred, grnd)
-
forward
(input, target)[source]¶ - Parameters
input – (tensor): the shape should be BCH[WD]. where C is the number of classes.
target – (tensor): the shape should be B1H[WD] or BCH[WD]. If the target’s shape is B1H[WD], the target that this loss expects should be a class index in the range [0, C-1] where C is the number of classes.
- Raises
ValueError – input and target must have the same number of dimensions, got {i.ndim} and {t.ndim}
ValueError – target must have one channel or have the same shape as the input. If it has one channel, it should be a class index in the range [0, C-1] where C is the number of classes inferred from ‘input’: C={i.shape[1]}.
ValueError – reduction={self.reduction} is invalid.
TverskyLoss¶
-
class
monai.losses.
TverskyLoss
(include_background=True, to_onehot_y=False, sigmoid=False, softmax=False, alpha=0.5, beta=0.5, reduction=<LossReduction.MEAN: 'mean'>)[source]¶ Compute the Tversky loss defined in:
Sadegh et al. (2017) Tversky loss function for image segmentation using 3D fully convolutional deep networks. (https://arxiv.org/abs/1706.05721)
- Parameters
include_background (
bool
) – If False channel index 0 (background category) is excluded from the calculation.to_onehot_y (
bool
) – whether to convert y into the one-hot format. Defaults to False.sigmoid (
bool
) – If True, apply a sigmoid function to the prediction.softmax (
bool
) – If True, apply a softmax function to the prediction.alpha (
float
) – weight of false positivesbeta (
float
) – weight of false negativesreduction (
Union
[LossReduction
,str
]) –{
"none"
,"mean"
,"sum"
} Specifies the reduction to apply to the output. Defaults to"mean"
."none"
: no reduction will be applied."mean"
: the sum of the output will be divided by the number of elements in the output."sum"
: the output will be summed.
- Raises
ValueError – sigmoid=True and softmax=True are not compatible.