PyTorchSegmentationLosses Module¶
Implements loss functions for segmentation tasks in PyTorch.
- class HMB.PyTorchSegmentationLosses.DiceLoss(weight=None, size_average=True)[source]¶
Bases:
ModuleImplements 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}}\]- Parameters:
weight (optional) – Not used, for compatibility.
size_average (optional) – Not used, for compatibility.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, targets, smooth=1)[source]¶
Computes the Dice loss between predictions and targets for binary segmentation.
- Parameters:
inputs (torch.Tensor) – Model outputs (logits or probabilities).
targets (torch.Tensor) – Ground truth binary mask.
smooth (float, optional) – Smoothing constant to avoid division by zero. Default is 1.
- Returns:
Dice loss value.
- Return type:
- class HMB.PyTorchSegmentationLosses.DiceBCELoss(weight=None, size_average=True)[source]¶
Bases:
ModuleImplements 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]\]- Parameters:
weight (optional) – Not used, for compatibility.
size_average (optional) – Not used, for compatibility.
Note
For best practice and autocasting safety, use raw logits as inputs.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, targets, smooth=1)[source]¶
Computes the sum of Dice loss and BCE loss for binary segmentation.
- Parameters:
inputs (torch.Tensor) – Model outputs (raw logits).
targets (torch.Tensor) – Ground truth binary mask.
smooth (float, optional) – Smoothing constant to avoid division by zero. Default is 1.
- Returns:
Combined Dice + BCE loss value.
- Return type:
- class HMB.PyTorchSegmentationLosses.JaccardLoss(weight=None, size_average=True)[source]¶
Bases:
ModuleImplements 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}}\]- Parameters:
weight (optional) – Not used, for compatibility.
size_average (optional) – Not used, for compatibility.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, targets, smooth=1)[source]¶
Computes the Jaccard loss (1 - IoU) between predictions and targets for binary segmentation.
- Parameters:
inputs (torch.Tensor) – Model outputs (logits or probabilities).
targets (torch.Tensor) – Ground truth binary mask.
smooth (float, optional) – Smoothing constant to avoid division by zero. Default is 1.
- Returns:
Jaccard loss value.
- Return type:
- class HMB.PyTorchSegmentationLosses.TverskyLoss(alpha=0.5, beta=0.5, weight=None, size_average=True)[source]¶
Bases:
ModuleImplements 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}}\]- Parameters:
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, targets, smooth=1)[source]¶
Computes the Tversky loss between predictions and targets for binary segmentation.
- Parameters:
inputs (torch.Tensor) – Model outputs (logits or probabilities).
targets (torch.Tensor) – Ground truth binary mask.
smooth (float, optional) – Smoothing constant to avoid division by zero. Default is 1.
- Returns:
Tversky loss value.
- Return type:
- class HMB.PyTorchSegmentationLosses.FocalLoss(alpha=0.25, gamma=2.0, reduction='mean')[source]¶
Bases:
ModuleImplements 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)\]- Parameters:
Note
For autocasting safety, this implementation uses binary_cross_entropy_with_logits directly on logits.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, targets)[source]¶
Computes the Focal loss between predictions and targets for binary segmentation.
- Parameters:
inputs (torch.Tensor) – Model outputs (logits).
targets (torch.Tensor) – Ground truth binary mask.
- Returns:
Focal loss value.
- Return type:
- class HMB.PyTorchSegmentationLosses.GeneralizedDiceLoss(epsilon=1e-06)[source]¶
Bases:
ModuleImplements 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 internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs, targets)[source]¶
Computes the Generalized Dice loss for multi-class segmentation.
- Parameters:
inputs (torch.Tensor) – Model outputs (logits) of shape (N, C, …).
targets (torch.Tensor) – Ground truth one-hot mask of shape (N, C, …).
- Returns:
Generalized Dice loss value.
- Return type: