We use segmentation evaluation to check how well a model finds the exact shapes in images. IoU and Dice scores tell us how close the model's guess is to the real shape.
Segmentation evaluation (IoU, Dice) in Computer Vision
def iou_score(pred_mask, true_mask): intersection = (pred_mask & true_mask).sum() union = (pred_mask | true_mask).sum() return intersection / union if union != 0 else 1.0 def dice_score(pred_mask, true_mask): intersection = (pred_mask & true_mask).sum() total = pred_mask.sum() + true_mask.sum() return 2 * intersection / total if total != 0 else 1.0
pred_mask and true_mask are binary masks (arrays of True/False or 1/0) of the same size.
IoU is also called Jaccard Index; Dice is similar but gives more weight to overlap.
pred_mask = [0, 1, 1, 0] true_mask = [1, 1, 0, 0] iou = iou_score(pred_mask, true_mask)
pred_mask = [1, 1, 1, 1] true_mask = [1, 1, 1, 1] dice = dice_score(pred_mask, true_mask)
This program calculates IoU and Dice scores for two small example masks. It shows how much the predicted shape overlaps with the true shape.
import numpy as np def iou_score(pred_mask, true_mask): intersection = np.logical_and(pred_mask, true_mask).sum() union = np.logical_or(pred_mask, true_mask).sum() return intersection / union if union != 0 else 1.0 def dice_score(pred_mask, true_mask): intersection = np.logical_and(pred_mask, true_mask).sum() total = pred_mask.sum() + true_mask.sum() return 2 * intersection / total if total != 0 else 1.0 # Example masks pred_mask = np.array([[0, 1, 1], [0, 1, 0], [0, 0, 0]], dtype=bool) true_mask = np.array([[1, 1, 0], [0, 1, 0], [0, 0, 0]], dtype=bool) print(f"IoU score: {iou_score(pred_mask, true_mask):.2f}") print(f"Dice score: {dice_score(pred_mask, true_mask):.2f}")
IoU and Dice scores range from 0 to 1, where 1 means perfect overlap.
Always use masks of the same size and shape for correct evaluation.
Dice score tends to be higher than IoU for the same masks because it doubles the intersection.
IoU and Dice are simple ways to measure how well a model finds shapes in images.
They compare predicted and true masks by looking at overlap and total area.
Higher scores mean better segmentation quality.
Practice
Solution
Step 1: Understand IoU definition
IoU measures how much the predicted mask and true mask overlap compared to their combined area.Step 2: Apply IoU formula
IoU = (Area of Overlap) / (Area of Union) between predicted and true masks.Final Answer:
The overlap between predicted and true masks divided by their union -> Option AQuick Check:
IoU = Overlap / Union [OK]
- Confusing IoU with difference or subtraction
- Using only predicted mask area
- Mixing IoU with accuracy or precision
Solution
Step 1: Recall Dice coefficient formula
Dice coefficient is defined as twice the overlap divided by the sum of sizes of predicted and true masks.Step 2: Match formula to options
Dice = (2 * |P ∩ T|) / (|P| + |T|) matches Dice = (2 * intersection) / (sum of areas).Final Answer:
Dice = (2 * |P ∩ T|) / (|P| + |T|) -> Option BQuick Check:
Dice = 2*Overlap / (Sum areas) [OK]
- Confusing Dice with IoU formula
- Using union instead of sum of areas
- Forgetting to multiply overlap by 2
Solution
Step 1: Calculate intersection and union
Intersection = {3,4} size = 2; Union = {1,2,3,4,5,6} size = 6.Step 2: Compute IoU
IoU = Intersection / Union = 2 / 6 = 0.3333 (approx). Check options carefully.Step 3: Re-check options
0.33 is 0.33, closest to calculation. But 0.4 is 0.4, which is incorrect.Final Answer:
0.33 -> Option AQuick Check:
IoU = 2/6 = 0.33 [OK]
- Counting union incorrectly
- Mixing intersection with union
- Rounding IoU incorrectly
def dice_score(pred, true):
intersection = len(pred & true)
return intersection / (len(pred) + len(true))
pred = {1,2,3}
true = {2,3,4}
print(dice_score(pred, true))Solution
Step 1: Review Dice formula
Dice = 2 * intersection / (sum of sizes). The code misses multiplying intersection by 2.Step 2: Check code calculation
Code returns intersection / sum, which is half the correct Dice score.Final Answer:
Missing multiplication by 2 in numerator -> Option DQuick Check:
Dice needs 2*intersection [OK]
- Forgetting to multiply intersection by 2
- Confusing intersection with union
- Using wrong data types for masks
Solution
Step 1: Calculate Dice score
Dice = 2 * overlap / (predicted + true) = 2*60 / (80+100) = 120/180 = 0.6667 (approx). Check options carefully.Step 2: Calculate IoU score
IoU = overlap / union = 60 / (80 + 100 - 60) = 60 / 120 = 0.5.Step 3: Re-check options
Dice = 0.67, IoU = 0.5 matches Dice ~0.67 and IoU 0.5 exactly.Final Answer:
Dice = 0.67, IoU = 0.5 -> Option CQuick Check:
Dice = 2*60/180=0.67, IoU=60/120=0.5 [OK]
- Using sum instead of union for IoU denominator
- Forgetting to multiply overlap by 2 for Dice
- Mixing Dice and IoU formulas
Ready to prove it?
Pass the test and this pattern moves unknown
