Segmentation masks are often mostly background, so pixel-wise cross-entropy is dominated by easy pixels. Soft Dice measures overlap directly:
with predicted probabilities , ground truth and smoothing . Implement dice_loss(p, g, smooth=1.0) over all elements, and dice_grad(p, g, smooth=1.0) returning .