准确度判断 语义分割_【语义分割】评价指标总结及代码实现

本文详述了语义分割中常用的评估指标,包括像素准确率(PA)、平均像素准确率(MPA)、平均交并比(mIoU)和频权交并比(FWIoU),并基于混淆矩阵提供了Python代码实现。
摘要由CSDN通过智能技术生成

本文记录了语义分割准确性评价指标的总结以及代码实现

对于像素级别的分类,最常用的评价指标是Pixel Accuracy(像素准确率)和Mean Inetersection over Union(平均交并比),二者的计算都是建立在混淆矩阵的基础上的。因此首先来介绍一下混淆矩阵,之后分别介绍PA,MPA,MIoU,FWIoU,最后附上代码实现。

首先假定数据集中有n+1类(0~n+1),0通常表示背景。使用Pii表示原本为i类同时预测为i类,即真正(TP)和真负(TN), Pij表示原本为i类被预测为j类,即假正(FP)和假负(FN),如果第i类为正类,i!=j时,那么Pii表示TP,Pjj表示TN,Pij表示FP,Pji表示FN。

像素准确率(PA)

像素准确率是所有分类正确的像素数占像素总数的比例。公式化如下:

e1c1985124fd5f16a3aeb73615274fb9.png

利用混淆矩阵计算则为(对角线元素之和除以矩阵所有元素之和)

84cab6216fe84f29c11db8932b4d1456.png

平均像素准确率(MPA)

平均像素准确率是分别计算每个类别分类正确的像素数占所有预测为该类别像素数的比例,即精确率,然后累加求平均。公式化如下:

9c894965b89ab5834cadaefa27147b9e.png

利用混淆矩阵计算公式为(每一类的精确率Pi都等于对角线上的TP除以对应列的像素数)

a7c0286cc6e7629a1505ec7ff43d5c14.png

平均交并比(mloU)

平均交并比是对每一类预测的结果和真实值的交集与并集的比值求和平均的结果。公式化如下

8827775d0aec2e69a9804fcaa8819f06.png

IoU利用混淆矩阵计算:

cc242bc779f7722b23624d339fd2cbef.png

解释如下:

如图所示,仅仅针对某一类来说,红色部分代表真实值,真实值有两部分组成TP,FN;黄色部分代表预测值,预测值有两部分组成TP,FP;白色部分代表TN(真负);

所以他们的交集就是TP+FP+FN,并集为TP

b61766f2139d162bd939566a440699fb.png

频权交并比(FWloU)

频权交并比是根据每一类出现的频率设置权重,权重乘以每一类的IoU并进行求和。公式化如下:

82d6f7ae8d66e7c151715a51be3a213c.png

利用混淆矩阵计算:每个类别的真实数目为TP+FN,总数为TP+FP+TN+FN,其中每一类的权重和其IoU的乘积计算公式如下,在将所有类别的求和即可

c9bed13a6957c0aa463e77a1aeb4ab87.png

代码实现

"""

refer to https://github.com/jfzhang95/pytorch-deeplab-xception/blob/master/utils/metrics.py

"""

import numpy as np

__all__ = ['SegmentationMetric']

"""

confusionMetric

P\L P N

P TP FP

N FN TN

"""

class SegmentationMetric(object):

def __init__(self, numClass):

self.numClass = numClass

self.confusionMatrix = np.zeros((self.numClass,)*2)

def pixelAccuracy(self):

# return all class overall pixel accuracy

# acc = (TP + TN) / (TP + TN + FP + TN)

acc = np.diag(self.confusionMatrix).sum() / self.confusionMatrix.sum()

return acc

def classPixelAccuracy(self):

# return each category pixel accuracy(A more accurate way to call it precision)

# acc = (TP) / TP + FP

classAcc = np.diag(self.confusionMatrix) / self.confusionMatrix.sum(axis=1)

return classAcc

def meanPixelAccuracy(self):

classAcc = self.classPixelAccuracy()

meanAcc = np.nanmean(classAcc)

return meanAcc

def meanIntersectionOverUnion(self):

# Intersection = TP Union = TP + FP + FN

# IoU = TP / (TP + FP + FN)

intersection = np.diag(self.confusionMatrix)

union = np.sum(self.confusionMatrix, axis=1) + np.sum(self.confusionMatrix, axis=0) - np.diag(self.confusionMatrix)

IoU = intersection / union

mIoU = np.nanmean(IoU)

return mIoU

def genConfusionMatrix(self, imgPredict, imgLabel):

# remove classes from unlabeled pixels in gt image and predict

mask = (imgLabel >= 0) & (imgLabel < self.numClass)

label = self.numClass * imgLabel[mask] + imgPredict[mask]

count = np.bincount(label, minlength=self.numClass**2)

confusionMatrix = count.reshape(self.numClass, self.numClass)

return confusionMatrix

def Frequency_Weighted_Intersection_over_Union(self):

# FWIOU = [(TP+FN)/(TP+FP+TN+FN)] *[TP / (TP + FP + FN)]

freq = np.sum(self.confusion_matrix, axis=1) / np.sum(self.confusion_matrix)

iu = np.diag(self.confusion_matrix) / (

np.sum(self.confusion_matrix, axis=1) + np.sum(self.confusion_matrix, axis=0) -

np.diag(self.confusion_matrix))

FWIoU = (freq[freq > 0] * iu[freq > 0]).sum()

return FWIoU

def addBatch(self, imgPredict, imgLabel):

assert imgPredict.shape == imgLabel.shape

self.confusionMatrix += self.genConfusionMatrix(imgPredict, imgLabel)

def reset(self):

self.confusionMatrix = np.zeros((self.numClass, self.numClass))

if __name__ == '__main__':

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

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

metric = SegmentationMetric(3)

metric.addBatch(imgPredict, imgLabel)

acc = metric.pixelAccuracy()

mIoU = metric.meanIntersectionOverUnion()

print(acc, mIoU)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值