6

目标检测新范式:End-to-End Object Detection with Transformers

 2 years ago
source link: https://blog.aimoon.top/detr/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

标题 Keep your Eyes on the Lane: Real-time Attention-guided Lane Detection

年份: 2020 年 5 月

GB/T 7714: [1] Carion N , Massa F , Synnaeve G , et al. End-to-End Object Detection with Transformers[J]. 2020.

DETR是FIR提出的基于Transformers的端到端目标检测,没有NMS后处理步骤、没有anchor,结果在coco数据集上效果与Faster RCNN相当,且可以很容易地将DETR迁移到其他任务例如全景分割。

原文链接【论文笔记】从Transformer到DETR (zhihu.com)

Transformer原理

Transformer的整体架构

Transformer是2017年NIPS上的文章,题目为Attention is All You Need。它使用attention组成了encoder-decoder的框架,并将其用于机器翻译。它的大致结构如下:

TranFormer

每个句子先经过六个Encoder进行编码,然后经过六个Decoder进行解码,得到最后的输出

https://gitee.com/xiaomoon/image/raw/master/Img/The_transformer_encoder_decoder_stack.png

Transformer Encoder

https://gitee.com/xiaomoon/image/raw/master/Img/encoder_with_tensors_2.pngEnconder

如上图所示,Encoder的输入是词向量(大概是word2vec之类的编码)和Positional Encoding(二者相加之后送入第一个Encoder),然后经过Self Attention和Layer Norm,最后是一个Feed Forward Network。另外每个Encoder中还有两个Skip Connection。这种Encoder结构重复六次之后就得到了句子的编码。我会在接下来几个subsection中介绍Encoder中的各个function。

Positional Encoding

Positional Encoding旨在为每个特征的位置与通道进行编码,其公式如下所示: PE(pos,2i)=sin(pos/1002i/dmodel) PE_{(pos,2i)} = sin(pos/100^{2i/d_{model}}) PE(pos,2i)​=sin(pos/1002i/dmodel​)

PE(pos,2i+1)=cos(pos/10002i/dmodel) PE_{(pos,2i+1)} = cos(pos/1000^{2i/d_{model}}) PE(pos,2i+1)​=cos(pos/10002i/dmodel​)

其中i是通道的下标,pos是位置的下标,dmodeld_{model}dmodel​是特征的总通道数。

奇数位置的PE使用余弦来表示,偶数位置的PE使用正弦表示。 10002i/dmodel1000^{2i/d_{model}}10002i/dmodel​ 用于控制不同通道PE的波长(这样不同通道的特征有一定的区分性)。

PE最后与输入的特征相加,并被送到第一个Encoder中(有种CoordConv的感觉)。

这篇文章可视化了Positional Encoding的前几维,大概是下图这样:

https://gitee.com/xiaomoon/image/raw/master/Img/986023-20191022192204824-1243950811.png

Self Attention

Encoder中的第一个模块是Self Attention,它可以用以下公式来表示 Attention⁡(Q,K,V)=softmax⁡(QKTdk)V \operatorname{Attention}(Q, K, V)=\operatorname{softmax}\left(\frac{Q K^{T}}{\sqrt{d_{k}}}\right) V Attention(Q,K,V)=softmax(dk​​QKT​)V 如下图:

https://gitee.com/xiaomoon/image/raw/master/Img/self-attention-matrix-calculation-2.pngThe self-attention calculation in matrix form

其中dkd_kdk​是特征的维度,Q、K、V分别是Query、Key、Value,是对X分别进行三次矩阵乘法得到的向量:

https://gitee.com/xiaomoon/image/raw/master/Img/self-attention-matrix-calculation.pngEvery row in the X matrix corresponds to a word in the input sentence. We again see the difference in size of the embedding vector (512, or 4 boxes in the figure), and the q/k/v vectors (64, or 3 boxes in the figure)

作者在文中采用了多头(Multi-Head)Attention,就是h个Self Attention的结果Cat起来,然后压到一个较低的维度。

MultiHead⁡(Q,K,V)= Concat ( head 1,…, head h)WO where head i= Attention (QWiQ,KWiK,VWiV) \begin{aligned} \operatorname{MultiHead}(Q, K, V) &=\text { Concat }\left(\text { head }_{1}, \ldots, \text { head }_{h}\right) W^{O} \\ \text { where head }_{i} &=\text { Attention }\left(Q W_{i}^{Q}, K W_{i}^{K}, V W_{i}^{V}\right) \end{aligned} MultiHead(Q,K,V) where head i​​= Concat ( head 1​,…, head h​)WO= Attention (QWiQ​,KWiK​,VWiV​)​

Feed Forward Network(FFN)

Encoder最后的操作是带Skip Connection的FFN。其本质是FC-ReLU-FC的操作。 FFN⁡(x)=max⁡(0,xW1+b1)W2+b2 \operatorname{FFN}(x)=\max \left(0, x W_{1}+b_{1}\right) W_{2}+b_{2} FFN(x)=max(0,xW1​+b1​)W2​+b2​

Transformer Decoder

Decoder的第一个Attention与Encoder类似,只不过是带了一个与位置有关的Mask(预测第i个位置的时候,输入是前i-1个位置的embedding,后面位置的编码被Mask掉了)。

第二个Attention依然是同样的结构,区别在其输入不同:Q来自于Decoder,K和V来自于Encoder。这种做法大抵是为了结合Encoder和Decoder的信息。

FFN的结构也与Encoder类似。

最后在经过6个Decoder之后,由线性层和SoftMax得到每一个单词的翻译结果。

Decoder

DETR的原理

引用

看代码后发现它的输出是定长的:100个检测框和类别。某自动化所的学长说,这种操作可能跟COCO评测的时候取top 100的框有关,我认为他说的有道理。

从这种角度看,DETR可以被认为具有100个adaptive anchor,其中Encoder和Object Query分别对特征和Anchor进行编码,最后用Decoder+FFN得到检测框和类别。

这篇文章也从侧面说明100个Anchor完全够用。但是进一步想,100个Anchor其实也是有一些冗余输出的:很多图里面物体很少,并不能用完100个检测框吧。

这一点在篇车道线检测的paper中也有体现,他设置的N为5可以达到最优效果

DETR的结构

DETR的整体结构Transformer类似:Backbone得到的特征铺平,加上Position信息之后送到一坨Encoder里,得到一些candidates的特征。这100个candidates是被Decoder并行解码的(需要很大的显存,但实现的时候可写成不并行的),以得到最后的检测框。

https://gitee.com/xiaomoon/image/raw/master/Img/image-20210514134434125.pngDETR模型

DETR Encoder

网络一开始是使用Backbone(比如ResNet)提取一些feature,然后降维到d×HWd×HWd×HW。

Feature降维之后与Spatial Positional Encoding相加,然后被送到Encoder里。

为了体现图像在x和y维度上的信息,作者的代码里分别计算了两个维度的Positional Encoding,然后Cat到一起。

pos_x = torch.stack((pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4).flatten(3)
pos_y = torch.stack((pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4).flatten(3)
pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2)

FFN、LN等操作也与Transformer类似。Encoder最后得到的结果是对N个物体编码后的特征。

DETR Decoder

DETR Decoder的结构也与Transformer类似,区别在于Decoder并行解码N个object。

每个Decoder有两个输入:一路是Object Query(或者是上一个Decoder的输出),另一路是Encoder的结果。

我一开始不明白Object Query是怎样得到的。后来从代码看,Object Query是一组nn.Embedding的weight(就是一组学到的参数)。

另外一个与Transformer不同的地方是,DETR的Decoder也加了Positional Encoding。

最后一个Decoder后面接了两个FFN,分别预测检测框及其类别。

Bipartite Matching

由于输出物体的顺序不一定与ground truth的序列相同,作者使用二元匹配将GT框与预测框进行匹配。其匹配策略如下:

σ^=arg⁡min⁡σ∈SN∑iNLmatch⁡(yi,y^σ(i)) \hat{\sigma}=\underset{\sigma \in \mathfrak{S}_{N}}{\arg \min } \sum_{i}^{N} \mathcal{L}_{\operatorname{match}}\left(y_{i}, \hat{y}_{\sigma(i)}\right) σ^=σ∈SN​argmin​i∑N​Lmatch​(yi​,y^​σ(i)​)
> 其中 > > yiy_iyi​为ground truth objects > > y^σ(i)\hat{y}_{\sigma (i)}y^​σ(i)​为predicted objects > >
> Lmatch⁡(yi,y^σ(i))=−1{ci≠∅}p^σ(i)(ci)+1{ci≠∅}Lbox(bi,b^σ(i))\mathcal{L}_{\operatorname{match}}\left(y_{i}, \hat{y}_{\sigma(i)}\right) = -\mathbb{1}_{\left\{c_{i} \neq \varnothing\right\}} \hat{p}_{\sigma(i)}\left(c_{i}\right)+\mathbb{1}_{\left\{c_{i} \neq \varnothing\right\}} \mathcal{L}_{\mathrm{box}}\left(b_{i}, \hat{b}_{\sigma(i)}\right)Lmatch​(yi​,y^​σ(i)​)=−1{ci​​=∅}​p^​σ(i)​(ci​)+1{ci​​=∅}​Lbox​(bi​,b^σ(i)​) >

最后的损失函数:

LHungarian (y,y^)=∑i=1N[−log⁡p^σ^(i)(ci)+1{ci≠∅}Lbox (bi,b^σ^(i))] \mathcal{L}_{\text {Hungarian }}(y, \hat{y})=\sum_{i=1}^{N}\left[-\log \hat{p}_{\hat{\sigma}(i)}\left(c_{i}\right)+\mathbb{1}_{\left\{c_{i} \neq \varnothing\right\}} \mathcal{L}_{\text {box }}\left(b_{i}, \hat{b}_{\hat{\sigma}}(i)\right)\right] LHungarian ​(y,y^​)=i=1∑N​[−logp^​σ^(i)​(ci​)+1{ci​​=∅}​Lbox ​(bi​,b^σ^​(i))]
Lbox(bi,b^σ(i))=λiou Liou (bi,b^σ(i))+λL1∥bi−b^σ(i)∥1 \mathcal{L}_{\mathrm{box}}(b_i, \hat{b}_{\sigma (i)}) = \lambda_{\text {iou }} \mathcal{L}_{\text {iou }}\left(b_{i}, \hat{b}_{\sigma(i)}\right)+\lambda_{\mathrm{L} 1}\left\|b_{i}-\hat{b}_{\sigma(i)}\right\|_{1} Lbox​(bi​,b^σ(i)​)=λiou ​Liou ​(bi​,b^σ(i)​)+λL1​∥∥∥∥​bi​−b^σ(i)​∥∥∥∥​1​
> Liou\mathcal{L}_{iou}Liou​是[GIoU](https://giou.stanford.edu/GIoU.pdf)损失,p^σ(i)(ci)\hat{p}_{\sigma (i)}(c_i)p^​σ(i)​(ci​)是物体被预测为某一类别的概率

Experiment and Ablation

与Faster RCNN有可比性,小目标要差一些

https://gitee.com/xiaomoon/image/raw/master/Img/image-20210514161835815.pngComparison with Faster R-CNN

测试Encoder和Decoder要用多少层

https://gitee.com/xiaomoon/image/raw/master/Img/image-20210514162004817.pngEffect of encoder size

https://gitee.com/xiaomoon/image/raw/master/Img/image-20210514162116130.png AP and AP50 performance after each decoder layer.

测试不同的Positional Encoding

https://gitee.com/xiaomoon/image/raw/master/Img/image-20210514162256393.png Results for different positional encodings

测试Loss

https://gitee.com/xiaomoon/image/raw/master/Img/image-20210514162401601.png Effect of loss components on AP

参考资料

【论文笔记】从Transformer到DETR (zhihu.com)

The Illustrated Transformer – Jay Alammar – Visualizing machine learning one concept at a time.

End-to-End Object Detection with Transformers_feng__shuai的博客

项目地址:https://github.com/facebookresearch/detr

快速查看detr效果:https://github.com/plotly/dash-detr


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK