目录
Dense Object Detection
将目标检测Loss和评价指标统一,提升检测精度。这是一篇挺好的论文,下面会将其拓展到其它领域。
主要做的贡献如下(可能之前有人已提出):
笔者给出简短说明:
- 先去看一下FCOS论文,其中使用 \(center-ness\) 计算预测框质量,两个作用:1)训练时抑制质量较差的框。2)前向计算时用于NMS操作指标。
- 问题来了。。。训练阶段、前向计算、评价指标没有统一?
- 论文魔改一下Focal-Loss、center-ness统一为一个Loss
此部分比较简单,基本和FCOS类似
@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
主要包括两个部分:
\(Delta\) 分布推广到任意分布
限制任意分布
@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
手机扫一扫
移动阅读更方便
你可能感兴趣的文章