im2sim.losses.DiceLoss#
- class DiceLoss(epsilon=1e-05, average='macro', class_weights=None, reduction='mean')[source]#
Bases:
TverskyLossDice loss, also known as F1 loss.
The Dice loss is:
\[L = \left( 1 - \frac{\mathrm{TP} + \epsilon} {\mathrm{TP} + 0.5 * \mathrm{FP} + 0.5 * \mathrm{FN} + \epsilon} \right)\]- Parameters:
epsilon (float) – Smoothing factor. Default is
1e-5.average (str) – Class averaging strategy. Default is
"macro". One of"micro","macro", or"weighted"."micro": Calculate metrics globally by counting the total true positives, false negatives and false positives."macro": Calculate metrics for each class, and find their unweighted mean. This does not take class imbalance into account."weighted": Calculate metrics for each class, and find their average weighted by support (the number of true instances for each class).class_weights (Tensor) – Optional class weights. Default is None. If provided, should be a 1D tensor of shape
[C]or a 2D tensor of shape[B, C].reduction (str) – Batch reduction strategy. Default is “mean”. One of
"none","mean", or"sum".
Note
Inputs
y_trueandy_predare expected to have shape[Batch, Channels, *Spatial], with channelicontaining labels/predictions for classi.y_true[:, i, ...]is 1 if the element represented byy_true[...]is a member of classiand 0 otherwise.y_pred[:,i,...]is the predicted probability, in the range[0.0, 1.0], that the element represented byy_pred[...]is a member of classi.The loss is computed for each batch element
y_true[i, ...]andy_pred[i, ...], and then reduced over this dimension as specified by argumentreduction.This loss works for binary, multiclass and multilabel classification and/or segmentation. In multiclass/multilabel problems, the different classes are combined according to the
averageandclass_weightsarguments.