这里默认 3 ∗ 3 3*3 3∗3个anchor,图中标出以输入为672*640
大小时,每层对应的尺度,以及每层对应anchor个数和每个特征层base anchor对应到原图的anchor大小.
在faster rcnn 中存在3个框,其中anchor, ground truth为已知固定的框, predict 为预测框
其中:
A n c h o r : ( A x , A y , A w , A h ) Anchor :(A_x,A_y,A_w,A_h) Anchor:(Ax,Ay,Aw,Ah)
P r e d i c t : ( P x , P y , P w , P h ) Predict :(P_x,P_y,P_w,P_h) Predict:(Px,Py,Pw,Ph)
G r o u n d T r u t h : ( G x , G y , G w , G h ) GroundTruth :(G_x,G_y,G_w,G_h) GroundTruth:(Gx,Gy,Gw,Gh)
边界框回归的目的即寻找一个 F F F,使得
F ( A x , A y , A w , A h ) = ( P x , P y , P w , P h ) ≈ ( G x , G y , G w , G h ) F(A_x,A_y,A_w,A_h) = (P_x,P_y,P_w,P_h) \approx (G_x,G_y,G_w,G_h) F(Ax,Ay,Aw,Ah)=(Px,Py,Pw,Ph)≈(Gx,Gy,Gw,Gh)
平移
P x = A w ⋅ d x ( A ) + A x P_x = A_w \cdot d_x(A)+A_x Px=Aw⋅dx(A)+Ax
P y = A h ⋅ d y ( A ) + A y P_y = A_h \cdot d_y(A) + A_y Py=Ah⋅dy(A)+Ay
放缩
P w = A w ⋅ e d w ( A ) P_w = A_w \cdot e^{d_w(A)} Pw=Aw⋅edw(A)
p h = A h ⋅ e d h ( A ) p_h = A_h \cdot e^{d_h(A)} ph=Ah⋅edh(A)
当输入 anchor 和 groundtruth 相差不远可以近似使用线性回归模型转换
d ∗ ( A ) = W ∗ T ⋅ Φ ( A ) d_*(A) = W_*^T \cdot \Phi(A) d∗(A)=W∗T⋅Φ(A)
其中 ∗ = x , y , w , h * = x,y,w,h ∗=x,y,w,h
L o s s = ∑ i N ( t ∗ i − W ∗ T ⋅ Φ ( A i ) ) 2 Loss = \sum_{i}^N (t_*^i - W_*^T \cdot \Phi(A^i))^2 Loss=i∑N(t∗i−W∗T⋅Φ(Ai))2
其中 t ∗ t_* t∗ 为 Anchor 与 Groundtruth 的位置位移参数
t x = G x − A x A w t_x = \frac {G_x - A_x}{A_w} tx=AwGx−Ax
t y = G y − A y A h t_y = \frac {G_y - A_y}{A_h} ty=AhGy−Ay
t w = ln G w A w t_w = \ln \frac {G_w}{A_w} tw=lnAwGw
t h = ln G h A h t_h = \ln \frac {G_h}{A_h} th=lnAhGh
retinaNet网络回归部分学习到的参数即为 t ∗ t_* t∗
l1 loss
l2 loss
Smooth l1 loss:结合二者优缺点
S m o o t h l 1 l o s s ( x ) = { 0.5 ∗ x 2 , ∣ x ∣ < 1 ∣ x ∣ − 0.5 , o t h e r Smooth l1 loss(x)= \begin{cases} 0.5*x^{2}, |x|<1\\ |x|-0.5, other \end{cases} Smoothl1loss(x)={0.5∗x2,∣x∣<1∣x∣−0.5,other
交叉熵损失函数:
C E = − l o g ( p ) CE = -log(p) CE=−log(p)
focal loss:
F L = − α ( 1 − p ) γ l o g ( p ) FL = -\alpha (1-p)^{\gamma}log(p) FL=−α(1−p)γlog(p)
原理:在网络预测中,正负样本比相差大(由anchor开以推测出预测样本有几万个,而gt只有几个),并且负样本大多为简单样本,大量的简单样本损失累计会导致训练缓慢,所以对预测错误的样本添加权重,从而使简单样本损失降得更大,从而优化了训练。