【论文笔记】Swin-Transformer系列阅读笔记

一、Swin Transformer V1

paper:Swin Transformer: Hierarchical Vision Transformer using Shifted Windows

github:https://github.com/microsoft/Swin-Transformer

本文提出了一个计算机视觉任务中的通用backbone模型:Swin Transformer。Swin将self-attention限制在局部窗口内进行,降低了Attention的计算量,同时利用滑动窗口机制使得不同窗口间建立联系,Swin在各个视觉任务屠榜(一个字:强)。

1、网络结构

Swin-T的网络结构如下所示,主要包含Patch Partion、Linear Embedding、Swin Transformer Block、Patch Merging等模块。

(a)Patch Partion/Linear Embedding

Patch Partion和Linear Embedding组合成Patch Embedding,通过步长为4、核大小为4的卷积实现1/4下采样。

(b)Patch Merging

除了Patch Partion,其他所有的下采样通过Patch Merging实现,假设输入维度为[N, C, H, W]对于相邻的2x2个embedding,将其concat组合,得到特征图维度为[N, C, H/4, W/4],在接一个linear proj即可得到[N, 2C, H/4, W/4]的输出,降低分辨率的同时,增加特征维度,可参考下图。(下图来自朱欤博士的【从零开始学transformer】,链接在文末)

 (c) Shifted Window based Self-Attention

假设输入维度为[batch_size, hw, c],那么其计算量如下所示,当特征图分辨率较高时,hw较大,计算量非常大。

 为了降低计算量,可以将其划分为不重复的窗口,计算量有显著降低(M为窗口尺寸):

 Attention计算量和划分窗口后的计算量对比:

划分了窗口能够显著降低计算量,但是又引入了一个新问题:窗口间的联系没了。为了解决这个问题,引入了shifted windows。如下图所示,在划分窗口时,使窗口位置变化,即可在不同的窗口间添加联系。但是直接划分窗口又会引入新的问题:窗口的数量和包含的patch会变化。

解决方法也比较简单,使用cyclic shift替代window partition策略,将特征图的值整体偏移一定位置即可(torch.roll函数),论文中偏移量为M/2,M为window的尺寸。

(d)Relative position bias

在self-attention中,引入了位置偏置B,因为相对位置的取值范围为[-M+1, M-1],所以设定可学习参数矩阵\widehat{B}\widehat{B} \in \mathbb{R}^{(2M-1)\times (2M-1)} ,B的值从\widehat{B}中取。实验证明使用Relative position bias好于不适用Relative position bias或者使用 absolute position embedding。

(e)Swin Transformer Block        

2个连续的transformer block,间隔采用shifted window(SW-MSA表示shifted window multi-head self-attention,W-MSA表示 window multi-head self-attention)。

 2、实验结果

1、ImageNet-1k

在ImageNet-22k上预训练,ImageNet-1k准确率达到87.3%(太强了)。

 2、目标检测COCO

 3、语义分割ADE20K

 二、Swin Transformer V2

待完成

参考链接:

1、【从零开始学视觉Transformer】

猜你喜欢

转载自blog.csdn.net/qq_40035462/article/details/123939058