资源技术动态目标检测算法之YOLOv2损失函数详解

目标检测算法之YOLOv2损失函数详解

2019-11-26 | |  135 |   0

原标题:目标检测算法之YOLOv2损失函数详解

来源:AI 研习社        链接:https://www.yanxishe.com/columnDetail/15977


前言

前面的 YOLOv2 推文详细讲解了 YOLOv2 的算法原理,但官方论文没有像 YOLOv1 那样提供 YOLOv2 的损失函数,难怪 Ng 说 YOLO 是目标检测中最难懂的算法。今天我们尝试结合 DarkNet 的源码来分析 YOLOv2 的损失函数。


关键点回顾

直接位置预测

YOLOv2 借鉴 RPN 网络使用 Anchor boxes 来预测边界框相对于先验框的 offsets。边界框的实际中心位置需要利用预测的坐标偏移值,先验框的尺度以及中心坐标来计算,这里的也即是特征图每个位置的中心点:
image.png


上面的公式也是 Faster-RCNN 中预测边界框的方式。但上面的预测方式是没有约束的,预测的边界框容易向任何方向偏移,例如当时边界框将向右偏移 Anchor 的一个宽度大小,导致每个位置预测的边界框可以落在图片的任意位置,这就导致模型训练的不稳定性,在训练的时候要花很长时间才可以得到正确的 offsets。以,YOLOv2 弃用了这种预测方式,而是沿用 YOLOv1 的方法,就是预测边界框中心点相对于对应 cell 左上角位置的相对偏移值,为了将边界框中心点约束在当前 cell 中,使用 sigmoid 函数处理偏移值,这样预测的偏移值在(0,1)范围内(每个 cell 的尺度看做 1)。

综上,根据边界框预测的 4 个偏移值,可以使用如下公式来计算边界框实际中心位置和长宽,公式在图中:
image.png

其中,为 cell 的左上角坐标。在 Fig3 中,当前的 cell 的左上角坐标为。由于函数的处理,边界框的中心位置会被约束在当前 cell 的内部,防止偏移过多,然后是先验框的宽度与高度,它们的值也是相对于特征图(这里是 13*13,我们把特征图的长宽记作 H,W)大小的,在特征图中的 cell 长宽均为 1。这样我们就可以算出边界框相对于整个特征图的位置和大小了,公式如下:




我们如果将上面边界框的 4 个值乘以输入图像长宽,就可以得到边界框在原图中的位置和大小了。


细粒度特征

YOLOv2 提取 Darknet-19 最后一个 max pool 层的输入,得到 26x26x512 的特征图。经过 1x1x64 的卷积以降低特征图的维度,得到 26x26x64 的特征图,然后经过 pass through 层的处理变成 13x13x256 的特征图(抽取原特征图每个 2x2 的局部区域组成新的 channel,即原特征图大小降低 4 倍,channel 增加 4 倍),再与 13x13x1024 大小的特征图连接,变成 13x13x1280 的特征图,最后在这些特征图上做预测。使用 Fine-Grained Features,YOLOv2 的性能提升了 1%。这个过程可以在下面的 YOLOv2 的结构图中看得很清楚:
image.png
这个地方今天还要补充一点,那就是 passthrough 层到底是怎么操作的,在 DarkNet 中 passthough 层叫作 reorg_layer,可以用下图来表示这个操作:
image.png


训练

上篇推文讲了 YOLOv2 的训练分为三个阶段,具体就不再赘述了。这里主要重新关注一下训练后的维度变化,我们从上一小节可以看到最后 YOLOv2 的输出维度是。这个 125 使用下面的公式来计算的:

和训练采用的数据集有关系。由于 anchors 数为 5,对于 VOC 数据集输出的 channels 数就是 125,而对于 COCO 数据集则为 425。这里以 VOC 数据集为例,最终的预测矩阵为,shape 为,可以将其 reshape 成,这样是边界框的位置和大小表示边界框的置信度,而表示类别预测值。


YOLOv2 的模型结构

01.png01-.png


损失函数


接下来就说一说今天的主题,损失函数。损失函数我看网上的众多讲解,发现有两种解释。


解释 1

YOLOv2 的损失函数和 YOLOv1 一样,对于训练集中的 ground truth,中心落在哪个 cell,那么该 cell 的 5 个 Anchor box 对应的边界框就负责预测它,具体由哪一个预测同样也是根据 IOU 计算后卡阈值来确定的,最后选 IOU 值最大的那个。这也是建立在每个 Cell 至多含有一个目标的情下,实际上也基本不会出现多余 1 个的情况。和 ground truth 匹配上的先验框负责计算坐标误差,置信度误差以及分类误差,而其它 4 个边界框只计算置信度误差。这个解释参考的 YOLOv2 实现是 darkflow.源码地址为:https://github.com/thtrieu/darkflow


解释 2

在官方提供的 Darknet 中,YOLOv2 的损失函数可以不是和 YOLOv1 一样的,损失函数可以用下图来进行表示:
02.png
可以看到这个损失函数是相当复杂的,损失函数的定义在 Darknet/src/region_layer.c 中。对于上面这一堆公式,我们先简单看一下,然后我们在源码中去找到对应部分。这里的代表的是特征图的高宽,都为,而 A 指的是 Anchor 个数,YOLOv2 中是 5,各个值是各个 loss 部分的权重系数。我们将损失函数分成 3 大部分来解释:

03.png

  • 第一部分:
    第一项需要好好解释一下,这个 loss 是计算 background 的置信度误差,这也是 YOLO 系列算法的特色,但是用哪些预测框来预测背景呢?这里需要计算各个预测框和所有的 ground truth 之间的 IOU 值,并且取最大值记作 MaxIOU,如果该值小于一定的阈值,YOLOv2 论文取了 0.6,那么这个预测框就标记为 background,需要计算这么多倍的损失函数。为什么这个公式可以这样表达呢?因为我们有物体的话,那么,如果没有物体,我们把这个值带入到下面的公式就可以推出第一项啦!


    04.png

  • 第二部分:
    05.png
    这一部分是计算 Anchor boxes 和预测框的坐标误差,但是只在前 12800 个 iter 计算,这一项应该是促进网络学习到 Anchor 的形状。

  • 第三部分:
    06.png
    这一部分计算的是和 ground truth 匹配的预测框各部分的损失总和,包括坐标损失,置信度损失以及分类损失。
    3.1 坐标损失 这里的匹配原则是指对于某个特定的 ground truth,首先要计算其中心点落在哪个 cell 上,然后计算这个 cell 的 5 个先验框和 grond truth 的 IOU 值,计算 IOU 值的时候不考虑坐标只考虑形状,所以先将 Anchor boxes 和 ground truth 的中心都偏移到同一位置,然后计算出对应的 IOU 值,IOU 值最大的先验框和 ground truth 匹配,对应的预测框用来预测这个 ground truth。
    3.2 置信度损失 在计算 obj 置信度时, 增加了一项权重系数,也被称为 rescore 参数,当其为 1 时,损失是预测框和 ground truth 的真实 IOU 值(darknet 中采用了这种实现方式)。而对于没有和 ground truth 匹配的先验框,除去那些 Max_IOU 低于阈值的,其它就全部忽略。YOLOv2 和 SSD 与 RPN 网络的处理方式有很大不同,因为它们可以将一个 ground truth 分配给多个先验框。
    3.3 分类损失 这个和 YOLOv1 一致,没什么好说的了。

我看了一篇讲解 YOLOv2 损失函数非常好的文章:https://www.cnblogs.com/YiXiaoZhou/p/7429481.html 。里面还有一个关键点:

在计算 boxes 的误差时,YOLOv1 中采用的是平方根以降低 boxes 的大小对误差的影响,而 YOLOv2 是直接计算,但是根据 ground truth 的大小对权重系数进行修正:l.coord_scale * (2 - truth.w*truth.h)(这里都归一化到(0,1)),这样对于尺度较小的其权重系数会更大一些,可以放大误差,起到和 YOLOv1 计算平方根相似的效果。


代码实现

贴一下 YOLOv2 在 Keras 上的复现代码,地址为:https://github.com/yhcc/yolo2 。网络结构如下,可以结合上面可视化图来看:

def darknet(images, n_last_channels=425):
    """Darknet19 for YOLOv2"""
    net = conv2d(images, 32, 3, 1, name="conv1")
    net = maxpool(net, name="pool1")
    net = conv2d(net, 64, 3, 1, name="conv2")
    net = maxpool(net, name="pool2")
    net = conv2d(net, 128, 3, 1, name="conv3_1")
    net = conv2d(net, 64, 1, name="conv3_2")
    net = conv2d(net, 128, 3, 1, name="conv3_3")
    net = maxpool(net, name="pool3")
    net = conv2d(net, 256, 3, 1, name="conv4_1")
    net = conv2d(net, 128, 1, name="conv4_2")
    net = conv2d(net, 256, 3, 1, name="conv4_3")
    net = maxpool(net, name="pool4")
    net = conv2d(net, 512, 3, 1, name="conv5_1")
    net = conv2d(net, 256, 1, name="conv5_2")
    net = conv2d(net, 512, 3, 1, name="conv5_3")
    net = conv2d(net, 256, 1, name="conv5_4")
    net = conv2d(net, 512, 3, 1, name="conv5_5")
    shortcut = net
    net = maxpool(net, name="pool5")
    net = conv2d(net, 1024, 3, 1, name="conv6_1")
    net = conv2d(net, 512, 1, name="conv6_2")
    net = conv2d(net, 1024, 3, 1, name="conv6_3")
    net = conv2d(net, 512, 1, name="conv6_4")
    net = conv2d(net, 1024, 3, 1, name="conv6_5")
    # ---------
    net = conv2d(net, 1024, 3, 1, name="conv7_1")
    net = conv2d(net, 1024, 3, 1, name="conv7_2")
    # shortcut
    shortcut = conv2d(shortcut, 64, 1, name="conv_shortcut")
    shortcut = reorg(shortcut, 2)
    net = tf.concat([shortcut, net], axis=-1)
    net = conv2d(net, 1024, 3, 1, name="conv8")
    # detection layer
    net = conv2d(net, n_last_channels, 1, batch_normalize=0,
                 activation=None, use_bias=True, name="conv_dec")
    return netdef darknet(images, n_last_channels=425):    """Darknet19 for YOLOv2"""
    net = conv2d(images, 32, 3, 1, name="conv1")
    net = maxpool(net, name="pool1")
    net = conv2d(net, 64, 3, 1, name="conv2")
    net = maxpool(net, name="pool2")
    net = conv2d(net, 128, 3, 1, name="conv3_1")
    net = conv2d(net, 64, 1, name="conv3_2")
    net = conv2d(net, 128, 3, 1, name="conv3_3")
    net = maxpool(net, name="pool3")
    net = conv2d(net, 256, 3, 1, name="conv4_1")
    net = conv2d(net, 128, 1, name="conv4_2")
    net = conv2d(net, 256, 3, 1, name="conv4_3")
    net = maxpool(net, name="pool4")
    net = conv2d(net, 512, 3, 1, name="conv5_1")
    net = conv2d(net, 256, 1, name="conv5_2")
    net = conv2d(net, 512, 3, 1, name="conv5_3")
    net = conv2d(net, 256, 1, name="conv5_4")
    net = conv2d(net, 512, 3, 1, name="conv5_5")
    shortcut = net
    net = maxpool(net, name="pool5")
    net = conv2d(net, 1024, 3, 1, name="conv6_1")
    net = conv2d(net, 512, 1, name="conv6_2")
    net = conv2d(net, 1024, 3, 1, name="conv6_3")
    net = conv2d(net, 512, 1, name="conv6_4")
    net = conv2d(net, 1024, 3, 1, name="conv6_5")    # ---------
    net = conv2d(net, 1024, 3, 1, name="conv7_1")
    net = conv2d(net, 1024, 3, 1, name="conv7_2")    # shortcut
    shortcut = conv2d(shortcut, 64, 1, name="conv_shortcut")
    shortcut = reorg(shortcut, 2)
    net = tf.concat([shortcut, net], axis=-1)
    net = conv2d(net, 1024, 3, 1, name="conv8")    # detection layer
    net = conv2d(net, n_last_channels, 1, batch_normalize=0,
                 activation=None, use_bias=True, name="conv_dec")    return net

然后,网络经过我们介绍的损失函数优化训练以后,对网络输出结果进行解码得到最终的检测结果,这部分代码如下:

def decode(detection_feat, feat_sizes=(13, 13), num_classes=80,
           anchors=None):
    """decode from the detection feature"""
    H, W = feat_sizes
    num_anchors = len(anchors)
    detetion_results = tf.reshape(detection_feat, [-1, H * W, num_anchors,
                                        num_classes + 5])

    bbox_xy = tf.nn.sigmoid(detetion_results[:, :, :, 0:2])
    bbox_wh = tf.exp(detetion_results[:, :, :, 2:4])
    obj_probs = tf.nn.sigmoid(detetion_results[:, :, :, 4])
    class_probs = tf.nn.softmax(detetion_results[:, :, :, 5:])

    anchors = tf.constant(anchors, dtype=tf.float32)

    height_ind = tf.range(H, dtype=tf.float32)
    width_ind = tf.range(W, dtype=tf.float32)
    x_offset, y_offset = tf.meshgrid(height_ind, width_ind)
    x_offset = tf.reshape(x_offset, [1, -1, 1])
    y_offset = tf.reshape(y_offset, [1, -1, 1])

    # decode
    bbox_x = (bbox_xy[:, :, :, 0] + x_offset) / W
    bbox_y = (bbox_xy[:, :, :, 1] + y_offset) / H
    bbox_w = bbox_wh[:, :, :, 0] * anchors[:, 0] / W * 0.5
    bbox_h = bbox_wh[:, :, :, 1] * anchors[:, 1] / H * 0.5

    bboxes = tf.stack([bbox_x - bbox_w, bbox_y - bbox_h,
                       bbox_x + bbox_w, bbox_y + bbox_h], axis=3)

    return bboxes, obj_probs, class_probsdef decode(detection_feat, feat_sizes=(13, 13), num_classes=80,
           anchors=None):    """decode from the detection feature"""
    H, W = feat_sizes
    num_anchors = len(anchors)
    detetion_results = tf.reshape(detection_feat, [-1, H * W, num_anchors,
                                        num_classes + 5])

    bbox_xy = tf.nn.sigmoid(detetion_results[:, :, :, 0:2])
    bbox_wh = tf.exp(detetion_results[:, :, :, 2:4])
    obj_probs = tf.nn.sigmoid(detetion_results[:, :, :, 4])
    class_probs = tf.nn.softmax(detetion_results[:, :, :, 5:])

    anchors = tf.constant(anchors, dtype=tf.float32)

    height_ind = tf.range(H, dtype=tf.float32)
    width_ind = tf.range(W, dtype=tf.float32)
    x_offset, y_offset = tf.meshgrid(height_ind, width_ind)
    x_offset = tf.reshape(x_offset, [1, -1, 1])
    y_offset = tf.reshape(y_offset, [1, -1, 1])    # decode
    bbox_x = (bbox_xy[:, :, :, 0] + x_offset) / W
    bbox_y = (bbox_xy[:, :, :, 1] + y_offset) / H
    bbox_w = bbox_wh[:, :, :, 0] * anchors[:, 0] / W * 0.5
    bbox_h = bbox_wh[:, :, :, 1] * anchors[:, 1] / H * 0.5

    bboxes = tf.stack([bbox_x - bbox_w, bbox_y - bbox_h,
                       bbox_x + bbox_w, bbox_y + bbox_h], axis=3)    return bboxes, obj_probs, class_probs


补充

这个损失函数最难的地方应该是 YOLOv2 利用 sigmoid 函数计算默认框坐标之后怎么梯度回传,这部分可以看下面的代码(来自 Darknet 源码):

// box误差函数,计算梯度
float delta_region_box(box truth, float *x, float *biases, int n, int index, int i, int j, int w, int h, float *delta, float scale, int stride)
{
    box pred = get_region_box(x, biases, n, index, i, j, w, h, stride);
    float iou = box_iou(pred, truth);
   
    // 计算ground truth的offsets值
    float tx = (truth.x*w - i);  
    float ty = (truth.y*h - j);
    float tw = log(truth.w*w / biases[2*n]);
    float th = log(truth.h*h / biases[2*n + 1]);

    delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
    delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
    delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
    delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
    return iou;
}// box误差函数,计算梯度float delta_region_box(box truth, float *x, float *biases, int n, int index, int i, int j, int w, int h, float *delta, float scale, int stride){
    box pred = get_region_box(x, biases, n, index, i, j, w, h, stride);    float iou = box_iou(pred, truth);   
    // 计算ground truth的offsets值    float tx = (truth.x*w - i);  
    float ty = (truth.y*h - j);    float tw = log(truth.w*w / biases[2*n]);    float th = log(truth.h*h / biases[2*n + 1]);

    delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
    delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
    delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
    delta[index + 3*stride] = scale * (th - x[index + 3*stride]);    return iou;
}

结合一下我们前面介绍的公式,这就是一个逆过程,现在是不是清晰一些了?有任何问题欢迎在留言区和我讨论哦。


后记

今天就介绍到这里了,YOLOv2 的损失函数实现都在 region_layer.c 里面了,同时推荐一下我的一个 Darknet 源码解析项目,我会在里面努力解析 YOLO 目标检测算法的细节,地址为:https://github.com/BBuf/Darknet 。明天开始讲解 YOLOv3,后面安排一下 YOLOv3 的实战,就用 NCNN 和 YOLOv3 为例子吧。


参考

https://zhuanlan.zhihu.com/p/35325884
https://www.cnblogs.com/YiXiaoZhou/p/7429481.html
https://github.com/yhcc/yolo2


欢迎关注我的微信公众号 GiantPadaCV,期待和你一起交流机器学习,深度学习,图像算法,优化技术,比赛及日常生活等。

THE END

免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com

上一篇:超全!深度学习在计算机视觉领域应用一览

下一篇:又见黑科技,关于宠物识别你知道多少?

用户评价
全部评价

热门资源

  • 应用笔画宽度变换...

    应用背景:是盲人辅助系统,城市环境中的机器导航...

  • 端到端语音识别时...

    从上世纪 50 年代诞生到 2012 年引入 DNN 后识别效...

  • GAN之根据文本描述...

    一些比较好玩的任务也就应运而生,比如图像修复、...

  • 人体姿态估计的过...

    人体姿态估计是计算机视觉中一个很基础的问题。从...

  • 谷歌发布TyDi QA语...

    为了鼓励对多语言问答技术的研究,谷歌发布了 TyDi...