概述
本文主要汇总目标检测算法中用于计算bounding box偏差的loss函数。
Smooth L1 Loss
由微软rgb大神于Fast RCNN论文提出该方法
(1)假设 x x x为预测框和真实框之间的数值差异,则 L 1 L_{1} L1、 L 2 L_{2} L2和 S m o o t h L 1 Smooth_{L_{1}} SmoothL1 L o s s Loss Loss定义为:
L 1 = ∣ x ∣ L_{1} = \mid x \mid L1=∣x∣
L 2 = x 2 L_{2} = x^2 L2=x2
S m o o t h L 1 ( x ) = { 0.5 x 2 , i f ∣ x ∣ < 1 ∣ x ∣ − 0.5 , o t h r e s w i s e Smooth_{L_{1}}(x) = \begin{cases} 0.5x^2, \qquad if \mid x \mid<1 \\ \mid x \mid - 0.5, othreswise \end{cases} SmoothL1(x)={0.5x2,if∣x∣<1∣x∣−0.5,othreswise
(2)3个损失函数对
x
x
x的导数分别为:
d
L
1
(
x
)
x
=
{
1
,
i
f
x
≥
0
−
1
,
o
t
h
e
r
s
w
i
s
e
\cfrac{\mathrm{d}L_{1}(x)}{x} = \begin{cases} 1, \qquad if x \geq 0 \\ -1, \quad otherswise \end{cases}
xdL1(x)={1,ifx≥0−1,otherswise
d L 2 ( x ) x = 2 x \cfrac{\mathrm{d}L_{2}(x)}{x} = 2x xdL2(x)=2x
d S m o o t h L 1 ( x ) x = { x , i f ∣ x ∣ < 1 ± 1 , o t h e r s w i s e \cfrac{\mathrm{d}Smooth_{L_{1}}(x)}{x} = \begin{cases} x, \qquad if \mid x \mid < 1 \\ \pm 1, \quad otherswise \end{cases} xdSmoothL1(x)={x,if∣x∣<1±1,otherswise
从损失函数对 x x x的导数可知:
- L 1 L_{1} L1损失函数对 x x x的导数为常数,在训练后期, x x x很小时,如果学习率不变,损失函数会在稳定值附近波动,很难收敛于更高的精度。
- L 2 L_{2} L2损失函数对 x x x的导数在 x x x很大时,其导数也非常大,在训练初期不稳定。
- S m o o t h L 1 Smooth_{L1} SmoothL1完美的避开了 L 1 L_{1} L1和 L 2 L_{2} L2 的缺点。
(3)实际目标检测回归任务中的loss为:
L
l
o
c
(
t
u
,
v
)
=
∑
i
∈
(
x
,
y
,
w
,
h
)
S
m
o
o
t
h
L
1
(
t
i
u
−
v
i
)
L_{loc}(t^u, v) = \sum_{i \in (x,y,w,h)} Smooth_{L_{1}}(t_{i}^u-v_{i})
Lloc(tu,v)=i∈(x,y,w,h)∑SmoothL1(tiu−vi)
其中,
v
v
v表示GT的框坐标,
t
u
t^u
tu表示预测的框坐标,即分别求4个参数的Loss,然后相加得到Bounding Box Regression Loss。这种方式的前提是假设4个参数是相互独立的,然而4个参数本身是有一定的相关性的。
IoU Loss
论文UnitBox: An Advanced Object Detection Network提出改方法
L
i
o
u
=
−
l
n
(
i
o
u
)
L_{iou} = -ln(iou)
Liou=−ln(iou)
存在问题:
- 当预测框和目标框不相交( i o u = 0 iou=0 iou=0)时,不能反映预测框与目标框距离的远近,损失函数此时不可导,无法优化2个框不相交的情况;
- 当预测框和目标框大小确定,只要2个框的相交值是确定的, i o u iou iou值就相同,所以 i o u iou iou不能反映2个框是如何相交的。
GIoU Loss
论文Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression提出该方法
I o U = ∣ B ⋂ B g t ∣ ∣ B ⋃ B g t ∣ IoU = \cfrac{\mid B \bigcap B^{gt} \mid}{\mid B \bigcup B^{gt} \mid} IoU=∣B⋃Bgt∣∣B⋂Bgt∣
G I o u = I o U − ∣ C − ( B ⋃ B g t ) ∣ ∣ C ∣ GIou = IoU - \cfrac{\mid C - (B \bigcup B^{gt}) \mid}{\mid C \mid} GIou=IoU−∣C∣∣C−(B⋃Bgt)∣
L G I o U = 1 − G I o U L_{GIoU} = 1 - GIoU LGIoU=1−GIoU
其中: B B B为预测框, B g t B^{gt} Bgt为真实框, C C C为 B B B和 B g t B^{gt} Bgt的最小外接矩形。
DIoU Loss
论文Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression提出该方法
L D I o U = 1 − I o U + ρ 2 ( b , b g t ) c 2 L_{DIoU} = 1 - IoU + \cfrac{\rho^2(b,b^{gt})}{c^2} LDIoU=1−IoU+c2ρ2(b,bgt)
其中:
- b b b和 b g t b_{gt} bgt分别表示预测框和目标框的中心点
- ρ ( ⋅ ) \rho(\cdot) ρ(⋅)表示欧氏距离
- c c c表示预测框和目标框的最小外接矩形的对角线长度
CIoU Loss
论文Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression提出该方法
C I o U L o s s CIoU Loss CIoULoss在 D I o U DIoU DIoU惩罚项的基础上加了一个影响因子 α υ \alpha \upsilon αυ,这个因子把预测框长宽比拟合目标框的长宽比考虑进去。
υ = 4 π 2 ( a r c t a n ( w g t h g t ) − a r c t a n ( w h ) ) 2 \upsilon = \cfrac{4}{\pi^2}(arctan(\cfrac{w^{gt}}{h^{gt}}) - arctan(\cfrac{w}{h}))^2 υ=π24(arctan(hgtwgt)−arctan(hw))2
α = υ ( 1 − I o U ) + υ \alpha = \cfrac{\upsilon}{(1-IoU)+\upsilon} α=(1−IoU)+υυ
L C I o U = 1 − I o U + ρ 2 ( b , b g t ) c 2 + α υ L_{CIoU} = 1 - IoU + \cfrac{\rho^2(b,b^{gt})}{c^2} + \alpha \upsilon LCIoU=1−IoU+c2ρ2(b,bgt)+αυ