当前位置:网站首页>常用损失函数(二):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边栏推荐
- 十七、Kotlin进阶学习:1、守护线程;2、线程和协程之间的效率对比;3、取消协程;
- MySQL 5.7 installation tutorial (all steps, nanny tutorials)
- Thread state of five
- Redis 客户端常见异常分析
- The most powerful and most commonly used SQL statements in history
- c#下Web3合约空投、转账调用代码
- A Spark task tuning 】 【 one day suddenly slow down how to solve
- Extraction of BaseDAO
- Reasons and solutions for Invalid bound statement (not found)
- 十、Kotlin基础学习:1、延迟加载;2、异常处理;3、使用 throw 主动抛出异常;4、自定义异常;
猜你喜欢
![Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc](/img/2d/50c9001125cd613087044d2b6c78b1.png)
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc

Servlet basic principles and application of common API methods

在线sql编辑查询工具sql-editor

MySQL 数据类型及占用空间

Xcode 建立 UIKit 项目(Hello World)

MySQL 5.7 安装教程(全步骤、保姆级教程)

21. Kotlin Advanced Learning: Implementing Simple Network Access Encapsulation

九、Kotlin基础学习:1、Companion的扩展方法和扩展属性;2、一般类的扩展方法和扩展属性;3、委托;

MySQL window function

JVM Learning (2) Garbage Collector
随机推荐
Nacos配置中心用法详细介绍
十五、Kotlin进阶学习:一、子类与子类型;二、协变;三、逆变;
The Request request body is repackaged to solve the problem that the request body can only be obtained once
Servlet basic principles and application of common API methods
php vulnerability full solution
Use kotlin to extend plugins/dependencies to simplify code (after the latest version 4.0, this plugin has been deprecated, so please choose to learn, mainly to understand.)
oracle row to column, column to row summary
SQL Server database generation and execution of SQL scripts
Flink CDC 实现Postgres到MySQL流式加工传输案例
【调优】一个 Spark 任务某天突然变慢怎么解决
sql concat() function
MySQL - 函数及约束命令
Trust anchor for certification path not found. Exception solution.
Detailed explanation of regular expression syntax and practical examples
Nodejs PM2 monitoring and alarm email (2)
十九、Kotlin进阶学习:1、管道数据的收和发;2、管道的关闭;3、生产者和消费者;4、管道的缓存区;
mysql delete duplicate data in the table, (retain only one row)
FastAPI Quick Start
GraphQL (1) Basic introduction and application examples
建造者模式(Swift 实现)