yolov5 代码解读 损失函数 loss.py

smooth_BCE

def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441
    # return positive, negative label smoothing BCE targets
    return 1.0 - 0.5 * eps, 0.5 * eps

把one-hot label 转换为soft label,一般认为这样更容易work。

BCEBlurWithLogitsLoss

self.loss_fcn = nn.BCEWithLogitsLoss(reduction='none') # must be nn.BCEWithLogitsLoss()
这里reduction用none因为在forward里返回的时候取mean。

dx = pred - true  # reduce only missing label effects, false positive
# dx = (pred - true).abs()  # reduce missing label and false label effects
alpha_factor = 1 - torch.exp((dx - 1) / (self.alpha + 1e-4))
loss *= alpha_factor

刚开始看这几行非常confused,查了很久。

https://github.com/ultralytics/yolov5/issues/1030
这个issue里说减少false negative的影响,我觉得应该写错了,是减少false positive的影响。

false negative指gt有框而network没有predict到,这时候的weight应该要比较大才对。

dx\in [-1,1],当 dx=1 ,即pred=1,true=0 时,alpha_factor=0,这应该是false positive的情况。

直白的说,network觉得这里有一个obj,但是gt说没有,这种情况不应该过多的惩罚。

如果采用绝对值的话,会减轻pred和gt差异过大造成的影响。

Focal Loss

class FocalLoss(nn.Module):
    # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
    def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):
        super(FocalLoss, self).__init__()
        self.loss_fcn = loss_fcn  # must be nn.BCEWithLogitsLoss()
        self.gamma = gamma
        self.alpha = alpha
        self.reduction = loss_fcn.reduction
        self.loss_fcn.reduction = 'none'  # required to apply FL to each element

    def forward(self, pred, true):
        loss = self.loss_fcn(pred, true)

        # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py
        pred_prob = torch.sigmoid(pred)  # prob from logits
        p_t = true * pred_prob + (1 - true) * (1 - pred_prob)
        alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha)
        modulating_factor = (1.0 - p_t) ** self.gamma
        loss *= alpha_factor * modulating_factor

        if self.reduction == 'mean':
            return loss.mean()
        elif self.reduction == 'sum':
            return loss.sum()
        else:  # 'none'
            return loss

假设gt是1,pred_prob如果很小,那么就是hard,这样算出来的p_t也会小,最后modulating_factor大。

对alpha_factor也是类似的。alpha_factor对应于foreground,一般设置为0.25。

QFocalLoss

class QFocalLoss(nn.Module):
    # Wraps Quality focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
    def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):
        super(QFocalLoss, self).__init__()
        self.loss_fcn = loss_fcn  # must be nn.BCEWithLogitsLoss()
        self.gamma = gamma
        self.alpha = alpha
        self.reduction = loss_fcn.reduction
        self.loss_fcn.reduction = 'none'  # required to apply FL to each element

    def forward(self, pred, true):
        loss = self.loss_fcn(pred, true)

        pred_prob = torch.sigmoid(pred)  # prob from logits
        alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha)
        modulating_factor = torch.abs(true - pred_prob) ** self.gamma
        loss *= alpha_factor * modulating_factor

        if self.reduction == 'mean':
            return loss.mean()
        elif self.reduction == 'sum':
            return loss.sum()
        else:  # 'none'
            return loss

这里modulating_factor的算法和QFL论文写的一致。

原本FL用class label,也就是one-hot discrete label来supervise;而QFL将其换成了IoU continuous label。

ComputeLoss

我们先明确一下p和targets的shape
p,也就是prediction,[num_dec_layer, batch_size, num_anchor, height, width, 85],这里85是80个class和4个offset以及1个confidence。
targets [nt, 6]

init

BCEcls, BCEobj是两个criteria,在scratch的hyp中g=0所以没有用focal loss,是普通的BCEloss

cp 和 cn 是soft label的probability,比如0.95 0.05。

balance控制obj loss的加权系数,autobalance决定加权系数也就是balance是否自动更新,autobalance一般是False。

self.balance = {3: [4.0, 1.0, 0.4]},有三个layer的输出,第一个layer的weight是4,第二个1,以此类推。如果有5个layer的输出才用右边那个weight数组。

gr 是iou ratio。

build_targets

targets就是这个batch里所有的labels,targets(img_idx, cls_idx, x, y, w, h),shape为[nt, 6]。可参考utils/datasets.py line 522, 599。
随便打印几行targets也可以验证我们的分析。

...,
[2.00000e+00, 5.40000e+01, 4.65345e-01, 8.84894e-01, 1.62460e-01, 2.30211e-01],
[3.00000e+00, 2.20000e+01, 3.53780e-01, 5.08301e-01, 6.05865e-01, 5.20621e-01],
...,

x, y, w, h是归一化后的结果。

gain = torch.ones(7, device=targets.device)  # normalized to gridspace gain, [7,]
ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt)  # same as .repeat_interleave(nt), [na,nt]
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2)  # append anchor indices, [na, nt, 7]

先复制了三份一样的targets,在最后面加了一维表明anchor idx,本来是6现在变成7。

...,
[3.00000e+00, 4.40000e+01, 8.23209e-01,  ..., 2.10690e-02, 2.45219e-02, 1.00000e+00]],
[[0.00000e+00, 4.50000e+01, 4.14857e-01,  ..., 8.29713e-01, 5.83730e-01, 2.00000e+00],
...,

gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain t = targets * gain这里是把 grid size 拿出来乘,恢复到特征图的维度。

在 match 里面比较简单,容易看懂,就是 anchor 和 target 不能差的太离谱,误差小于阈值就 match。

下一步在扩展 targets,个人认为是 positive examples 太少了,所以根据 center 在 cell 中的相对位置,添加相邻的两个 cell 作为 targets。

举个例子,如果 center 在 cell 的左上角,那么 cell 本身,和 cell 的左边一个位置,还有 cell 的上边一个位置,这三个 cell 都作为 targets。

我个人觉得这里的写法是非常巧妙的,取了 grid xy 和 inverse(类似于 flip)。

(gxy % 1. < g),这里的 g 是 0.5,如果仅考虑这个条件,好像可以直接判断是否选取左边 cell 和上边 cell。

但是要考虑到边界情况,如果当前已经处于最上方,已经没有上边 cell 可以选择了,这就是(gxy > 1.)起到的作用,判断 edge case。

如果本来大于 0.5,那么 inverse 后就小于 0.5 了,所以可以使用相同的逻辑选择右边 cell 和下边 cell ,类似地推理到 edge case。

最后一点要提的是使用 clamp_ 确保返回的 grid indices 不是非法值,旧版本 code 没用这个检查,不过好像也没什么差。

call

lcls, lbox, lobj 这三个用来存放loss,默认使用pytorch提供的BCE loss。

pxy = ps[:, :2].sigmoid() * 2. - 0.5 在learn的时候不需要加cx cy。

bbox回归的公式可以参考model/yolo.py line56, 57。

Objectness 这里 gr 设置为 1.0,也就意味着直接拿 iou 作为 confidence。

至于为什么返回 loss 的时候为什么要乘 bs,还不是很清楚,第二个返回值就是为了打印信息用的。

tips

scale

在train的时候,target是在feature map的scale。

在inference的时候,直接乘img map scale的anchor size就可以了,也就是配置文件里的anchor。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容

  • 开源 自从yolov5开源以来,(不管因为啥原因)深受瞩目,我最近用tensorflow实现了其主要部分。可能是第...
    半壶雪阅读 7,935评论 0 0
  • 目录faster rcnn论文备注caffe代码框架简介faster rcnn代码分析后记 faster rcnn...
    db24cc阅读 9,610评论 2 12
  • 本文依次讲解YOLOv1,v2,v3。博客地址https://blog.csdn.net/hancoder/art...
    HAN_望向阅读 7,545评论 2 4
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 124,954评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,046评论 0 4