ACT (Action Chunking Transformer) 模型微调
概述
ACT(Action Chunking Transformer)是一种专为精细操作任务设计的端到端模仿学习模型。该模型通过预测动作块(action chunks)来克服传统模仿学习中的复合误差问题,在低成本硬件上实现了高成功率的机器人操作。
核心特点
- 动作分块预测:一次预测多个连续动作,减少复合误差
- Transformer 架构:利用注意力机制处理序列数据
- 端到端训练:从原始观察直接预测动作
- 高成功率:在精细操作任务上表现优异
- 硬件友好:可在消费级硬件上运行
先决条件
系统要求
- 操作系统:Linux(推荐 Ubuntu 20.04+)或 macOS
- Python 版本:3.8+
- GPU:NVIDIA GPU(推荐 RTX 3070 或更高),至少 6GB 显存
- 内存:至少 16GB RAM
- 存储空间:至少 30GB 可用空间
环境准备
1. 安装 LeRobot
# 克隆 LeRobot 仓库
git clone https://github.com/huggingface/lerobot.git
cd lerobot
# 创建虚拟环境
conda create -n lerobot python=3.10
conda activate lerobot
# 安装依赖
pip install -e .
2. 安装 ACT 特定依赖
# 安装额外的依赖包
pip install einops
pip install timm
pip install wandb
# 登录 Weights & Biases(可选)
wandb login
ACT 模型架构
核心组件
- 视觉编码器:处理多视角图像输入
- 状态编码器:处理机器人状态信息
- Transformer 解码器:生成动作序列
- 动作头:输出最终的动作预测
关键参数
- Chunk Size:每次预测的动作数量(通常 50-100)
- Context Length:历史观察的长度
- Hidden Dimension:Transformer 的隐藏维度
- Number of Heads:注意力头的数量
- Number of Layers:Transformer 层数
数据 准备
LeRobot 格式数据
ACT 需要使用 LeRobot 格式的数据集,包含以下结构:
your_dataset/
├── data/
│ ├── chunk-001/
│ │ ├── observation.images.cam_high.png
│ │ ├── observation.images.cam_low.png
│ │ ├── observation.images.cam_left_wrist.png
│ │ ├── observation.images.cam_right_wrist.png
│ │ ├── observation.state.npy
│ │ ├── action.npy
│ │ └── ...
│ └── chunk-002/
│ └── ...
├── meta.json
├── stats.safetensors
└── videos/
├── episode_000000.mp4
└── ...
数据质量要求
- 最少 50 个 episode 用于基本训练
- 推荐 200+ episode 以获得最佳效果
- 每个 episode 应包含完整的任务执行
- 多视角图像(至少 2 个摄像头)
- 高质量的动作标注
微调训练
基本训练命令
# 设置环境变量
export HF_USER="your-huggingface-username"
export CUDA_VISIBLE_DEVICES=0
# 启动 ACT 训练
lerobot-train \
--policy.path=lerobot/act \
--dataset.repo_id=${HF_USER}/your_dataset \
--batch_size=8 \
--steps=50000 \
--output_dir=outputs/train/act_finetuned \
--job_name=act_finetuning \
--policy.device=cuda \
--policy.chunk_size=50 \
--policy.n_obs_steps=1 \
--policy.camera_names="[cam_high,cam_low,cam_left_wrist,cam_right_wrist]" \
--training.learning_rate=1e-5 \
--training.weight_decay=1e-4 \
--wandb.enable=true
高级训练配置
多摄像头配置
# 针对多摄像头设置的 ACT 训练
lerobot-train \
--policy.path=lerobot/act \
--dataset.repo_id=${HF_USER}/your_dataset \
--batch_size=4 \
--steps=100000 \
--output_dir=outputs/train/act_multicam \
--job_name=act_multicam_training \
--policy.device=cuda \
--policy.chunk_size=100 \
--policy.n_obs_steps=2 \
--policy.camera_names="[cam_high,cam_low,cam_left_wrist,cam_right_wrist]" \
--policy.vision_backbone=resnet18 \
--policy.hidden_dim=512 \
--policy.dim_feedforward=3200 \
--policy.n_layer=8 \
--policy.n_head=8 \
--training.learning_rate=1e-5 \
--training.weight_decay=1e-4 \
--training.lr_scheduler=cosine \
--wandb.enable=true
内存优化配置
# 针对显存较小的 GPU
lerobot-train \
--policy.path=lerobot/act \
--dataset.repo_id=io-ai-data/lerobot_data \
--batch_size=2 \
--steps=75000 \
--output_dir=outputs/train/act_memory_opt \
--job_name=act_memory_optimized \
--policy.device=cuda \
--policy.chunk_size=50 \
--policy.camera_names="[cam_high,cam_low]" \
--policy.vision_backbone=resnet18 \
--policy.hidden_dim=256 \
--training.learning_rate=1e-5 \
--training.gradient_accumulation_steps=4 \
--training.mixed_precision=fp16 \
--wandb.enable=true
参数详细说明
核心参数
参数 | 含义 | 推荐值 | 说明 |
---|---|---|---|
--policy.path | 预训练模型路径 | lerobot/act | LeRobot 官方 ACT 模型 |
--dataset.repo_id | 数据集仓库 ID | ${HF_USER}/dataset | 你的 HuggingFace 数据集 |
--batch_size | 批处理大小 | 8 | 根据显存调整,RTX 3070 推荐 4-8 |
--steps | 训练步数 | 50000 | 精细任务推荐 50000-100000 步 |
--output_dir | 输出目录 | outputs/train/act_finetuned | 模型保存路径 |
--job_name | 任务名称 | act_finetuning | 用于日志和实验跟踪 |