Vision metrics
calculate_dsc
def calculate_dsc(
pred:Tensor, targ:Tensor
)->Tensor:
Calculate Dice score using MONAI’s compute_dice.
Accepts tensors of various shapes and automatically reshapes to 5D format.
Args: pred: Binary prediction tensor. Accepts: - [D, H, W] single 3D volume - [C, D, H, W] single volume with channel - [B, C, D, H, W] batched volumes targ: Binary target tensor (same shape options as pred).
Returns: Dice score(s). Single value for 3D/4D input, tensor of values for 5D batch.
calculate_haus
def calculate_haus(
pred:Tensor, targ:Tensor
)->Tensor:
Compute 95th percentile Hausdorff distance (HD95) using MONAI.
HD95 is more robust than standard Hausdorff distance as it ignores the top 5% of outlier distances.
Accepts tensors of various shapes and automatically reshapes to 5D format.
Args: pred: Binary prediction tensor. Accepts: - [D, H, W] single 3D volume - [C, D, H, W] single volume with channel - [B, C, D, H, W] batched volumes targ: Binary target tensor (same shape options as pred).
Returns: HD95 value(s). Single value for 3D/4D input, tensor of values for 5D batch.
binary_dice_score
def binary_dice_score(
act:Tensor, targ:Tensor
)->Tensor:
Calculates the mean Dice score for binary semantic segmentation tasks.
Args: act: Activation tensor with dimensions [B, C, W, H, D]. targ: Target masks with dimensions [B, C, W, H, D].
Returns: Mean Dice score.
multi_dice_score
def multi_dice_score(
act:Tensor, targ:Tensor
)->Tensor:
Calculate the mean Dice score for each class in multi-class semantic segmentation tasks.
Args: act: Activation tensor with dimensions [B, C, W, H, D]. targ: Target masks with dimensions [B, C, W, H, D].
Returns: Mean Dice score for each class.
binary_hausdorff_distance
def binary_hausdorff_distance(
act:Tensor, targ:Tensor
)->Tensor:
Calculate the mean HD95 for binary semantic segmentation tasks.
Args: act: Activation tensor with dimensions [B, C, W, H, D]. targ: Target masks with dimensions [B, C, W, H, D].
Returns: Mean HD95.
multi_hausdorff_distance
def multi_hausdorff_distance(
act:Tensor, targ:Tensor
)->Tensor:
Calculate the mean HD95 for each class in multi-class semantic segmentation tasks.
Args: act: Activation tensor with dimensions [B, C, W, H, D]. targ: Target masks with dimensions [B, C, W, H, D].
Returns: Mean HD95 for each class.
Sensitivity and Precision
calculate_confusion_metrics
def calculate_confusion_metrics(
pred:Tensor, targ:Tensor, metric_name:str
)->Tensor:
Calculate confusion matrix-based metric using MONAI.
Args: pred: Binary prediction tensor [B, C, W, H, D]. targ: Binary target tensor [B, C, W, H, D]. metric_name: One of “sensitivity”, “precision”, “specificity”, “f1 score”.
Returns: Metric values for each sample in batch.
binary_sensitivity
def binary_sensitivity(
act:Tensor, targ:Tensor
)->Tensor:
Calculate mean sensitivity (recall) for binary segmentation.
Sensitivity = TP / (TP + FN) - measures the proportion of actual positives that are correctly identified.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D].
Returns: Mean sensitivity score.
multi_sensitivity
def multi_sensitivity(
act:Tensor, targ:Tensor
)->Tensor:
Calculate mean sensitivity for each class in multi-class segmentation.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D].
Returns: Mean sensitivity for each class.
binary_precision
def binary_precision(
act:Tensor, targ:Tensor
)->Tensor:
Calculate mean precision for binary segmentation.
Precision = TP / (TP + FP) - measures the proportion of positive predictions that are actually correct.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D].
Returns: Mean precision score.
multi_precision
def multi_precision(
act:Tensor, targ:Tensor
)->Tensor:
Calculate mean precision for each class in multi-class segmentation.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D].
Returns: Mean precision for each class.
Lesion Detection Rate
calculate_lesion_detection_rate
def calculate_lesion_detection_rate(
pred:Tensor, targ:Tensor, threshold:float=0.0
)->Tensor:
Calculate lesion-wise detection rate.
For each connected component (lesion) in the target, check if it is detected by the prediction. Detection criteria depends on threshold: - threshold=0: any overlap counts as detected - threshold>0: per-lesion Dice score must exceed threshold
Args: pred: Binary prediction tensor [B, C, W, H, D]. targ: Binary target tensor [B, C, W, H, D]. threshold: Minimum Dice score for a lesion to be considered detected. Default 0.0 means any overlap counts as detected.
Returns: Detection rate (detected lesions / total lesions) for each sample.
binary_lesion_detection_rate
def binary_lesion_detection_rate(
act:Tensor, targ:Tensor, threshold:float=0.0
)->Tensor:
Calculate mean lesion detection rate for binary segmentation.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D]. threshold: Minimum Dice score for a lesion to be considered detected. Default 0.0 means any overlap counts as detected.
Returns: Mean lesion detection rate.
multi_lesion_detection_rate
def multi_lesion_detection_rate(
act:Tensor, targ:Tensor, threshold:float=0.0
)->Tensor:
Calculate mean lesion detection rate for each class in multi-class segmentation.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D]. threshold: Minimum Dice score for a lesion to be considered detected. Default 0.0 means any overlap counts as detected.
Returns: Mean lesion detection rate for each class.
Signed Relative Volume Error (RVE)
calculate_signed_rve
def calculate_signed_rve(
pred:Tensor, targ:Tensor
)->Tensor:
Calculate signed Relative Volume Error.
RVE = (pred_volume - targ_volume) / targ_volume
Positive values indicate over-segmentation (model predicts too large), negative values indicate under-segmentation (model predicts too small).
Args: pred: Binary prediction tensor [B, C, W, H, D]. targ: Binary target tensor [B, C, W, H, D].
Returns: Signed RVE for each sample in batch.
binary_signed_rve
def binary_signed_rve(
act:Tensor, targ:Tensor
)->Tensor:
Calculate mean signed RVE for binary segmentation.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D].
Returns: Mean signed RVE.
multi_signed_rve
def multi_signed_rve(
act:Tensor, targ:Tensor
)->Tensor:
Calculate mean signed RVE for each class in multi-class segmentation.
Args: act: Activation tensor [B, C, W, H, D]. targ: Target masks [B, C, W, H, D].
Returns: Mean signed RVE for each class.
Accumulated Dice Metrics (nnU-Net Style)
These metrics accumulate true positives, false positives, and false negatives across all validation batches before computing Dice. This is more statistically robust than averaging per-batch Dice scores, especially for patch-based training where patches have variable foreground ratios.
AccumulatedDice
def AccumulatedDice(
n_classes:int=2, include_background:bool=False
):
nnU-Net-style accumulated Dice metric for reliable pseudo dice during training.
Instead of averaging per-batch Dice scores, this metric accumulates true positives, false positives, and false negatives across ALL validation batches, then computes Dice from the totals. This gives more weight to batches with more foreground voxels and is more statistically robust.
Args: n_classes: Number of classes including background (default: 2 for binary). include_background: Whether to include background in metric (default: False).
Example: ```python learn = Learner(dls, model, loss_func=loss_func, metrics=[AccumulatedDice(n_classes=2)])
# For checkpoint selection based on accumulated dice:
save_best = SaveModelCallback(
monitor='accumulated_dice',
comp=np.greater, # Higher dice is better
fname='best_model'
)
```
AccumulatedMultiDice
def AccumulatedMultiDice(
n_classes:int=2, include_background:bool=False
):
Multi-class version of AccumulatedDice that returns per-class Dice scores.
Instead of returning a single mean Dice, this returns a tensor with the Dice score for each foreground class. Useful for monitoring per-class performance during training.
Example: python # For 3-class segmentation (background + 2 foreground classes) learn = Learner(dls, model, loss_func=loss_func, metrics=[AccumulatedMultiDice(n_classes=3)])