【论文笔记】Video Vision Transformer(ViViT)

以下是我看了ViViT这篇文章的理解,如果理解有误,欢迎各位大佬指正。

原文:https://arxiv.org/abs/2103.15691

代码:https://github.com/google-research/scenic.

目录

一.介绍

二.简单介绍ViT原理

 三.Video Vision Transformer原理(ViViT)

 1.Tublet Embedding

2.“central  frame initialisation”3D滤波器生成方法

3.四种变体Attention的模型原理

1)Spatio-Temporal Attention Model

2) Factorised Encoder Model

3)Factorised Self-Attention Model

4)Factorised Dot-Product Attention Model

4.四个模型的区别


一.介绍

ViViT是一个视频分类模型,基于ViT模型进行了一些改进。ViT只用于2D图像的分类识别,视频与图像的区别是,视频引入了时间维度,因此ViViT模型在识别视频的时候也引入了时间维度,提出了tublet embedding来划分patches;3D滤波器的另一种生成方式;引入了时间维度的4种变体Attention模型,在各个数据集上的精确度都表现优秀:

改进点:

1.提出了一种"Tubelet embedding "方法划分patches

2.提出了3D卷积滤波器的另一种生成方法:"central frame initialisation",来生成tokens

3.提出了4种变体Attention的模型:

        1) Spatial-Temporal Attention Model(采用了joint space-time attention机制)

        2)Factorised Encoder Model(效果在这四个提出的模型中最好)

        3)Factorised Self-Attention Model(类似于TimeSformer的divided space-time attention机制)

        4)Factorised Dot-Product Attention(在MSA多头自注意力机制中,一半的头只算空间的Attention,一半的头只算时间的Attention)

二.简单介绍ViT原理

        

 三.Video Vision Transformer原理(ViViT)
 1.Tublet Embedding

在没有提出tublet embedding之前,一般用于划分视频patches用的是Uniform Frame Sampling:

该论文提出了Tubelet embedding生成patches:

2.“central  frame initialisation”3D滤波器生成方法

不论图像或者视频变成token都需要对划分的patches进行变换,其中图像生成token用的是2D卷积滤波器,因为图像划分的patches是2D的,而tublet embedding划分的patches是3D的,需要用3D卷积滤波器对其进行处理。通常3D滤波器生成用的是沿着时间维度复制2D滤波器并将其平均化,以此来“膨胀”它们变成3D滤波器:

        

该论文提出了用“central  frame initialisation”:即只有在[t/2]取整数的位置有2D滤波器,其余沿时间位置都是0,这样模型能够在训练过程中学习从多个帧中聚合时间信息:

3.四种变体Attention的模型原理
1)Spatio-Temporal Attention Model

该模型与ViT模型不同之处在于计算注意力的方式不同,采用了联合时空注意力机制,同时输入的token也不同,ViT是2D patches通过2D卷积滤波器降维生成token,该模型是3D tublet embedding patches通过3D卷积滤波器(用“central  frame initialisation”方法生成)降维生成token,将 token输入到以下结构中:

 我通过TimeSformer那篇论文,觉得这里的Joint Space-Time Attention中计算注意力得分的公式应该是:

2) Factorised Encoder Model

Factorised Encoder的主要思想是用了两个编码器来处理时间和空间的信息:空间编码器来提取空间特征;时间编码器用来提取时间特征,由于该结构是分别处理时间维度和空间维度的信息的,因此计算量相比于1)的联合时空计算大大降低,下图的Factorised Encoder的结构:

从这个结构来看,Factorised Encoder将来自不同patch的tokens分别输入空间transformer编码器中,生成空间分类token, 再将生成的空间分类token(一个patch生成一个空间分类token)输入到时间transformer编码器中,经过MLP,得到最终的分类结果。

这里我认为空间transformer编码器,时间transformer编码器跟transformer编码器的区别就是注意力机制的不同,空间transformer编码器中算的是只有空间变量的自注意力分数,时间transformer编码器算的是只有时间变量的自注意力分数。

3)Factorised Self-Attention Model

该模型与ViT不同之处是采用的注意力机制不同,该模型采用的计算注意力方式类似于TimeSformer的Divided Space-Time Attention,但TimeSformer是先计算时间attention分数,再计算空间attention分数。该模型是先计算空间attention,再计算时间attention。

4)Factorised Dot-Product Attention Model

该模型与ViT不同之处也是采用的注意力机制不同,原ViT模型采用的是多头自注意力机制(MSA),每个头部(heads)都有一组Wq,Wk,Wv来计算q,k,v,qkv生成空间的自注意力分数z,对每个头部(heads)生成的z进行拼接,再线性变换降维生成了多头自注意力分数。

而该模型将MSA进行了改进,其中一半头用来计算仅空间自注意分数。另一半头用来计算仅时间自注意力分数。

主要思想是通过对每一个query修改keys,values,使其只关注来自同一时空的tokens:

4.四个模型的区别

        模型1)是一个编码器内部的计算,计算联合时空的注意力,再编码分类输出;

        模型2)是两个编码器的串联,在不同编码器中计算仅时间、仅空间的注意力,在外部顺序的先执行空间注意力编码分类,再执行时间注意力编码分类输出;

        模型3)是一个编码器内部的串行计算,先计算仅空间注意力,再计算仅时间注意力,再编码分类输出;

        模型4)是一个编码器内部的并行计算,通过不同的头,同时计算仅时间注意力和仅空间注意力,再编码分类输出。

         

### Video Vision Transformer 架构与实现 #### 概述 Vision Transformers (ViTs) 已经被扩展至处理视频数据,形成了 Video Vision Transformers (VidTrans)[^1]。这类模型通过引入时间维度来捕捉动态场景中的时空特征。 #### 基本架构设计 Video Vision Transformer 主要由以下几个部分组成: - **输入层**: 将连续帧图像切分为固定大小的patch序列,并映射到相同维度的空间向量表示。 - **位置编码**: 添加一维的时间嵌入和二维空间嵌入以保留像素间相对距离信息以及各帧之间顺序关系。 - **多头自注意力机制(Multi-head Self-Attention)**: 这种结构允许模型关注不同片段内的局部区域及其跨帧变化模式[^2]。 - **前馈神经网络(Feed Forward Networks, FFNs)** 和 **残差连接(Residual Connections)** : 提升表达能力和加速收敛过程。 - **分类头部(Classification Head)** 或其他特定任务模块:用于最终预测输出如动作识别标签等。 ```python import torch.nn as nn from einops.layers.torch import Rearrange class VidTrans(nn.Module): def __init__(self, num_frames=8, image_size=224, patch_size=16, dim=768, depth=12, heads=12, mlp_dim=3072): super().__init__() self.patch_embedding = nn.Sequential( Rearrange('b t c (h p1) (w p2) -> b t (h w) (p1 p2 c)', p1=patch_size, p2=patch_size), nn.Linear(patch_size * patch_size * 3, dim) ) # Positional embedding for spatial & temporal dimensions self.pos_emb_spatial = nn.Parameter(torch.randn((image_size//patch_size)**2 + 1, dim)) self.pos_emb_temporal = nn.Parameter(torch.randn(num_frames + 1, dim)) transformer_layers = [] for _ in range(depth): layer = nn.TransformerEncoderLayer(d_model=dim, nhead=heads, dim_feedforward=mlp_dim) transformer_layers.append(layer) self.transformer_encoder = nn.TransformerEncoder(transformer_layers[-1], num_layers=len(transformer_layers)) self.class_token = nn.Parameter(torch.zeros(1, 1, dim)) def forward(self, x): B, T, C, H, W = x.shape cls_tokens = self.class_token.expand(B*T, -1, -1).to(x.device) patches = self.patch_embedding(x.view(-1,C,H,W)).reshape(B,T,-1,self.dim) tokens_with_cls = torch.cat([cls_tokens.unsqueeze(dim=1),patches],dim=-2) pos_embedded = tokens_with_cls + self.pos_emb_spatial[:tokens_with_cls.size(-2)] + \ self.pos_emb_temporal[:T].unsqueeze(1).expand_as(tokens_with_cls[:,:,:,0]) out = self.transformer_encoder(pos_embedded.permute(1,0,2)) return out.mean(dim=[0,1]) # Global average pooling over all frames and patches. ``` 此代码展示了如何构建一个简单的 Video Vision Transformer 模型框架,在实际应用中可能还需要考虑更多细节优化,比如更复杂的预处理方式、正则化手段或是针对具体应用场景调整超参数设置等问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值