Mean Intersection over Union (mIoU)
Implement a metric to evaluate the performance of semantic segmentation models, focusing on mean Intersection over Union (mIoU). In semantic segmentation, the goal is to assign a class label to each pixel in an image, and mIoU is a key metric for assessing the accuracy of these assignments. The concept of IoU is based on the idea of comparing the overlap between predicted and actual masks for each class, calculated as IoUc​=TPc​+FPc​+FNc​TPc​​, where TPc​, FPc​, and FNc​ represent true positives, false positives, and false negatives for class c, respectively.
To calculate IoU for each class, one must consider the intersection and union of predicted and actual masks, which can be represented as IoUc​=∣Pc​∪Gc​∣∣Pc​∩Gc​∣​. The process involves:
- Creating binary masks for each class in both predicted and actual labels.
- Computing the intersection (logical AND) and union (logical OR) of these masks.
- Calculating IoU for each class using the intersection and union.
This technique is widely used in autonomous vehicles for scene understanding.
Example:
pred = [[0, 1, 1], [0, 1, 2], [2, 2, 2]] target = [[0, 0, 1], [0, 1, 1], [2, 2, 2]] num_classes = 3
0.72
Class 0: pred=[4 pixels], target=[3 pixels] TP=2, FP=2, FN=1 → IoU = 2/(2+2+1) = 0.4
Class 1: pred=[3 pixels], target=[3 pixels] TP=2, FP=1, FN=1 → IoU = 2/(2+1+1) = 0.5
Class 2: pred=[4 pixels], target=[3 pixels] TP=3, FP=1, FN=0 → IoU = 3/(3+1+0) = 0.75
mIoU = (0.4 + 0.5 + 0.75) / 3 ≈ 0.55
Constraints:
- pred: Predicted segmentation map (H, W) with class indices
- target: Ground truth segmentation (H, W)
- num_classes: Total number of classes
- Return: mIoU score (0-1)
- Background Knowledge
In semantic segmentation, each pixel in an image is assigned a class label (e.g., road, car, background). To evaluate how good these predictions are, we compare the predicted mask with the ground-truth mask per class. Intersection over Union (IoU) for a class c measures how well the predicted region of that class overlaps the true region: it is the size of the intersection divided by the size of the union of predicted and true pixels for that class.
For class c, define:
- Pc​: set of pixels predicted as class c
- Gc​: set of pixels whose ground-truth is class c
Then:
IoUc​=∣Pc​∪Gc​∣∣Pc​∩Gc​∣​=TPc​+FPc​+FNc​TPc​​where TPc​ (true positives) are pixels correctly predicted as c, FPc​ are pixels predicted as c but actually another class, and FNc​ are pixels that are truly c but predicted as something else. Mean IoU (mIoU) is simply the average of IoUc​ over all classes C.
- Algorithm/Approach
General pattern for this problem type:
- Build a confusion matrix M of size C×C, where M[g,p] counts pixels with ground-truth class g and predicted class p.
- For each class c:
- TPc​=M[c,c]
- FNc​=\sum_p M[c, p] - M[c, c](ground−truthc$, predicted something else)
- FPc​=∑g​M[g,c]−M[c,c] (predicted c, ground-truth something else)
- Or more compactly:
- Average valid class IoUs to get mIoU.
In practice, you compute the confusion matrix with a single vectorized operation, then derive all IoUs from it.
- Step-by-Step Strategy
Assume you are given:
- preds: an array of shape (H, W) (or (N, H, W)) with predicted class indices in [0, C-1]
- gts: same shape with ground-truth classes in [0, C-1]
- num_classes = C
Steps:
- Flatten inputs
- If batched, first reshape to 1D:
pred = preds.reshape(-1)
gt = gts.reshape(-1)
- (Optional) Mask out ignored labels
- If an ignore_index is used in the dataset, filter those pixels out before counting.
- Build confusion matrix
- Map each (gt, pred) pair to a single integer idx = gt * C + pred.
- Use bincount (or similar) to count how many times each pair occurs.
- Reshape back to (C, C):
conf = np.bincount(
gt * C + pred, minlength=C*C
).reshape(C, 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.