wound_segmentation.utils module
Utility functions for wound segmentation evaluation and result visualization.
This module contains helper functions used throughout the segmentation pipeline, including:
Metric computation (e.g., Dice coefficient and Dice-based loss)
Combined loss function (BCE + Dice) for model training.
Visualization and saving of segmentation results.
These utilities are designed to be modular and testable.
Functions
dice_coef : Compute Dice similarity coefficient between two binary masks.
dice_loss : Calculate Dice loss from Dice coefficient.
bce_dice_loss_weighted : Weighted combination of BCE and Dice losses.
save_result : Generate and save a 3-panel plot (input, mask, overlay).
- wound_segmentation.utils.dice_coef(y_true: tensorflow.Tensor, y_pred: tensorflow.Tensor, smooth: float = 1e-06) tensorflow.Tensor
This function computes the Dice coefficient, a measure of similarity between two binary masks.
- Parameters:
y_true (tf.Tensor) -- Ground truth binary mask.
y_pred (tf.Tensor) -- Predicted binary mask.
smooth (float, optional) -- Smoothing constant to avoid division by zero (default is 1e-6).
- Returns:
Dice similarity coefficient.
- Return type:
tf.Tensor
- Raises:
TypeError -- If y_true or y_pred is not a tf.Tensor.
Examples
>>> dice_coef(y_true, y_pred)
- wound_segmentation.utils.dice_loss(y_true: tensorflow.Tensor, y_pred: tensorflow.Tensor) tensorflow.Tensor
This function computes the Dice loss, defined as 1 - Dice coefficient.
- Parameters:
y_true (tf.Tensor) -- Ground truth binary mask.
y_pred (tf.Tensor) -- Predicted binary mask.
- Returns:
Dice loss value in the range [0, 1].
- Return type:
tf.Tensor
Examples
>>> dice = dice_loss(y_true, y_pred)
- wound_segmentation.utils.bce_dice_loss_weighted(y_true: tensorflow.Tensor, y_pred: tensorflow.Tensor, alpha: float = 0.2) tensorflow.Tensor
This function computes a weighted combination of Binary Crossentropy and Dice loss.
- Parameters:
y_true (tf.Tensor) -- Ground truth binary mask.
y_pred (tf.Tensor) -- Predicted binary mask..
alpha (float, optional) -- Weight for BCE loss; (1 - alpha) is used for Dice loss.
- Returns:
Weighted loss value.
- Return type:
tf.Tensor
- Raises:
ValueError -- If alpha is not in the range [0, 1].
Examples
>>> bce_dice_loss_weighted(y_true, y_pred, alpha=0.3)
- wound_segmentation.utils.save_result(image: numpy.ndarray, mask: numpy.ndarray, basename: str, output_dir: str) None
This function saves the input image, predicted mask and the overlay of the mask on the image, side-by-side as a PNG.
- Parameters:
image (np.ndarray) -- Input RGB image of shape (H, W, 3).
mask (np.ndarray) -- Binary predicted mask.
basename (str) -- Output image file name prefix.
output_dir (str) -- Directory path to save the result image.
- Raises:
OSError -- If the output directory cannot be created.
Examples
>>> save_result(image, mask, "sample", "outputs/")