A Survey on Visual Transformer 论文笔记

A Survey on Visual Transformer

论文链接: https://arxiv.org/abs/2012.12556

一、 Problem Statement

本文是transformer的综述。

二、 Direction

  1. Formulation of Transformer
  2. Vision Transformer

三、 Method

1、 Formulation of Transformer

Transformer 一开始是用在NLP领域的。它主要是由相同结构的多个encoder, decoder模块组成。每一个encoder和decoder都是由 self-attention layer和feed-forward neural network 组成。而decoder还包括 encoder-decoder attention layer

接下来分别拆开每个模块来看以下。

(1) Self-Attention layer

简单的self-attention layer输入是三个不同的vectors: query vector qkey vector kvalue vector v, 各自维度为: d q = d k = d v = d m o d e l = 512 d_q = d_k = d_v = d_{model}=512 dq=dk=dv=dmodel=512
来自不同的向量组合成三个不同的矩阵,Q, K, V。然后 attention function 根据下面步骤来进行计算:

  • Step 1: 计算不同输入向量的分数 S = Q ⋅ K T S = Q \cdot K^T S=QKT
  • Step 2: 为了梯度稳定性,需要标准化分数 S n = S / d k S_n=S/\sqrt{d_k} Sn=S/dk
  • Step 3: 用softmax function 把分数转换为概率 P = softmax ( S n ) P = \text{softmax}(S_n) P=softmax(Sn)
  • Step 4: 获取带权重的value matrix Z = V ⋅ P Z=V\cdot P Z=VP

上面的步骤可以融合成一个公式:

Attention ( Q , K , V ) = softmax ( Q ⋅ K T d k ) ⋅ V \text{Attention}(Q,K,V) = \text{softmax}(\frac{Q \cdot K^T}{\sqrt{d_k}})\cdot V Attention(Q,K,V)=softmax(dk QKT)V
而对于在Decoder中的 encoder-decoder attention layer, 它的attention function 和上面大致上一样,除了以下一点:
key matrix K 和 value matrix V 是来自于encoder 模块的,query matrix Q是来自于前面一层。

前面的步骤是位置无关的,意味着self-attention layer 没有能力去捕捉位置信息。为了解决这个问题,在原始的input embedding上添加一个维度为 d m o d e l d_{model} dmodel的positional encoding。具体如下:

PE ( p o s , 2 i ) = sin ⁡ ( p o s 1000 0 2 i d m o d e l ) \text{PE}(pos, 2i) = \sin(\frac{pos}{10000^{\frac{2i}{d_{model}}}}) PE(pos,2i)=sin(10000dmodel2ipos)
PE ( p o s , 2 i + 1 ) = cos ⁡ ( p o s 10000 2 i d m o d e l ) \text{PE}(pos, 2i+1)=\cos(\frac{pos}{10000\frac{2i}{d_{model}}}) PE(pos,2i+1)=cos(10000dmodel2ipos)

其中,pos 是词在句子中的位置, i 表示positional encoding的当前维度。

(2) Multi-Head Attention

Multi-head attention 是一个提升普通self-attentioin layer的一个机制。一个简单的single-head self-attention layer 限制了我们专注于一个或多个特定位置的能力,而不会同时影响对其他同样重要位置的注意。具体实施就是,给定一个输入向量和heads的数量, h h h,输入向量首先回变成三个不同的向量组: the query group, the key group, 和the value group。在每一个group里面,分别有 h h h个vecotors,维度为 d q ′ = d k ′ = d v ′ = d m o d e l / h = 64 d_{q'}=d_{k'}=d_{v'}=d_{model}/h=64 dq=dk=dv=dmodel/h=64。来自于不同输入的向量会形成三个不同的groups of matrixs { Q i } i = 1 h , { K i } i = 1 h \{Q_i\}^h_{i=1}, \{K_i\}^h_{i=1} {Qi}i=1h,{Ki}i=1h { V i } i = 1 h \{V_i\}^h_{i=1} {Vi}i=1h。Multi-head attention的步骤可以总结为:

MultiHead ( Q ′ , K ′ , V ′ ) = Concat ( h e a d 1 , . . . , h e a d h ) W ∘ \text{MultiHead}(Q',K',V')=\text{Concat}(head_1, ..., head_h)W^{\circ} MultiHead(Q,K,V)=Concat(head1,...,headh)W
其中,
h e a d i = A t t e n t i o n ( Q i , K i , V i ) head_i = Attention(Q_i, K_i, V_i) headi=Attention(Qi,Ki,Vi)

Q ′ , K ′ , V ′ Q', K', V' Q,K,V分别是 { Q i } i = 1 h , { K i } i = 1 h , { V i } i = 1 h \{Q_i\}^h_{i=1},\{K_i\}^h_{i=1},\{V_i\}^h_{i=1} {Qi}i=1h,{Ki}i=1h,{Vi}i=1h的连接。 W ∘ W^{\circ} W是维度为 R d m o d e l × d m o d e l R^{d_{model} \times d_{model}} Rdmodel×dmodel的linear projection matrix。

(3) Residual Connection in the Encoder and Decoder

在每一个encoder和decoder的sub-layer之间添加了residual connection。加强了信息的流动。在residual connection之后还添加了layer-normalization。这些操作可以归结为:

LayerNorm(X+Attention(X)) \text{LayerNorm(X+Attention(X))} LayerNorm(X+Attention(X))
其中, X X X是self-attention layer的输入。这是因为query, key和value matrices Q, K, V是来自于同样的输入matrix X。

(4) Feed-Forward Network

在self-attention layers 之后都会有Feed-Forward Network(FFN)。其包含两个linear transformation layer 和 一个 nonlinear activation function。

FFN ( X ) = W 2 σ ( W 1 X ) \text{FFN}(X) = W_2 \sigma(W_1X) FFN(X)=W2σ(W1X)

其中 σ ( ) \sigma() σ()是GELU。

(4) Final Layer in the Decoder

final layer是用来把向量转化回词汇。由一个 linear layer 和 softmax layer组成。linear layer 把向量映射回维度为 d w o r d d_{word} dword的逻辑向量, d w o r d d_{word} dword是字典里面词汇的数量。softmax layer把逻辑向量转换为probabilities。

而对于CV的任务,大多说transformers会使用original transformer’s encoder module。这样的transformers会被认为是一个新的feature selector。相比于只关注local characteristics的CNNs, transformer可以捕捉long-distance characteristics,意味着它可以更好地获得全局信息。

相比于hidden state必须逐个计算的RNNs, transformer更有效因为其self-attention layer的输出和FCN可以 parallel computation。

Vision Transformer

视觉任务也越来越多使用transformer。 我们从Classification, objection detection等等的任务进行大概的了解。

1. Classification

Transformer可以作为网络的Backbone。对于visual tranformer, 它们使用了 tokenizer 来把像素group成一个小的 visual tokens 。然后这些 visual tokens直接用于图像分类,transformers用于构建tokens之间的联系。最近使用transformer作分类任务的有 iGPT, ViT, 和 DeiT。

(1) iGPT

Generative pre-training methods 包含了两个阶段: pre-training stage和 fine-tunning stage。

(2) ViT

Vision Transformer 是直接对sequences of image patches进行classification。
首先,图像 x ∈ R H × W × C x \in R^{H \times W \times C} xRH×W×C 变为一系列的 flattened 2D patches x p ∈ R N × ( P 2 ⋅ C ) x_p \in R^{N \times (P^2 \cdot C)} xpRN×(P2C) ( P , P ) (P, P) (P,P)是每一个image patch 的大小。 sequence length 是 N = H W / P 2 N = HW/P^2 N=HW/P2。因为transformer要求在所有的层上输入固定的长度,因此需要一个linear projection layer生成patch 和 position 的embedding输入进encoder。随后使用了transformer的标准encoder,和MLP head进行分类。

(3) DeiT

Data-efficient image Transformer 是一个 convolution-free transformer。与ViT-B有同样的结构但参数比较少。如果使用CNN 蒸馏会提高性能。

2. Generic Object Detection

Transformer-based object detection methods 大致上分为两类: transformer-based set prediction 和 transformer-based backbone。

(1) Transformer-based Set Prediction

著名的DETR就是基于这个方法。它是一个end-to-end的目标检测器,能够把目标检测任务当成简单的set prediction 问题,消除了传统人工设计的Anchor 和 NMS。具体框架如下:

DETR一开始使用CNN backbone提取特征,然后和position encoding结合,形成一个flattened feature输入到transformer encoder。Decoder 会把encoder的 embeddings和 positional encodings(object queries)当成输入,产生 N N N个输出embeddings。 其中这里的 N N N是一个预先设定的值,通常大于图像的目标数。最后简单的Feed-Forward Networks会用来计算最后的预测结果,包括bounding boxes 和 class labels。 DETR采用 bipartite matching algorithm来比较ground-truth和prediction。 Loss fuinction为 Hungarian loss
L Hungarian ( y , y ^ ) ∑ i = 1 N [ − l o g P ^ σ ^ ( i ) ( c i ) + 1 { c i ≠ ϕ } L b o x ( b i , b ^ σ ^ ( i ) ) ] L_{\text{Hungarian} (y, \hat{y})} \sum_{i=1}^N [-log\hat{P}_{\hat{\sigma}_{(i)}}(c_i) + 1_{\{c_i \neq \phi\}}L_{box}(b_i, \hat{b}_{\hat{\sigma}}(i))] LHungarian(y,y^)i=1N[logP^σ^(i)(ci)+1{ci=ϕ}Lbox(bi,b^σ^(i))]
其中, y , y ^ y, \hat{y} y,y^是ground-truth和prediction。 σ ^ \hat{\sigma} σ^是 optimal assignment, C i C_i Ci是ground-truth label和 P ^ σ ^ ( i ) ( c i ) \hat{P}_{\hat{\sigma}_{(i)}}(c_i) P^σ^(i)(ci)是预测的label。 b i b_i bi是ground-truth bounding boxes, b ^ σ ^ ( i ) \hat{b}_{\hat{\sigma}}(i) b^σ^(i)是预测的bounding boxes。
但是DETR有它的局限性,比如说训练时间长和对小目标的检测效果较差。因此,有学者进一步提出了Deformable DETR, Adaptive Clustering Transformer 等等。

(2) Transformer-based Backbone

Transformer也可以作为backbone。输入的图像分割成数个patches,然后输入到vision transformer,输出enbedding features,最后通过dection head进行预测。ViT-FRCNN是其中的一个例子。

四、 Conclusion

目前先关注这么多transformer知识,后续补充。

五、 Reference

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值