Skip to content

Instantly share code, notes, and snippets.

@garybradski
Created June 30, 2018 09:37
Show Gist options
  • Select an option

  • Save garybradski/270942bc9904f630a2acfb33e88d3c4d to your computer and use it in GitHub Desktop.

Select an option

Save garybradski/270942bc9904f630a2acfb33e88d3c4d to your computer and use it in GitHub Desktop.
Python implementation of intersection over union, iou
# From https://gist.github.com/zacharybell/8d9b1b25749fe6494511f843361bb167
import numpy as np
def mean_iou(labels, predictions, n_classes):
mean_iou = 0.0
seen_classes = 0
for c in range(n_classes):
labels_c = (labels == c)
pred_c = (predictions == c)
labels_c_sum = (labels_c).sum()
pred_c_sum = (pred_c).sum()
if (labels_c_sum > 0) or (pred_c_sum > 0):
seen_classes += 1
intersect = np.logical_and(labels_c, pred_c).sum()
union = labels_c_sum + pred_c_sum - intersect
mean_iou += intersect / union
return mean_iou / seen_classes if seen_classes else 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment