目录

1.BCEWithLogitsLoss

1.1pytorch源码中的相关代码

1.2 数学原理

2.FocalLoss

2.1 pytorch源码

2.2 数学原理


1.BCEWithLogitsLoss

1.1pytorch源码中的相关代码

class BCEWithLogitsLoss(_Loss):def __init__(self, weight: Optional[Tensor] = None, size_average=None, reduce=None, reduction: str = 'mean',pos_weight: Optional[Tensor] = None) -> None:super(BCEWithLogitsLoss, self).__init__(size_average, reduce, reduction)self.register_buffer('weight', weight)self.register_buffer('pos_weight', pos_weight)self.weight: Optional[Tensor]self.pos_weight: Optional[Tensor]def forward(self, input: Tensor, target: Tensor) -> Tensor:return F.binary_cross_entropy_with_logits(input, target,self.weight,pos_weight=self.pos_weight,reduction=self.reduction)

1.2 数学原理

BCEWithLogitsLoss是将BCELoss(BCE:Binary cross entropy)和sigmoid融合了,也就是说省略了sigmoid这个步骤;

BCELoss的数学公式:

对于二分类的三个训练样本,计算方法:

import torch
import torch.nn as nninput = torch.randn(3,3)
target = torch.FloatTensor([[0,1,1],[0,0,1],[1,0,1]])loss = nn.BCELoss()
m = nn.Sigmoid()
input_m = m(input)
result = loss(input_m, target)

结果 result=tensor(1.0224)

而使用BCEWithLogitsLoss

loss_1 = nn.BCEWithLogitsLoss()
result = loss(input_m, target)

结果result=tensor(1.0224)

2.FocalLoss

2.1 pytorch源码

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 = gammaself.alpha = alphaself.reduction = loss_fcn.reductionself.loss_fcn.reduction = 'none'  # required to apply FL to each elementdef 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.pypred_prob = torch.sigmoid(pred)  # prob from logitsp_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.gammaloss *= alpha_factor * modulating_factorif self.reduction == 'mean':return loss.mean()elif self.reduction == 'sum':return loss.sum()else:  # 'none'return loss

2.2 数学原理

Focal Loss 是何恺明设计的为了解决one-stage目标检测在训练阶段前景类和背景类极度不均衡(如1:1000)的场景的损失函数。它是由二分类交叉熵改造而来的。

其中,均可以调节的超参数。为模型预测,其值介于(0-1)之间。

当y=1时,->1,表示easy positive,它对权重的贡献->0;

当y=0时,->0,表示easy negative,它对权重的贡献->0.

因此,Focal Loss降低了背景类的同时,也降低了easy positive和easy negative的权重;

是对Focal Loss的调节;

由标准交叉熵推理出Focal Loss:

标准交叉熵

其中,p是模型预测属于y=1的概率。为了方便标记,定义如下:

交叉熵CE重写为:

-平衡交叉熵:

有一种解决类别不平衡的方法就是引入[0,1]之间的权重因子:当y=1时,取;当 y=0时,取1-.随着

的增大,会对背景类的权重进行降低,从而加大对背景类的惩罚,从而减轻背景类数量太多对训练造成的影响;类似pt ,可将-CE写为:

替他链接:

Pytorch详解BCELoss和BCEWithLogitsLoss_豪哥的博客-CSDN博客_bcewithlogitsloss

BCELoss()与BCEWithLogitsLoss()区别 - 知乎

Focal Loss笔记 - HOU_JUN - 博客园

目标检测 YOLOV5:loss介绍相关推荐

  1. 目标检测 YOLOv5 - 卷积层和BN层的融合

    目标检测 YOLOv5 - 卷积层和BN层的融合 即Conv2d和 BatchNorm2d融合 flyfish 为了减少模型推理时间,YOLOv5源码中attempt_load已经包括两层的合并,主要 ...

  2. 《深度学习与目标检测 YOLOv5》

    <深度学习与目标检测 YOLOv5> flyfish 基础 深度学习基础 - 向量 深度学习基础 - 累加符号和连乘符号 深度学习基础 - 最大似然估计 深度学习基础 - 朴素贝叶斯 深度 ...

  3. 目标检测 YOLOv5 anchor设置

    目标检测 YOLOv5 anchor设置 1 anchor的存储位置 1.1 yaml配置文件中例如 models/yolov5s.yaml # anchors anchors:- [10,13, 1 ...

  4. 目标检测 YOLOv5 自定义网络结构

    目标检测 YOLOv5 自定义网络结构(YOLOv5-ShuffleNetV2) flyfish 版本:YOLOv5:v5 具体已经借鉴的自定义网络结构包括 YOLOv5-MobileNetV3 Mo ...

  5. 【目标检测】56、目标检测超详细介绍 | Anchor-free/Anchor-based/Backbone/Neck/Label-Assignment/NMS/数据增强

    文章目录 1.双阶段和单阶段目标检测器 1.1 双阶段目标检测器 1.1.1 R-CNN 1.1.2 SPP 1.1.3 Fast R-CNN 1.1.4 Faster R-CNN 1.2 单阶段目标 ...

  6. 目标检测 YOLOv5 - 如何提高模型的指标,提高精确率,召回率,mAP等

    目标检测 YOLOv5 - 如何提高模型的指标,提高精确率,召回率,mAP等 flyfish 文中包括了YOLOv5作者分享的提高模型指标小技巧和吴恩达(Andrew Ng)在做缺陷检测项目( ste ...

  7. 目标检测 YOLOv5网络v6 0版本总结

    目标检测 YOLOv5网络v6.0版本总结 YOLOv5对比YOLOv4 输入端:在模型训练阶段,提出了Mosaic数据增强.自适应锚框计算.自适应图片缩放等: Backbone网络:融合其它检测算法 ...

  8. 目标检测——YOLOv5(八)

    简介: YOLOv4 (2020.4.23)发布还不到 2 个月,很多人都没来及仔细看...突然 YOLOv5 (2020.6.10)又双叕来了... YOLOv5的大小仅有 27 MB,而使用 da ...

  9. 目标检测 YOLOv5 - 模型的样子

    目标检测 YOLOv5 - 模型的样子 flyfish 文章目录 目标检测 YOLOv5 - 模型的样子 开始加载模型文件 模型的层 模型的属性 模块的名称以及模块本身 模型的权重 模型权重的名字和权 ...

  10. 目标检测 YOLOv5 - ncnn模型的加密 C++实现封装库和Android调用库示例

    目标检测 YOLOv5 - ncnn模型的加密 C++实现封装库和Android调用库示例 flyfish 文章目录 目标检测 YOLOv5 - ncnn模型的加密 C++实现封装库和Android调 ...

最新文章

  1. 自动驾驶LiDAR点云深度学习综述
  2. 直线宽度2 points wide_OpenGL 绘图实例二之直线和圆弧的绘制
  3. glVertexPointer
  4. 使用Scalatra创建Scala WEB工程
  5. python 示例_在Python中带有示例的while关键字
  6. 使用apache搭建tomcat集群
  7. myelicpes2019初次使用设置_实况足球2019球员数据编辑器怎么使用
  8. 计算机上未检测到u盾,u盾检测不到-电脑上检测不到我的U盾怎么办? 爱问知识人...
  9. 云计算时代的域名解析
  10. 云上城之个服务器维护时间,云上城之歌开服时间表 官方最新开服情况
  11. 数据可视化总结——matplotlib、seaborn
  12. OCR/STR生僻字数据训练 | PaddleOCR的Fine-tune常见问题汇总(3)
  13. 欧几里得扩展欧几里得
  14. 【Axure交互教程】 隐藏页面滚动条的3种方法
  15. 图解Semaphore信号量之AQS共享锁-非公平模式
  16. matlab 振动信号 阀值去噪,基于MATLAB的振动信号去噪研究
  17. 事件抽取与事理图谱(一)
  18. 基于zynq的千兆网udp项目_基于FPGA的千兆网UDP通信分析
  19. [转]局域网共享一键修复 18.5.8 https://zhuanlan.zhihu.com/p/24178142
  20. 乐谱播放器 android,光遇乐谱 免费版

热门文章

  1. sas libname mysql_SAS libname语法,通过ODBC连接到SQL Server
  2. 饿了么超时20分钟_美团、饿了么,你凭什么让我多等几分钟?
  3. linux 命令:df 详解
  4. [笔记]8组LVDS_TX和LVDS_RX的调试心得
  5. html点击按钮没有反应
  6. 点击高级系统设置无反应
  7. linux 软raid恢复,Linux软RAID部署系统分区之恢复攻略
  8. 浅谈零信任网络,入门必读!
  9. FAT AP上行链路完整性检测功能
  10. 打开目录报错:Stale file handle