«上一篇 下一篇»
  计算机工程  2022, Vol. 48 Issue (3): 236-243  DOI: 10.19678/j.issn.1000-3428.0060479
0

引用本文  

沈记全, 陈相均, 翟海霞. 基于改进边界框回归损失的YOLOv3检测算法[J]. 计算机工程, 2022, 48(3), 236-243. DOI: 10.19678/j.issn.1000-3428.0060479.
SHEN Jiquan, CHEN Xiangjun, ZHAI Haixia. YOLOv3 Detection Algorithm Based on the Improved Bounding Box Regression Loss[J]. Computer Engineering, 2022, 48(3), 236-243. DOI: 10.19678/j.issn.1000-3428.0060479.

基金项目

国家自然科学基金(61972134);河南省科技攻关项目(182102310946)

作者简介

沈记全(1969-), 男, 教授、博士, 主研方向为计算机视觉、智能信息系统;
陈相均, 硕士研究生;
翟海霞, 副教授、硕士

文章历史

收稿日期:2021-01-04
修回日期:2021-03-26
基于改进边界框回归损失的YOLOv3检测算法
沈记全 , 陈相均 , 翟海霞     
河南理工大学 计算机科学与技术学院, 河南 焦作 454000
摘要:YOLOv3检测算法中的边界框回归损失函数对边界框尺度敏感,且与算法检测效果评价标准交并比(IoU)之间的优化不具有强相关性,无法准确反映真值框与预测框之间的重叠情况,造成收敛效果不佳。针对上述问题,提出基于IoU的改进边界框回归损失算法BR-IoU。将IoU作为边界框回归损失函数的损失项,使不同尺度的边界框在回归过程中获得更均衡的损失优化权重。在此基础上,通过添加惩罚项最小化预测框与真值框中心点间围成的矩形面积,并提高预测框与真值框之间宽高比的一致性,从而优化边界框的回归收敛效果。在PASCAL VOC和COCO数据集上的实验结果表明,在不影响实时性的前提下,BR-IoU能够有效提高检测精度,采用BR-IoU的YOLOv3算法在PASCAL VOC 2007测试集上mAP较原YOLOv3算法和G-YOLO算法分别提高2.5和1.51个百分点,在COCO测试集上分别提高2.07和0.66个百分点。
关键词YOLOv3检测算法    边界框回归    交并比    BR-IoU损失算法    宽高比    
YOLOv3 Detection Algorithm Based on the Improved Bounding Box Regression Loss
SHEN Jiquan , CHEN Xiangjun , ZHAI Haixia     
College of Computer Science and Technology, Henan Polytechnic University, Jiaozuo, Henan 454000, China
Abstract: The bounding box regression loss function in the YOLOv3 detection algorithm is sensitive to the bounding box scale but does not have a strong correlation with the optimization of the algorithm detection effect evaluation standard Intersection over Union(IoU).Furthermore, and the loss function cannot accurately reflect the overlap between the ground truth and prediction boxes, resulting in poor convergence effect.In response to these problems, in this study, an improved bounding box regression loss algorithm based on IoU is proposed, namely, BR-IoU.This algorithm adopts IoU as the loss term of the bounding box regression loss function, so that the boundary boxes with different scales can obtain more balanced loss optimization weight in the regression process.On this basis, it adds a penalty item to minimize the area of the rectangle enclosed by the center point of the ground truth and prediction boxes.The added penalty item improves the consistency of the aspect ratio between the ground truth and prediction boxes, which improves regression convergence effect of the bounding box.The experimental results on PASCAL VOC and COCO datasets demonstrate that YOLOv3 using BR-IoU can effectively improve the detection accuracy without affecting the real-time performance.Compared to YOLOV3 and G-YOLO algorithms, mAP value of the proposed algorithm increases by 2.5 and 1.5 percentage points respectively on PASCAL VOC test set and 2.07 and 0.66 percentage points respectively on COCO test set.
Key words: YOLOv3 detection algorithm    bounding box regression    Intersection over Union(IoU)    BR-IoU loss algorithm    aspect ratio    

开放科学(资源服务)标志码(OSID):

0 概述

目标检测技术是很多计算机视觉任务的基础,如实例分割[1-3]、图像描述[4-5]、目标跟踪[6]等,并且在工业、安防、视频监控、人脸识别[7]、机器人视觉[8]、自动驾驶[9]等诸多领域有极大的研究价值和应用前景,受到了学者们的广泛关注。当前,目标检测的研究已取得较大进展,检测精度及速度在不断提高。但是,由于尺度变化、旋转、遮挡、光照以及物体的稠密度、角度等不同因素的影响,目标检测的精度仍有较大的提升空间。近年来,基于深度学习的目标检测成为研究热点,卷积神经网络可以快速、准确地从大量样本中学习更通用的特征,并且无需对样品进行预处理,避免了复杂的手工制作和设计。

基于深度学习的目标检测算法大致分为两阶段检测算法和单阶段检测算法两种:一种是包括R-CNN[10]、Fast R-CNN[11]、Faster R-CNN[12]和Pyramid Networks[13]的两阶段检测算法,被称为基于候选区域的目标检测算法,其将目标检测过程分解为候选区域提取、候选区域分类和候选区域坐标修正3个步骤;另一种是包括SSD[14]、RetinaNet[15]和YOLO[16-18]系列的单阶段检测算法,被称为基于回归分析的目标检测算法,其将目标检测问题视为对目标位置和类别信息的回归分析问题,通过一个神经网络模型直接输出检测结果。尽管存在这些不同的检测框架,但无论对于单阶段算法还是两阶段算法,边界框回归都是预测矩形框以对目标对象进行定位的关键步骤。

现有的目标检测算法常用平均绝对误差(Mean Absolute Error,MAE)损失(又称L1范数损失)、均方误差(Mean Square Error,MSE)损失(又称L2范数损失)函数计算边界框位置坐标的回归损失,但是L1、L2范数损失对边界框的尺度具有敏感性,尺度越小的边界框预测偏差对其影响越大,且卷积神经网络在评价边界框的回归效果时使用交并比(Intersection over Union,IoU)作为评价标准,而L1、L2范数损失与IoU之间的优化并非等价。文献[19]提出将IoU作为边界框回归损失函数对检测模型进行优化,但是存在真值框与预测框无交集时优化效果不佳的情况。文献[20]提出一种通用的优化边界框的方式破折号GIoU,解决了在真值框和预测框无交集的情况下将IoU作为边界框损失函数不能反映边界框之间的距离、以及函数梯度为零导致模型无法被优化等问题。文献[21]采用GIoU损失对YOLOv2模型损失函数进行改进,提高了模型对车身焊点的检测精度。文献[22]在YOLOv3网络中引入空间金字塔池化单元,并采用GIoU损失计算坐标损失,在不降低速度的情况下,提高了无人机对罂粟的检测效率。上述损失函数虽然总体上比传统边界框损失函数效果更好,但在某些情况下仍存在收敛效果不佳的问题。

本文提出边界框回归损失算法BR-IoU。将IoU作为边界框回归的损失项,加强损失函数的优化与IoU之间的联系,并添加惩罚项加快真值框与预测框中心点之间的重叠速度,通过使预测框和真值框宽高比保持一致,提升边界框的回归收敛效果。

1 相关工作 1.1 IoU与GIoU

交并比(IoU)也被称为Jaccard系数,用于衡量两个边界框重叠面积的相对大小,是2D和3D目标检测中常用的评价标准。IoU具有尺度不变性、非负性和对称性,不仅可以用来确定正负样本,而且还可以反映模型中预测框的检测效果。如图 1所示,A和B为2个重叠的边界框,其Jaccard系数公式如下:

$ {I}_{\mathrm{I}\mathrm{o}\mathrm{U}}(\mathrm{A}, \mathrm{B})=\frac{|\mathrm{A}\bigcap \mathrm{B}|}{|\mathrm{A}\bigcup \mathrm{B}|} $ (1)
Download:
图 1 矩形A、B与最小封闭框C Fig. 1 Rectangle A, B and the smallest enclosed frame C

将IoU作为边界框回归损失,其损失函数如下:

$ {{I}_{\mathrm{I}\mathrm{o}\mathrm{U}\_}}_{\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}=1-{I}_{\mathrm{I}\mathrm{o}\mathrm{U}} $ (2)

虽然IoU作为评价标准具有优势,但是当边框无交集时将IoU作为损失函数会导致无法反映两个边界框彼此之间的距离、损失函数梯度为零不能被优化等问题。如图 2所示,图 2(a)场景预测框的位置相较于图 2(b)场景离真实框更近,但是IoU的值却都为0。

Download:
图 2 边界框无交集时IoU的比较 Fig. 2 IoU comparison when bounding boxes have no intersection

基于此,2019年REZATOFGHI等[20]提出的GIoU继承了IoU具有的尺度不变性、非负性和对称性,同时克服了IoU在边界框无交集情况下的不足。令图 1中包含A和B的最小封闭框为C,则GIoU公式如下:

$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}}(\mathrm{A}, \mathrm{B})={I}_{\mathrm{I}\mathrm{o}\mathrm{U}}(\mathrm{A}, \mathrm{B})-\frac{|\mathrm{C}\backslash (\mathrm{A}\bigcup \mathrm{B}\left)\right|}{\left|\mathrm{C}\right|} $ (3)

当A、B两个边界框完全重叠时,$ {I}_{\mathrm{I}\mathrm{o}\mathrm{U}}(\mathrm{A}, \mathrm{B})=1 $;当A、B两个边界框无交集时,$ {I}_{\mathrm{I}\mathrm{o}\mathrm{U}}(A, B)=0 $。因此,IoU的取值区间为[0, 1]。而GIoU的取值区间具有对称性,即$ -1\le {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}}(\mathrm{A}, \mathrm{B})\le 1 $,与IoU相似:当两个边界框完全重叠,即$ \left|\mathrm{A}\bigcup \mathrm{B}\right|=\left|\mathrm{A}\bigcap \mathrm{B}\right| $时,$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}}(\mathrm{A}, \mathrm{B})=1 $;当两个边界框无交集且距离无限大时,最小包围框C的面积趋近于无穷大,此时$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}}(\mathrm{A}, \mathrm{B})=-1 $

$ \underset{\frac{|\mathrm{A}\bigcup \mathrm{B}|}{\left|\mathrm{C}\right|}\to 0}{\mathrm{l}\mathrm{i}\mathrm{m}}{G}_{\mathrm{G}\mathrm{I}\mathrm{O}\mathrm{U}}(\mathrm{A}, \mathrm{B})=-1 $ (4)

GIoU取值区间的对称性使其比IoU更能反映边界框的重叠和非重叠情况。本文将GIoU作为边界框回归损失,其损失函数如下:

$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}=1-{G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}} $ (5)

可以看出,边界框之间的GIoU值越大,$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}} $值越小,边界框的回归收敛效果越好。

1.2 YOLOv3模型

YOLOv3模型是具有代表性的单阶段目标检测算法,其通过缩放、填充将不同尺寸输入图像的大小调整为32的倍数,再分成S×S个非重叠的网格,如图 3所示(S=7)。

Download:
图 3 输入图像网格示意图 Fig. 3 Grid diagram of input image

每个网格负责检测中心点位置坐标落入该格的物体,网格中预设有3个边界框和C个类别概率。在边界框预测的5个分量xywh和置信度中,(xy)坐标表示边界框的中心点相对于网格单元的位置,(wh)坐标表示边界框的尺寸,置信度用来判断边界框中存在物体的概率。YOLOv3通过对置信度设定阈值过滤掉低分的边界框,再对剩下的边界框使用非极大值抑制(Non-Maximum Supression,NMS)算法去除冗余边界框,得到预测结果,因此,YOLOv3每个网格有$ 3\times (5+C) $个属性值。为了加强对小物体的检测性能,YOLOv3借鉴了特征金字塔网络(Feature Pyramid Network,FPN)的思想,采用多尺度特征融合的方式,在不同特征层对不同大小的物体进行预测。同时,YOLOv3通过上采样,自顶向下地融合底层的高分辨率信息和高层的高语义信息,提高了对不同尺度物体的检测精度,尤其增强了对小物体的检测精度。

YOLOv3采用L2范数计算边界框坐标的回归损失,L2范数损失中不同尺寸边界框对其预测框坐标产生偏差的敏感度也不同。相对于大尺寸边界框,尺寸越小的边界框预测偏差对其IoU的影响越大,因此,YOLOv3在边界框位置损失部分乘以系数$ (2-{t}_{\mathrm{w}}\times {t}_{\mathrm{h}}) $来缓解这一情况。为了实现多标签分类,YOLOv3采用二元交叉熵函数取代softmax函数计算边界框的类别损失。softmax函数假设每个对象只属于一个类别,但是在一些复杂场景下,一个对象可能属于多个类别(例如女人和人),因此,在目标类别较复杂的数据集上,多标签分类的二元交叉熵函数能取得更好的预测结果。YOLOv3网络模型的损失函数分为边界框坐标损失、置信度损失和分类损失。损失函数如式(6)所示:

$ \begin{array}{l}{L}_{\mathrm{L}\mathrm{o}\mathrm{s}\mathrm{s}}={b}_{\mathrm{b}\mathrm{b}\mathrm{o}\mathrm{x}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}+{c}_{\mathrm{c}\mathrm{o}\mathrm{n}\mathrm{f}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}+{p}_{\mathrm{p}\mathrm{r}\mathrm{o}\mathrm{b}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}=\\ {\lambda }_{\mathrm{c}\mathrm{o}\mathrm{o}\mathrm{r}\mathrm{d}}\sum\limits_{i=0}^{{S}^{2}}\sum\limits_{j=0}^{B}{I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}}(2-{w}_{i}^{j}\times {h}_{i}^{j})\times \\ \left[({x}_{i}^{j}-{\widehat{x}}_{i}^{j}{)}^{2}+({y}_{i}^{j}-{\widehat{y}}_{i}^{j}{)}^{2}+({w}_{i}^{j}-{\widehat{w}}_{i}^{j}{)}^{2}+({h}_{i}^{j}-{\widehat{h}}_{i}^{j}{)}^{2}\right]-\\ \sum\limits_{i=0}^{{S}^{2}}\sum\limits_{j=0}^{B}{I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}}\left[{\widehat{C}}_{i}^{j}\mathrm{l}\mathrm{n}\left({C}_{i}^{j}\right)+(1-{\widehat{C}}_{i}^{j})\mathrm{l}\mathrm{n}(1-{C}_{i}^{j})\right]-\\ {\lambda }_{\mathrm{n}\mathrm{o}\mathrm{b}\mathrm{j}}\sum\limits_{i=0}^{{S}^{2}}\sum\limits_{j=0}^{B}{I}_{ij}^{\mathrm{n}\mathrm{o}\mathrm{o}\mathrm{b}\mathrm{j}}\left[{\widehat{C}}_{i}^{j}\mathrm{l}\mathrm{n}\left({C}_{i}^{j}\right)+(1-{\widehat{C}}_{i}^{j})\mathrm{l}\mathrm{n}(1-{C}_{i}^{j})\right]-\\ \sum\limits_{i=0}^{{s}^{2}}{I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}}\sum\limits_{c\in \mathrm{c}\mathrm{l}\mathrm{a}\mathrm{s}\mathrm{s}\mathrm{e}\mathrm{s}}\left[{\widehat{P}}_{i}^{j}\mathrm{l}\mathrm{n}\left({P}_{i}^{j}\right)+(1-{\widehat{P}}_{i}^{j})\mathrm{l}\mathrm{n}(1-{P}_{i}^{j})\right]\end{array} $ (6)

其中:$ {I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}} $$ {I}_{ij}^{\mathrm{n}\mathrm{o}\mathrm{o}\mathrm{b}\mathrm{j}} $表示第i个网格中的第j个先验框是否存在待检测物体,若存在,则$ {I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}} $值为1,$ {I}_{ij}^{\mathrm{n}\mathrm{o}\mathrm{o}\mathrm{b}\mathrm{j}} $值为0,否则$ {I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}} $值为0,$ {I}_{ij}^{\mathrm{n}\mathrm{o}\mathrm{o}\mathrm{b}\mathrm{j}} $值为1。YOLOv3算法检测过程如图 4所示。

Download:
图 4 YOLOv3算法检测过程 Fig. 4 Detection process of YOLOv3 algorithm
2 BR-IoU边界框回归损失算法

采用L2范数边界框回归损失函数的YOLOv3检测算法通常会出现以下2个问题:

1)如图 5所示,4组边界框角点坐标距离的L2范数损失值相等,但IoU值却不同,这表明L2范数损失不能准确地反映IoU值的变化,与IoU之间不具有强相关性。

Download:
图 5 L2范数损失值相等时IoU的变化 Fig. 5 Change of IoU when L2 norm loss values are equal

2)L2范数损失具有尺度敏感性,在算法训练过程中,当两个边界框的IoU值相同时,大尺寸边界框会产生更多的损失值使小尺寸边界框难以被优化,导致算法对小尺寸目标的检测效果不佳。

针对上述问题,本文提出BR-IoU算法,为方便不同边界框损失算法的性能对比,将BR-IoU算法分为BR-IoU-A算法和BR-IoU-B算法。

BR-IoU-A算法将IoU作为边界框回归的损失项,强化与IoU之间的联系,并继承了IoU的尺度不变性,使得不同尺度的边界框在回归过程中获得更均衡的损失优化权重。然后算法在IoU损失项的基础上添加一个惩罚项,通过最小化预测框与真值框中心点围成矩形的面积,加快预测框中心点与真值框中心点的重叠速度。算法的惩罚项使边界框在任何情况下都存在梯度,在边界框无交集的情况下仍然能够反映边界框间的相对距离,为边界框的优化提供移动方向,如图 6所示,D是真值框Bg的中心点$ {\mathrm{b}}^{\mathrm{g}} $和预测框B的中心点$ \mathrm{b} $围成的矩形框,Bc是真值框Bg与预测框$ \mathrm{B} $的最小封闭框,$ w $$ h $为D的宽和高,$ {w}^{\mathrm{c}} $$ {h}^{\mathrm{c}} $为Bc的宽和高,则BR-IoU-A计算公式如下:

$ {B}_{\mathrm{B}\mathrm{R}-\mathrm{I}\mathrm{o}\mathrm{U}-\mathrm{A}}={I}_{\mathrm{I}\mathrm{o}\mathrm{U}}({\mathrm{B}}^{\mathrm{g}}, \mathrm{B})-\frac{w\times h}{{w}^{\mathrm{c}}\times {h}^{\mathrm{c}}} $ (7)
Download:
图 6 BR-IoU-A算法示意图 Fig. 6 Schematic diagram of BR-IoU-A algorithm

利用BR-IoU-A算法计算边界框回归损失,损失函数如下:

$ {B}_{\mathrm{B}\mathrm{R}-\mathrm{I}\mathrm{o}\mathrm{U}-\mathrm{A}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}=1-{I}_{\mathrm{I}\mathrm{o}\mathrm{U}}({\mathrm{B}}^{\mathrm{g}}, \mathrm{B})+\frac{w\times h}{{w}^{\mathrm{c}}\times {h}^{\mathrm{c}}} $ (8)

BR-IoU-A损失继承了IoU和GIoU损失的优良属性:1)BR-IoU-A损失对边界框的尺度不敏感,具有尺度不变性;2)BR-IoU-A损失在边界框无交集情况下仍然存在损失梯度,可以为边界框的优化提供移动方向;3)当两个边框完全重叠时,$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}}={B}_{\mathrm{B}\mathrm{R}-\mathrm{I}\mathrm{o}\mathrm{U}-\mathrm{A}}=1 $$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}= $ $ {B}_{\mathrm{B}\mathrm{R}-\mathrm{I}\mathrm{o}\mathrm{U}-\mathrm{A}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}=0 $;当两个边框距离无限远时,$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}}={B}_{\mathrm{B}\mathrm{R}-\mathrm{I}\mathrm{o}\mathrm{U}-\mathrm{A}}=-1 $$ {G}_{\mathrm{G}\mathrm{I}\mathrm{o}\mathrm{U}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}={B}_{\mathrm{B}\mathrm{R}-\mathrm{I}\mathrm{o}\mathrm{U}-\mathrm{A}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}=2 $。但在某些情景下,如图 7所示(其中,实线框为真值框,虚线框为平面测框),真值框宽和高的值都为1,预测框宽和高的值为0.5。此时无论预测框与真值框的中心点间的距离如何变化,GIoU与IoU损失值都不变,由此可见,BR-IoU-A损失算法能很好地区分边界框之间的重叠情况。

Download:
图 7 中心点间相对位置比较 Fig. 7 Comparison of relative positions between center points

图 7(a)图像的检测效果优于图 7(b)图像,BR-IoU-A损失算法通过值的变化来反映这种情况。

算法   BR-IoU-A边界框回归损失算法

输入   预测框B和真实框Bg,坐标分别为$ ({x}_{1}, {y}_{1}, {x}_{2}, {y}_{2}) $$ ({x}_{1}^{g}, {y}_{1}^{g}, {x}_{2}^{g}, {y}_{2}^{g}) $

输出   $ {B}_{\mathrm{B}\mathrm{R}‒\mathrm{I}\mathrm{o}\mathrm{U}‒\mathrm{A}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}} $

1.确保预测框B中$ {\mathrm{x}}_{2} > {\mathrm{x}}_{1} $$ {\mathrm{y}}_{2} > {\mathrm{y}}_{1} $

$ {\widehat{\mathrm{x}}}_{1}=\mathrm{m}\mathrm{i}\mathrm{n}({\mathrm{x}}_{1}, {\mathrm{x}}_{2}) \text{,} {\widehat{\mathrm{x}}}_{2}=\mathrm{m}\mathrm{a}\mathrm{x}({\mathrm{x}}_{1}, {\mathrm{x}}_{2}) $
$ {\widehat{\mathrm{y}}}_{1}=\mathrm{m}\mathrm{i}\mathrm{n}({\mathrm{y}}_{1}, {\mathrm{y}}_{2}) \text{,} {\widehat{\mathrm{y}}}_{2}=\mathrm{m}\mathrm{a}\mathrm{x}({\mathrm{y}}_{1}, {\mathrm{y}}_{2}) $
$ {\mathrm{A}}^{\mathrm{g}}=({\mathrm{x}}_{2}^{\mathrm{g}}-{\mathrm{x}}_{1}^{\mathrm{g}})\times ({\mathrm{y}}_{2}^{\mathrm{g}}-{\mathrm{y}}_{1}^{\mathrm{g}}) $

3.计算预测框B的面积:

$ \mathrm{A}=({\widehat{\mathrm{x}}}_{2}-{\widehat{\mathrm{x}}}_{1})\times ({\widehat{\mathrm{y}}}_{2}-{\widehat{\mathrm{y}}}_{1}) $

4.计算预测框B与真值框Bg的重叠面积I:

$ {\mathrm{x}}_{1}^{\mathrm{I}}=\mathrm{m}\mathrm{a}\mathrm{x}({\widehat{\mathrm{x}}}_{1}, {\mathrm{x}}_{1}^{\mathrm{g}}) \text{,} {\mathrm{x}}_{2}^{\mathrm{I}}=\mathrm{m}\mathrm{i}\mathrm{n}({\widehat{\mathrm{x}}}_{2}, {\mathrm{x}}_{2}^{\mathrm{g}}) $
$ {\mathrm{y}}_{1}^{\mathrm{I}}=\mathrm{m}\mathrm{a}\mathrm{x}({\widehat{\mathrm{y}}}_{1}, {\mathrm{y}}_{1}^{\mathrm{g}}) \text{,} {\mathrm{y}}_{2}^{\mathrm{I}}=\mathrm{m}\mathrm{i}\mathrm{n}({\widehat{\mathrm{y}}}_{2}, {\mathrm{y}}_{2}^{\mathrm{g}}) $
$ \mathrm{I}=\left\{\begin{array}{l}({\mathrm{x}}_{2}^{\mathrm{I}}-{\mathrm{x}}_{1}^{\mathrm{I}})\times ({\mathrm{y}}_{2}^{\mathrm{I}}-{\mathrm{y}}_{1}^{\mathrm{I}})\begin{array}{cc}, {\mathrm{x}}_{2}^{\mathrm{I}} > {\mathrm{x}}_{1}^{\mathrm{I}}, {\mathrm{y}}_{2}^{\mathrm{I}} > {\mathrm{y}}_{1}^{\mathrm{I}}& \end{array}\\ 0, \mathrm{o}\mathrm{t}\mathrm{h}\mathrm{e}\mathrm{r}\mathrm{w}\mathrm{i}\mathrm{s}\mathrm{e}\end{array}\right. $

5.$ {\mathrm{I}}_{\mathrm{I}\mathrm{o}\mathrm{U}}=\frac{\mathrm{I}}{\mathrm{u}} $,其中$ \mathrm{u}=\mathrm{A}+{\mathrm{A}}^{\mathrm{g}}-\mathrm{I} $

6.求最小封闭框$ {\mathrm{B}}^{\mathrm{C}} $的坐标:

$ {\mathrm{x}}_{1}^{\mathrm{c}}=\mathrm{m}\mathrm{i}\mathrm{n}({\widehat{\mathrm{x}}}_{1}, {\mathrm{x}}_{1}^{\mathrm{g}}) \text{,} {\mathrm{x}}_{2}^{\mathrm{c}}=\mathrm{m}\mathrm{a}\mathrm{x}({\widehat{\mathrm{x}}}_{2}, {\mathrm{x}}_{2}^{\mathrm{g}}) $
$ {\mathrm{y}}_{1}^{\mathrm{c}}=\mathrm{m}\mathrm{i}\mathrm{n}({\widehat{\mathrm{y}}}_{1}, {\mathrm{y}}_{1}^{\mathrm{g}}) \text{,} {\mathrm{y}}_{2}^{\mathrm{c}}=\mathrm{m}\mathrm{a}\mathrm{x}({\widehat{\mathrm{y}}}_{2}, {\mathrm{y}}_{2}^{\mathrm{g}}) $

7.计算最小封闭框$ {\mathrm{B}}^{\mathrm{C}} $的宽和高:

$ {\mathrm{w}}^{\mathrm{c}}={\mathrm{x}}_{2}^{\mathrm{c}}-{\mathrm{x}}_{1}^{\mathrm{c}} \text{,} {\mathrm{h}}^{\mathrm{c}}={\mathrm{y}}_{2}^{\mathrm{c}}-{\mathrm{y}}_{1}^{\mathrm{c}} $

8.计算预测框$ \mathrm{B} $和真值框$ {\mathrm{B}}^{\mathrm{g}} $的中心点坐标$ \mathrm{b} $$ {\mathrm{b}}^{\mathrm{g}} $

$ \mathrm{b}=\left(\frac{{\widehat{\mathrm{x}}}_{1}+{\widehat{\mathrm{x}}}_{2}}{2}, \frac{{\widehat{\mathrm{y}}}_{1}+{\widehat{\mathrm{y}}}_{2}}{2}\right) \text{,} {\mathrm{b}}^{\mathrm{g}}=\left(\frac{{\mathrm{x}}_{1}^{\mathrm{g}}+{\mathrm{x}}_{2}^{\mathrm{g}}}{2}, \frac{{\mathrm{y}}_{1}^{\mathrm{g}}+{\mathrm{y}}_{2}^{\mathrm{g}}}{2}\right) $

9.计算预测框B与真值框Bg中心点围成的矩形框D的坐标:

$ {\mathrm{x}}_{1}^{\mathrm{D}}=\mathrm{m}\mathrm{i}\mathrm{n}\left(\frac{{\widehat{\mathrm{x}}}_{1}+{\widehat{\mathrm{x}}}_{2}}{2}, \frac{{\mathrm{x}}_{1}^{\mathrm{g}}+{\mathrm{x}}_{2}^{\mathrm{g}}}{2}\right) $
$ {\mathrm{x}}_{2}^{\mathrm{D}}=\mathrm{m}\mathrm{a}\mathrm{x}\left(\frac{{\widehat{\mathrm{x}}}_{1}+{\widehat{\mathrm{x}}}_{2}}{2}, \frac{{\mathrm{x}}_{1}^{\mathrm{g}}+{\mathrm{x}}_{2}^{\mathrm{g}}}{2}\right) $
$ {\mathrm{y}}_{1}^{\mathrm{D}}=\mathrm{m}\mathrm{i}\mathrm{n}\left(\frac{{\widehat{\mathrm{y}}}_{1}+{\widehat{\mathrm{y}}}_{2}}{2}, \frac{{\mathrm{y}}_{1}^{\mathrm{g}}+{\mathrm{y}}_{2}^{\mathrm{g}}}{2}\right) $
$ {\mathrm{y}}_{2}^{\mathrm{D}}=\mathrm{m}\mathrm{a}\mathrm{x}\left(\frac{{\widehat{\mathrm{y}}}_{1}+{\widehat{\mathrm{y}}}_{2}}{2}, \frac{{\mathrm{y}}_{1}^{\mathrm{g}}+{\mathrm{y}}_{2}^{\mathrm{g}}}{2}\right) $

10.计算矩形框D的宽和高:

$ \mathrm{w}={\mathrm{x}}_{2}^{\mathrm{D}}-{\mathrm{x}}_{1}^{\mathrm{D}} \text{,} \mathrm{h}={\mathrm{y}}_{2}^{\mathrm{D}}-{\mathrm{y}}_{1}^{\mathrm{D}} $

11.$ {\mathrm{B}}_{\mathrm{B}\mathrm{R}‒\mathrm{I}\mathrm{o}\mathrm{U}‒\mathrm{A}}={\mathrm{I}}_{\mathrm{I}\mathrm{o}\mathrm{U}}-\frac{\mathrm{w}\times \mathrm{h}}{{\mathrm{w}}^{\mathrm{c}}\times {\mathrm{h}}^{\mathrm{c}}} $,BBR-IoU-A_loss$ =1-{\mathrm{B}}_{\mathrm{B}\mathrm{R}‒\mathrm{I}\mathrm{o}\mathrm{U}‒\mathrm{A}} $

在BR-IoU-A算法中,$ \frac{w\times h}{{w}^{\mathrm{c}}\times {h}^{\mathrm{c}}} $是BR-IoU-A损失在IoU损失的基础上添加的惩罚项,由于最小封闭框Bc的面积大于真值框Bg的面积,因此$ \frac{w\times h}{{w}^{\mathrm{c}}\times {h}^{\mathrm{c}}} $的分母$ {w}^{\mathrm{c}}\times {h}^{\mathrm{c}} $始终为正非零数且$ 0\le \frac{w\times h}{{w}^{\mathrm{c}}\times {h}^{\mathrm{c}}}\le 1 $。IoU和GIoU损失仅考虑到边界框的重叠区域,而BR-IoU-A损失同时考虑到边界框重叠区域和中心点间的相对位置。然而,预测框与真值框宽高比的一致性也是评价检测效果的重要几何因素,如图 8所示(其中,实线框为真值框,虚框为预测框),真值框的宽和高的值都为4,左侧预测框的宽和高的值分别为4和0.25,右侧预测框宽和高的值都为1。

Download:
图 8 预测框不同宽高比下的检测效果对比 Fig. 8 Comparison of detection effects under different aspect ratios of predicted bounding boxes

图 8(a)预测框的宽高比值相较于图 8(b)预测框更接近真实框,预测效果更好,但BR-IoU-A值却相同。因此,本文在BR-IoU-A的基础上,结合预测框与真值框之间宽高比的一致性提出BR-IoU-B损失算法,计算公式如下:

$ B _{{\rm{BR-IoU-B}}} = B _{{\rm{BR-IoU-A}}} -V\times C $ (9)

其中,参数V使预测框宽高比与真值框的保持一致,CV的平衡参数,当两个边界框无交集或IoU值较低时,使边界框之间的重叠因子在回归过程中获得较高优先级,从而加快边界框回归的收敛速度。

$ V={\left(\frac{{w}^{\mathrm{g}}}{{h}^{\mathrm{g}}+{w}^{\mathrm{g}}}-\frac{{w}^{\mathrm{p}}}{{h}^{\mathrm{p}}+{w}^{\mathrm{p}}}\right)}^{2} $
$ C=\frac{V}{(1-{I}_{\mathrm{I}\mathrm{O}\mathrm{U}})+V} $ (10)

其中:$ {w}^{\mathrm{g}}\mathrm{、}{h}^{\mathrm{g}} $分别为真值框的宽和高;$ {w}^{\mathrm{p}}\mathrm{、}{h}^{\mathrm{p}} $分别为预测框的宽和高。综上,BR-IoU-B损失函数如下:

$ B _{{\rm{BR-IoU-B_loss}}} = 1- B _{{\rm{BR-IoU-A_loss}}} +V\times C $ (11)

使用BR-IoU边界框回归损失算法对YOLOv3模型进行改进,改进后的损失函数如下:

$ \begin{array}{l}{L}_{\mathrm{L}\mathrm{o}\mathrm{s}\mathrm{s}}={b}_{\mathrm{b}\mathrm{b}\mathrm{o}\mathrm{x}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}+{c}_{\mathrm{c}\mathrm{o}\mathrm{n}\mathrm{f}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}+{p}_{\mathrm{p}\mathrm{r}\mathrm{o}\mathrm{b}\_\mathrm{l}\mathrm{o}\mathrm{s}\mathrm{s}}=\\ {\lambda }_{\mathrm{c}\mathrm{o}\mathrm{o}\mathrm{r}\mathrm{d}}\sum\limits_{i=0}^{{S}^{2}}\sum\limits_{j=0}^{B}{I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}}(2-{w}_{i}^{j}\times {h}_{i}^{j})\times (1-{B}_{\mathrm{B}\mathrm{R}‒\mathrm{I}\mathrm{o}\mathrm{U}})-\\ \sum\limits_{i=0}^{{S}^{2}}\sum\limits_{j=0}^{B}{I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}}\left[{\widehat{C}}_{i}^{j}\mathrm{l}\mathrm{n}\left({C}_{i}^{j}\right)+(1-{\widehat{C}}_{i}^{j})\mathrm{l}\mathrm{n}(1-{C}_{i}^{j})\right]-\\ {\lambda }_{\mathrm{n}\mathrm{o}\mathrm{b}\mathrm{j}}\sum\limits_{i=0}^{{S}^{2}}\sum\limits_{j=0}^{B}{I}_{ij}^{\mathrm{n}\mathrm{o}\mathrm{o}\mathrm{b}\mathrm{j}}\left[{\widehat{C}}_{i}^{j}\mathrm{l}\mathrm{n}\left({C}_{i}^{j}\right)+(1-{\widehat{C}}_{i}^{j})\mathrm{l}\mathrm{n}(1-{C}_{i}^{j})\right]-\\ \sum\limits_{i=0}^{{s}^{2}}{I}_{ij}^{\mathrm{o}\mathrm{b}\mathrm{j}}\sum\limits_{c\in \mathrm{c}\mathrm{l}\mathrm{a}\mathrm{s}\mathrm{s}\mathrm{e}\mathrm{s}}\left[{\widehat{P}}_{i}^{j}\mathrm{l}\mathrm{n}\left({P}_{i}^{j}\right)+(1-{\widehat{P}}_{i}^{j})\mathrm{l}\mathrm{n}(1-{P}_{i}^{j})\right]\end{array} $ (12)
3 实验结果与分析

本文实验模型基于Tensorflow1.13.1框架,编程语言为python3.7,实验操作系统为Ubuntu16.04,GPU型号为NVIDIA Tesla P40,CUDA版本为10.0。为了验证BR-IoU算法的实际性能,将原始YOLOv3的边界框回归损失部分分别替换成GIoU损失算法(简称G-YOLO)、BR-IoU-A损失算法(简称BR-YOLO-A)、BR-IoU-B损失算法(简称BR-YOLO-B)。在PASCAL VOC 2007+2012数据集和COCO 2014数据集上进行模型评测和对比实验。

在算法训练过程中,为了增强模型性能,对数据集中的原始图像进行数据增强操作,通过图像随机裁剪、旋转、平移、颜色变化等方法,增加图像的多样性,使神经网络具有更强的泛化效果,提高模型鲁棒性。此外,为检测不同尺度图像中包含的不同尺度和形状的目标,在每个检测层中设定不同尺度的先验框与真实标签进行匹配。网络输入大小为416×416,模型参数更新方式为Adam,初始学习率为0.000 1,权重衰减设置为0.000 5。对于VOC和COCO数据集,模型加载预训练权重来完成初始化,令其他没有预训练权重的部分在训练过程中自适应微调参数,然后训练整个模型。为了在训练过程中使模型稳定,算法为模型训练设置两个轮次迭代的热身阶段。实验采用平均精度(Average Precision,AP)反映每一类目标的检测效果,平均精度是从准确率和召回率两个方面来衡量检测算法的准确性,可以直观地表现模型对单个类别的检测效果;采用平均精度均值(mean Average Precision,mAP)来衡量多类目标的平均检测精度,mAP值越高,模型在全部类别中综合性能越高。

3.1 在PASCAL VOC数据集上的对比实验

PASCAL VOC 2007+2012训练集共包含16 551张图像、40 058个样本框,分为4个大类和20个小类。VOC 2007测试集包含4 952张图像、12 032个样本框。PASCAL VOC通过计算数据集IoU=0.5时的mAP对模型进行评价。该组实验在PASCAL VOC数据集上的训练迭代次数为100轮,测试集上的mAP及各类别目标的AP检测结果如图 9表 1所示。

Download:
图 9 PASCAL VOC数据集上不同算法的AP对比 Fig. 9 AP comparison of different algorithms on PASCAL VOC data set
下载CSV 表 1 PASCAL VOC数据集上不同算法的性能对比 Table 1 Performance comparison of different algorithms on PASCAL VOC data set

实验结果表明,在不影响模型检测速度的情况下BR-YOLO-B算法的mAP高出原YOLOv3算法2.54个百分点,高出G-YOLO算法1.51个百分点。

3.2 在COCO数据集上的对比实验

COCO 2014训练集共包含82 783张图像,分为80个类别。从验证集中选取5 000张图像作为测试集对模型进行评价。COCO数据集将IoU在[0.5∶0.95]区间内,步长为0.05的10个取值分别计算mAP并取平均值作为检测模型的评价结果,相比VOC数据集,COCO数据集的多IoU评价标准对检测算法的要求更高,更能反映检测算法的综合性能。该组实验在COCO 2014数据集上的迭代次数为120轮,实验结果如图 10表 2所示。可以看出,后3种模型相对YOLOv3的检测效果均有所提高,这得益于BR-IoU-A损失算法相比L2损失算法对不同尺度边界框的回归优化更均衡,并且与评价指标IoU之间的优化更加密切,模型BR-YOLO-A算法的mAP对比YOLOv3提高了1.63个百分点。此外,由于BR-IoU-B算法进一步对预测框与真值框之间宽高比的一致性进行优化,因此BR-YOLO-B算法的mAP比YOLOv3提高了2.07个百分点。

Download:
图 10 PASCAL VOC数据集上的检测效果对比 Fig. 10 Comparison of detection effects on PASCAL VOC data set
下载CSV 表 2 COCO 2014数据集上不同算法的mAP对比 Table 2 mAP comparison of different algorithms on COCO 2014 data set  

表 3所示为4种检测算法的空间与时间复杂度比较结果,其中时间复杂度为模型检测每张图像所需要的时间。由于模型的骨干网络都为Darknet53,因此4种算法的网络参数数量基本相同,约为236×106。由表 3可以看出,相比YOLOv3算法,BR-YOLO-A和BR-YOLO-B算法检测速度更快。

下载CSV 表 3 不同算法的时空间复杂度对比 Table 3 Time and space complexity comparison of different algorithms
4 结束语

针对YOLOv3的L2范数边界框回归损失函数对边界框的尺度敏感,以及与算法检测效果的评价标准IoU之间相关性不强等问题,本文提出BR-IoU算法,通过将IoU作为损失项强化边界框回归与算法评估标准的相关性,最小化真值框和预测框中心点形成矩形的面积,同时提高预测框与真值框之间宽高比的一致性,以加强边界框的回归收敛性能。实验结果表明,BR-IoU能够有效提高YOLOv3算法的检测精度和检测速度。下一步将结合本文算法设计思想对其他目标检测算法的边界框回归损失函数进行改进,在不影响检测速度的情况下达到更高的检测精度。

参考文献
[1]
HARIHARAN B, ARBELAEZ P, GIRSHICK R, et al. Simultaneous detection and segmentation[C]//Proceedings of European Conference on Computer Vision. Berlin, Germany: Springer, 2014: 297-312.
[2]
DAI J F, HE K M, SUN J. Instance-aware semantic segmentation via multi-task network cascades[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2016: 3150-3158.
[3]
HE K, GKIOXARI G, DOLLAR P, et al. Mask R-CNN[C]//Proceedings of IEEE International Conference on Computer Vision. Washington D.C., USA: IEEE Press, 2017: 2980-2988.
[4]
KARPATHY A, FEI-FEI L. Deep visual-semantic alignments for generating image descriptions[J]. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2017, 39(4): 664-676. DOI:10.1109/TPAMI.2016.2598339
[5]
WU Q, SHEN C, WANG P, et al. Image captioning and visual question answering based on attributes and external knowledge[J]. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2018, 40(6): 1367-1381. DOI:10.1109/TPAMI.2017.2708709
[6]
KANG K, LI H S, YAN J J, et al. T-CNN: tubelets with convolutional neural networks for object detection from videos[J]. IEEE Transactions on Circuits and Systems for Video Technology, 2018, 28(10): 2896-2907. DOI:10.1109/TCSVT.2017.2736553
[7]
DENG J, GUO J, XUE N, et al. ArcFace: additive angular margin loss for deep face recognition[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2019: 4685-4694.
[8]
崔坤坤, 樊绍胜. 基于动态双窗口的机器人视觉导航与特征识别方法[J]. 计算机工程, 2020, 46(9): 313-320.
CUI K K, FAN S S. Visual navigation and feature recognition method of robot based on dynamic double windows[J]. Computer Engineering, 2020, 46(9): 313-320. (in Chinese)
[9]
CHEN X Z, MA H M, WAN J, et al. Multi-view 3d object detection network for autonomous driving[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2017: 6526-6534.
[10]
GIRSHICK R, DONAHUE J, DARRELL T, et al. Rich feature hierarchies for accurate object detection and semantic segmentation[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2014: 580-587.
[11]
GIRSHICK R. Fast R-CNN[C]//Proceedings of IEEE International Conference on Computer Vision. Washington D.C., USA: IEEE Press, 2015: 1440-1448.
[12]
REN S, HE K, GIRSHICK R, et al. Faster R-CNN: towards real-time object detection with region proposal networks[J]. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2017, 39(6): 1137-1149. DOI:10.1109/TPAMI.2016.2577031
[13]
LIN T, DOLLAR P, GIRSHICK R, et al. Feature pyramid networks for object detection[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2017: 936-944.
[14]
LIU W, ANGUELOV D, ERHAN D, et al. SSD: single shot multibox detector[C]//Proceedings of European Conference on Computer Vision. [S. l. ]: Eurographics Association Press, 2016: 21-37.
[15]
LIN T, GOYAL P, GIRSHICK R, et al. Focal loss for dense object detection[C]//Proceedings of IEEE International Conference on Computer Vision. Washington D.C., USA: IEEE Press, 2017: 2999-3007.
[16]
REDMON J, DIVVALA S, GIRSHICK R, et al. You only look once: unified, real-time object detection[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2016: 779-788.
[17]
REDMON J, FARHADI A. YOLO9000: better, faster, stronger[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2017: 6517-6525.
[18]
REDMON J, FARHADI A. YOLOv3: an incremental improvement[EB/OL]. [2019-11-02]. https://arxiv.org/abs/1804.02767.
[19]
YU J H, JIANG Y N, WANG Z Y, et al. Unitbox: an advanced object detection network[C]//Proceedings of the 24th ACM International Conference on Multimedia. New York, USA: ACM Press, 2016: 516-520.
[20]
REZATOFIGHI H, TSOI N, GWAK J, et al. Generalized intersection over union: a metric and a loss for bounding box regression[C]//Proceedings of IEEE Conference on Computer Vision and Pattern Recognition. Washington D.C., USA: IEEE Press, 2019: 658-666.
[21]
何智成, 王振兴. 基于改进YOLOv2的白车身焊点检测方法[J]. 计算机工程, 2020, 46(11): 246-254.
HE Z C, WANG Z X. Welding spot detection method for body in white based on improved YOLOv2[J]. Computer Engineering, 2020, 46(11): 246-254. (in Chinese)
[22]
ZHOU J, TIAN Y C, YUAN C, et al. Improved UAV opium poppy detection using an updated YOLOv3 model[J]. Sensors, 2019, 19(22): 4851. DOI:10.3390/s19224851