Created
June 30, 2018 09:37
-
-
Save garybradski/270942bc9904f630a2acfb33e88d3c4d to your computer and use it in GitHub Desktop.
Python implementation of intersection over union, iou
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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