Loss Functions

This module provides a collection of customizable loss functions implemented in PyTorch. Each loss function is designed for different tasks, including segmentation and classification.

Adding a Custom Loss Function

To add a custom loss function, you need to define a new class that extends the BaseLoss class and implements the forward method.

Here is an example of how to define a custom loss function class:

from loss import BaseLoss
import torch
import torch.nn as nn

class CustomLoss(BaseLoss):
    """
    CustomLoss is an example implementation extending BaseLoss.

    This class implements a custom loss function logic.
    """

    def __init__(self):
        super().__init__()

    def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
        # Custom loss computation logic
        loss = torch.mean((y_pred - y_true) ** 2)
        return loss

After defining the custom class, you can configure it using a YAML configuration file and update the __init__.py file to make it accessible.

BaseLoss Class

class lis2.loss.BaseLoss(*args, **kwargs)[source]

Bases: TypedConfigurable, Module, ABC

BaseLoss for defining customizable loss functions.

This abstract class provides a structure for loss functions that can be configured dynamically.

Configuration:
  • name (str): The name of the loss function.

abstractmethod forward(y_pred: Tensor, y_true: Tensor) → Tensor[source]

Computes the loss between predictions and ground truth.

Parameters:
  • y_pred (torch.Tensor) – The predicted tensor.

  • y_true (torch.Tensor) – The ground truth tensor.

Returns:

Computed loss value.

Return type:

torch.Tensor

The BaseLoss class is an abstract base class that provides a structure for defining customizable loss functions. It allows dynamic configuration of loss functions.

Configuration:
  • name (str): The name of the loss function.

Loss Zoo

DiceLoss Class

class lis2.loss.DiceLoss(*args, **kwargs)[source]

Bases: BaseLoss

DiceLoss for segmentation tasks.

This loss function calculates the Dice coefficient loss, which is commonly used in segmentation tasks to measure overlap between the predicted and ground truth masks.

Configuration:
  • name (str): The name of the loss function.

  • smooth (float): Smoothing factor to avoid division by zero. Default is 1.0.

Example Configuration (YAML):
name: "dice_loss"
smooth: 1.0
Aliases:

dice_loss

aliases = ['dice_loss']
config_schema: Dict = {'smooth': Schema(type=<class 'float'>, aliases=[], optional=True, default=1.0)}
forward(y_pred: Tensor, y_true: Tensor) → Tensor[source]

Computes the loss between predictions and ground truth.

Parameters:
  • y_pred (torch.Tensor) – The predicted tensor.

  • y_true (torch.Tensor) – The ground truth tensor.

Returns:

Computed loss value.

Return type:

torch.Tensor

The DiceLoss class implements the Dice coefficient loss, which is commonly used in segmentation tasks to measure overlap between predicted and ground truth masks.

Configuration:
  • name (str): The name of the loss function.

  • smooth (float): Smoothing factor to avoid division by zero. Default is 1.0.

Example Configuration (YAML):

name: "dice_loss"
smooth: 1.0
Aliases:
  • dice_loss

BinaryCrossEntropyDiceSum Class

class lis2.loss.BinaryCrossEntropyDiceSum(*args, **kwargs)[source]

Bases: BaseLoss

BinaryCrossEntropyDiceSum combines BCE and Dice loss.

This loss function is a weighted sum of Binary Cross-Entropy (BCE) loss and Dice loss, commonly used in segmentation tasks to balance pixel-wise classification with region-based overlap.

Configuration:
  • name (str): The name of the loss function.

  • alpha (float): Weighting factor for BCE loss. Default is 0.5.

Example Configuration (YAML):
name: "bce_dice_loss"
alpha: 0.5
Aliases:

bce_dice_loss

aliases = ['bce_dice_loss']
config_schema: Dict = {'alpha': Schema(type=<class 'float'>, aliases=[], optional=True, default=0.5)}
forward(y_pred: Tensor, y_true: Tensor) → Tensor[source]

Computes the loss between predictions and ground truth.

Parameters:
  • y_pred (torch.Tensor) – The predicted tensor.

  • y_true (torch.Tensor) – The ground truth tensor.

Returns:

Computed loss value.

Return type:

torch.Tensor

The BinaryCrossEntropyDiceSum class combines Binary Cross-Entropy (BCE) loss and Dice loss. It is commonly used in segmentation tasks to balance pixel-wise classification with region-based overlap.

Configuration:
  • name (str): The name of the loss function.

  • alpha (float): Weighting factor for BCE loss. Default is 0.5.

Example Configuration (YAML):

name: "bce_dice_loss"
alpha: 0.5
Aliases:
  • bce_dice_loss