【深度学习】Focal Loss

https://mp.weixin.qq.com/s/s6Hl8g4mCq-AiMxR03Y6KQ

Focal loss用于解决一阶段目标检测场景中前后背景不平衡的问题。使用此损失让网络学习过程中更加专注于难例。

down-weight easy examples and thus focus training on hard negatives

easily classified (pt ≫ 0.5)

Focal loss

F L ( p t ) = − α t ( 1 − p t ) γ log ⁡ ( p t ) \mathrm{FL}\left(p_{\mathrm{t}}\right)=-\alpha_{\mathrm{t}}\left(1-p_{\mathrm{t}}\right)^{\gamma} \log \left(p_{\mathrm{t}}\right) FL(pt)=αt(1pt)γlog(pt)

p t = { p  if  y = 1 1 − p  otherwise  p_{\mathrm{t}}=\left\{\begin{array}{ll}{p} & {\text { if } y=1} \\ {1-p} & {\text { otherwise }}\end{array}\right. pt={p1p if y=1 otherwise 

As pt → 1, the factor goes to 0 and the loss for well-classified examples is down-weighted.

tensorflow实现
import tensorflow as tf

def focal_loss_sigmoid(labels, logits, gamma=2, alpha=0.25):
    """
        Computer focal loss for binary classification
        Args:
          labels: A int32 tensor of shape [batch_size].
          logits: A float32 tensor of shape [batch_size].
          alpha: class weights
          gamma: A scalar for focal loss gamma hyper-parameter.
        Returns:
          A tensor of the same shape as `lables`
        """
    logits = tf.nn.sigmoid(logits)
    focal_loss = -labels * (1 - alpha) * tf.pow(1 - logits, gamma) * tf.log(logits) \
                 - (1 - labels) * (alpha) * tf.pow(logits, gamma) * tf.log(1 - logits)
    return focal_loss

参考:focal_loss

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值