Training Utilities

SkiNet.ML.training.training_utils.find_best_threshold(probs: torch.Tensor, targets: torch.Tensor, n_thresholds: int = 51) dict[str, float][source]

Find the optimal threshold that maximizes the Dice (F1) score for binary predictions.

This function evaluates multiple thresholds in a fully vectorized manner on the GPU, avoiding Python loops and repeated device synchronization.

Parameters:
  • probs – 1D tensor of predicted probabilities with shape [N]. Must be on the same device where computation should occur (CPU or GPU).

  • targets – 1D tensor of ground-truth binary labels with shape [N]. Values will be converted to boolean internally.

  • n_thresholds – Number of evenly spaced thresholds in the range [0.0, 1.0]. Defaults to 51.

Returns:

A dict with keys "best_threshold" (float) and "best_dice" (float).