पाठ 20 / 26

Segmentation Metrics: IoU and Dice

Measure mask overlap.

Two related scores

Mask quality is measured by overlap with the ground truth. IoU (Jaccard) is intersection over union; Dice (F1 for pixels) is twice the intersection over the sum of both areas. Dice is always at least as high as IoU for the same masks, so never compare a Dice score with an IoU score. Report per class (mean IoU across classes) and per image, because large objects dominate pixel-level averages and small structures (thin vessels, small defects) may be ignored.

IoU and Dice for four predicted masks, run

I ran this on CPU with Python 3, OpenCV 5.0.0, scikit-image 0.26.0, PyTorch 2.14.1 and torchvision 0.29.1, using scikit-image's bundled sample photos and torchvision's published pretrained weights. A 5-pixel shift of a 50 x 50 object gives IoU 0.818 and Dice 0.900; a mask that is too small gives 0.360 and 0.529; one that is too large 0.510 and 0.676. Dice is higher than IoU each time.

import numpy as np
truth = np.zeros((100, 100), bool); truth[20:70, 30:80] = True          # 50x50 object
def scores(pred):
    inter = (pred & truth).sum(); union = (pred | truth).sum()
    return inter / union, 2 * inter / (pred.sum() + truth.sum())
for name, (r0, r1, c0, c1) in [("exact", (20, 70, 30, 80)), ("shifted 5px", (25, 75, 30, 80)),
                               ("too small", (30, 60, 40, 70)), ("too large", (10, 80, 20, 90))]:
    pred = np.zeros_like(truth); pred[r0:r1, c0:c1] = True
    iou, dice = scores(pred)
    print(f"{name:<12} IoU {iou:.3f}  Dice {dice:.3f}")

Output:

exact        IoU 1.000  Dice 1.000
shifted 5px  IoU 0.818  Dice 0.900
too small    IoU 0.360  Dice 0.529
too large    IoU 0.510  Dice 0.676

Watch small structures

Add per-object or boundary metrics when small or thin structures matter more than their pixel count suggests.

त्वरित जाँच: How do Dice and IoU compare for the same masks?

  • Dice is always lower
  • Dice is always greater than or equal to IoU
  • They are identical
  • They measure different images
Answer

Dice is always greater than or equal to IoU — Do not compare one with the other.