当前位置:网站首页>focal loss原理及简单代码实现
focal loss原理及简单代码实现
2022-08-09 14:52:00 【pomelo33】
论文:Focal Loss for Dense Object Detection
参考:https://zhuanlan.zhihu.com/p/49981234/
目标检测算法可以分为两大类:Two-stage detector 和 One-stage detector。由于像RCNN系列的二阶段检测器拥有region proposal的操作,这类算法可以达到很高的准确率,但是速度较慢。而像YOLO,SSD这样直接回归的检测算法,这类算法速度很快,但是准确率不如前者。作者提出focal loss的出发点也是希望one-stage detector可以达到two-stage detector的准确率,同时不影响原有的速度。
这篇论文发现,one-stage detector精度不如two-stage detector的根本原因是样本类别不均匀导致的。因为一张图片中,真实目标(正样本)的数量远远大于负样本数量,负样本占总的loss的大部分,而且多是容易分类的,因此使得模型的优化方向并不是我们所希望的那样。
CE ( p , y ) = { − log ( p ) if y = 1 − log ( 1 − p ) o t h e r w i s e . \textbf{CE}(p,y)=\begin{cases}-\textbf{log}(p) & \text{ if } y=1 \\ -\textbf{log}(1-p) & otherwise. \end{cases} CE(p,y)={ −log(p)−log(1−p) if y=1otherwise.
为了方便,用 p t p_t pt代替p,如下公式2。这里的pt就是前面Figure1中的横坐标。
为了表示简便,我们用 p t p_t pt表示样本属于true class的概率。所以(1)式可以写成
从交叉熵的公式可以看出无论是正样本还是负样本,权重都是一样的。当负样本的数量远多于正样本时,网络的优化方向会倾向于负样本。因此作者考虑增加一个权重系数控制正负样本的权重。这样对于正样本权重 a t a_t at可以设置大一点,而负样本则小一点。这样对于正负样本的优化就均衡了不少。
但是仍然存在一个问题,即数据中存在着多数容易分类的样本(例如置信度接近于1的样本),而难分类的样本占少数(例如置信度在0.5左右的样本,即网络很难确定分类的对不对)。因此还希望网络的优化更倾向于难分类的样本。于是就有了focal loss:
Focal ( p t ) = − ( 1 − p t ) γ log ( p t ) \textbf{Focal}(p_t)=-(1-p_t)^{\gamma }\text{log}(p_t) Focal(pt)=−(1−pt)γlog(pt)
( 1 − p t ) γ (1-p_t)^{\gamma } (1−pt)γ称为调制系数(modulating factor),为什么要加上这个调制系数呢?目的是通过减少易分类样本的权重,从而使得模型在训练时更专注于难分类的样本。
简单的代码实现:https://zhuanlan.zhihu.com/p/85832201
class FocalLoss(nn.Module):
def __init__(self, weight=None, reduction='mean', gamma=0, eps=1e-7):
super(FocalLoss, self).__init__()
self.gamma = gamma
self.eps = eps
self.ce = torch.nn.CrossEntropyLoss(weight=weight, reduction=reduction)
def forward(self, input, target):
logp = self.ce(input, target)
p = torch.exp(-logp)
loss = (1 - p) ** self.gamma * logp
return loss.mean()
边栏推荐
- docker安装单机版redis、集群版redis
- 流程控制学习
- 量化程序化交易如何去使用以及执行?
- Mysql two engines comparison
- 一种基于视频帧差异视频卡顿检测方案
- DBCO-PEG-DSPE,磷脂-聚乙二醇-二苯并环辛炔,在无铜离子的催化下反应
- 量化投资者是如何获取实时行情数据的呢?
- What is a template engine?What are the common template engines?Introduction to common commands of thymeleaf.
- DSPE-PEG-Hydrazide,DSPE-PEG-HZ,磷脂-聚乙二醇-酰肼MW:1000
- Shell programming loop statement
猜你喜欢

爬虫处理乱码问题

基于FPGA的FIR滤波器的实现(2)—采用kaiserord & fir2 & firpm函数设计

一种基于视频帧差异视频卡顿检测方案

navicat for Oraclel链接oracle 报错oracle library is not loaded的解决办法

Similar image detection method

DSPE-PEG-Hydrazide, DSPE-PEG-HZ, Phospholipid-Polyethylene Glycol-Hydrazide MW: 1000

DMPE-PEG-Mal Maleimide-PEG-DMPE dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide

DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基MW:1000

常见自动化测试工具及框架的选用

shell------常用小工具,sort,uniq,tr,cut
随机推荐
相干光(光学)
约束性统计星号‘*’
量化程序化交易都有哪些热点争议?
如何通过股票量化交易接口实现盈利稳定?
常用类学习
How to achieve stable profit through the stock quantitative trading interface?
[Mysql]--事务、事务的隔离级别、脏读、不可重复读、幻读解析
注释,标识符,数据类型
抢占量化交易基金产品先机,量化投资有发展空间?
In the process of quantitative trading, retail investors can do this
JS——循环结构经典例题解析与分享
【Qt】 No such slot...,No such signal
Seize the opportunity of quantitative trading fund products, and quantitative investment has room for development?
C语言——指针和数组、指针数组和数组指针、指针和二维数组
浅析Servlet三大容器的常用方法及其作用域
How to flexibly use the advantages of the quantitative trading interface to complement each other?
MySql中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
Mind map FreeMind installation problems and simple use
一些需要思考的物理问题
对程序化交易系统接口有什么误区?