模型四元素:
- 背景
- 模型结构与输出
- 后处理
- 损失函数
背景
模型的效率在计算机视觉中变得越来越重要,作者系统地研究了用于目标检测的神经网络体系结构设计选择并提出一些关键的优化措施以提高效。首先,提出一个新的FPN结构BiFPN(重复的进行自上而下和自下而上的特征融合)。其次,我们提出了一种复合缩放方法,该方法可以在同一时间统一缩放所有主干,特征网络和框/类预测网络的分辨率,深度和宽度。
EfficientDet是一个基于anchor的检测模型,通过anchor来建模,一个anchor代表一个样本,通过anchor来预测目标的位置和类别。特征图的同一个位置会设置多个anchor,不同特征图中anchor的基本大小也不一样。
与anchor free模型的差异:
- anchor free模型一般通过目标的中心点建模,基于anchor的模型通过预先设置的anchor建模。
- 基于anchor的模型需要设置有关anchor的许多超参数。
- 每个位置的样本数不同: anchor free模型输出特征图上的一个位置只设置一个样本,而基于anchor的检测模型会设置多个样本(同一个位置设置多个不同形状anchor)。
- 正样本的确定方式不同:anchor free模型正样本通过位置确定,基于anchor的检测模型正样本通过IOU来确定
性能
model | AP | Params |
---|---|---|
EfficientDet-D0 (512) | 34.3 | 3.9M |
EfficientDet-D1 (640) | 40.2 | 6.6M |
EfficientDet-D3 (896) | 46.8 | 12M |
EfficientDet-D4 (1024) | 49.3 | 21M |
EfficientDet-D5 (1280) | 51.3 | 34M |
EfficientDet-D6 (1280) | 52.2 | 52M |
EfficientDet-D7 (1536) | 53.4 | 52M |
EfficientDet-D7x (1536) | 54.4 | 77M |
模型结构与输出
efficientdet的主干网络是EfficientNet,FPN结构是BiFPN。fpn输出共享分类和回归的预测网络。
训练pipeline
classification的输出形状为(batch,n,num_classes)
classification的输出形状为(batch,n,4),其中n的大小与每个位置设置的anchor数量以及特征图的大小有关,4是指4个位置偏移量(dx,dy,dw,dh),定义如下:
也是指encode的过程
d x = g t c x − a n c h o r c x a n c h o r w i d t h dx=\frac{gt_{cx}-anchor_{cx}}{anchor_{width}} dx=anchorwidthgtcx−anchorcx
d y = g t c y − a n c h o r c y a n c h o r h e i g h t dy=\frac{gt_{cy}-anchor_{cy}}{anchor_{height}} dy=anchorheightgtcy−anchorcy
d w = log ( g t w i d t h a n c h o r w i d t h ) dw=\log \left( \frac{gt_{width}}{anchor_{width}} \right) dw=log(anchorwidthgtwidth)
d h = log ( g t h e i g h t a n c h o r h e i g h t ) dh=\log \left( \frac{gt_{height}}{anchor_{height}} \right) dh=log(anchorheightgtheight)
cx代表中心点的横坐标,cy代表中心点的纵坐标。
后处理
注意:如果分类考虑背景类的话输出时要用softmax处理,如果不考虑背景类的话就用sigmoid处理。softmax是输入一个向量输出每个元素的概率,sigmoid是对一个元素进行非线性变换。
- 先中分类的输出结果中找到置信度超过阈值的样本
- 再找到这些样本对应的bboxes,进行nms
decode的过程为:
b b o x c x = d x ^ × a n c h o r w i d t h + a n c h o r c x bbox_{cx}=\widehat{dx} \times anchor_{width}+anchor_{cx} bboxcx=dx ×anchorwidth+anchorcx
b b o x c y = d y ^ × a n c h o r h e i g h t + a n c h o r c y bbox_{cy}=\widehat{dy} \times anchor_{height}+anchor_{cy} bboxcy=dy ×anchorheight+anchorcy
b b o x w i d t h = exp ( d w ^ ) × a n c h o r w i d t h bbox_{width}=\exp \left( \widehat{dw} \right) \times anchor_{width} bboxwidth=exp(dw )×anchorwidth
b b o x h e i g h t = exp ( d y ^ ) × a n c h o r h e i g h t bbox_{height}=\exp \left( \widehat{dy} \right) \times anchor_{height} bboxheight=exp(dy )×anchorheight
anchor 参数:
anchor_size = 4
anchor_num = 9
scales = [2 ** 0, 2 ** (1.0 / 3.0), 2 ** (2.0 / 3.0)]
ratios = [(1.0, 1.0), (1.4, 0.7), (0.7, 1.4)]
每一个featuremap中anchor的大小为anchor_size x step x scale
损失函数
Focal Loss
Focal Loss是为了解决模型训练过程中正负样本不平衡问题。
首先介绍一下cross entropy(CE):
C E ( p , y ) = { − log ( p ) i f y = 1 − log ( 1 − p ) o t h e r w i s e CE(p,y)=\begin{cases}-\log \left( p\right) &if\ y=1\\ -\log \left( 1-p\right) &otherwise\end{cases} CE(p,y)={−log(p)−log(1−p)if y=1otherwise
y y y是真实值, y ∈ { ± 1 } y\in \left\{ \pm 1\right\} y∈{±1}
p p p是模型的预测值, p ∈ [ 0 , 1 ] p\in \left[ 0,1\right] p∈[0,1]
为了方便记忆,我们定义
p
t
p_{t}
pt:
p
t
=
{
p
i
f
y
=
1
1
−
p
o
t
h
e
r
w
i
s
e
p_{t}=\begin{cases}p &if\ y=1\\ 1-p &otherwise\end{cases}
pt={p1−pif y=1otherwise
这样 C E ( p , y ) CE(p,y) CE(p,y)可以写成 C E ( p t ) = − log ( p t ) CE(p_{t})=-\log \left( p_{t}\right) CE(pt)=−log(pt)
Focal Loss Definition:
F L ( p t ) = − ( 1 − p t ) γ log ( p t ) FL\left( p_{t}\right) =- \left( 1-p_{t}\right)^{\gamma }\log \left( p_{t}\right) FL(pt)=−(1−pt)γlog(pt)
其中 γ ∈ [ 0 , 5 ] \gamma \in \left[ 0,5\right] γ∈[0,5],当 γ = 0 \gamma=0 γ=0时,FL就变成了CE
也可以在上面的基础上加上一个系数 α t \alpha_{t} αt
F L ( p t ) = − α t ( 1 − p t ) γ log ( p t ) FL\left( p_{t}\right) =-\alpha_{t} \left( 1-p_{t}\right)^{\gamma }\log \left( p_{t}\right) FL(pt)=−αt(1−pt)γlog(pt)
补充
anchor的正负样本分配问题
计算gt与anchor的口矩阵
每个gt都有一个与自己iou最大的anchor,与自己iou最大的anchor就是正样本。
每个anchor也有与自己最大iou的gt,如果这个iou超过设定的阈值,那么这个anchor就是正样本。