大佬教程收集整理的这篇文章主要介绍了遥感图像语义分割基础知识——常用精度指标及其Python实现,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
深度学习中的常用精度指标:
前言
在介绍精度指标前我们先来明确以下几个概念,对应的示意图如下图所示:
TP(True PositivE):分类准确的正类,意思是预测结果为正类,实际上是正类。
FP(false PositivE):被错分类为正类的负类,意思是实际为负类,但是却被预测为正类。
TN(True NegativE):分类准确的负类,意思是预测结果为负类,实际上是负类。
FN(false NegativE):被错分类为负类的正类,意思是实际为正类,但是却被预测为负类。
精度评价指标
1.精确率
精确率(Precision)就是被准确分类为正类的样本数与所有被分类为正类的样本数之比,意味着预测结果是正类的样本里具体有多少个样本真的是正类,计算方法如下式所示:
2.召回率召回率(Recall)就是被分为正类的样本数与测试数据集中的实际正类的样本数之比,意味着应该被分为正类的样本中会有多少是被正确分类出来,如下式所示:
我们希望精确率和召回率同时非常高。但实际上这两个指标是一对矛盾体,无法做到双高。如果想要找到二者之间的一个平衡点,我们就需要一个新的指标:F1分数(F1-score)。F1分数同时考虑了查准率和查全率,让二者同时达到最高,取一个平衡。计算方法如下式所示:
4.交并比
交并比(Intersection-over-Union, IoU)是指实际类别样本和预测类别样本的交集和并集之比,即分类准确的正类样本数和分类准确的正类样本数与被错分类为负类的正类样本数以及被错分类为正类的负类之和的比值。计算方法如下式所示:
5.平均交并比
平均交并比(mean Intersection-over-Union, mIoU)是对每一类交并比求和平均的结果。计算方法如下式所示:
6 频权交并比
频权交并比(Frequency Weighted Intersection-over-Union, FWIoU)是根据每一类出现的频率设置权重,权重乘以每一类的IoU并进行求和。计算方法如下式所示:
import numpy as np
import cv2
import os
"""
混淆矩阵
PL P N
P TP FP
N FN TN
"""
# 获取颜色字典
# labelFolder 标签文件夹,之所以遍历文件夹是因为一张标签可能不包含所有类别颜色
# classNum 类别总数(含背景)
def color_Dict(labelFolder, classNum):
colorDict = []
# 获取文件夹内的文件名
ImagenameList = os.listdir(labelFolder)
for i in range(len(ImagenameList)):
ImagePath = labelFolder + "/" + ImagenameList[i]
img = cv2.imread(ImagePath).astype(np.uint32)
# 如果是灰度,转成RGB
if (len(img.shapE) == 2):
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB).astype(np.uint32)
# 为了提取唯一值,将RGB转成一个数
img_new = img[:, :, 0] * 1000000 + img[:, :, 1] * 1000 + img[:, :, 2]
unique = np.unique(img_new)
# 将第i个像素矩阵的唯一值添加到colorDict中
for j in range(unique.shape[0]):
colorDict.append(unique[j])
# 对目前i个像素矩阵里的唯一值再取唯一值
colorDict = sorted(set(colorDict))
# 若唯一值数目等于总类数(包括背景)ClassNum,停止遍历剩余的图像
if (len(colorDict) == classNum):
break
# 存储颜色的BGR字典,用于预测时的渲染结果
colorDicT_BGR = []
for k in range(len(colorDict)):
# 对没有达到九位数字的结果进行左边补零(eg:5,201,111->005,201,111)
color = str(colorDict[k]).rjust(9, '0')
# 前3位B,中3位G,后3位R
color_BGR = [int(color[0: 3]), int(color[3: 6]), int(color[6: 9])]
colorDicT_BGR.append(color_BGR)
# 转为numpy格式
colorDicT_BGR = np.array(colorDicT_BGR)
# 存储颜色的GRAY字典,用于预处理时的onehot编码
colorDict_GRAY = colorDicT_BGR.reshape((colorDicT_BGR.shape[0], 1, colorDicT_BGR.shape[1])).astype(np.uint8)
colorDict_GRAY = cv2.cvtColor(colorDict_GRAY, cv2.COLOR_BGR2GRAY)
return colorDicT_BGR, colorDict_GRAY
def ConfusionMatrix(numClass, imgPreDict, Label):
# 返回混淆矩阵
mask = (Label >= 0) & (Label < numClass)
label = numClass * Label[mask] + imgPreDict[mask]
count = np.bincount(label, minlength=numClass ** 2)
confusionMatrix = count.reshape(numClass, numClass)
return confusionMatrix
def OverallAccuracy(confusionMatriX):
# 返回所有类的整体像素精度OA
# acc = (TP + TN) / (TP + TN + FP + TN)
OA = np.diag(confusionMatriX).sum() / confusionMatrix.sum()
return OA
def Precision(confusionMatriX):
# 返回所有类别的精确率precision
precision = np.diag(confusionMatriX) / confusionMatrix.sum(axis=0)
return precision
def Recall(confusionMatriX):
# 返回所有类别的召回率recall
recall = np.diag(confusionMatriX) / confusionMatrix.sum(axis=1)
return recall
def F1Score(confusionMatriX):
precision = np.diag(confusionMatriX) / confusionMatrix.sum(axis=0)
recall = np.diag(confusionMatriX) / confusionMatrix.sum(axis=1)
f1score = 2 * precision * recall / (precision + recall)
return f1score
def IntersectionOverUnion(confusionMatriX):
# 返回交并比IoU
intersection = np.diag(confusionMatriX)
union = np.sum(confusionMatrix, axis=1) + np.sum(confusionMatrix, axis=0) - np.diag(confusionMatriX)
IoU = intersection / union
return IoU
def MeanIntersectionOverUnion(confusionMatriX):
# 返回平均交并比mIoU
intersection = np.diag(confusionMatriX)
union = np.sum(confusionMatrix, axis=1) + np.sum(confusionMatrix, axis=0) - np.diag(confusionMatriX)
IoU = intersection / union
mIoU = np.nanmean(IoU)
return mIoU
def Frequency_Weighted_Intersection_over_Union(confusionMatriX):
# 返回频权交并比FWIoU
freq = np.sum(confusionMatrix, axis=1) / np.sum(confusionMatriX)
iu = np.diag(confusionMatriX) / (
np.sum(confusionMatrix, axis=1) +
np.sum(confusionMatrix, axis=0) -
np.diag(confusionMatriX))
FWIoU = (freq[freq > 0] * iu[freq > 0]).sum()
return FWIoU
#################################################################
# 标签图像文件夹
LabelPath = r""
# 预测图像文件夹
PreDictPath = r""
# 类别数目(包括背景)
classNum = 2
#################################################################
# 获取类别颜色字典
colorDicT_BGR, colorDict_GRAY = color_Dict(LabelPath, classNum)
# 获取文件夹内所有图像
labelList = os.listdir(LabelPath)
PreDictList = os.listdir(PreDictPath)
# 读取第一个图像,后面要用到它的shape
Label0 = cv2.imread(LabelPath + "//" + labelList[0], 0)
# 图像数目
label_num = len(labelList)
# 把所有图像放在一个数组里
label_all = np.zeros((label_num,) + Label0.shape, np.uint8)
preDict_all = np.zeros((label_num,) + Label0.shape, np.uint8)
for i in range(label_num):
Label = cv2.imread(LabelPath + "//" + labelList[i])
Label = cv2.cvtColor(Label, cv2.COLOR_BGR2GRAY)
label_all[i] = Label
PreDict = cv2.imread(PreDictPath + "//" + PreDictList[i])
PreDict = cv2.cvtColor(PreDict, cv2.COLOR_BGR2GRAY)
preDict_all[i] = PreDict
# 把颜色映射为0,1,2,3...
for i in range(colorDict_GRAY.shape[0]):
label_all[label_all == colorDict_GRAY[i][0]] = i
preDict_all[preDict_all == colorDict_GRAY[i][0]] = i
# 拉直成一维
label_all = label_all.flatten()
preDict_all = preDict_all.flatten()
# 计算混淆矩阵及各精度参数
confusionMatrix = ConfusionMatrix(classNum, preDict_all, label_all)
precision = Precision(confusionMatriX)
recall = Recall(confusionMatriX)
OA = OverallAccuracy(confusionMatriX)
IoU = IntersectionOverUnion(confusionMatriX)
FWIOU = Frequency_Weighted_Intersection_over_Union(confusionMatriX)
mIOU = MeanIntersectionOverUnion(confusionMatriX)
f1ccore = F1Score(confusionMatriX)
for i in range(colorDicT_BGR.shape[0]):
# 输出类别颜色,需要安装webcolors,直接pip install webcolors
try:
import webcolors
rgb = colorDicT_BGR[i]
rgb[0], rgb[2] = rgb[2], rgb[0]
print(webcolors.rgb_to_name(rgb), end=" ")
# 不安装的话,输出灰度值
except:
print(colorDict_GRAY[i][0], end=" ")
print("")
print("混淆矩阵:")
print(confusionMatriX)
print("精确度:")
print(precision)
print("召回率:")
print(recall)
print("F1-Score:")
print(f1ccorE)
print("整体精度:")
print(OA)
print("IoU:")
print(IoU)
print("mIoU:")
print(mIOU)
print("FWIoU:")
print(FWIOU)
感谢知乎大佬王正庆,大佬地址https://www.zhihu.com/people/WangZhenqing_AIR
参考博客:https://zhuanlan.zhihu.com/p/150556249
以上是大佬教程为你收集整理的遥感图像语义分割基础知识——常用精度指标及其Python实现全部内容,希望文章能够帮你解决遥感图像语义分割基础知识——常用精度指标及其Python实现所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。