日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python实现语义分割_遥感图像语义分割常用精度指标及其python实现(支持多类)

發(fā)布時(shí)間:2023/12/10 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现语义分割_遥感图像语义分割常用精度指标及其python实现(支持多类) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

在介紹個(gè)精度指標(biāo)前我們先來(lái)明確以下幾個(gè)概念,對(duì)應(yīng)的示意圖如下圖所示:TP(True Positive):分類準(zhǔn)確的正類,意思是預(yù)測(cè)結(jié)果為正類,實(shí)際上是正類。

FP(False Positive):被錯(cuò)分類為正類的負(fù)類,意思是實(shí)際為負(fù)類,但是卻被預(yù)測(cè)為正類。

TN(True Negative):分類準(zhǔn)確的負(fù)類,意思是預(yù)測(cè)結(jié)果為負(fù)類,實(shí)際上是負(fù)類。

FN(False Negative):被錯(cuò)分類為負(fù)類的正類,意思是實(shí)際為正類,但是卻被預(yù)測(cè)為負(fù)類。

精度指標(biāo)

1 精確率

精確率(Precision)就是被準(zhǔn)確分類為正類的樣本數(shù)與所有被分類為正類的樣本數(shù)之比,意味著預(yù)測(cè)結(jié)果是正類的樣本里具體有多少個(gè)樣本真的是正類,計(jì)算方法如下式所示:

2 召回率

召回率(Recall)就是被分為正類的樣本數(shù)與測(cè)試數(shù)據(jù)集中的實(shí)際正類的樣本數(shù)之比,意味著應(yīng)該被分為正類的樣本中會(huì)有多少是被正確分類出來(lái),如下式所示:

3 F1分?jǐn)?shù)

我們希望精確率和召回率同時(shí)非常高。但實(shí)際上這兩個(gè)指標(biāo)是一對(duì)矛盾體,無(wú)法做到雙高。如果想要找到二者之間的一個(gè)平衡點(diǎn),我們就需要一個(gè)新的指標(biāo):F1分?jǐn)?shù)(F1-Score)。F1分?jǐn)?shù)同時(shí)考慮了查準(zhǔn)率和查全率,讓二者同時(shí)達(dá)到最高,取一個(gè)平衡。

4 交并比

交并比(Intersection-over-Union, IoU)是指實(shí)際類別樣本和預(yù)測(cè)類別樣本的交集和并集之比,即分類準(zhǔn)確的正類樣本數(shù)和分類準(zhǔn)確的正類樣本數(shù)與被錯(cuò)分類為負(fù)類的正類樣本數(shù)以及被錯(cuò)分類為正類的負(fù)類之和的比值。

5 平均交并比

平均交并比(mean Intersection-over-Union, mIoU)是對(duì)每一類交并比求和平均的結(jié)果。

6 頻權(quán)交并比

頻權(quán)交并比(Frequency Weighted Intersection-over-Union, FWIoU)是根據(jù)每一類出現(xiàn)的頻率設(shè)置權(quán)重,權(quán)重乘以每一類的IoU并進(jìn)行求和。

python實(shí)現(xiàn)

import numpy as np

import cv2

import os

"""混淆矩陣P\L P NP TP FPN FN TN"""

# 獲取顏色字典

# labelFolder 標(biāo)簽文件夾,之所以遍歷文件夾是因?yàn)橐粡垬?biāo)簽可能不包含所有類別顏色

# classNum 類別總數(shù)(含背景)

def color_dict(labelFolder, classNum):

colorDict = []

# 獲取文件夾內(nèi)的文件名

ImageNameList = os.listdir(labelFolder)

for i in range(len(ImageNameList)):

ImagePath = labelFolder + "/" + ImageNameList[i]

img = cv2.imread(ImagePath).astype(np.uint32)

# 如果是灰度,轉(zhuǎn)成RGB

if(len(img.shape) == 2):

img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB).astype(np.uint32)

# 為了提取唯一值,將RGB轉(zhuǎn)成一個(gè)數(shù)

img_new = img[:,:,0] * 1000000 + img[:,:,1] * 1000 + img[:,:,2]

unique = np.unique(img_new)

# 將第i個(gè)像素矩陣的唯一值添加到colorDict中

for j in range(unique.shape[0]):

colorDict.append(unique[j])

# 對(duì)目前i個(gè)像素矩陣?yán)锏奈ㄒ恢翟偃∥ㄒ恢?/p>

colorDict = sorted(set(colorDict))

# 若唯一值數(shù)目等于總類數(shù)(包括背景)ClassNum,停止遍歷剩余的圖像

if(len(colorDict) == classNum):

break

# 存儲(chǔ)顏色的BGR字典,用于預(yù)測(cè)時(shí)的渲染結(jié)果

colorDict_BGR = []

for k in range(len(colorDict)):

# 對(duì)沒(méi)有達(dá)到九位數(shù)字的結(jié)果進(jìn)行左邊補(bǔ)零(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)

# 轉(zhuǎn)為numpy格式

colorDict_BGR = np.array(colorDict_BGR)

# 存儲(chǔ)顏色的GRAY字典,用于預(yù)處理時(shí)的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 = 1)

return precision

def Recall(confusionMatrix):

# 返回所有類別的召回率recall

recall = np.diag(confusionMatrix) / confusionMatrix.sum(axis = 0)

return recall

def F1Score(confusionMatrix):

precision = np.diag(confusionMatrix) / confusionMatrix.sum(axis = 1)

recall = np.diag(confusionMatrix) / confusionMatrix.sum(axis = 0)

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):

# 返回頻權(quán)交并比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

#################################################################

# 標(biāo)簽圖像文件夾

LabelPath = r"Data\test\label1"

# 預(yù)測(cè)圖像文件夾

PredictPath = r"Data\test\predict1"

# 類別數(shù)目(包括背景)

classNum = 3

#################################################################

# 獲取類別顏色字典

colorDict_BGR, colorDict_GRAY = color_dict(LabelPath, classNum)

# 獲取文件夾內(nèi)所有圖像

labelList = os.listdir(LabelPath)

PredictList = os.listdir(PredictPath)

# 讀取第一個(gè)圖像,后面要用到它的shape

Label0 = cv2.imread(LabelPath + "//" + labelList[0], 0)

# 圖像數(shù)目

label_num = len(labelList)

# 把所有圖像放在一個(gè)數(shù)組里

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()

# 計(jì)算混淆矩陣及各精度參數(shù)

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)labelpredict

輸出:

yellow fuchsia white

混淆矩陣:

[[ 59238 142 415]

[ 0 21221 168]

[ 725 1714 178521]]

精確度:

[0.99068484 0.9921455 0.98652188]

召回率:

[0.98790921 0.9195736 0.99674491]

F1-Score:

[0.98929508 0.95448208 0.99160705]

整體精度:

0.9879302978515625

IoU:

[0.97881692 0.91292751 0.98335381]

mIoU:

0.9583660791015158

FWIoU:

0.9765726814389497

后記

有問(wèn)題歡迎留言評(píng)論,覺得不錯(cuò)可以動(dòng)動(dòng)手指點(diǎn)個(gè)贊同&喜歡

參考https://github.com/jfzhang95/pytorch-deeplab-xception/blob/master/utils/metrics.py?github.com【語(yǔ)義分割】評(píng)價(jià)指標(biāo)總結(jié)及代碼實(shí)現(xiàn) - 極客分享?www.geek-share.com

總結(jié)

以上是生活随笔為你收集整理的python实现语义分割_遥感图像语义分割常用精度指标及其python实现(支持多类)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。