- 人工智能
- 计算机视觉
- 深度学习
【免费下载链接】mmaction2
OpenMMLab's Next Generation Video Understanding Toolbox and Benchmark
本篇以 MMAction2 仓库中 UniFormerV2 的官方算法文档为主体,完整覆盖其方法动机、模型库结果、配置参数与评测流程,并结合 backbone 源码、分类头实现 与实际训练配置,帮助读者理解“给预训练图像 ViT 加装视频 UniFormer 模块”这一范式的工程实现细节,并能够复现 UniFormerV2-B/16 的训练与 Large 模型的推理评测。
方法动机:UniFormerV2 解决什么问题
UniFormerV2(论文:UniFormerV2: Spatiotemporal Learning by Arming Image ViTs with Video UniFormer,arXiv:2211.09552)针对的核心矛盾是:
- Vision Transformer(ViT)凭借自注意力机制擅长建模长程视频依赖,但由于 token 之间是“盲”的全局比较,难以处理局部视频冗余问题;
- 第一代 UniFormer 通过把卷积和自注意力统一为 Transformer 格式下的“关系聚合器”缓解了该问题,但代价是需要一段繁琐复杂的图像预训练阶段才能用于视频微调,阻碍了实际落地;
- 而开源社区的图像 ViT(如 CLIP 视觉编码器)已经过丰富图像监督充分预训练,触手可及。
基于上述观察,UniFormerV2 提出一种通用范式:直接给预训练图像 ViT 装配高效的 UniFormer 设计。它保留了 UniFormer block 的简洁风格,但包含全新的局部关系聚合器(Local MHRA)与全局关系聚合器(Global Block),在 ViT 与 UniFormer 之间取得更优的精度—计算平衡。官方报告其在不使用任何额外技巧(bells and whistles)的情况下,在 Kinetics-400/600/700、Moments in Time(场景相关)与 Something-Something V1/V2、ActivityNet、HACS(时序相关)等 8 个常用基准上取得当时的最优识别性能,并且是已知首个在 Kinetics-400 上达到 90% top-1 精度量级目标的模型家族(L/14 变体在 32 帧 + 336 分辨率下 top-1 达 89.5%)。
在 MMAction2 中,该模型的 backbone 以 UniFormerV2 类注册于MODELS注册表,配套的UniFormerHead分类头支持从 Kinetics-710 预训练权重迁移到其他 Kinetics 数据集。
架构解析:双分支的局部/全局关系聚合
从源码结构看,UniFormerV2 的整体前向计算(forward)分为:ViT 式 patch embedding → 拼接 class token 与位置编码 → 送入Transformer主干完成局部/全局双分支聚合。
ViT 侧:patch 嵌入与 CLIP 权重装配
UniFormerV2 主干 中:
conv1是 3D 卷积 patch 嵌入:temporal_downsample=False时核为(1, patch_size, patch_size)(即逐帧 patch 化,与图像 ViT 权重完全兼容);temporal_downsample=True时使用(kernel_size, patch_size, patch_size)核并沿时间减半,t_size随之减半;class_embedding与positional_embedding沿用 CLIP 的尺度初始化scale = width ** -0.5,输入 token 数固定为(input_resolution // patch_size)**2 + 1;- MLP 激活使用 CLIP 的
QuickGELU(x * sigmoid(1.702 * x),见 QuickGELU)。
CLIP 视觉编码器权重通过模块内的_MODELS表按名称加载(L18-L28),支持三个规格:
| 配置名 | 对应 ViT |
|---|---|
ViT-B/16 | CLIP ViT-B/16(res224) |
ViT-L/14 | CLIP ViT-L/14(res224) |
ViT-L/14_336 | CLIP ViT-L/14(res336) |
_load_pretrained 的关键在于权重 inflate:当图像权重与 3D 模型形状不一致时,_inflate_weight 把 2D 卷积核沿时间维复制,center=True时只把权重放进中间时间帧(其余帧为零),center=False时则均匀复制到所有帧。这保证了图像 ViT 权重能无损迁移到视频 backbone,最终以strict=False载入。
局部 UniBlock(Local UniBlock)
ResidualAttentionBlock 是局部关系聚合器,其结构为:
- Local MHRA(局部多关系头聚合):Local_MHRA 把 token 从
(1+HW, NT, C)重排为(N, C, T, H, W)的 5D 张量,再经BatchNorm3d → 1×1×1 卷积(降通道,比例为 dw_reduction,默认 1.5)→ 时间方向 depthwise 3D 卷积(pos_kernel_size=3)→ 1×1×1 卷积(恢复通道)。最后一层卷积零初始化,使局部聚合分支初始时不干扰 ViT 主干。no_lmhra=True可整体移除该分支,double_lmhra=True时 MHSA 前后各有一个 Local MHRA; - MHSA:标准多头自注意力,作用于
(1+HW, NT, C)的全部 token; - 可选第二个 Local MHRA;
- FFN:
Linear(d_model → 4*d_model) → QuickGELU → Linear,配DropPath随机深度(由backbone_drop_path_rate线性分配)。
从源码结构看,t_size(即t_size配置,等于采样帧数num_frames)被用于forward(x, T)中把时间维还原,从而保证局部聚合只在“同一空间位置、跨时间帧”的 token 上进行。
全局 UniBlock(Global Block)
Extractor 是全局关系聚合器:一个可学习的temporal_cls_token作为 query,对空间—时间特征(key/value)做交叉注意力,再经过 FFN。主干Transformer中(forward):
return_list(B 模型为[8, 9, 10, 11],L 模型为[20, 21, 22, 23])指定哪些局部 UniBlock 的特征喂给n_layers=4个全局 UniBlock;- 每个全局块前还有一层
dpe(nn.Conv3d,kernel_size=3, groups=n_dim,bias 零初始化)为特征注入时空位置先验; - 全局块与局部主干的权重初始化均为零初始化
out_proj/mlp[-1],保证预训练 ViT 的表征在装配后初期不被破坏。
双分支融合输出
主干输出不是简单取 class token,而是带可学习平衡权重的融合(L387-L390):
weight = self.sigmoid(self.balance) residual = x.view(L, N, T_down, C)[0].mean(1) # 空间-时间 token 的均值 out = self.norm((1 - weight) * cls_token[0, :, :] + weight * residual)即全局 cls token(长程时序建模)与空间—时间残差特征(局部细节)以 sigmoid 权重线性融合,最终输出[N, n_dim]的单 token 表示送入分类头。backbone 单元测试 也验证了这一点:输入(1, 3, 8, 64, 64),B 模型输出形状为(1, 768);16 帧输入同样通过。
模型规格与关键配置参数
UniFormerV2 家族的核心 backbone 参数(源自 类定义):
| 参数 | 含义 | B/16 取值 | L/14 取值 |
|---|---|---|---|
input_resolution | 输入分辨率(短边) | 224(或 336) | 224(或 336) |
patch_size | patch 边长 | 16 | 14 |
width/n_dim | 通道宽度 | 768 | 1024 |
layers | 局部 UniBlock 层数 | 12 | 24 |
heads/n_head | 注意力头数 | 12 | 16 |
t_size | patch 嵌入后的时间维(= 帧数num_frames) | 8 / 16 / 32 | 8 / 16 / 32 |
dw_reduction | Local MHRA 通道压缩比 | 1.5 | 1.5 |
no_lmhra/double_lmhra | 是否移除/双份 Local MHRA | True/True | True/True |
return_list | 送入全局块的局部层索引 | [8, 9, 10, 11] | [20, 21, 22, 23] |
n_layers | 全局 UniBlock 数量 | 4 | 4 |
mlp_factor | 全局块 MLP 隐层倍数 | 4.0 | 4.0 |
mlp_dropout | 各全局块 MLP dropout | [0.5, 0.5, 0.5, 0.5] | [0.5, 0.5, 0.5, 0.5] |
backbone_drop_path_rate/drop_path_rate | 随机深度率 | 0.0 | 0.0 |
temporal_downsample | 时间维下采样 | False | False |
clip_pretrained/pretrained | 是否加载 CLIP 视觉编码器 | True/ViT-B/16 | True/ViT-L/14(_336) |
计算量与参数量(README 模型表口径):B/16 约115M 参数、0.1T FLOPs(8 帧);L/14 约354M 参数,8/16/32 帧分别约 0.7T / 1.3T / 2.7T FLOPs,336 分辨率下约 6.3T FLOPs。
模型库与评测结果
以下结果完整继承自 README 与 metafile。标注\*的模型权重移植自 UniFormerV2 官方仓库并在本仓库数据上复测;由于算力限制,仓库中只提供B/16(base)模型的可靠训练配置,L 系列配置以测试/推理为主。
Kinetics-400
| uniform sampling | resolution | backbone | pretrain | top1 | top5 | 论文参考 top1 | 论文参考 top5 | mm-Kinetics top1 | mm-Kinetics top5 | 测试协议 | FLOPs | params | config |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 8 | short-side 320 | UniFormerV2-B/16 | clip | - | - | 84.3 | 96.4 | 84.4 | 96.3 | 4 clips x 3 crop | 0.1T | 115M | 配置 |
| 8 | short-side 320 | UniFormerV2-B/16 | clip-kinetics710 | - | - | 85.6 | 97.0 | 85.8 | 97.1 | 4 clips x 3 crop | 0.1T | 115M | 配置 |
| 8 | short-side 320 | UniFormerV2-L/14* | clip-kinetics710 | 88.7 | 98.1 | 88.8 | 98.1 | 88.7 | 98.1 | 4 clips x 3 crop | 0.7T | 354M | 配置 |
| 16 | short-side 320 | UniFormerV2-L/14* | clip-kinetics710 | 89.0 | 98.2 | 89.1 | 98.2 | 89.0 | 98.2 | 4 clips x 3 crop | 1.3T | 354M | 配置 |
| 32 | short-side 320 | UniFormerV2-L/14* | clip-kinetics710 | 89.3 | 98.2 | 89.3 | 98.2 | 89.4 | 98.2 | 2 clips x 3 crop | 2.7T | 354M | 配置 |
| 32 | short-side 320 | UniFormerV2-L/14@336* | clip-kinetics710 | 89.5 | 98.4 | 89.7 | 98.3 | 89.5 | 98.4 | 2 clips x 3 crop | 6.3T | 354M | 配置 |
Kinetics-600
| uniform sampling | resolution | backbone | pretrain | top1 | top5 | 论文参考 top1 | 论文参考 top5 | mm-Kinetics top1 | mm-Kinetics top5 | 测试协议 | FLOPs | params | config |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 8 | Raw | UniFormerV2-B/16 | clip-kinetics710 | - | - | 86.1 | 97.2 | 86.4 | 97.3 | 4 clips x 3 crop | 0.1T | 115M | 配置 |
| 8 | Raw | UniFormerV2-L/14* | clip-kinetics710 | 89.0 | 98.3 | 89.0 | 98.2 | 87.5 | 98.0 | 4 clips x 3 crop | 0.7T | 354M | 配置 |
| 16 | Raw | UniFormerV2-L/14* | clip-kinetics710 | 89.4 | 98.3 | 89.4 | 98.3 | 87.8 | 98.0 | 4 clips x 3 crop | 1.3T | 354M | 配置 |
| 32 | Raw | UniFormerV2-L/14* | clip-kinetics710 | 89.2 | 98.3 | 89.5 | 98.3 | 87.7 | 98.1 | 2 clips x 3 crop | 2.7T | 354M | 配置 |
| 32 | Raw | UniFormerV2-L/14@336* | clip-kinetics710 | 89.8 | 98.5 | 89.9 | 98.5 | 88.8 | 98.3 | 2 clips x 3 crop | 6.3T | 354M | 配置 |
Kinetics-700
| uniform sampling | resolution | backbone | pretrain | top1 | top5 | 论文参考 top1 | 论文参考 top5 | mm-Kinetics top1 | mm-Kinetics top5 | 测试协议 | FLOPs | params | config |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 8 | Raw | UniFormerV2-B/16 | clip | - | - | 75.8 | 92.8 | 75.9 | 92.9 | 4 clips x 3 crop | 0.1T | 115M | 配置 |
| 8 | Raw | UniFormerV2-B/16 | clip-kinetics710 | - | - | 76.3 | 92.7 | 76.3 | 92.9 | 4 clips x 3 crop | 0.1T | 115M | 配置 |
| 8 | Raw | UniFormerV2-L/14* | clip-kinetics710 | 80.8 | 95.2 | 80.8 | 95.4 | 79.4 | 94.8 | 4 clips x 3 crop | 0.7T | 354M | 配置 |
| 16 | Raw | UniFormerV2-L/14* | clip-kinetics710 | 81.2 | 95.6 | 81.2 | 95.6 | 79.2 | 95.0 | 4 clips x 3 crop | 1.3T | 354M | 配置 |
| 32 | Raw | UniFormerV2-L/14* | clip-kinetics710 | 81.4 | 95.7 | 81.5 | 95.7 | 79.8 | 95.3 | 2 clips x 3 crop | 2.7T | 354M | 配置 |
| 32 | Raw | UniFormerV2-L/14@336* | clip-kinetics710 | 82.1 | 96.0 | 82.1 | 96.1 | 80.6 | 95.6 | 2 clips x 3 crop | 6.3T | 354M | 配置 |
MiTv1(Moments in Time v1)
| uniform sampling | resolution | backbone | pretrain | top1 | top5 | 论文参考 top1 | 论文参考 top5 | 测试协议 | FLOPs | params | config |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 8 | Raw | UniFormerV2-B/16 | clip-kinetics710-kinetics400 | 42.3 | 71.5 | 42.6 | 71.7 | 4 clips x 3 crop | 0.1T | 115M | 配置 |
| 8 | Raw | UniFormerV2-L/14* | clip-kinetics710-kinetics400 | 47.0 | 76.1 | 47.0 | 76.1 | 4 clips x 3 crop | 0.7T | 354M | 配置 |
| 8 | Raw | UniFormerV2-L/14@336* | clip-kinetics710-kinetics400 | 47.7 | 76.8 | 47.8 | 76.0 | 4 clips x 3 crop | 1.6T | 354M | 配置 |
Kinetics-710(预训练数据集)
| uniform sampling | resolution | backbone | pretrain | top1 | top5 | config |
|---|---|---|---|---|---|---|
| 8 | Raw | UniFormerV2-B/16* | clip | 78.9 | 94.2 | 配置 |
| 8 | Raw | UniFormerV2-L/14* | clip | - | - | 配置 |
| 8 | Raw | UniFormerV2-L/14@336* | clip | - | - | 配置 |
结果口径说明(继承自 README 脚注):
- 表中“论文参考”列是 UniFormerV2 官方仓库的结果;
top1/5 acc是在与原仓库相同的数据列表上测得,label map 由 UniFormerV2 官方提供;mm-Kinetics列是在 MMAction2 自持 Kinetics 数据上的测试结果(与本仓库其他模型同一份数据)。由于各版本 Kinetics 数据存在差异,两列之间有小幅 gap;且由于训练集与作者版本不同,仅报告推理结果,因此数值略低于作者版本;- 由于 Kinetics-400/600/700 原始模型采用了不同的 label 文件,本仓库按 label 名称映射权重;新的 Kinetics-400/600/700 label 文件见 tools/data/kinetics;
- 由于 SlowFast 与 MMAction2 存在实现差异,与其他方法的性能对比有少量 gap;
- Kinetics-710 用于预训练,可高效提升其他数据集上的表现(详见论文);K710 checkpoint 的权重也做了 label 映射,label map 位于 label_map_k710.txt。
各模型的权重与训练日志地址见 README 表格与 metafile.yml 中的Weights/Training Log字段。
配置文件深度解析
Base 训练配置:从 K710 预训练到 K400 微调
以 uniformerv2-base-p16-res224_clip-kinetics710-pre_8xb32-u8_kinetics400-rgb.py 为例,这是仓库中完整的“可训练”配置,关键设计如下:
模型部分:backbone 使用UniFormerV2,init_cfg指定从 K710 预训练 checkpoint 按prefix='backbone.'加载;分类头为UniFormerHead,其init_cfg按prefix='cls_head.'加载同一 checkpoint,并指定channel_map='configs/recognition/uniformerv2/k710_channel_map/map_k400.json'。数据预处理ActionDataPreprocessor使用 mean/std =[114.75]×3 / [57.375]×3,format_shape='NCTHW'。
分类头的通道选择机制:K710 预训练头输出 710 类,微调到 K400 需要 400 类。UniFormerHead 通过_select_channels读取channel_mapJSON(一个索引列表),对预训练fc_cls权重按行索引筛选,只保留 K400 类别对应行。k710_channel_map 目录 下提供map_k400.json、map_k600.json、map_k700.json三套映射;init_weights中若使用Pretrained初始化会断言必须指定 channel map(见 L67-L81)。
数据管线:
- 训练:
DecordInit → UniformSample(clip_len=8, num_clips=1) → DecordDecode → Resize(-1, 256) → PytorchVideoWrapper(RandAugment, magnitude=7, num_layers=4) → RandomResizedCrop → Resize(224) → Flip(0.5) → FormatShape(NCTHW) → PackActionInputs; - 验证:
UniformSample(test_mode=True) → Resize(-1, 224) → CenterCrop(224); - 测试:
UniformSample(num_clips=4, test_mode=True) → Resize(-1, 224) → ThreeCrop(224),对应表中 “4 clips x 3 crop” 的测试协议。
优化与调度:AdamW,base_lr=2e-6,betas=(0.9, 0.999),weight_decay=0.05,paramwise_cfg对 norm 层与 bias 关闭权重衰减(norm_decay_mult=0.0, bias_decay_mult=0.0),梯度裁剪max_norm=20;调度为第 0 个 epoch 内LinearLR预热(start_factor=0.5, convert_to_iter_based=True)+ 第 1–4 个 epochCosineAnnealingLR(T_max=4, eta_min_ratio=0.5);train_cfg共 5 个 epoch,auto_scale_lr以 8 卡 × 32 = 256 为基准自动缩放学习率。该配置与 metafile 中 “Batch Size: 32, Training Resources: 8 GPUs” 的记录一致。
纯 CLIP 预训练配置
uniformerv2-base-p16-res224_clip_8xb32-u8_kinetics400-rgb.py 展示的是不经 K710 预训练、直接用 CLIP 初始化的训练路线:backbone 设置clip_pretrained=True, pretrained='ViT-B/16'(触发上文_load_pretrained的 inflate 逻辑),UniFormerHead不带channel_map,从TruncNormal随机初始化分类头。K700 上同样提供该路线配置,可对比两条路线在模型表中的差距(K400 上 84.4 vs 85.8 top-1)。
Large 模型:测试导向的移植配置
L 系列配置(如 uniformerv2-large-p14-res224_clip-kinetics710-pre_u8_kinetics400-rgb.py)只定义test_pipeline与test_dataloader,没有训练管线与优化器,符合 README “仅提供 base 模型可靠训练配置” 的说明。可以观察到的测试口径差异:
num_frames随配置名中的u8/u16/u32分别为 8/16/32,backbone.t_size与之一致;- 分类头使用
TimeSformerHead(average_clips='prob')做多次裁剪的概率平均; - 测试数据来自
data/k400/val.csv(delimiter=','); - 帧数/分辨率越大,
batch_size越小(u8@224 为 32,u16@224 为 16,u32@336 为 4),这是大模型显存受限下的实用做法。
运行与评测
评测命令(继承自 README 的 Test 一节):
python tools/test.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [optional arguments]以在 Kinetics-400 上评测 UniFormerV2-B/16 并把结果 dump 到 pkl 为例:
python tools/test.py configs/recognition/uniformerv2/uniformerv2-base-p16-res224_clip-kinetics710-pre_u8_kinetics400-rgb.py \ checkpoints/SOME_CHECKPOINT.pth --dump result.pkl更多参数细节可参考 训练与测试教程 的Test部分。数据准备方面,Kinetics 与 MiT 数据集的处理脚本与说明分别位于 tools/data/kinetics/README.md 和 tools/data/mit/README.md。
总结与延伸
UniFormerV2 在 MMAction2 中的实现体现了一条清晰的工程路线:以 CLIP 等现成图像 ViT 为底座,通过权重 inflate + 零初始化装配加上局部(Local MHRA / Local UniBlock)与全局(cross-attention Global Block)两级关系聚合器,用极少的训练成本(base 模型仅 5 个 epoch、2e-6 学习率)即可在多个基准上达到高水平精度。源码入口 mmaction/models/backbones/uniformerv2.py、分类头 mmaction/models/heads/uniformer_head.py、单元测试 tests/models/backbones/test_uniformerv2.py 以及 configs/recognition/uniformerv2 下的全部配置,构成了理解与复现该模型的完整路径。
Citation
@article{Li2022UniFormerV2SL, title={UniFormerV2: Spatiotemporal Learning by Arming Image ViTs with Video UniFormer}, author={Kunchang Li and Yali Wang and Yinan He and Yizhuo Li and Yi Wang and Limin Wang and Y. Qiao}, journal={ArXiv}, year={2022}, volume={abs/2211.09552} }- 人工智能
- 计算机视觉
- 深度学习
【免费下载链接】mmaction2
OpenMMLab's Next Generation Video Understanding Toolbox and Benchmark
相关推荐
MMAction2 中 MViTv2 视频动作识别:骨干网络实现、模型配置与训练推理实战指南
MMAction2 中 MViTv2 视频动作识别:骨干网络实现、模型配置与训练推理实战指南 本文以 MMAction2 中 MViT(Multiscale V
人工智能计算机视觉深度学习MMAction2 中 UniFormer 视频动作识别模型:架构原理、配置详解与测试实战
MMAction2 中 UniFormer 视频动作识别模型:架构原理、配置详解与测试实战 本文以 MMAction2 仓库中 configs/recognit
人工智能计算机视觉深度学习MMAction2 中 SlowOnly 视频识别模型的配置、训练与评测实战详解
MMAction2 中 SlowOnly 视频识别模型的配置、训练与评测实战详解 SlowOnly 是 MMAction2 支持的经典慢路径视频识别架构:它仅使
人工智能计算机视觉深度学习
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考