1. 环境配置
1.1 创建虚拟环境及安装依赖
# 创建并激活 Conda 虚拟环境
conda create -n aloha python=3.8.10
conda activate aloha
# 安装依赖项
pip install pyquaternion
pip install pyyaml
pip install rospkg
pip install pexpect
pip install mujoco==2.3.7
pip install dm_control==1.0.14
pip install opencv-python
pip install matplotlib
pip install einops
pip install packaging
pip install h5py
pip install ipython
pip install diffusers
pip install torch==2.0.1+cu118 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118
1.2 安装 DETR
# 安装 DETR 模块
cd act/detr
pip install -e .
1.3 安装 GPU 版本 PyTorch
根据 CUDA 版本(如 CUDA 11.8)选择合适的命令,以下为示例:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
pip3 install torch==2.0.1+cu118 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118
1.4 编译安装 egl-probe(不报错可忽略)
如果报错ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (egl_probe)或者subprocess.CalledProcessError: Command 'cmake ..; make -j' returned non-zero exit status 1.
进到egl_probe的setup.py把这个改为这样,
subprocess.check_call("cmake, cwd=build_dir, shell=True)
如果是mac或linux改为
subprocess.check_call("make -j, cwd=build_dir, shell=True)
1.5 安装 robomimic(支持 diffusion_policy 模块)
直接安装 Release 版本可能不兼容,需安装 diffusion-policy-mg 分支:
git clone https://github.com/ARISE-Initiative/robomimic.git -b diffusion-policy-mg
cd robomimic
pip install -v -e .
1.6 配置 util 库(不报错可忽略)
将 act/detr/util 复制到虚拟环境的 site-packages 路径中(如 D:\Anaconda3\envs\aloha\Lib\site-packages)。
2. SIM 仿真环境训练
2.1 数据集构建
-
运行以下命令收集数据集(如生成 50 集数据):
python record_sim_episodes.py --task_name sim_transfer_cube_scripted \
--dataset_dir data/sim_transfer_cube_scripted --num_episodes 50 --onscreen_render
-
可视化仿真数据:
python visualize_episodes.py --dataset_dir data/sim_transfer_cube_scripted --episode_idx 0
-
act-plus-plus/data
├── sim_insertion_human
├── sim_insertion_scripted
├── sim_transfer_cube_human
└── sim_transfer_cube_scripted
2.2 修改配置(这句一定要改)
-
修改数据集路径 (constants.py):
DATA_DIR = 'D:/aloha/data'
-
修改 detr_vae.py 文件:
# 285 行,替换 encoder 实现方式
encoder = build_encoder(args)
2.3 运行训练
运行训练脚本:
python imitate_episodes.py --task_name sim_transfer_cube_scripted \
--ckpt_dir trainings --policy_class ACT --kl_weight 10 --chunk_size 100 \
--hidden_dim 512 --batch_size 8 --dim_feedforward 3200 --num_steps 2000 --lr 1e-5 --seed 0
注意:
- 如果不需要 W&B 可视化结果,选择选项
3。 - 建议根据硬件能力调整
chunk_size 和 batch_size。
2.4 评估模型
添加 --eval 标志运行测试:
python imitate_episodes.py --eval --task_name sim_transfer_cube_scripted \
--ckpt_dir trainings/test1 --policy_class ACT --kl_weight 10 --chunk_size 100 \
--hidden_dim 512 --batch_size 8 --dim_feedforward 3200 --lr 1e-5 --seed 0 --num_steps 100 --onscreen_render
2.5 导出评估视频
添加视频保存逻辑:
frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # 转换图像格式
out.write(frame) # 保存到视频流
out.release() # 释放资源
3. 额外说明
- 调优提示:
- 训练时长:对于复杂数据集,建议至少 5000 步或 3-4 倍时间。
- 成功率:可以通过延长训练时间提升模型的稳定性与准确率。
- 使用 W&B 可视化(可选):