IoUとは: Intersection over Union (IoU) for object detection - PyImageSearch
import numpy as np from sklearn.metrics import jaccard_score N = 100 # 精度 # rect: top, left, bottom, right (0~1, float) def rect_to_array(rect): top, left, bottom, right = (np.array(rect) * N).astype(int) box = np.pad(np.ones((bottom - top, right - left)), ((top, N - bottom), (left, N - right))) return box def iou(true, pred): true_array = rect_to_array(true) pred_array = rect_to_array(pred) return jaccard_score(true_array.flatten(), pred_array.flatten()) print (iou((0.5,0.5,1,1), (0,0, 0.8, 0.8))) # 0.1125