Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection


Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for

Dense Object Detection

一. 論文簡介

將目標檢測Loss和評價指標統一,提升檢測精度。這是一篇挺好的論文,下面會將其拓展到其它領域。

主要做的貢獻如下(可能之前有人已提出):

  1. 分類Loss+評價指標
  2. Regression分布推廣到一般性

二. 模塊詳解

2.1 談談分布

  1. 什么是分布?表示一個數發生的概率,設 \(f=P(x)\) 表示分布函數,\(f\) 表示發生的概率,\(x\) 可能存在的數。1)顯而易見,\(\int_{-\infty}^{+\infty}P(x)dx=1\),所有的數存在概率總和為1。 2)\(y=\int_{-\infty}^{+\infty}P(x)*xdx\) ,它的整體期望(平均值)肯定是等於目標值的。
  2. 什么是 \(Dirac\) 分布? reference\(f=\delta(x-\mu)\) , 當 \(x=\mu\) 概率為1,其它都是0。這是什么意思?此分布簡稱為絕對分布,只要是直接求目標的,都屬於此分布。比如:1)直接計算 \(one-hot\) 交叉熵 \(label=[0,0,0,1],pred=[0.2,0.1,0.1,0.6]\),我們的目的就是兩者相等,其它的值都是不存在的。你問我按照\(Delta\) 分布應該其他值為0才對啊,那loss=0(實際loss為什么不是0)怎么回傳呢?記住Loss和分布不是一個概念,Loss是我們用一種方式使得結果達到理想分布,分布是一種理想的狀態,簡單點說 \(Loss \to Sample\)。2)那么直接進行BBox回歸也是一種 \(Delta\) 分布,因為都是預測一個值,然后直接和Label進行smoothL1計算Loss。
  3. 什么是 \(Gaussian\) 分布,這個不多說大家都知道。\(Gaussian-YOLO\)\(Heatmap\) 都是屬於此分布。舉個例子:剛開始做關鍵點(當前小模型人臉也是這樣做的)直接使用坐標 \((x,y)\) 進行回歸,顯然這是屬於 \(Delta\) 分布的,后面人們將其改進為 \(Heatmap\),這就是將分布改為 \(Gaussian\),所以稱為\(Gaussian-Heatmap\) .
  4. 什么是任意分布?只滿足分布的兩個條件,沒有具體的公式。直接使用期望和Label進行計算Loss即可。
  5. 進一步理解Loss和分布的關系,期望和Label計算Loss(前向推導使用期望做結果),中間概率和期望計算Loss(使得輸出按照一定分布進行,容易收斂提高精度)。

2.2 分類Loss

筆者給出簡短說明:

  • 先去看一下FCOS論文,其中使用 \(center-ness\) 計算預測框質量,兩個作用:1)訓練時抑制質量較差的框。2)前向計算時用於NMS操作指標。
  • 問題來了。。。訓練階段、前向計算、評價指標沒有統一?
  • 論文魔改一下Focal-Loss、center-ness統一為一個Loss
  • 此部分比較簡單,基本和FCOS類似

# 代碼出自mmdetection
@weighted_loss
def quality_focal_loss(pred, target, beta=2.0):
    """Quality Focal Loss (QFL) is from
    Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes
    for Dense Object Detection
    https://arxiv.org/abs/2006.04388

    Args:
        pred (torch.Tensor): Predicted joint representation of classification
            and quality (IoU) estimation with shape (N, C), C is the number of
            classes.
        target (tuple([torch.Tensor])): Target category label with shape (N,)
            and target quality label with shape (N,).
        beta (float): The beta parameter for calculating the modulating factor.
            Defaults to 2.0.

    Return:
        torch.Tensor: Loss tensor with shape (N,).
    """
    assert len(target) == 2, """target for QFL must be a tuple of two elements,
        including category label and quality label, respectively"""
    # label denotes the category id, score denotes the quality score
    label, score = target

    # negatives are supervised by 0 quality score
    pred_sigmoid = pred.sigmoid()
    scale_factor = pred_sigmoid
    zerolabel = scale_factor.new_zeros(pred.shape)
    loss = F.binary_cross_entropy_with_logits(
        pred, zerolabel, reduction='none') * scale_factor.pow(beta)

    # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
    bg_class_ind = pred.size(1)
    pos = ((label >= 0) & (label < bg_class_ind)).nonzero().squeeze(1)
    pos_label = label[pos].long()
    # positives are supervised by bbox quality (IoU) score
    scale_factor = score[pos] - pred_sigmoid[pos, pos_label]
    loss[pos, pos_label] = F.binary_cross_entropy_with_logits(
        pred[pos, pos_label], score[pos],
        reduction='none') * scale_factor.abs().pow(beta)

    loss = loss.sum(dim=1, keepdim=False)
    return loss

2.3 回歸Loss

主要包括兩個部分:

  • \(Delta\) 分布推廣到任意分布

    • 論文公式(3)是 \(Delta\) 分布的期望,公式(4)和(5)是任意分布的期望
    • 直接預測多個(論文設置為16)值,求期望得到最佳值
    • TIPS: 效果肯定比 \(Delta\) 分布好,但是計算量會增加。小模型一般不適用,大模型使用較多。
  • 限制任意分布

    • 任意分布會過於離散,實際真實的值距離label都不會太遠
    • 限制分布范圍,論文公式(6)
    • TIPS: 按照公式推導應該效果好(正在推廣到關鍵點檢測),使用任意分布的都可以加上試試。
# 代碼出自mmdetection
@weighted_loss
def distribution_focal_loss(pred, label):
    """Distribution Focal Loss (DFL) is from
    Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes
    for Dense Object Detection
    https://arxiv.org/abs/2006.04388

    Args:
        pred (torch.Tensor): Predicted general distribution of bounding boxes
            (before softmax) with shape (N, n+1), n is the max value of the
            integral set `{0, ..., n}` in paper.
        label (torch.Tensor): Target distance label for bounding boxes with
            shape (N,).

    Return:
        torch.Tensor: Loss tensor with shape (N,).
    """
    # 完全按照論文公式(6)所示,label是真實值(目標框和anchor之間的偏差,參考FCOS)
    # pred的shape(偏差*分布),如果沒有后面的分布,那就變成delta分布
    dis_left = label.long() # label范圍[0,正無窮],感覺這里應該-1然后限制一下范圍最好。作者說long()向下取整,但是這解決不了對稱問題。
    dis_right = dis_left + 1
    weight_left = dis_right.float() - label
    weight_right = label - dis_left.float()
    loss = F.cross_entropy(pred, dis_left, reduction='none') * weight_left \
        + F.cross_entropy(pred, dis_right, reduction='none') * weight_right
    return loss

三. 參考文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM