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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【机器学习】图像语义分割常用指标Dice系数 敏感性 特异性 IOU及python代码实现

發布時間:2023/12/14 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【机器学习】图像语义分割常用指标Dice系数 敏感性 特异性 IOU及python代码实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 知識鋪墊
  • 1. Dice系數和IOU
  • 2.敏感性(=Recall)、特異性和精確度(=precision=PPV)
    • 2.1 敏感性(召回率)和特異性
    • 2.2 敏感性和特異性之間的關系
    • 2.3 Recall和Precision之間的關系
  • 3. F1

知識鋪墊

首先,對于像素點,我們要知道,當預測的像素點類別和其真實類別不同或者相同時,我們可以用混淆矩陣來表示,如下圖:

1. Dice系數和IOU

解釋鏈接
由于網上給的Dice系數的求解代碼基本上都是batch_size=1的,當batch_size>1的時候,就沒法用了,因此在這里對網上流行的代碼和自己改的代碼分別進行總結。

Dice系數的計算公式如下:
Dice=2×TP(TP+FN)+(TP+FP)Dice=\frac{2\times TP}{(TP+FN)+(TP+FP)}Dice=(TP+FN)+(TP+FP)2×TP?

def dice_coef(output, target): # batch_size=1smooth = 1e-5#output = torch.sigmoid(output).view(-1).data.cpu().numpy()output=torch.sigmoid(output)output[output > 0.5] = 1 #將概率輸出變為于標簽相匹配的矩陣output[output <= 0.5] = 0#target = target.view(-1).data.cpu().numpy()intersection = (output * target).sum() = TP# \符號有換行的作用return (2. * intersection + smooth) / \(output.sum() + target.sum() + smooth) # dice=2*TP/(TP+FN)+(TP+FP) def dice_coef(output, target): # Batch_size>1時smooth = 1e-5#output = torch.sigmoid(output).view(-1).data.cpu().numpy()output=torch.sigmoid(output).data.cpu().numpy()output[output > 0.5] = 1 #將概率輸出變為于標簽相匹配的矩陣output[output <= 0.5] = 0# target = target.view(-1).data.cpu().numpy()target = target.data.cpu().numpy()dice=0.# ipdb.set_trace() # 用于斷掉調試if len(output)>1:# 如果樣本量>1,則逐樣本累加for i in range(len(output)):intersection = (output[i] * target[i]).sum()dice += (2. * intersection + smooth)/(output[i].sum() + target[i].sum() + smooth)else:intersection = (output * target).sum() # 一個數字,=TPdice = (2. * intersection + smooth) /(output.sum() + target.sum() + smooth)return dice

IOU的計算公式如下:
Dice=TPTP+FN+FPDice=\frac{TP}{TP+FN+FP}Dice=TP+FN+FPTP?

# batch_size = 1 def iou_score(output, target):smooth = 1e-5if torch.is_tensor(output):output = torch.sigmoid(output).data.cpu().numpy()if torch.is_tensor(target):target = target.data.cpu().numpy()output_ = output > 0.5target_ = target > 0.5intersection = (output_ & target_).sum()union = (output_ | target_).sum()return (intersection + smooth) / (union + smooth) #batch_size > 1 def iou_score(output, target):smooth = 1e-5if torch.is_tensor(output):output = torch.sigmoid(output).data.cpu().numpy()if torch.is_tensor(target):target = target.data.cpu().numpy()output_ = output > 0.5target_ = target > 0.5# intersection = (output_ & target_).sum()# union = (output_ | target_).sum()iou = 0.if len(output)>1:for i in range(len(output)):union = (output_[i] | target_[i]).sum()intersection = (output_[i] & target_[i]).sum()iou += (intersection + smooth) / (union + smooth)else:intersection = (output_ & target_).sum()union = (output_ | target_).sum()iou = (intersection + smooth) / (union + smooth)return iou

2.敏感性(=Recall)、特異性和精確度(=precision=PPV)

2.1 敏感性(召回率)和特異性

Recall公式

def get_sensitivity(output, gt): # 求敏感度 se=TP/(TP+FN)SE = 0.output = output > 0.5gt = gt > 0.5TP = ((output==1).byte() + (gt==1).byte()) == 2FN = ((output==0).byte() + (gt==1).byte()) == 2#wfy:batch_num>1時,改進if len(output)>1:for i in range(len(output)):SE += float(torch.sum(TP[i])) / (float(torch.sum(TP[i]+FN[i])) + 1e-6)else:SE = float(torch.sum(TP)) / (float(torch.sum(TP+FN)) + 1e-6) #原本只用這一句#SE = float(torch.sum(TP)) / (float(torch.sum(TP + FN)) + 1e-6) # 原本只用這一句return SE #返回batch中所有樣本的SE和

特異性:

def get_specificity(SR, GT, threshold=0.5):#求特異性 sp=TN/(FP+TN)SR = SR > threshold #得到true和falseGT = GT > thresholdSP=0.# wfy# TN : True Negative# FP : False PositiveTN = ((SR == 0).byte() + (GT == 0).byte()) == 2FP = ((SR == 1).byte() + (GT == 0).byte()) == 2#wfy:batch_num>1時,改進if len(SR)>1:for i in range(len(SR)):SP += float(torch.sum(TN[i])) / (float(torch.sum(TN[i] + FP[i])) + 1e-6)else:SP = float(torch.sum(TN)) / (float(torch.sum(TN + FP)) + 1e-6) # 原本只用這一句## SP = float(torch.sum(TN)) / (float(torch.sum(TN + FP)) + 1e-6)return SP

這兩個指標在醫療領域很常用,而在機器學習領域常用的是Recall和Precision。

2.2 敏感性和特異性之間的關系

暫略

2.3 Recall和Precision之間的關系

ppv=precision

def ppv(output, target): #陽性預測值,準確率(precision)pr = TP/(TP+FP)smooth = 1e-5if torch.is_tensor(output):output = torch.sigmoid(output).data.cpu().numpy()if torch.is_tensor(target):target = target.data.cpu().numpy()ppv=0.if len(output)>1:for i in range(len(output)):intersection = (output[i] * target[i]).sum()ppv += (intersection + smooth)/(output[i].sum() + smooth)else:intersection = (output * target).sum() # 一個數字,=TPppv = (intersection + smooth)/(output.sum() + smooth)# intersection = (output * target).sum() # TPreturn ppv

3. F1

def get_F1(output, gt):se = get_sensitivity(output, gt)pc = get_precision(output, gt)f1 = 2*se*pc / (se+pc+1e-6)return f1

總結

以上是生活随笔為你收集整理的【机器学习】图像语义分割常用指标Dice系数 敏感性 特异性 IOU及python代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。