23

如何计算本文中的平均IU(平均交叉联合)分数?

朗、乔纳森、埃文·谢尔哈默和特雷弗·达雷尔。“用于语义分割的全卷积网络。”

4

4 回答 4

33

对于每个班级的交集(IU)分数是:

真阳性/(真阳性+假阳性+假阴性)

平均 IU只是所有类别的平均值。


关于论文中的符号:

  • n_cl : 类数
  • t_i : i类中的像素总数
  • n_ij :预测属于j类的i类的像素数。所以对于类:

    • n_ii :正确分类的像素数(真阳性)
    • n_ij :错误分类的像素数(误报)
    • n_ji :错误未分类的像素数(假阴性)

你可以在这里找到直接在 Pascak DevKit 中计算的 matlab 代码

于 2015-08-02T18:20:25.280 回答
16
 from sklearn.metrics import confusion_matrix  
 import numpy as np

 def compute_iou(y_pred, y_true):
     # ytrue, ypred is a flatten vector
     y_pred = y_pred.flatten()
     y_true = y_true.flatten()
     current = confusion_matrix(y_true, y_pred, labels=[0, 1])
     # compute mean iou
     intersection = np.diag(current)
     ground_truth_set = current.sum(axis=1)
     predicted_set = current.sum(axis=0)
     union = ground_truth_set + predicted_set - intersection
     IoU = intersection / union.astype(np.float32)
     return np.mean(IoU)
于 2018-07-16T12:38:37.477 回答
2

这应该有帮助

def computeIoU(y_pred_batch, y_true_batch):
    return np.mean(np.asarray([pixelAccuracy(y_pred_batch[i], y_true_batch[i]) for i in range(len(y_true_batch))])) 

def pixelAccuracy(y_pred, y_true):
    y_pred = np.argmax(np.reshape(y_pred,[N_CLASSES_PASCAL,img_rows,img_cols]),axis=0)
    y_true = np.argmax(np.reshape(y_true,[N_CLASSES_PASCAL,img_rows,img_cols]),axis=0)
    y_pred = y_pred * (y_true>0)

    return 1.0 * np.sum((y_pred==y_true)*(y_true>0)) /  np.sum(y_true>0)
于 2017-02-11T14:05:20.570 回答
0

jaccard_similarity_score根据How to find IoU from segmentation mask?)可以用来获得与上面@Alex-zhai 的代码相同的结果:

import numpy as np
from sklearn.metrics import jaccard_score

y_true = np.array([[0, 1, 1],
                   [1, 1, 0]])
y_pred = np.array([[1, 1, 1],
                   [1, 0, 0]])

labels = [0, 1]
jaccards = []
for label in labels:
    jaccard = jaccard_score(y_pred.flatten(),y_true.flatten(), pos_label=label)
    jaccards.append(jaccard)
print(f'avg={np.mean(jaccards)}')
于 2020-10-09T13:29:12.893 回答