如何计算图像分割中的平均IU分数?

时间:2015-07-27 12:50:05

标签: computer-vision image-segmentation evaluation-strategy

如何在this论文中计算平均IU (平均交叉联盟)得分?

  

Long,Jonathan,Evan Shelhamer和Trevor Darrell。 "用于语义分割的完全卷积网络。"

4 个答案:

答案 0 :(得分:23)

对于每个班级联盟的交叉(IU)得分为:

  

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

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

关于文件中的符号:

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

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

您可以在Pascak DevKit中找到直接计算的matlab代码here

答案 1 :(得分:9)

 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)

答案 2 :(得分: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)

答案 3 :(得分:0)

jaccard_similarity_score(每个How to find IoU from segmentation masks?)可用于获得与上面@ 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)}')