CeiT:训练更快的多层特征抽取ViT

news/2024/7/19 11:49:06 标签: transformer, 深度学习, 人工智能

【GiantPandaCV导语】来自商汤和南洋理工的工作,也是使用卷积来增强模型提出low-level特征的能力,增强模型获取局部性的能力,核心贡献是LCA模块,可以用于捕获多层特征表示。

引言

针对先前Transformer架构需要大量额外数据或者额外的监督(Deit),才能获得与卷积神经网络结构相当的性能,为了克服这种缺陷,提出结合CNN来弥补Transformer的缺陷,提出了CeiT:

(1)设计Image-to-Tokens模块来从low-level特征中得到embedding。

(2)将Transformer中的Feed Forward模块替换为Locally-enhanced Feed-Forward(LeFF)模块,增加了相邻token之间的相关性。

(3)使用Layer-wise Class Token Attention(LCA)捕获多层的特征表示。

经过以上修改,可以发现模型效率方面以及泛化能力得到了提升,收敛性也有所改善,如下图所示:

方法

1. Image-to-Tokens

使用卷积+池化来取代原先ViT中7x7的大型patch。

x ′ = I 2   T ( x ) = MaxPool ⁡ ( BN ⁡ ( Conv ⁡ ( x ) ) ) \mathbf{x}^{\prime}=\mathrm{I} 2 \mathrm{~T}(\mathbf{x})=\operatorname{MaxPool}(\operatorname{BN}(\operatorname{Conv}(\mathbf{x}))) x=I2 T(x)=MaxPool(BN(Conv(x)))

2. LeFF

将tokens重新拼成feature map,然后使用深度可分离卷积添加局部性的处理,然后再使用一个Linear层映射至tokens。

x c h , x p h = Split ⁡ ( x t h ) x p l 1 = GELU ⁡ ( BN ⁡ ( Linear ⁡ ( ( x p h ) ) ) x p s = SpatialRestore ⁡ ( x p l 1 ) x p d = GELU ⁡ ( BN ⁡ ( DWConv ⁡ ( x p s ) ) ) x p f = Flatten ⁡ ( x p d ) x p l 2 = GELU ⁡ ( BN ⁡ ( Linear ⁡ 2 ( x p f ) ) ) x t h + 1 = Concat ⁡ ( x c h , x p l 2 ) \begin{aligned} \mathbf{x}_{c}^{h}, \mathbf{x}_{p}^{h} &=\operatorname{Split}\left(\mathbf{x}_{t}^{h}\right) \\ \mathbf{x}_{p}^{l_{1}} &=\operatorname{GELU}\left(\operatorname{BN}\left(\operatorname{Linear}\left(\left(\mathbf{x}_{p}^{h}\right)\right)\right)\right.\\ \mathbf{x}_{p}^{s} &=\operatorname{SpatialRestore}\left(\mathbf{x}_{p}^{l_{1}}\right) \\ \mathbf{x}_{p}^{d} &=\operatorname{GELU}\left(\operatorname{BN}\left(\operatorname{DWConv}\left(\mathbf{x}_{p}^{s}\right)\right)\right) \\ \mathbf{x}_{p}^{f} &=\operatorname{Flatten}\left(\mathbf{x}_{p}^{d}\right) \\ \mathbf{x}_{p}^{l_{2}} &=\operatorname{GELU}\left(\operatorname{BN}\left(\operatorname{Linear} 2\left(\mathbf{x}_{p}^{f}\right)\right)\right) \\ \mathbf{x}_{t}^{h+1} &=\operatorname{Concat}\left(\mathbf{x}_{c}^{h}, \mathbf{x}_{p}^{l_{2}}\right) \end{aligned} xch,xphxpl1xpsxpdxpfxpl2xth+1=Split(xth)=GELU(BN(Linear((xph)))=SpatialRestore(xpl1)=GELU(BN(DWConv(xps)))=Flatten(xpd)=GELU(BN(Linear2(xpf)))=Concat(xch,xpl2)

3. LCA

前两个都比较常规,最后一个比较有特色,经过所有Transformer层以后使用的Layer-wise Class-token Attention,如下图所示:

LCA模块会将所有Transformer Block中得到的class token作为输入,然后再在其基础上使用一个MSA+FFN得到最终的logits输出。作者认为这样可以获取多尺度的表征。

实验

SOTA比较:

I2T消融实验:

LeFF消融实验:

LCA有效性比较:

收敛速度比较:

代码

模块1:I2T Image-to-Token

  # IoT
  self.conv = nn.Sequential(
      nn.Conv2d(in_channels, out_channels, conv_kernel, stride, 4),
      nn.BatchNorm2d(out_channels),
      nn.MaxPool2d(pool_kernel, stride)    
  )
  
  feature_size = image_size // 4

  assert feature_size % patch_size == 0, 'Image dimensions must be divisible by the patch size.'
  num_patches = (feature_size // patch_size) ** 2
  patch_dim = out_channels * patch_size ** 2
  self.to_patch_embedding = nn.Sequential(
      Rearrange('b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = patch_size, p2 = patch_size),
      nn.Linear(patch_dim, dim),
  )

模块2:LeFF

class LeFF(nn.Module):
    
    def __init__(self, dim = 192, scale = 4, depth_kernel = 3):
        super().__init__()
        
        scale_dim = dim*scale
        self.up_proj = nn.Sequential(nn.Linear(dim, scale_dim),
                                    Rearrange('b n c -> b c n'),
                                    nn.BatchNorm1d(scale_dim),
                                    nn.GELU(),
                                    Rearrange('b c (h w) -> b c h w', h=14, w=14)
                                    )
        
        self.depth_conv =  nn.Sequential(nn.Conv2d(scale_dim, scale_dim, kernel_size=depth_kernel, padding=1, groups=scale_dim, bias=False),
                          nn.BatchNorm2d(scale_dim),
                          nn.GELU(),
                          Rearrange('b c h w -> b (h w) c', h=14, w=14)
                          )
        
        self.down_proj = nn.Sequential(nn.Linear(scale_dim, dim),
                                    Rearrange('b n c -> b c n'),
                                    nn.BatchNorm1d(dim),
                                    nn.GELU(),
                                    Rearrange('b c n -> b n c')
                                    )
        
    def forward(self, x):
        x = self.up_proj(x)
        x = self.depth_conv(x)
        x = self.down_proj(x)
        return x
        
class TransformerLeFF(nn.Module):
    def __init__(self, dim, depth, heads, dim_head, scale = 4, depth_kernel = 3, dropout = 0.):
        super().__init__()
        self.layers = nn.ModuleList([])
        for _ in range(depth):
            self.layers.append(nn.ModuleList([
                Residual(PreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout))),
                Residual(PreNorm(dim, LeFF(dim, scale, depth_kernel)))
            ]))
    def forward(self, x):
        c = list()
        for attn, leff in self.layers:
            x = attn(x)
            cls_tokens = x[:, 0]
            c.append(cls_tokens)
            x = leff(x[:, 1:])
            x = torch.cat((cls_tokens.unsqueeze(1), x), dim=1) 
        return x, torch.stack(c).transpose(0, 1)

模块3:LCA

class LCAttention(nn.Module):
    def __init__(self, dim, heads = 8, dim_head = 64, dropout = 0.):
        super().__init__()
        inner_dim = dim_head *  heads
        project_out = not (heads == 1 and dim_head == dim)

        self.heads = heads
        self.scale = dim_head ** -0.5

        self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False)

        self.to_out = nn.Sequential(
            nn.Linear(inner_dim, dim),
            nn.Dropout(dropout)
        ) if project_out else nn.Identity()

    def forward(self, x):
        b, n, _, h = *x.shape, self.heads
        qkv = self.to_qkv(x).chunk(3, dim = -1)
        q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), qkv)
        q = q[:, :, -1, :].unsqueeze(2) # Only Lth element use as query

        dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale

        attn = dots.softmax(dim=-1)

        out = einsum('b h i j, b h j d -> b h i d', attn, v)
        out = rearrange(out, 'b h n d -> b n (h d)')
        out =  self.to_out(out)
        return out

class LCA(nn.Module):
    # I remove Residual connection from here, in paper author didn't explicitly mentioned to use Residual connection, 
    # so I removed it, althougth with Residual connection also this code will work.
    def __init__(self, dim, heads, dim_head, mlp_dim, dropout = 0.):
        super().__init__()
        self.layers = nn.ModuleList([])
        self.layers.append(nn.ModuleList([
                PreNorm(dim, LCAttention(dim, heads = heads, dim_head = dim_head, dropout = dropout)),
                PreNorm(dim, FeedForward(dim, mlp_dim, dropout = dropout))
            ]))
    def forward(self, x):
        for attn, ff in self.layers:
            x = attn(x) + x[:, -1].unsqueeze(1)
            x = x[:, -1].unsqueeze(1) + ff(x)
        return x

参考

https://arxiv.org/abs/2103.11816

https://github.com/rishikksh20/CeiT-pytorch/blob/master/ceit.py


http://www.niftyadmin.cn/n/1003132.html

相关文章

史上最牛的脑筋急转弯

1、什么动物最没有方向感?答案:麋鹿(迷路) 2、什么动物猜拳永远不会有输赢?答案:螃蟹 3、什么动物最容易摔倒?答案:狡猾的狐狸(脚滑) 4、什么鸡没有翅膀&…

CoAtNet: 90.88% Paperwithcode榜单第一,层层深入考虑模型设计

【GiantPandaCV导语】CoAtConvolution Attention,paperwithcode榜单第一名,通过结合卷积与Transformer实现性能上的突破,方法部分设计非常规整,层层深入考虑模型的架构设计。 引言 Transformer模型的容量大,由于缺乏…

Android提高十六篇之使用NDK把彩图转换灰度图

在Android上使用JAVA实现彩图转换为灰度图,跟J2ME上的实现类似,不过遇到频繁地转换或者是大图转换时,就必须使用NDK来提高速度了。本文主要通过JAVA和NDK这两种方式来分别实现彩图转换为灰度图,并给出速度的对比。 先来简单…

php5.4(64位)+apache2.4(64位)+mysql环境搭建

2019独角兽企业重金招聘Python工程师标准>>> 参考文章: http://www.myhack58.com/Article/sort099/sort0100/2012/35578_2.htm http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html 第一步:安装apache 下载地址:http://www.apachelounge.com/downlo…

基于cglib动态代理的缓存框架

2019独角兽企业重金招聘Python工程师标准>>> 首先,以下代码是在一位大神代码的基础上进行了稍加修改 一、自定义缓存框架 所用到的技术:cglib动态代理(MethodInterceptor),自定义注解,BeanPostProcessor接口的使用&…

当可变形注意力机制引入Vision Transformer

【GiantPandaCV导语】通过在Transformer基础上引入Deformable CNN中的可变性能力,在降低模型参数量的同时提升获取大感受野的能力,文内附代码解读。 引言 Transformer由于其更大的感受野能够让其拥有更强的模型表征能力,性能上超越了很多CN…

jquery 父窗口 子窗口 相关操作

最近项目的需要&#xff0c;使用到了iframe&#xff0c;也遇到了一些问题。 <1> js或者jQuery访问页面中的框架iframe. 注意:框架内的页面是不能跨域的! 假设有两个页面,在相同域下. 假设&#xff1a;父窗口 index.html &#xff0c;有 id 为 subifrm 的 iframe 1. 在in…

Weblogic生产模式将用户名密码写入配置文件的方法(本人亲测,木有问题)

方法1&#xff1a;用户名、密码没有被加密vi /home/weblogic/bea/user_projects/domains/test1_domain/bin/startWebLogic.sh文件前面参数赋值部分加入:WLS_USERweblogicWLS_PWweblogicvi /home/weblogic/bea/user_projects/domains/test1_domain/bin/stopWebLogic.sh文件前面参…