CLDiceLoss
- class topolosses.losses.cldice.CLDiceLoss(*args: Any, **kwargs: Any)[source]
Bases:
_Loss
A loss function for segmentation that combines a base loss and a CLDice component.
- The loss has been defined in:
Shit et al. (2021) clDice – A Novel Topology-Preserving Loss Function for Tubular Structure Segmentation. (https://arxiv.org/abs/2003.07311)
By default the cl dice component is combined with a (weighted) default dice loss. For more flexibility a custom base loss function can be passed.
- Parameters:
iter (int) – Number of iterations for soft skeleton computation. Higher values refine the skeleton but increase computation time. Defaults to 3.
smooth (float) – Smoothing factor to avoid division by zero in CLDice and the default base dice calculations. Defaults to 1e-5.
batch (bool) – If True, reduces the loss across the batch dimension by summing intersection and union areas before division. Defaults to False, where the loss is computed independently for each item for the CLDice and default base dice component calculation.
include_background (bool) – If True, includes the background class in CLDice computation. Defaults to False.
alpha (float) – Weighting factor for combining the CLDice component (i.e.: base_loss + alpha*cldice_loss). Defaults to 0.5.
sigmoid (bool) – If True, applies a sigmoid activation to the input before computing the CLDice and the default dice component. Sigmoid is not applied before passing it to a custom base loss function. Defaults to False.
softmax (bool) – If True, applies a softmax activation to the input before computing the CLDice loss. Softmax is not applied before passing it to a custom base loss function. Defaults to False.
use_base_component (bool) – if false the loss only consists of the CLDice component. A forward call will return the full CLDice component. base_loss and alpha will be ignored if this flag is set to false.
base_loss (_Loss, optional) – The base loss function to be used alongside the CLDice loss. Defaults to None, meaning a Dice component with default parameters will be used.
- Raises:
ValueError – If more than one of [sigmoid, softmax] is set to True.
- compute_cldice_loss(input: torch.Tensor, target: torch.Tensor, reduce_axis: List[int]) torch.Tensor [source]
Computes the CLDice loss.
- Parameters:
input (torch.Tensor) – The predicted segmentation map with shape (N, C, …), where N is batch size, C is the number of classes.
target (torch.Tensor) – The ground truth segmentation map with the same shape as input.
smooth (float) – Smoothing factor to avoid division by zero.
iter (int) – Number of iterations for soft skeleton computation.
reduce_axis (List[int]) – The axes along which to reduce the loss computation. It decides whether to sum the intersection and union areas over the batch dimension before the dividing.
- Returns:
The CLDice loss as a scalar tensor.
- Return type:
torch.Tensor
- forward(input: torch.Tensor, target: torch.Tensor) torch.Tensor [source]
Computes the CLDice loss and base loss for the given input and target.
- Parameters:
input (torch.Tensor) – Predicted segmentation map of shape BC[spatial dimensions], where C is the number of classes, and [spatial dimensions] represent height, width, and optionally depth.
target (torch.Tensor) – Ground truth segmentation map of shape BC[spatial dimensions]
- Returns:
The calculated CLDice loss
- Return type:
Tensor
- Raises:
ValueError – If the shape of the ground truth is different from the input shape.
ValueError – If softmax=True and the number of channels for the prediction is 1.