Soft Dice Loss for Segmentation

medium
Unit 19 · Memory0/2 to milestone

Segmentation masks are often mostly background, so pixel-wise cross-entropy is dominated by easy pixels. Soft Dice measures overlap directly:

Dice=2∑p g+s∑p+∑g+s,L=1−Dice\text{Dice} = \frac{2\sum p\,g + s}{\sum p + \sum g + s},\qquad L = 1 - \text{Dice}

with predicted probabilities pp, ground truth g∈{0,1}g \in \{0,1\} and smoothing ss. Implement dice_loss(p, g, smooth=1.0) over all elements, and dice_grad(p, g, smooth=1.0) returning ∂L/∂p\partial L/\partial p.

#segmentation#losses#imbalance2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Soft Dice Loss for Segmentation · TechGradify