PIXELBANKv8.2.1
Menu

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=TPcTPc+FPc+FNcIoU_c = \frac{TP_c}{TP_c + FP_c + FN_c}, where TPcTP_c, FPcFP_c, and FNcFN_c represent true positives, false positives, and false negatives for class cc, 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=PcGcPcGcIoU_c = \frac{|P_c \cap G_c|}{|P_c \cup G_c|}. The process involves:

  1. Creating binary masks for each class in both predicted and actual labels.
  2. Computing the intersection (logical AND) and union (logical OR) of these masks.
  3. Calculating IoU for each class using the intersection and union.
mIoU=1Cc=1CIoUcmIoU = \frac{1}{C} \sum_{c=1}^{C} IoU_c

This technique is widely used in autonomous vehicles for scene understanding.

Example:

Input:
pred = [[0, 1, 1], [0, 1, 2], [2, 2, 2]]
target = [[0, 0, 1], [0, 1, 1], [2, 2, 2]]
num_classes = 3
Output:
0.72
Reasoning:

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)
Editor

Test Results

0/0
Run code to see test results.