PIXELBANKv9.1.0
Menu

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
πŸ”’

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

0/0
Run code to see test results.
Mean IoU (mIoU) - Medium | PixelBank