TFSegmentationLosses Module

TensorFlow implementations of segmentation losses and related utilities.

class HMB.TFSegmentationLosses.DiceLoss(smooth=1.0, name='DiceLoss', **kwargs)[source]

Bases: Loss

Implements Dice Loss for binary segmentation tasks. Dice loss measures the overlap between predicted and ground truth masks.

\[\text{Dice} = 1 - \frac{2 \times |X \cap Y| + \text{smooth}}{|X| + |Y| + \text{smooth}}\]

Initialize the DiceLoss with a smoothing constant for numerical stability.

Parameters:
  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “DiceLoss”.

  • **kwargs – Additional keyword arguments for the base class.

__init__(smooth=1.0, name='DiceLoss', **kwargs)[source]

Initialize the DiceLoss with a smoothing constant for numerical stability.

Parameters:
  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “DiceLoss”.

  • **kwargs – Additional keyword arguments for the base class.

call(yTrue, yPred)[source]

Compute the Dice loss between ground truth and predictions.

Parameters:
  • yTrue (tensorflow.Tensor) – Ground truth binary mask tensor.

  • yPred (tensorflow.Tensor) – Predicted logits tensor.

Returns:

Scalar tensor representing the Dice loss.

Return type:

tensorflow.Tensor

class HMB.TFSegmentationLosses.DiceBCELoss(smooth=1.0, name='DiceBCELoss', **kwargs)[source]

Bases: Loss

Implements Dice + BCE Loss for binary segmentation tasks. Combines Dice loss and binary cross-entropy loss for improved performance on imbalanced data.

\[\text{Loss} = \text{BCE}(X, Y) + \left[1 - \frac{2 \times |X \cap Y| + \text{smooth}}{|X| + |Y| + \text{smooth}}\right]\]

Note

For best practice and autocasting safety, use raw logits as inputs.

Initialize the DiceBCELoss with a smoothing constant for numerical stability.

Parameters:
  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “DiceBCELoss”.

  • **kwargs – Additional keyword arguments for the base class.

__init__(smooth=1.0, name='DiceBCELoss', **kwargs)[source]

Initialize the DiceBCELoss with a smoothing constant for numerical stability.

Parameters:
  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “DiceBCELoss”.

  • **kwargs – Additional keyword arguments for the base class.

call(yTrue, yPred)[source]

Compute the combined Dice and Binary Cross-Entropy loss between ground truth and predictions.

Parameters:
  • yTrue (tensorflow.Tensor) – Ground truth binary mask tensor.

  • yPred (tensorflow.Tensor) – Predicted logits tensor.

Returns:

Scalar tensor representing the combined Dice + BCE loss.

Return type:

tensorflow.Tensor

class HMB.TFSegmentationLosses.JaccardLoss(smooth=1.0, name='JaccardLoss', **kwargs)[source]

Bases: Loss

Implements Jaccard Loss (IoU Loss) for binary segmentation tasks. Jaccard loss measures the intersection over union between predicted and ground truth masks.

\[\text{Jaccard} = 1 - \frac{|X \cap Y| + \text{smooth}}{|X \cup Y| + \text{smooth}}\]

Initialize the JaccardLoss with a smoothing constant for numerical stability.

Parameters:
  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “JaccardLoss”.

  • **kwargs – Additional keyword arguments for the base class.

__init__(smooth=1.0, name='JaccardLoss', **kwargs)[source]

Initialize the JaccardLoss with a smoothing constant for numerical stability.

Parameters:
  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “JaccardLoss”.

  • **kwargs – Additional keyword arguments for the base class.

call(yTrue, yPred)[source]

Compute the Jaccard loss (IoU loss) between ground truth and predictions.

Parameters:
  • yTrue (tensorflow.Tensor) – Ground truth binary mask tensor.

  • yPred (tensorflow.Tensor) – Predicted logits tensor.

class HMB.TFSegmentationLosses.TverskyLoss(alpha=0.5, beta=0.5, smooth=1.0, name='TverskyLoss', **kwargs)[source]

Bases: Loss

Implements Tversky Loss for binary segmentation tasks. Tversky loss generalizes Dice loss by allowing control over penalties for false positives and false negatives.

\[\text{Tversky} = 1 - \frac{|X \cap Y| + \text{smooth}}{|X \cap Y| + \alpha \times |X \setminus Y| + \beta \times |Y \setminus X| + \text{smooth}}\]

Initialize the TverskyLoss with alpha, beta and smoothing parameters.

Parameters:
  • alpha (float) – Weight for false positives. Default is 0.5.

  • beta (float) – Weight for false negatives. Default is 0.5.

  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “TverskyLoss”.

  • **kwargs – Additional keyword arguments for the base class.

__init__(alpha=0.5, beta=0.5, smooth=1.0, name='TverskyLoss', **kwargs)[source]

Initialize the TverskyLoss with alpha, beta and smoothing parameters.

Parameters:
  • alpha (float) – Weight for false positives. Default is 0.5.

  • beta (float) – Weight for false negatives. Default is 0.5.

  • smooth (float) – Smoothing constant to avoid division by zero. Default is 1.

  • name (str) – Name of the loss function. Default is “TverskyLoss”.

  • **kwargs – Additional keyword arguments for the base class.

call(yTrue, yPred)[source]

Compute the Tversky loss between ground truth and predictions.

Parameters:
  • yTrue (tensorflow.Tensor) – Ground truth binary mask tensor.

  • yPred (tensorflow.Tensor) – Predicted logits tensor.

Returns:

Scalar tensor representing the Tversky loss.

Return type:

tensorflow.Tensor

class HMB.TFSegmentationLosses.FocalLoss(alpha=0.25, gamma=2.0, reduction='mean', name='FocalLoss', **kwargs)[source]

Bases: Loss

Implements Focal Loss for binary segmentation tasks. Focal loss focuses training on hard examples and addresses class imbalance.

\[\text{Focal}(p_t) = - \alpha \times (1-p_t)^{\gamma} \times \log(p_t)\]

Note

For autocasting safety, this implementation uses binary_cross_entropy_with_logits directly on logits.

Initialize the FocalLoss with alpha, gamma and reduction parameters.

Parameters:
  • alpha (float) – Weighting factor for the rare class. Default is 0.25.

  • gamma (float) – Focusing parameter to reduce the relative loss for well-classified examples. Default is 2.0.

  • reduction (str) – Reduction method to apply to the loss. Options are “mean”, “sum”, or “none”. Default is “mean”.

  • name (str) – Name of the loss function. Default is “FocalLoss”.

  • **kwargs – Additional keyword arguments for the base class.

__init__(alpha=0.25, gamma=2.0, reduction='mean', name='FocalLoss', **kwargs)[source]

Initialize the FocalLoss with alpha, gamma and reduction parameters.

Parameters:
  • alpha (float) – Weighting factor for the rare class. Default is 0.25.

  • gamma (float) – Focusing parameter to reduce the relative loss for well-classified examples. Default is 2.0.

  • reduction (str) – Reduction method to apply to the loss. Options are “mean”, “sum”, or “none”. Default is “mean”.

  • name (str) – Name of the loss function. Default is “FocalLoss”.

  • **kwargs – Additional keyword arguments for the base class.

call(yTrue, yPred)[source]

Compute the Focal loss between ground truth and predictions.

Parameters:
  • yTrue (tensorflow.Tensor) – Ground truth binary mask tensor.

  • yPred (tensorflow.Tensor) – Predicted logits tensor.

Returns:

Tensor representing the Focal loss, unreduced.

Return type:

tensorflow.Tensor

class HMB.TFSegmentationLosses.GeneralizedDiceLoss(epsilon=1e-06, name='GeneralizedDiceLoss', dataFormat='channels_last', **kwargs)[source]

Bases: Loss

Implements Generalized Dice Loss for multi-class segmentation tasks. Weights each class inversely to its frequency to address class imbalance.

\[\text{Generalized\ Dice} = 1 - \frac{2 \times \sum_c w_c \sum_i p_{ci} \times g_{ci}}{\sum_c w_c \sum_i (p_{ci} + g_{ci})} \quad \text{where} \quad w_c = \frac{1}{(\sum_i g_{ci})^2}\]

Initialize the GeneralizedDiceLoss with epsilon for numerical stability and data format.

Parameters:
  • epsilon (float) – Small constant to avoid division by zero. Default is 1e-6.

  • name (str) – Name of the loss function. Default is “GeneralizedDiceLoss”.

  • dataFormat (str) – Data format, either “channels_first” or “channels_last”.

  • **kwargs – Additional keyword arguments for the base class.

__init__(epsilon=1e-06, name='GeneralizedDiceLoss', dataFormat='channels_last', **kwargs)[source]

Initialize the GeneralizedDiceLoss with epsilon for numerical stability and data format.

Parameters:
  • epsilon (float) – Small constant to avoid division by zero. Default is 1e-6.

  • name (str) – Name of the loss function. Default is “GeneralizedDiceLoss”.

  • dataFormat (str) – Data format, either “channels_first” or “channels_last”.

  • **kwargs – Additional keyword arguments for the base class.

call(yTrue, yPred)[source]

Compute the Generalized Dice loss between ground truth and predictions for multi-class segmentation.

Parameters:
  • yTrue (tensorflow.Tensor) – Ground truth one-hot encoded tensor of shape (batch, classes, height, width) or (batch, height, width, classes) depending on dataFormat.

  • yPred (tensorflow.Tensor) – Predicted logits tensor of the same shape as yTrue.

Returns:

Tensor representing the Generalized Dice loss, unreduced.

Return type:

tensorflow.Tensor