前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Transformer在视觉领域的应用

Transformer在视觉领域的应用

作者头像
YoungTimes
发布2023-09-01 08:57:18
4150
发布2023-09-01 08:57:18
举报

论文: An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale

这篇文章将NLP领域常用的Transformer结构引入到视觉领域,打通了NLP领域与视觉领域的鸿沟,证明了视觉任务对于CNN的依赖不是必要的。在大型数据集上预训练的VIT模型,在中小型(ImageNet、CIFAR-100、VTAB等)图像识别Benchmark上,可以取得与基于Convolutional Network的SOTA模型相媲美的效果。

VIT的核心思想是把原始图像划分成一个个的小Patch,每个Patch相对于NLP的一个Word,就可以对其应用Transformer结构了。

Image Patches

Standard Transformer需要1D输入序列,因为为了处理2D图像,需要把HxWxC的图片变长NX(

P^2

C) 的1D序列,其中(H, W)是原始图像的尺寸,C是图像的Channel数,(P, P)是单个Image Patch的大小,N是Patch的数量,N = HW/

P^2

如上图所示,原始图像的大小为224x224x3,每个Patch的大小16x16x3,将原始图像切分为14 x 14 = 196个Patch,相当于NLP中的196个Word,每个word的dimension是768,所以整个图像的输入维度为197x768。

Class Token

为了分类,这里借鉴了Bert,额外增加一个1x768的Extra Learning Embedding,与图像的Embedding一起送入Transformer Encoder,因此Transformer Encoder的输入维度是: 197 x 768。

网络最终会把Class Token的输出作为Image Representation,连接到MLP Head,用于图像分类。

附录中,作者讲到,其实对Transformer的所有输出应用Global Average-Pooling (GAP)也可以达到同样的效果。之所以使用Class Token,是为了与原始Transformer模型尽可能相像,以此证明本文的效果确实是引入Transformer得到的。

Position Encoding

HXW的Image被打散为PxP的Patch之后,Patch之间是有相对位置关系的,所以需要Position Embedding来记录位置信息。论文中用了1D Learnable Position Embedding,将Position Information编码为1X768的向量。

附录中,对比了1D、2D、Relative Position的位置编码的效果,差别不大。

作者认为,出现这种结果的原因是:

“We speculate that since our Transformer encoder operates on patch-level inputs, as opposed to pixel-level, the differences in how to encode spatial information is less important. More precisely, in patch-level inputs, the spatial dimensions are much smaller than the original pixel-level inputs, e.g., 14 × 14 as opposed to 224 × 224, and learning to represent the spatial relations in this resolution is equally easy for these different positional encoding strategies.

模型分析

Inductive bias(归纳偏置)

在CNN中,局部性(Locality)和平移等边性(Translation Equivariance)贯穿模型的始终。

“In CNNs, locality, two-dimensional neighborhood structure, and translation equivariance are baked into each layer throughout the whole model.

而在VIT很少用到先验的归纳偏置,所有关于位置的信息都要从头开始学。因此在中小数据集上,VIT比CNN效果差也可以理解的。

“In ViT, only MLP layers are local and translationally equivariant, while the self-attention layers are global. The two-dimensional neighborhood structure is used very sparingly: in the beginning of the model by cutting the image into patches and at fine-tuning time for adjusting the position embeddings for images of different resolution.

“Other than that, the position embeddings at initialization time carry no information about the 2D positions of the patches and all spatial relations between the patches have to be learned from scratch.

Hybrid Architecture

既然CNN在小数据集上效果好,Transformer在获取全局信息上能力更强,那么可以将二者结合一下。

“As an alternative to raw image patches, the input sequence can be formed from feature maps of a CNN.

即在将2D图像转换为1D序列时,不将图片打成Patch,而是直接将224X224X3的图像直接喂给CNN,得到14 X 14 x 768的特征图,就得到了与Patch方法相同维度的序列数据。

Experiments

在小规模训练集上,VIT的效果不如Resnet,作者给出的原因是,VIT没有CNN的归纳偏置、Weight Decay等策略,在小规模训练集上容易过拟合,导致迁移效果不好。

随着数据集规模的增加,VIT的效果逐渐超越Resnet。

在同等计算量的情形下,VIT的效果要比Resnet好,这也对应了论文中提到的VIT比CNN需要更少的计算资源。

“Vision Transformer (ViT) attains excellent results compared to state-of-the-art convolutional networks while requiring substantially fewer computational resources to train。

Inspecting Vision Transformer

为了更好的理解VIT,我们研究下它的内部实现过程。首先是Linear Project Layer,如下图所示,跟CNN一样,该层确实学到了每个Patch的低维表达。

“Figure 7 (left) shows the top principal components of the the learned embedding filters. The components resemble plausible basis functions for a low-dimensional representation of the fine structure within each patch.

RGB embedding filters(first 28 principal components)

其实是Position Embedding。可以看到,虽然输入的是一维位置,但网络确实学到了图像的二维位置表达,这也解释了前面提到的,为什么不同的Position Encoding方法对于最终的效果没有影响;

最后,分析下Self-Attention的全局获取信息的能力。如下图所示,可以看到,在Low layer中,VIT已经可以同时获取到局部和全局的信息,随着模型Depth增加,网络越来越关注全局信息,意味这模型已经很好的学习到了High-Level语义信息。

参考材料

1.[李沐的VIT论文精读] https://www.youtube.com/watch?v=FRFt3x0bO94&t=65s

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 半杯茶的小酒杯 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Image Patches
  • Class Token
  • Position Encoding
  • 模型分析
    • Inductive bias(归纳偏置)
      • Hybrid Architecture
      • Experiments
      • Inspecting Vision Transformer
      • 参考材料
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档