v9.1.1
Menu

Mean IoU (mIoU)

mediumPROSemantic SegmentationSegmentationCV: Recognition

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=TPcTPc+FPc+FNcIoU_c = \frac{TP_c}{TP_c + FP_c + FN_c}

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=1C∑c=0C−1IoUcmIoU = \frac{1}{C}\sum_{c=0}^{C-1} IoU_c

Only include classes that appear in either pred or gt (skip classes with TP=FP=FN=0).

Example

Input
pred = [[0, 1], [1, 0]]
gt = [[0, 1], [0, 0]]
num_classes = 2
Output
0.5833
Reasoning

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

Learn this in

More from CV: Recognition

PRO

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.

solution.py

Test results

Sign in to run your code against the test cases.