[机器学习] focal loss:解决样本不平衡的一种通用方案

文章目录

 

focal loss可以解决很多场景下class imbalance,是一种通用的方案。但是focal loss的论文,我觉得还是很有信息量的。

focal loss 提出的场景和针对的问题

我超喜欢的一个老师说:“没有放之四海而皆准的方法”。我深以为然。

focal loss 好用,得加定语,说它在什么场景,什么任务下好用。这个任务,这个场景有什么样的特点,而由于这个特点,可以使得这个方法发挥作用,获得更好的结果。

所以,弄清 focal loss 提出的场景、针对的问题就是要弄清的第一个问题


focal loss 提出的场景:目标检测

focal loss 提出的场景是“目标检测”。目标检测是从图像或视频中检测出目标,一般来说再把目标用方框框出来,比如YOLO算法。

我现在了解不深,就不再瞎说了。感兴趣的可以深入一下。

论文中很多结论是在“目标检测”这个场景下得到的,在别的场景不一定对

在最后我会以图像分类场景为例子,分析一下,来说明 focal loss 在不同场景下效果的差别。


focal loss 针对的问题:类别不平衡

针对的问题是”类别不平衡“。关于类别不平衡,作者的表述如下:

We discover that the extreme foreground-background class imbalance encountered during training of dense detectors is the central cause.

可以看到论文讨论的“类别不平衡”有以下两个特点:

  • 这是一个二分类问题。所有的样本被分为两个 class,即 foreground 或 background。在目标检测这个场景下,作者认为 foreground = negative (负样本),background = positive (正样本)。

    当然,多分类可以在二分类的基础上推广,二者并没有很大的鸿沟。

  • 这里的样本不平均是非常极端 extreme 的。

    有多极端,论文表述如下:

    These detectors evaluate 1 0 4 10^4104 - 1 0 5 10^5105 candidate locations per image but only a few locations contain objects.

    而在 Two-stage Detectors 中 (Two-stage Detectors 在后面讲) ,通过使用 biased minibatch sampling (一种 sampling 方式) removes the vast majority of easy negatives,最后得到的正负样本的比例为1:3这个数量级:

    When training the second stage, biased sampling is typically used to construct minibatches that contain, for instance, a 1:3 ratio of positive to negative examples.

    可以想象,和初始相比,一开始的时候类别不平衡有多么极端!


如何处理目标检测下的类别不平衡

如何理解目标检测场景下的样本和类别

还是那个我超喜欢的老师说的 (男神!):做机器学习的,首先要是一个数据科学家

focal loss 在目标检测有亮眼的表现,是因为目标检测这个任务性质。而任务的性质,往往取决于其数据的情况,比如样本的性质、分布 (类别平衡 or not) 等。

这里的样本有以下几个性质:

  • 样本只有两种标签,即foreground或background。

  • 样本中既有正样本,又有负样本。正样本是我们需要的,对模型起正作用样本;负样本是我们不想要的,对模型取副作用的样本。

    而这并不对于所有场景成立,比如图像分类中,数据集样本基本都是正样本。

  • foreground标签,都是正样本positive;background标签,都是负样本negative。

  • foreground标签少,background标签少,二者存在类别不平衡 (class imbalance)。

  • background (negative) 中,容易样本占大部分。即全部样本中background (negative) 中占极大部分,negative中 easy negative 占大部分。

    引用中国移不动在知乎的一张图,来说明两种样本在目标检测的分布:
    在这里插入图片描述
    如此的结果就是:大量的负易样本的损失会主导损失函数,尽管其单个的损失较小

所以,为了减小负易样本的影响,减少负易样本的数目是一个直接的思路。这就有了two-stage的模型。


two-stage

在目标检测场景中,效果比较好的是two-stage (论文证明使用 focal loss 可以使 one-stage 的结果逼近甚至优于 two-stage) 。原因是,two-stage 在 the first stage 会滤除大部分的easy negatives,从而有效地降低the second stage中positive和negative的比例,论文中表述如下:

As pioneered in the Selective Search work [35], the first stage generates a sparse set of candidate proposals that should contain all objects while filtering out the majority of negative locations, and the second stage classifies the proposals into foreground classes / background.

以及:

As popularized in the R-CNN framework [11], the first stage generates a sparse set of candidate object locations and the second stage classifies each candidate location as one of the foreground classes or as background using a convolutional neural network.

也就是说,在the first stage,会通过一些操作,滤除大部分的easy negatives,从而使得the second stage中的模型 (比如CNN) 不再受easy negatives的影响,从而面临的 class imbalance 大大减弱 (到大概1:3的数量级) 。


one-stage

没有了two-stage中the first stage对easy negative的滤除,one-stage就要直面easy negatives的影响,即:

the training procedure is still dominated by easily classified background examples.

说细一点,是:

These detectors evaluate 1 0 4 10^4104 - 1 0 5 10^5105 candidate locations per image but only a few locations contain objects. This imbalance causes two problems:

(1) training is inefficient as most locations are easy negatives that contribute no useful learning signal;

(2) en masse, the easy negatives can overwhelm training and lead to degenerate models.

这样一来,借鉴two-stage中的思路,我们设法减小easy negatives对模型优化的影响,这就引出了 focal loss。

其实focal loss可以算是一种隐式的样本加权方式。显示的意思就是我事先给每个样本一个权重当作它的重要性。隐式的意思就是我给一个规则(函数)具体由算法学习时计算出来。


focal loss的算法

focal loss的思想

robust loss functions (e.g., Huber loss [13]) that reduce the contribution of outliers by down-weighting the loss of examples with large errors (hard examples).

In contrast, rather than addressing outliers, our focal loss is designed to address class imbalance by down-weighting inliers (easy examples) such that their contribution to the total loss is small even if their number is large.

In other words, the focal loss performs the opposite role of a robust loss: it focuses training on a sparse set of hard examples.

作者认为:易分样本(即置信度高的样本)对模型的提升效果非常小,模型应该主要关注与那些难分样本

所以,为了解决难易样本不平衡的问题,focal loss对置信度高的样本产生的损失增加一个衰减,衰减因子为 γ \gammaγ。
在这里插入图片描述
比如,γ = 2 \gamma=2γ=2,如果 p = 0.968 p=0.968p=0.968,则( 1 − 0.968 ) 2 = 0.001 (1-0.968)^2=0.001(1−0.968)2=0.001,即该样本产生的衰减减小了1000倍!

同时,还有正负样本不平衡的问题,那不妨就对负样本增加的损失也增加一个衰减,衰减因子为 α \alphaα。
在这里插入图片描述
这样最后的结果就是(借用中国移不动 ):

 
正难正易,γ \gammaγ衰减
负难,α \alphaα衰减负易,α \alphaα,γ \gammaγ衰减

而 γ \gammaγ 是指数衰减,α \alphaα 是一次衰减,即γ \gammaγ衰减的效果要大于α \alphaα衰减。

也就是模型的专注度:正难 > 负难 > 正易 > 负易。


focal loss的局限

假设的局限

第一个局限是由于作者的假设:易分样本(即,置信度高的样本)对模型的提升效果非常小,模型应该主要关注与那些难分样本

这一点,作者是这样说的:

our focal loss is designed to address class imbalance by down-weighting inliers (easy examples)

也就是说,作者假设没有outliers,但outliers是确实存在的。

这就带来了outlier的局限。在focal loss中,算法把outliers也当作inliers中的hard来计算了,因为二者都是模型难以分类的。

这显然是不合理的。可能模型已经收敛了但是这些离群点还是会被判断错误,让模型去关注这样的样本,怎么可能是最好的呢? (依旧从中国移不动借图)

在这里插入图片描述


适用场景的局限

论文立足的目标检测场景,挺特殊的。

特殊的地方在“如何理解目标检测场景下的样本和类别”这一部分说了。

总的来说,focal loss的思路是很brilliant的。一个老师说,focal loss是一种通用的方案。

一般来说,focal loss相比于传统交叉熵,会用更好的效果。但这个提升有多大,是要打一个问号的,而且一般没有在目标检测场景的提升大。


focal loss用于图像分类

有提升,但提升没那么大。

提升肯定是有的。因为focal loss的思路是很brilliant的,是一种通用的方案。而且一般图像分类,也会面临class imbalance的情况,focal loss是适用的。(而且数据集越差,class imbalance越严重)。比如我参加的一个比赛,云状识别,其他一个队伍用了focal loss,然后评价颇高。

因为focal loss的动机是解决容易困难样本问题。容易困难和样本多少有很大关系。不考虑极端特征的话,某一类的样本越多自然学起来就越简单。所以容易困难和样本多少这两个概念尽管不相同,但有很大联系。 从这个角度看,focal loss可以解决很多场景下class imbalance,是一种通用的方案

(不要问我为什么不用,我当时还是小白中的小白!)。

但是提升可能没有那么大。因为图片分类面对的class imbalance,远没有那么严重。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值