Explore Library
Code QuizIntermediate

IoU for Segmentation

Computing Intersection-over-Union between predicted and ground-truth masks, with a union bug.

Codepython
import numpy as np

pred = np.array([[1, 1, 0],
                 [0, 1, 0],
                 [0, 0, 1]])
gt   = np.array([[1, 0, 0],
                 [0, 1, 0],
                 [0, 1, 1]])

# IoU for the foreground class (label 1)
intersection = np.sum((pred == 1) & (gt == 1))
union = np.sum((pred == 1) & (gt == 1))
iou = intersection / union
print(iou)

What is the bug in this IoU computation?