MIoU

M I o U MIoU MIoU

MIoU(Mean Intersection over Union):平均交并比

这是一个标准的衡量metric ,计算两个集合之间交集和并集的比例,在图像分割中,就是真实值(Ground Truth)和预测值两个集合。可以转换为TP(intersection)与TP ,FN ,FP之和(union)的比值。先计算每个类内的交并比,然后计算均值。

实现

gt_image = np.array([
    [0,1,2,4],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0]
])

pre_image = np.array([
    [0,1,2,4],
    [0,1,0,0],
    [0,1,0,0],
    [0,0,1,0]
])

def generate_matrix(gt_image, pre_image,num_class=8):
        mask = (gt_image >= 0) & (gt_image < num_class)#ground truth中所有正确(值在[0, classe_num])的像素label的mask
        
        label = num_class * gt_image[mask].astype('int') + pre_image[mask] 
        # np.bincount计算了从0到n**2-1这n**2个数中每个数出现的次数,返回值形状(n, n)
        count = np.bincount(label, minlength=num_class**2)
        confusion_matrix = count.reshape(num_class, num_class)#21 * 21(for pascal)
        return confusion_matrix
        
matrix =generate_matrix(gt_image,pre_image)
def Mean_Intersection_over_Union(confusion_matrix):
    MIoU = np.diag(confusion_matrix) / (
                np.sum(confusion_matrix, axis=1) + np.sum(confusion_matrix, axis=0) -
                np.diag(confusion_matrix))
    MIoU = np.nanmean(MIoU) #跳过0值求mean,shape:[21]
    return MIoU
 
Mean_Intersection_over_Union(matrix)

你可能感兴趣的:(MIoU)