深圳nft网站开发公司,wordpress设置语言,酒店网站建设研究,中国住房和城乡建设网站文章目录 1. F-score1. 1 原理1. 2 代码2. Dice Loss2.1 原理2.2 代码 通过看开源图像语义分割库的源码#xff0c;发现它对 Dice Loss 的实现方式#xff0c;是直接调用 F-score 函数#xff0c;换言之#xff0c;Dice Loss 是 F-score的特殊情况。于是就研究了一下这背后… 文章目录 1. F-score1. 1 原理1. 2 代码2. Dice Loss2.1 原理2.2 代码 通过看开源图像语义分割库的源码发现它对 Dice Loss 的实现方式是直接调用 F-score 函数换言之Dice Loss 是 F-score的特殊情况。于是就研究了一下这背后的原理作文以记之。
1. F-score
1. 1 原理
首先介绍 F-score 要理解F-score就要先回顾一下 Precision 和 Recall首先给出公式 这两个指标衡量算法的准确性时通常是相互排斥的。例如输入一个数据算法根据数据预测一个分数现在为该分数设定阈值大于阈值的预测为真小于该阈值的预测为假。
如果这个阈值得过低低到测试集中所有的样本均判定为真那么此时FN0False negative, 压根就没有预测出来 negative 的样本代入公式 (2) 得 Recall 1。但此时预测为真的样本中包含大量的 FP即 False Positive将会导致 Precision 过低。如果这个阈值设置得过高使得所有被判定为正的样本都是真的那么 FP0Precision1此时将不可避免有很多本应被判定为正的样本被错误地判定为负也就是 FN 很大导致 Recall 过低。
在不同的应用场景下对这两个指标的侧重不同。例如新冠感染者检测就应该尽量提高 Recall务求没有漏网之鱼。但在检测垃圾邮件时应该尽量提升 Precision即每个被判定为垃圾邮件的都是板上钉钉毫无争议的防止出现误伤把正常邮件当成垃圾邮件处理。
F-score 则是将这两个指标综合起来 β \beta β控制 Precision 和 Recall 的重要程度, 当 β 1 \beta1 β1, 对应 F1-score此时 Precision 和 Recall 同样重要。 β \beta β两个常用的取值是 0.5 和 2当取 0.5 时Precision 对 F-score 的影响更大当取 2 时Recall 对 F-score 的影响更大。(可以考虑得更极端一点当 β → 0 \beta\rightarrow0 β→0公式(3)趋于 Precision;当 β → ∞ \beta\rightarrow\infty β→∞公式(3)上下同除以分子易知其将趋于 Recall)
最后把 (1) (2) 代入 (3) 得
1. 2 代码
def f_score(inputs, target, beta1, smooth 1e-5, threhold 0.5):n, c, h, w inputs.size()nt, ht, wt, ct target.size()if h ! ht and w ! wt:inputs F.interpolate(inputs, size(ht, wt), modebilinear, align_cornersTrue)temp_inputs torch.softmax(inputs.transpose(1, 2).transpose(2, 3).contiguous().view(n, -1, c),-1)temp_target target.view(n, -1, ct)#--------------------------------------------## 计算dice系数#--------------------------------------------#temp_inputs torch.gt(temp_inputs, threhold).float()tp torch.sum(temp_target[...,:-1] * temp_inputs, axis[0,1])fp torch.sum(temp_inputs , axis[0,1]) - tpfn torch.sum(temp_target[...,:-1] , axis[0,1]) - tpscore ((1 beta ** 2) * tp smooth) / ((1 beta ** 2) * tp beta ** 2 * fn fp smooth)score torch.mean(score)return scoreinputs为分割模型的预测输出未经过softmax, target为gttemp_target中将channels维度设为num_classes1为了方便处理白边因此在实际计算时需要去掉最后一个channel: temp_target[...,:-1] 预测分割图temp_inputs与 GT 分割图的点乘然后再(n,hw)方向上求和作为tp 参考自: Dice系数(Dice coefficient)与mIoU与Dice Loss 因为预测temp_inputs (pred) fptp, 因此已知temp_inputs和tp, 就可以求出fp同理temp_target (gt) fntp, 因此已知temp_target和tp, 就可以求出fn然后根据F-score的计算公式在已知tp,fp,fn以及beta系数就可以计算出F-score值了 score ((1 beta ** 2) * tp smooth) / ((1 beta ** 2) * tp beta ** 2 * fn fp smooth)score torch.mean(score)2. Dice Loss
2.1 原理
Dice Loss 是语义分割中常用的一种损失它的计算方法如下 即分子为预测值与真实值的交集元素数目的两倍分母为两个集合元素数目之和注意并不是并集而是和。而 因此(6) 相当于 1 − 2 T P 2 T P F P F N 1-\frac{2TP}{2TPFPFN} 1−2TPFPFN2TP
而上式的结果正是公式 (5) 中 β 1 \beta 1 β1的情况,也就是F1 score。因此
Dice Loss 1 - F1 score 2.2 代码
def Dice_loss(inputs, target, beta1, smooth 1e-5):n, c, h, w inputs.size()nt, ht, wt, ct target.size()if h ! ht and w ! wt:inputs F.interpolate(inputs, size(ht, wt), modebilinear, align_cornersTrue)temp_inputs torch.softmax(inputs.transpose(1, 2).transpose(2, 3).contiguous().view(n, -1, c),-1)temp_target target.view(n, -1, ct)#--------------------------------------------## 计算dice loss#--------------------------------------------#tp torch.sum(temp_target[...,:-1] * temp_inputs, axis[0,1])fp torch.sum(temp_inputs , axis[0,1]) - tpfn torch.sum(temp_target[...,:-1] , axis[0,1]) - tpscore ((1 beta ** 2) * tp smooth) / ((1 beta ** 2) * tp beta ** 2 * fn fp smooth)dice_loss 1 - torch.mean(score)return dice_loss可以看到dice_loss的实现跟F-score基本上是一模一样的, 将torch.mean(score)求得的F-soce, 然后通过dice_loss 1- F-score 来实现。代码中默认 β 1 \beta1 β1, 所以更精确的说 dice_loss 1- F1-scoreDIce _loss的在训练损失中的使用如下:
参考
F-score 和 Dice Losshttps://github.com/bubbliiiing/deeplabv3-plus-pytorch/blob/main/utils/utils_metrics.py