Mean IoU (mIoU)
You are given predicted and ground truth segmentation masks and need to calculate the mean Intersection over Union across all classes.
For each class c, IoU is computed as:
IoUcβ=TPcβ+FPcβ+FNcβTPcββ
Where for class c:
- TP (True Positive): pixels correctly predicted as class c
- FP (False Positive): pixels incorrectly predicted as class c
- FN (False Negative): pixels of class c predicted as something else
Mean IoU averages over all classes:
mIoU=C1ββc=0Cβ1βIoUcβ
Only include classes that appear in either pred or gt (skip classes with TP=FP=FN=0).
Example:
pred = [[0, 1], [1, 0]] gt = [[0, 1], [0, 0]] num_classes = 2
0.5833
For class 0:
- TP: pixels where pred=0 AND gt=0 β positions (0,0), (1,1) β 2
- FP: pixels where pred=0 AND gtβ 0 β none β 0
- FN: pixels where predβ 0 AND gt=0 β position (1,0) β 1
- IoU_0 = 2/(2+0+1) = 2/3 = 0.6667
For class 1:
- TP: pixels where pred=1 AND gt=1 β position (0,1) β 1
- FP: pixels where pred=1 AND gtβ 1 β position (1,0) β 1
- FN: pixels where predβ 1 AND gt=1 β none β 0
- IoU_1 = 1/(1+1+0) = 1/2 = 0.5
mIoU = (0.6667 + 0.5) / 2 = 0.5833
Constraints:
- pred and gt are 2D lists of class indices
- num_classes is the total number of possible classes
- Return mIoU rounded to 4 decimal places
- If no valid classes exist, return 0.0
You are computing mean Intersection over Union (mIoU) over class labels for semantic segmentation masks.
1. Background Knowledge
In semantic segmentation, each pixel in an image is assigned a class label (e.g., background, road, car). A model outputs a predicted mask, and we compare it to the ground truth mask to evaluate how well it identified each class.
Intersection over Union (IoU) for a class c measures how well the predicted region for that class overlaps with the true region. It is defined as:
IoUcβ=β£predictioncββͺground_truthcββ£β£predictioncββ©ground_truthcββ£βThis can be rewritten using TP, FP, FN counts for pixels of class c:
IoUcβ=TPcβ+FPcβ+FNcβTPcββMean IoU (mIoU) is the average IoU over all classes being evaluated:
mIoU=Cβ²1βcβSββIoUcβwhere S is the set of classes that actually appear in either prediction or ground truth (you skip classes where TPcβ=FPcβ=FNcβ=0).
2. Algorithm / General Approach
High-level pattern:
- Loop over classes cβ{0,β¦,Cβ1}.
- For each class, compute TP, FP, FN by comparing pred and gt pixel-wise.
- If all of TP, FP, FN are zero, skip this class (it doesnβt appear at all).
- Otherwise, compute:
- Collect all valid IoUcβ values and average them to get mIoU.
This is essentially building a per-class confusion summary (TP/FP/FN) from the full confusion matrix and then turning those into IoUs.
3. Step-by-Step Strategy
Assume pred and gt are same shape arrays of integer class labels (e.g., H x W or N x H x W).
- Identify number of classes
- Either given as C, or infer as max(pred.max(), gt.max()) + 1.
- Optional: flatten arrays
- For convenience, convert masks to 1D arrays:
pred_flat = pred.reshape(-1)
gt_flat = gt.reshape(-1)
- Initialize accumulators
- Arrays or dicts for TP, FP, FN per class:
TP = * C
FP = * C
FN = * C
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.