This project implements imitation learning techniques (GAIL and AIRL) for the Unitree G1 quadruped robot, based on the PPO algorithm. The implementation addresses numerical stability issues in complex robotic control tasks.
You can set up the environment using the following steps:
-
Create a Python 3.10 virtual environment:
python3.10 -m venv mujoco-env-py310-compat source mujoco-env-py310-compat/bin/activate -
Install required packages:
pip install numpy==1.24.3 torch gymnasium tqdm tensorboard mujoco
-
Install MuJoCo:
mkdir -p ~/.mujoco curl -OL https://github.com/deepmind/mujoco/releases/download/2.1.0/mujoco210-macos-x86_64.tar.gz tar -xf mujoco210-macos-x86_64.tar.gz -C ~/.mujoco/ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/.mujoco/mujoco210/bin export MUJOCO_PY_MUJOCO_PATH=~/.mujoco/mujoco210/ pip install "gymnasium[mujoco]"
First, we train an expert policy using Soft Actor-Critic (SAC):
python train_expert.py --env_id G1-v0 --num_steps 1000000 --eval_interval 10000 --seed 0This will train an expert policy and save checkpoints in the logs/G1-v0/sac/seed0-TIMESTAMP/ directory.
Next, collect expert demonstrations using the trained policy:
python collect_demo.py \
--env_id G1-v0 \
--weight logs/G1-v0/sac/seed0-TIMESTAMP/model/step1000000/actor.pth \
--buffer_size 50000 --std 0.01 --p_rand 0.0 --seed 0Replace TIMESTAMP with the actual timestamp from your training run. This will create a buffer file in the buffers/G1-v0/ directory.
For standard GAIL training:
python train_imitation.py \
--algo gail --env_id G1-v0 \
--buffer buffers/G1-v0/size50000_std0.01_prand0.0.pth \
--num_steps 1000000 --eval_interval 5000 --rollout_length 2048 --seed 0For training with numerical stability improvements:
python train_imitation_stable.py \
--algo gail --env_id G1-v0 \
--buffer buffers/G1-v0/size50000_std0.01_prand0.0.pth \
--num_steps 1000000 --eval_interval 5000 --rollout_length 2048 \
--lr 5e-5 --max_grad_norm 0.5 --entropy_coef 0.01 --seed 0The stable training script includes:
- NaN/Inf detection and handling
- Reduced learning rates
- More aggressive gradient clipping
- Better reward scaling
- Environment stability improvements
You can also try AIRL by changing --algo gail to --algo airl.
Evaluate the trained policy:
python evaluate_policy.py \
--env_id G1-v0 \
--weight logs/G1-v0/gail/seed0-TIMESTAMP/model/step1000000/actor.pth \
--episodes 10python visualize_expert.py \
--env_id G1-v0 \
--weight logs/G1-v0/sac/seed0-TIMESTAMP/model/step1000000/actor.pthpython visualize_policy.py \
--env_id G1-v0 \
--algo gail \
--weight logs/G1-v0/gail/seed0-TIMESTAMP/model/step1000000/actor.pthtensorboard --logdir logsIf you encounter numerical stability issues during training (NaN/Inf values):
- Use the stable training script (
train_imitation_stable.py) - Decrease the learning rate with
--lr 1e-5 - Reduce gradient clipping with
--max_grad_norm 0.5 - Increase entropy coefficient with
--entropy_coef 0.02 - Use smaller batch sizes with
--batch_size 32
g1_env.py: Custom G1 robot environment with stability improvementsmake_buffer.py: Create expert demonstration bufferstrain_expert.py: Train expert policy using SACcollect_demo.py: Collect demonstrations from trained policytrain_imitation.py: Original imitation learning training scripttrain_imitation_stable.py: Enhanced training script with stability featuresvisualize_policy.py: Visualize trained policyvisualize_expert.py: Visualize expert demonstrationsevaluate_policy.py: Evaluate policy performancegail_airl_ppo/: Core algorithm implementationsalgo/ppo.py: PPO algorithmalgo/gail.py: GAIL algorithmalgo/airl.py: AIRL algorithmalgo/sac.py: SAC algorithmbuffer.py: Replay and rollout buffersnetwork.py: Neural network architecturestrainer.py: Training utilities
- Ho, J., & Ermon, S. (2016). Generative Adversarial Imitation Learning. NIPS 2016.
- Fu, J., Luo, K., & Levine, S. (2017). Learning Robust Rewards with Adversarial Inverse Reinforcement Learning. arXiv:1710.11248.
- Schulman, J., Wolski, F., Dhariwal, P., Radford, A., & Klimov, O. (2017). Proximal Policy Optimization Algorithms. arXiv:1707.06347.
- Haarnoja, T., Zhou, A., Abbeel, P., & Levine, S. (2018). Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor. ICML 2018.