yolo系列相关损失函数

news/2024/7/11 1:35:10 标签: YOLO, 深度学习, 机器学习

参考:https://blog.csdn.net/geek0105/article/details/129549229?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-129549229-blog-134171750.235v40pc_relevant_3m_sort_dl_base3&spm=1001.2101.3001.4242.1&utm_relevant_index=3

目录

1.BCEBlurWithLogitsLoss

2.FocalLoss

3.QFocalLoss

4.APLoss

5.aLRPLoss

6.RankSortLoss

7.IOULoss

GIoU

DIoU

CIoU(Complete IoU loss)

Enhanced Completed IoU

Efficient IoU Loss

αIoU

SIoU

1.BCEBlurWithLogitsLoss
BCEBlurWithLogitsLoss是BCE函数的一个变种,在yolov5中提出来的,其目的是削弱missing样本(就是存在目标但是没有标注出来)带来的负面影响。其实就是通过降低missing样本loss的权重,降低其在反向传播中的比重,达到降低missing样本的负面影响的目的。但是笔者认为,这样可能导致模型无法区分目标和混淆目标,提高混淆目标的误检率。代码如下:

class BCEBlurWithLogitsLoss(nn.Module):
    # BCEwithLogitLoss() with reduced missing label effects.
    def __init__(self, alpha=0.05):
        super().__init__()
        self.loss_fcn = nn.BCEWithLogitsLoss(reduction='none')  # must be nn.BCEWithLogitsLoss()
        self.alpha = alpha
 
    def forward(self, pred, true):
        loss = self.loss_fcn(pred, true)
        pred = torch.sigmoid(pred)  # prob from logits
        dx = pred - true  # reduce only missing label effects
        # dx = (pred - true).abs()  # reduce missing label and false label effects
        alpha_factor = 1 - torch.exp((dx - 1) / (self.alpha + 1e-4))
        loss *= alpha_factor
        return loss.mean()

2.FocalLoss
FcoalLoss是何凯明大神在2017年发表的一篇论文:Focal Loss for Dense Object Detection.中提出的。
论文地址:https://arxiv.org/abs/1708.02002
主要思路是: 通过增加困难样本的权重,让模型专注于困难样本(hard_sample)的学习,防止简单样本(easy_sample)过多主导训练的进程,可以解决难样本过少的问题。代码如下

class FocalLoss(nn.Module):
    # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
    def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):
        super(FocalLoss, self).__init__()
        self.loss_fcn = loss_fcn  # must be nn.BCEWithLogitsLoss()
        self.gamma = gamma
        self.alpha = alpha
        self.reduction = loss_fcn.reduction
        self.loss_fcn.reduction = 'none'  # required to apply FL to each element
 
    def forward(self, pred, true):
        loss = self.loss_fcn(pred, true)
        # p_t = torch.exp(-loss)
        # loss *= self.alpha * (1.000001 - p_t) ** self.gamma  # non-zero power for gradient stability
 
        # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py
        pred_prob = torch.sigmoid(pred)  # prob from logits
        p_t = true * pred_prob + (1 - true) * (1 - pred_prob)
        alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha)
        modulating_factor = (1.0 - p_t) ** self.gamma
        loss *= alpha_factor * modulating_factor
 
        if self.reduction == 'mean':
            return loss.mean()
        elif self.reduction == 'sum':
            return loss.sum()
        else:  # 'none'
            return loss

3.QFocalLoss
QFocalLoss是2020年的一篇文章,主要是解决FocalLoss只能适用于标签是0-1这样的二分类或者多分类任务,对于使用了label smooth的任务则无法使用。代码如下:

class QFocalLoss(nn.Module):
    # Wraps Quality focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
    def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):
        super(QFocalLoss, self).__init__()
        self.loss_fcn = loss_fcn  # must be nn.BCEWithLogitsLoss()
        self.gamma = gamma
        self.alpha = alpha
        self.reduction = loss_fcn.reduction
        self.loss_fcn.reduction = 'none'  # required to apply FL to each element
 
    def forward(self, pred, true):
        loss = self.loss_fcn(pred, true)
 
        pred_prob = torch.sigmoid(pred)  # prob from logits
        alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha)
        modulating_factor = torch.abs(true - pred_prob) ** self.gamma
        loss *= alpha_factor * modulating_factor
 
        if self.reduction == 'mean':
            return loss.mean()
        elif self.reduction == 'sum':
            return loss.sum()
        else:  # 'none'
            return loss

从个人在私有任务使用看,使用QFocalLoss对于map基本没有提升,甚至误检增多了(recall升高)。分析可能是和FocalLoss一样的问题,由于提高了困难样本的权重,会造成过于关注一些模棱两可的样本。对于小样本来说,估计会有提升,对于一般任务,不如使用BCEBlurWithLogitsLoss。
7.IOULoss

IoU、GIoU、DIoU、CIoU、EIoU、αIoU、SIoU


http://www.niftyadmin.cn/n/5294760.html

相关文章

C#语言发展历程(1-7)

一、类型发展 C#1中是没有泛型的 在C#2中在逐渐推出泛型。C#2还引入了可空类型。 示例:C#泛型(详解)-CSDN博客 1 C#3:引入了匿名类型、和隐式的局部变量(var) 匿名类型:我们主要是使用在LIN…

从大佬那偷学的 20个事半功倍的社交技巧

1,先给别人好处,再说出自己的需要,先予后取。 2,回礼回请,都略微高于对方请自己的价格,不会让他感觉你在占便宜,下次再想麻烦他帮你办事才有筹码。 3,投资人情,雪中送炭…

知识笔记(六十一)———Vue生命周期

组件生命周期--------组件从创建到销毁 创建 前 后 挂载 前 后 更新 前 后 销毁 前 后 vue给组件的某个阶段提供了特定的函数(钩子函数)来执行特定的逻辑,当到了某个节点会自动调用 创建前的函数 beforeCreate() { } 创建后的函数 cr…

41.坑王驾到第七期:uniapp开发微信小程序引用组件时报错!

一、错误再现 页面login引用了一个组件register,运行至小程序开发工具报错。 xxx.js 已被代码依赖分析忽略,无法被其他模块引用。 二、解决办法 在微信小程序的配置文件中找到setting节点,增加两个配置项。 “ignoreDevUnusedFiles”: fa…

“产品经理必懂的关键术语“

产品经理是现代企业中非常重要的一个角色,他们负责制定产品策略、规划产品开发流程、管理产品质量和用户反馈等等。然而,对于产品经理来说,了解并掌握相关的专业术语是非常重要的。本篇文章会介绍一些产品经理需要掌握的专业术语,…

LCR 150. 彩灯装饰记录 II

解题思路&#xff1a; 与LCR. 彩灯装饰记录 I类似&#xff0c;增加了分层输出。 class Solution {public List<List<Integer>> decorateRecord(TreeNode root) {Queue<TreeNode> queue new LinkedList<>();List<List<Integer>> res ne…

集群部署篇--Redis 哨兵模式

文章目录 前言一、哨兵模式介绍&#xff1a;1.1 介绍&#xff1a;1.2 工作机制&#xff1a; 二、哨兵模式搭建&#xff1a;2. 1 redis 主从搭建&#xff1a;2.2 setinel 集群搭建&#xff1a;2.2.1 配置&#xff1a; sentinel.conf &#xff1a;2.2.2 运行容器&#xff1a;2.2.…

11、分布式事务高频面试题

1、什么是分布式事务 在分布式系统中&#xff0c;一个业务因为跨越不同数据库或者跨越不同微服务而包含多个子事务&#xff0c;要求所有子事务同时成功或失败&#xff0c;这就是分布式事务。 比如一个电商系统的下单操作需要请求三个服务来完成&#xff0c;这三个服务分别是&…