- 人工智能
- 计算机视觉
- 深度学习
【免费下载链接】ViTPose
The official repo for [NeurIPS'22] "ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation" and [TPAMI'23] "ViTPose++: Vision Transformer for Generic Body Pose Estimation"
导读
本文以 ViTPose 开源仓库中 resnext_coco.md 文档为主体,系统讲解如何在 COCO 数据集上使用ResNeXt(Aggregated Residual Transformations)骨干网络构建 Top-Down 热图式(heatmap-based)2D 人体姿态估计模型。读完本文,你将掌握:六套 ResNeXt 官方配置(50/101/152 三种深度 × 256x192/384x288 两种输入分辨率)的完整参数含义、配置文件中骨干与头部、优化器、学习率、数据管线、推理后处理的逐项解读,以及对应的训练、分布式训练与评测命令。
一、方法背景:ResNeXt 骨干与 COCO 数据集
ResNeXt 由 Xie 等人在 CVPR 2017 提出,论文标题为《Aggregated residual transformations for deep neural networks》。其核心思想是在 ResNet 的残差分支上引入cardinality(基数)维度——把中间 3x3 卷积替换为多个并行的分组卷积,从而在不显著增加参数量的情况下提升表征能力。仓库中对应论文引用记录如下:
@inproceedings{xie2017aggregated, title={Aggregated residual transformations for deep neural networks}, author={Xie, Saining and Girshick, Ross and Doll{\'a}r, Piotr and Tu, Zhuowen and He, Kaiming}, booktitle={Proceedings of the IEEE conference on computer vision and pattern recognition}, pages={1492--1500}, year={2017} }评测使用的数据集为COCO(Common Objects in Context,ECCV 2014),其引用记录为:
@inproceedings{lin2014microsoft, title={Microsoft coco: Common objects in context}, author={Lin, Tsung-Yi and Maire, Michael and Belongie, Serge and Hays, James and Perona, Pietro and Ramanan, Deva and Doll{\'a}r, Piotr and Zitnick, C Lawrence}, booktitle={European conference on computer vision}, pages={740--755}, year={2014}, organization={Springer} }在 ViTPose 仓库中,ResNeXt 是作为人体姿态估计骨干网络家族(ResNet、ResNeXt、ResNest、SEResNet、SCNet 等)的一员被完整集成到 Top-Down 姿态估计框架中的,相关源码见 resnext.py。
二、官方评测结果总览
以下结果来自 resnext_coco.md 与 resnext_coco.yml,评测协议为COCO val2017,使用在 COCO val2017 上人体 AP 为56.4的检测器提供候选框(即COCO_val2017_detections_AP_H_56_person.json,对应tools/misc/keypoints2coco_without_mmdet.py配套的检测结果文件)。指标含义:AP/AP50/AP75 为 COCO 关键点平均精度,AR/AR50 为平均召回率。
| Arch | 输入尺寸 | AP | AP50 | AP75 | AR | AR50 | 配置文件 |
|---|---|---|---|---|---|---|---|
| pose_resnext_50 | 256x192 | 0.714 | 0.898 | 0.789 | 0.771 | 0.937 | resnext50_coco_256x192.py |
| pose_resnext_50 | 384x288 | 0.724 | 0.899 | 0.794 | 0.777 | 0.935 | resnext50_coco_384x288.py |
| pose_resnext_101 | 256x192 | 0.726 | 0.900 | 0.801 | 0.782 | 0.940 | resnext101_coco_256x192.py |
| pose_resnext_101 | 384x288 | 0.743 | 0.903 | 0.815 | 0.795 | 0.939 | resnext101_coco_384x288.py |
| pose_resnext_152 | 256x192 | 0.730 | 0.904 | 0.808 | 0.786 | 0.940 | resnext152_coco_256x192.py |
| pose_resnext_152 | 384x288 | 0.742 | 0.902 | 0.810 | 0.794 | 0.939 | resnext152_coco_384x288.py |
从结果可以观察到的规律:在相同输入分辨率下,骨干越深 AP 越高(256x192 下 50→101→152 的 AP 为 0.714→0.726→0.730);在相同骨干深度下,更高的输入分辨率(384x288)带来约 0.01 AP 的提升,同时伴随着更大的计算开销。每个模型的预训练权重(ckpt)与训练日志(log)下载地址均登记在 resnext_coco.yml 中(Weights字段),可直接按模型名topdown_heatmap_resnext{50,101,152}_coco_{256x192,384x288}检索获取。
三、配置文件逐段解析
六套配置共享完全相同的结构,仅骨干深度、输入分辨率、预训练权重与部分训练超参不同。以下以 resnext50_coco_256x192.py 为基准逐段拆解。
3.1 继承的公共配置
_base_ = [ '../../../../_base_/default_runtime.py', '../../../../_base_/datasets/coco.py' ]default_runtime.py(见 default_runtime.py)定义了运行时公共设置:checkpoint_config = dict(interval=10)(每 10 个 epoch 保存一次 checkpoint)、log_config(每 50 次迭代输出一次日志)、dist_params = dict(backend='nccl')、workflow = [('train', 1)],以及关闭 OpenCV 多线程、使用fork多进程启动方式等环境优化。datasets/coco.py(见 coco.py)定义了 COCO 数据集的dataset_info:17 个关键点(nose、左右眼/耳/肩/肘/腕/髋/膝/踝)、骨架连接关系、joint_weights(肘/腕/膝/踝等肢体末端关键点权重更高,用于加权损失)以及 OKS 评估所需的sigmas。
3.2 评测与优化器
evaluation = dict(interval=10, metric='mAP', save_best='AP') optimizer = dict(type='Adam', lr=5e-4) optimizer_config = dict(grad_clip=None) lr_config = dict( policy='step', warmup='linear', warmup_iters=500, warmup_ratio=0.001, step=[170, 200]) total_epochs = 210- 每 10 个 epoch 在验证集上评测一次,指标为 COCO 的
mAP,并以AP为准则保存最佳模型。 - 优化器为Adam,初始学习率5e-4,不使用梯度裁剪。这与部分仓库中 ResNet 配置一致,是 Top-Down 热图模型的常见训练设置。
- 学习率策略为
step阶梯衰减:先线性 warmup 500 步(初始比例 0.001),在第 170 与 200 个 epoch 各衰减一次(默认因子 0.1),总训练 210 个 epoch。
3.3 模型结构:ResNeXt 骨干 + 简单热图头
model = dict( type='TopDown', pretrained='mmcls://resnext50_32x4d', backbone=dict(type='ResNeXt', depth=50), keypoint_head=dict( type='TopdownHeatmapSimpleHead', in_channels=2048, out_channels=channel_cfg['num_output_channels'], loss_keypoint=dict(type='JointsMSELoss', use_target_weight=True)), train_cfg=dict(), test_cfg=dict( flip_test=True, post_process='default', shift_heatmap=True, modulate_kernel=11))pretrained='mmcls://resnext50_32x4d':加载 mmclassification 提供的 ResNeXt-50 32x4d 在 ImageNet 上的预训练权重。101/152 对应mmcls://resnext101_32x4d、mmcls://resnext152_32x4d。backbone=dict(type='ResNeXt', depth=50):注册在BACKBONES下的 ResNeXt 骨干,默认groups=32, width_per_group=4(即 32x4d 结构),详细源码见 resnext.py。keypoint_head使用TopdownHeatmapSimpleHead,in_channels=2048对应 ResNeXt 最后一个 stage 的输出通道数(各深度配置均相同,因为 ResNeXt 各深度最后一个 stage 输出均为 2048 通道),out_channels=17对应 COCO 的 17 个关键点。头部实现见 topdown_heatmap_simple_head.py。- 损失函数为
JointsMSELoss(关键点 MSE 损失),use_target_weight=True表示按关键点可见性与类型权重加权(对应dataset_info中的joint_weights)。 - 推理后处理:
flip_test=True开启水平翻转测试(测试时对原图与翻转图分别前向并融合);post_process='default'表示采用默认的热图峰值定位与坐标变换流程;shift_heatmap=True在翻转融合时对翻转热图做 1 像素平移补偿(实现于TopdownHeatmapSimpleHead.inference_model,见 topdown_heatmap_simple_head.py);modulate_kernel=11指定了后处理中用于调制峰值定位的核大小。
3.4 数据通道与检测框配置
channel_cfg = dict( num_output_channels=17, dataset_joints=17, dataset_channel=[[0, 1, ..., 16]], inference_channel=[0, 1, ..., 16]) data_cfg = dict( image_size=[192, 256], heatmap_size=[48, 64], num_output_channels=channel_cfg['num_output_channels'], num_joints=channel_cfg['dataset_joints'], dataset_channel=channel_cfg['dataset_channel'], inference_channel=channel_cfg['inference_channel'], soft_nms=False, nms_thr=1.0, oks_thr=0.9, vis_thr=0.2, use_gt_bbox=False, det_bbox_thr=0.0, bbox_file='data/coco/person_detection_results/' 'COCO_val2017_detections_AP_H_56_person.json', )image_size=[192, 256](W, H)为输入图像尺寸,heatmap_size=[48, 64]为网络输出的热图尺寸,两者保持 4 倍下采样关系(384x288 配置对应image_size=[288, 384]、heatmap_size=[72, 96])。- 在 COCO 上训练/测试时 17 个关键点的通道顺序与推理顺序一致,因此
dataset_channel与inference_channel均为[0..16]。 - 后处理阈值:
soft_nms=False(关闭软 NMS)、nms_thr=1.0、oks_thr=0.9、vis_thr=0.2。 - Top-Down 流程依赖外部检测器提供人体框:
use_gt_bbox=False表示评测不使用 GT 框,而是读取bbox_file指定的检测结果(human AP 56.4 的那份检测框 JSON),det_bbox_thr=0.0表示不按置信度过滤检测框。
3.5 训练与验证数据管线
训练管线(train_pipeline)完整流程为:
train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='TopDownRandomFlip', flip_prob=0.5), dict(type='TopDownHalfBodyTransform', num_joints_half_body=8, prob_half_body=0.3), dict(type='TopDownGetRandomScaleRotation', rot_factor=40, scale_factor=0.5), dict(type='TopDownAffine'), dict(type='ToTensor'), dict(type='NormalizeTensor', mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), dict(type='TopDownGenerateTarget', sigma=2), dict(type='Collect', keys=['img', 'target', 'target_weight'], meta_keys=['image_file', 'joints_3d', 'joints_3d_visible', 'center', 'scale', 'rotation', 'bbox_score', 'flip_pairs']), ]- 随机水平翻转(概率 0.5)、半身变换(一半概率只保留上半身 8 个关键点、概率 0.3)、随机尺度/旋转增强(旋转 ±40°、尺度 ±50%)。
- 图像归一化使用 ImageNet 的 mean/std。
- 关键差异点:
TopDownGenerateTarget的sigma在 256x192 配置中为2,在 384x288 配置中为3(对应更高分辨率热图上的高斯核尺寸)。该变换的实现见 top_down_transform.py,默认采用MSRA编码方式,按 3-sigma 规则在热图上放置高斯峰。 - 验证/测试管线(
val_pipeline/test_pipeline)不含任何随机增强,仅包含加载、仿射变换、张量化、归一化与采样,且只收集img。
3.6 数据集与 DataLoader 配置
data = dict( samples_per_gpu=64, workers_per_gpu=2, val_dataloader=dict(samples_per_gpu=32), test_dataloader=dict(samples_per_gpu=32), train=dict(type='TopDownCocoDataset', ann_file=f'{data_root}/annotations/person_keypoints_train2017.json', img_prefix=f'{data_root}/train2017/', data_cfg=data_cfg, pipeline=train_pipeline, dataset_info={{_base_.dataset_info}}), val=dict(type='TopDownCocoDataset', ann_file=f'{data_root}/annotations/person_keypoints_val2017.json', img_prefix=f'{data_root}/val2017/', ...), test=dict(type='TopDownCocoDataset', ann_file=f'{data_root}/annotations/person_keypoints_val2017.json', img_prefix=f'{data_root}/val2017/', ...))- 训练
samples_per_gpu=64,验证/测试 32;数据根目录为data/coco,需要预先按docs/en/data_preparation.md的说明组织目录结构(train2017/、val2017/ 与 annotations/)。 - 六套配置在
samples_per_gpu上有所不同:resnext50 与 resnext101 的 256x192 配置为 64,384x288 配置中 resnext50 为 64、resnext101 为 32,resnext152 的两种配置均为 32——这与更大模型/更高分辨率下的显存占用直接相关。
四、ResNeXt 骨干的源码实现
仓库中的 resnext.py 复用了 ResNet 的框架:ResNeXt类继承自ResNet,通过重写Bottleneck与arch_settings实现分组残差变换:
arch_settings = { 50: (Bottleneck, (3, 4, 6, 3)), 101: (Bottleneck, (3, 4, 23, 3)), 152: (Bottleneck, (3, 8, 36, 3)) }对应 ResNet-50/101/152 的四阶段堆叠结构。在Bottleneck中,中间层通道数不再仅由输出通道决定,而是由groups与width_per_group决定:
self.mid_channels = groups * width_per_group * self.mid_channels // base_channels以默认 32x4d 为例,groups=32, width_per_group=4,瓶颈中间层为 128 通道的分组卷积(每组 4 个通道,32 组),这就是 ResNeXt 相对 ResNet 的核心区别:用"基数"维度换取更强的多分支聚合能力。配置中的backbone=dict(type='ResNeXt', depth=50)即自动采用上述默认分组设置。
五、Top-Down 简单热图头与后处理
TopdownHeatmapSimpleHead(见 topdown_heatmap_simple_head.py)的结构为:3 层转置卷积(deconv,每层 256 个滤波器、4x4 核、stride 2)将 2048 通道的骨干特征逐级上采样,再经一个 1x1 卷积输出 17 通道热图。其前向与推理流程为:
forward:deconv_layers上采样 →final_layer输出[N, 17, H, W]热图;- 开启
flip_test时,调用flip_back(实现见 post_transforms.py)按flip_pairs交换左右关键点通道并水平翻转热图;若配置shift_heatmap=True,还会对翻转热图做 1 像素平移以补偿特征未对齐; - 最终坐标通过
transform_preds结合检测框的center、scale映射回原图(见 post_transforms.py)。
六、训练与评测命令
训练单卡模型:
python tools/train.py configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/resnext50_coco_256x192.py训练脚本 tools/train.py 支持--work-dir(工作目录,默认./work_dirs/<config文件名>)、--resume-from(断点续训)、--no-validate(关闭训练中评测)、--seed、--deterministic、--cfg-options(命令行覆盖配置,例如--cfg-options model.backbone.depth=101)、--launcher等参数。
多卡分布式训练:
bash tools/dist_train.sh configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/resnext50_coco_256x192.py 8tools/dist_train.sh通过torch.distributed.launch启动,第二个参数为 GPU 数量。注意 train.py 中提供了--autoscale-lr:启用后按lr × GPU数 / 8线性缩放学习率。
下载对应 ckpt(可从 resnext_coco.yml 的Weights字段获取地址)后执行评测:
python tools/test.py configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/resnext50_coco_256x192.py <checkpoint.pth> --eval mAPtools/test.py(见 tools/test.py)会自动将cfg.model.pretrained = None并设置测试模式;--eval mAP指定 COCO 关键点评估指标,还支持--out(保存结果)、--fuse-conv-bn(融合 Conv 与 BN 以略微提升推理速度)、--gpu-id等参数。要复现文档中的结果,需要预先准备检测框文件data/coco/person_detection_results/COCO_val2017_detections_AP_H_56_person.json(对应 human AP 56.4 的检测器输出)。
七、小结:如何选择 ResNeXt 配置
- 追求精度:选择
resnext101_coco_384x288.py(AP 0.743,六套配置中最高)或resnext152_coco_384x288.py(AP 0.742),两者差距极小,可结合显存与推理时延取舍。 - 兼顾效率:
resnext50_coco_256x192.py(AP 0.714)计算量最小、samples_per_gpu可达 64,适合快速迭代与轻量部署。 - 中间档位:
resnext101_coco_256x192.py(AP 0.726)在输入分辨率与深度间取得平衡。
在 ViTPose 仓库中,这些 ResNeXt 配置与 ResNet、ResNest、SEResNet、HRNet 等骨干的 COCO 配置保持完全一致的框架接口(Top-Down + 热图 + 简单头),因此可无缝替换骨干、直接复用训练与评测流程,作为模型架构对比实验的可靠基线。
- 人工智能
- 计算机视觉
- 深度学习
【免费下载链接】ViTPose
The official repo for [NeurIPS'22] "ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation" and [TPAMI'23] "ViTPose++: Vision Transformer for Generic Body Pose Estimation"
相关推荐
ViTPose 仓库 ResNet+SimpleBaseline2D 人体姿态估计实战:COCO Top-Down 热图配置、训练与评测深度解析
ViTPose 仓库 ResNet+SimpleBaseline2D 人体姿态估计实战:COCO Top Down 热图配置、训练与评测深度解析 本篇技术指南以
人工智能计算机视觉深度学习ViTPose 仓库中的 AlexNet 姿态估计基线:COCO top-down 配置、源码实现与训练评估全解
ViTPose 仓库中的 AlexNet 姿态估计基线:COCO top down 配置、源码实现与训练评估全解 本指南以 ViTPose 仓库中 alexne
人工智能计算机视觉深度学习REA AI 助手接入完整指南:12 款客户端的接入方式与验证排障
REA AI 助手接入完整指南:12 款客户端的接入方式与验证排障 REA 是一个本地运行的逆向分析服务器,通过 MCP(模型上下文协议,一种让 AI 助手调用
逆向工程MCP 服务AI 技能
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考