Skip to content

losses

DiceLoss

Bases: nn.Module

torch.nn.Module for dice loss (given by 1-dice_score) computation.

Parameters:

Name Type Description Default
smooth float

smooth value for dice score.

1
reduction str

reduction method for computed dice scores. Can be one of "mean", "sum" or "none".

'mean'

FocalLoss

Bases: nn.Module

torch.nn.Module for focal loss (given by :math:FL(y, t) = -\beta t(1-y)^\gamma \log{y} - (1-\beta)(1-t)(y^\gamma \log{y})) computation.

Parameters:

Name Type Description Default
reduction str

reduction method for computed dice scores. Can be one of "mean", "sum" or "none".

'mean'
beta float

coefficient ratio for positive class. Negative class has coefficient 1-beta.

0.5
gamma float

focusing parameter for focal loss. When gamma=0, focal loss is equivalent to binary cros-sentropy. When gamma increases, highly misclassified predictions will have higher weight.

2.0

SumLosses

Bases: nn.Module

Wrapper used to compute a weighted sum of different losses.

Parameters:

Name Type Description Default
*losses_with_coef Sequence[Tuple[nn.Module, float]]

tuples containg a loss function and a float corresponding to the coefficient to use for the weighted sum of the losses.

()

dice_loss(input, target, smooth=1, reduction='mean')

Compute dice loss (given by 1-dice_score) for given prediction and target.

Parameters:

Name Type Description Default
input torch.Tensor

predicted input tensor of shape (N, ...).

required
target torch.Tensor

target ground truth tensor of shape (N, ...).

required
smooth float

smooth value for dice score.

1
reduction str

reduction method for computed dice scores. Can be one of "mean", "sum" or "none".

'mean'

Returns:

Type Description
torch.Tensor

Computed dice loss, optionally reduced using specified reduction method.

focal_loss(input, target, reduction='mean', beta=0.5, gamma=2.0, eps=1e-07)

Compute focal loss (given by :math:FL(y, t) = -\beta t(1-y)^\gamma \log{y} - (1-\beta)(1-t)(y^\gamma \log{y})) for given prediction and target. Sigmoid is applied to input before computation.

Parameters:

Name Type Description Default
input torch.Tensor

predicted input tensor of shape (N, ...).

required
target torch.Tensor

target ground truth tensor of shape (N, ...).

required
reduction str

reduction method for computed dice scores. Can be one of "mean", "sum" or "none".

'mean'
beta float

coefficient ratio for positive class. Negative class has coefficient 1-beta.

0.5
gamma float

focusing parameter for focal loss. When gamma=0, focal loss is equivalent to binary cros-sentropy. When gamma increases, highly misclassified predictions will have higher weight.

2.0
eps float

term added for numerical stability.

1e-07

Returns:

Type Description
torch.Tensor

Computed focal loss, optionally reduced using specified reduction method.

get_loss(name)

Get loss object from its name. If name is not recognized, raises ValueError.

Parameters:

Name Type Description Default
name str

can either be "bce" for :class:~torch.nn.BCEWithLogitsLoss, "focal" for :class:FocalLoss, "dice" for :class:DiceLoss or "sum_loss1_coef1_loss2_coef2_..._lossN_coefN" for :class:SumLosses.

required

Returns:

Type Description
nn.Module

Loss function as a torch.nn.Module object.

get_loss_name(loss)

Get a loss object's name. If object is not recognized, will return the class name.

Parameters:

Name Type Description Default
loss nn.Module

loss module.

required

Returns:

Type Description
str

Either "bce" for :class:~torch.nn.BCEWithLogitsLoss, "focal" for :class:FocalLoss, "dice" for :class:DiceLoss, "sum_loss1_coef1_loss2_coef2_..._lossN_coefN" for :class:SumLosses or the class name if none of the above.