当前位置:网站首页>常用损失函数(二):Dice Loss
常用损失函数(二):Dice Loss
2022-07-30 05:44:00 【CV技术指南】
Dice Loss是由Dice系数而得名的,Dice系数是一种用于评估两个样本相似性的度量函数,其值越大意味着这两个样本越相似,Dice系数的数学表达式如下:

其中
表示X和Y之间交集元素的个数,
和
分别表示X、Y中元素的个数。Dice Loss表达式如下:

1、①Dice Loss常用于语义分割问题中,X表示真实分割图像的像素标签,Y表示模型预测分割图像的像素类别,
近似为预测图像的像素与真实标签图像的像素之间的点乘,并将点乘结果相加,
和
分别近似为它们各自对应图像中的像素相加。
②对于二分类问题,真实分割标签图像的像素只有0,1两个值,因此
可以有效地将在预测分割图像中未在真实分割标签图像中激活的所有像素值清零,对于激活的像素,主要是惩罚低置信度的预测,置信度高的预测会得到较高的Dice系数,从而得到较低的Dice Loss。即:

其中,
与
分别表示像素
的标签值与预测值,N为像素点总个数,等于单张图像的像素个数乘以batchsize。
2、可以说Dice Loss是直接优化F1 score而来的,是对F1 score的高度抽象,可用于多分类分割问题上。其中查准率(精确率)公式如下,表示在预测为1的样本中实际为1的概率:

查全率(召回率)的公式如下,表示在实际为1的样本中预测为1的概率:

查准率和查全率往往是相互制约的,如果提高模型的查准率,就会降低模型的查全率;提高模型的查全率就会降低模型的查准率。为了平衡这两者的关系,F1 score就被提出,其公式如下:

在二分类问题中,Dice系数也可以写成
3、Dice Loss可以缓解样本中前景背景(面积)不平衡带来的消极影响。Dice Loss训练更关注对前景区域的挖掘,即保证有较低的FN,但会存在损失饱和问题,而CE Loss是平等地计算每个像素点的损失,当前点的损失只和当前预测值与真实标签值的距离有关,这会导致一些问题(见Focal Loss)。因此单独使用Dice Loss往往并不能取得较好的结果,需要进行组合使用,比如Dice Loss+CE Loss或者Dice Loss+Focal Loss等。
4、Dice Loss的代码实现如下:
def Dice_loss(inputs, target, beta=1, 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), mode="bilinear", align_corners=True)
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]) - tp
fn = torch.sum(temp_target[...,:-1] , axis=[0,1]) - tp
score = ((1 + beta ** 2) * tp + smooth) / ((1 + beta ** 2) * tp + beta ** 2 * fn + fp + smooth)
dice_loss = 1 - torch.mean(score)
return dice_loss边栏推荐
- Trust anchor for certification path not found. Exception solution.
- Invalid bound statement (not found)出现的原因和解决方法
- Defense Ideas for a Type of SMS Vulnerability
- MySQL 索引的数据结构及类型
- 【数仓】数据质量
- FastAPI 快速入门
- Go简单实现协程池
- [Getting C language from zero basis - navigation summary]
- Online sql editing query tool sql-editor
- protobuf编码及网络通信应用(一)
猜你喜欢

Jdbc & Mysql timeout分析

21. Kotlin Advanced Learning: Implementing Simple Network Access Encapsulation

Servlet basic principles and application of common API methods

原型模式(Prototype):Swift 实现

sqli-labs shooting range SQL injection learning Less-1

Nodejs PM2 monitoring and alarm email (2)

Flink-流/批/OLAP一体得到Flink引擎

mysql is not an internal or external command, nor is it a runnable program or batch file to resolve

SQL Server Installation Tutorial

MYSQL一站式学习,看完即学完
随机推荐
原型模式(Prototype):Swift 实现
十五、Kotlin进阶学习:一、子类与子类型;二、协变;三、逆变;
Using custom annotations, statistical method execution time
Trust anchor for certification path not found.异常解决方法。
MySQL index optimization and failure scenarios
JVM学习(二) 垃圾收集器
正则表达式语法详解及实用实例
FastAPI 快速入门
MySQL 5.7 安装教程(全步骤、保姆级教程)
Arthas 命令解析(jvm/thread/stack/heapdump)
【OS】操作系统高频面试题英文版(1)
[Ten years of network security engineers finishing] - 100 penetration testing tools introduction
mysql is not an internal or external command, nor is it a runnable program or batch file to resolve
Nacos配置中心用法详细介绍
MySQL achievement method 】 【 5 words, single table SQL queries
Flink-stream/batch/OLAP integrated to get Flink engine
TDengine cluster construction
十一、Kotlin进阶学习:1、集合;2、List操作;3、可变集合——MutableList;4、Set;5、Map;6、MutableMap;
Using PyQt5 to add an interface to YoloV5 (1)
Trust anchor for certification path not found. Exception solution.