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+FNcTPc, 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:
This technique is widely used in autonomous vehicles for scene understanding.
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