Condados
← All posts The Computer Vision Metrics Handbook · Chapter 4 of 6 View all →

Segmentation Metrics: IoU, Dice, mIoU, and Panoptic Quality

Scoring masks instead of boxes — pixel IoU/Jaccard and the Dice coefficient (and why they're related), mIoU for semantic segmentation, mask-AP for instance segmentation, and Panoptic Quality for the unified task — on one running cat-mask example, with the equations and code.

Luis Condados · · Updated June 6, 2026
Segmentation Metrics: IoU, Dice, mIoU, and Panoptic Quality

Segmentation predicts a label for every pixel, so its metrics measure region overlap, not box overlap. Two — IoU and Dice — do almost all the work and are secretly the same idea. We’ll compute them on one tiny cat mask, drag the overlap around, and build up to Panoptic Quality.

There are three flavors, each with its own metric: semantic (class per pixel, no instances), instance (separate objects, like detection with masks), and panoptic (both at once).

The running example: segmenting Whiskers’ cat

Back to the cat camera. This time the job isn’t “is there a cat?” but “which pixels are cat?” — a mask. Take a small 10×10=10010\times10 = 100-pixel patch. The true cat covers 20 pixels; the model paints 22 pixels as cat. They overlap on 18 pixels. So, counting pixels:

  • TP = 18 — cat pixels correctly painted,
  • FP = 4 — background pixels wrongly painted cat (22 − 18),
  • FN = 2 — cat pixels missed (20 − 18),
  • TN = 76 — background correctly left alone.

Every metric below is a different ratio of those four counts.

Why not pixel accuracy?

Intuition: count the fraction of pixels labelled correctly — and watch it lie.

IoU / Jaccard, and mIoU

Intuition: ignore the easy background entirely — score only the relevant region, as overlap ÷ total area covered.

IoU=TPTP+FP+FN=PGPG.\text{IoU} = \frac{TP}{TP + FP + FN} = \frac{\lvert P \cap G \rvert}{\lvert P \cup G \rvert}.

Drag the two masks (think of each box as a blob of cat pixels) and watch IoU climb as they line up — and Dice with it:

ground truth prediction
Drag either box.

The semantic-segmentation headline is mean IoU — IoU averaged over the CC classes so rare classes carry equal weight:

mIoU=1Cc=1CIoUc.\text{mIoU} = \frac{1}{C} \sum_{c=1}^{C} \text{IoU}_c.

Dice coefficient (a.k.a. F1 for pixels)

Intuition: the same overlap, but counting the agreement twice — which makes it gentler than IoU on small errors. It’s exactly F1, applied to pixels.

Dice=2TP2TP+FP+FN=2PGP+G.\text{Dice} = \frac{2\,TP}{2\,TP + FP + FN} = \frac{2\lvert P \cap G \rvert}{\lvert P \rvert + \lvert G \rvert}.
DiceIoUDice = IoU0.75 → 0.86
Dice (green) sits above the Dice = IoU diagonal everywhere between 0 and 1 — it’s the more forgiving of the two.

For thin structures, region overlap is unforgiving (one-pixel-wide errors swing it hugely), so boundary IoU or Hausdorff distance complement these by scoring the contour instead [4].

Now you try

Edit the pixel counts — TP, FP, FN, TN — and compare how IoU and Dice react versus the flattering accuracy. Add 10 false-positive pixels (FP): accuracy barely flinches while IoU drops noticeably. That gap is the whole reason segmentation reports IoU, not accuracy.

Instance segmentation: mask AP

When you must separate individual objects, segmentation becomes detection with masks: the entire AP/mAP machinery applies unchanged — just swap box IoU for mask IoU when matching predictions to ground truth. COCO reports it as mask AP (iou_type="segm"). So the LotCam AP playground is the same tool here — only the overlap definition changed.

Panoptic Quality (PQ)

Intuition: one score for “did you find the right objects and draw them well?” — the product of how clean your matched masks are and how many objects you got.

Match predicted and ground-truth segments at IoU > 0.5, then:

PQ=(p,g)TPIoU(p,g)TPSQ (mask quality)×TPTP+12FP+12FNRQ (F1 over segments).\text{PQ} = \underbrace{\frac{\sum_{(p,g) \in TP} \text{IoU}(p, g)}{\lvert TP \rvert}}_{\text{SQ (mask quality)}} \times \underbrace{\frac{\lvert TP \rvert}{\lvert TP \rvert + \tfrac{1}{2}\lvert FP \rvert + \tfrac{1}{2}\lvert FN \rvert}}_{\text{RQ (F1 over segments)}}.
SQ (mask)0.75×RQ (segments)0.67=PQ0.50
PQ splits into mask quality (SQ) and object recognition (RQ), so a low score tells you which half to fix.

In code

import torch
from torchmetrics.classification import MulticlassJaccardIndex

# Semantic segmentation: a class index per pixel.
preds  = torch.randint(0, 4, (2, 256, 256))   # (batch, H, W)
target = torch.randint(0, 4, (2, 256, 256))

miou = MulticlassJaccardIndex(num_classes=4, average="macro")  # mIoU
print("mIoU", miou(preds, target).item())

# Dice for one binary mask, from first principles — our cat mask:
def dice(pred_mask, true_mask, eps=1e-7):
    p, g = pred_mask.bool(), true_mask.bool()
    inter = (p & g).sum().float()
    return (2 * inter / (p.sum() + g.sum() + eps)).item()

# 18 TP, 4 FP, 2 FN  ->  pred has 22 ones, truth has 20 ones, 18 shared
# Dice = 2*18 / (22 + 20) = 36/42 = 0.857

For instance/panoptic, use pycocotools (mask AP) and the official panopticapi (PQ) rather than rolling your own — the matching rules have edge cases.

Caveats & common pitfalls

  • Region-overlap metrics are blind to boundaries. IoU/Dice can look high while thin structures and edges are wrong — report boundary IoU or Hausdorff distance when contour accuracy matters [4].
  • mIoU depends on the class set and how “ignore”/void pixels are handled. Two papers reporting “mIoU” on the same dataset can differ purely on which classes and void regions they count — state the protocol [1].
  • Micro vs per-class mIoU diverge under imbalance. A pixel-pooled IoU is dominated by big classes; the standard semantic headline is the macro mean over classes — don’t mix them up.
  • PQ’s IoU > 0.5 matching is a hard cut. Segments that overlap at 0.49 count as both a false positive and a false negative, so PQ can look harsh near the threshold [3]. Use the official panopticapi for the edge cases.

Takeaways

  • Skip pixel accuracy; background drowns it (0.94 “looked great” while IoU was 0.75).
  • IoU and Dice are the workhorses and rank identically (Dice = 2·IoU/(1+IoU), so 0.75 → 0.86); mIoU (mean over classes) is the semantic-segmentation headline.
  • Instance segmentation = detection with mask IoU — reuse mAP.
  • Panoptic Quality = SQ × RQ (0.75 × 0.67 = 0.50), so a single number still tells you whether masks or detections are the problem.

Next: when objects must keep their identity across time → Object tracking metrics: MOTA, IDF1, and HOTA.

Further reading

  • Go deeper: Szeliski, Computer Vision (2nd ed.) §5.5 — the semantic/instance/panoptic methods behind these metrics.
  • Why Dice dominates medical imaging: Milletari et al., V-Net (2016), arXiv:1606.04797 — the soft-Dice loss in its original setting.
  • Related on CondadosAI: object-detection mAP (mask AP reuses it); next, tracking metrics.

References

[1] Everingham, M., et al. (2015). The PASCAL Visual Object Classes Challenge: A Retrospective. International Journal of Computer Vision, 111(1), 98–136. (mIoU protocol.) DOI:10.1007/s11263-014-0733-5.

[2] Dice, L. R. (1945). Measures of the Amount of Ecologic Association Between Species. Ecology, 26(3), 297–302. DOI:10.2307/1932409.

[3] Kirillov, A., He, K., Girshick, R., Rother, C., & Dollár, P. (2019). Panoptic Segmentation. CVPR. arXiv:1801.00868.

[4] Cheng, B., Girshick, R., Dollár, P., Berg, A. C., & Kirillov, A. (2021). Boundary IoU: Improving Object-Centric Image Segmentation Evaluation. CVPR. arXiv:2103.16562.

[5] Szeliski, R. (2022). Computer Vision: Algorithms and Applications (2nd ed.), §5.5 (segmentation). Springer. Free PDF. Tooling: pycocotools (mask AP), panopticapi (PQ).