欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

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

发布时间:2023/12/14 python 70 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【机器学习】图像语义分割常用指标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代码实现的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。