151 lines
6.0 KiB
Python
151 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
# Copyright (c) 2022-2025, Fan Yang and Per Frivik, ETH Zurich.
|
||
|
|
# All rights reserved.
|
||
|
|
#
|
||
|
|
# SPDX-License-Identifier: MIT
|
||
|
|
|
||
|
|
"""Train a navigation policy using RSL-RL (PPO/MDPO algorithms).
|
||
|
|
|
||
|
|
Usage:
|
||
|
|
python scripts/train.py --task <task_name> --num_envs <num> [options]
|
||
|
|
|
||
|
|
Arguments:
|
||
|
|
--task Task name (required)
|
||
|
|
--num_envs Number of parallel environments
|
||
|
|
--seed Random seed
|
||
|
|
--max_iterations Training iterations
|
||
|
|
--run_name Custom run name for logging
|
||
|
|
--video Enable video recording
|
||
|
|
--video_length Video length in steps (default: 200)
|
||
|
|
--video_interval Recording interval in steps (default: 2000)
|
||
|
|
|
||
|
|
Examples:
|
||
|
|
python scripts/train.py --task Isaac-Navigation-B2W-v0 --num_envs 2048
|
||
|
|
python scripts/train.py --task Isaac-Navigation-B2W-v0 --video --seed 42
|
||
|
|
|
||
|
|
Logs saved to: logs/rsl_rl/<experiment_name>/<timestamp>/
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import sys
|
||
|
|
|
||
|
|
# Add the parent directory to the path so we can import from the extension
|
||
|
|
from isaaclab.app import AppLauncher
|
||
|
|
|
||
|
|
# Add argparse arguments
|
||
|
|
parser = argparse.ArgumentParser(description="Train a navigation policy with RSL-RL.")
|
||
|
|
parser.add_argument("--video", action="store_true", default=False, help="Record videos during training.")
|
||
|
|
parser.add_argument("--video_length", type=int, default=200, help="Length of the recorded video (in steps).")
|
||
|
|
parser.add_argument("--video_interval", type=int, default=2000, help="Interval between video recordings (in steps).")
|
||
|
|
parser.add_argument("--num_envs", type=int, default=None, help="Number of environments to simulate.")
|
||
|
|
parser.add_argument("--task", type=str, default=None, help="Name of the task.")
|
||
|
|
parser.add_argument("--seed", type=int, default=None, help="Seed used for the environment")
|
||
|
|
parser.add_argument("--max_iterations", type=int, default=None, help="RL Policy training iterations.")
|
||
|
|
parser.add_argument("--run_name", type=str, default=None, help="Name of the wandb run (appended to log directory).")
|
||
|
|
parser.add_argument("--checkpoint", type=str, default=None, help="Path to a checkpoint (.pt) to warm-start from before training begins.")
|
||
|
|
parser.add_argument("--load_optimizer", action="store_true", default=False, help="Also load the optimizer state from the checkpoint (default: only load model weights, recommended for cross-robot warm-start).")
|
||
|
|
|
||
|
|
# Append AppLauncher cli args
|
||
|
|
AppLauncher.add_app_launcher_args(parser)
|
||
|
|
args_cli, hydra_args = parser.parse_known_args()
|
||
|
|
|
||
|
|
# always enable cameras to record video
|
||
|
|
if args_cli.video:
|
||
|
|
args_cli.enable_cameras = True
|
||
|
|
|
||
|
|
# Launch simulation
|
||
|
|
app_launcher = AppLauncher(args_cli)
|
||
|
|
simulation_app = app_launcher.app
|
||
|
|
|
||
|
|
# Import after launching simulation
|
||
|
|
import gymnasium as gym
|
||
|
|
import os
|
||
|
|
import torch
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
from rsl_rl.runners import OnPolicyRunner
|
||
|
|
|
||
|
|
# Import Isaac Lab extensions
|
||
|
|
import isaaclab_tasks # noqa: F401
|
||
|
|
import isaaclab_nav_task # noqa: F401
|
||
|
|
|
||
|
|
from isaaclab.envs import ManagerBasedRLEnvCfg
|
||
|
|
from isaaclab.utils.dict import print_dict
|
||
|
|
from isaaclab.utils.io import dump_pickle, dump_yaml
|
||
|
|
from isaaclab_tasks.utils import get_checkpoint_path
|
||
|
|
from isaaclab_tasks.utils.parse_cfg import load_cfg_from_registry
|
||
|
|
from isaaclab_rl.rsl_rl import RslRlOnPolicyRunnerCfg, RslRlVecEnvWrapper
|
||
|
|
|
||
|
|
# Set torch backends for better performance
|
||
|
|
torch.backends.cuda.matmul.allow_tf32 = True
|
||
|
|
torch.backends.cudnn.allow_tf32 = True
|
||
|
|
torch.backends.cudnn.deterministic = False
|
||
|
|
torch.backends.cudnn.benchmark = False
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
"""Train navigation policy with RSL-RL."""
|
||
|
|
# Load the configurations from the registry
|
||
|
|
env_cfg = load_cfg_from_registry(args_cli.task, "env_cfg_entry_point")
|
||
|
|
agent_cfg: RslRlOnPolicyRunnerCfg = load_cfg_from_registry(args_cli.task, "rsl_rl_cfg_entry_point")
|
||
|
|
|
||
|
|
# Override config from command line
|
||
|
|
if args_cli.num_envs is not None:
|
||
|
|
env_cfg.scene.num_envs = args_cli.num_envs
|
||
|
|
if args_cli.seed is not None:
|
||
|
|
agent_cfg.seed = args_cli.seed
|
||
|
|
if args_cli.max_iterations is not None:
|
||
|
|
agent_cfg.max_iterations = args_cli.max_iterations
|
||
|
|
if args_cli.run_name is not None:
|
||
|
|
agent_cfg.run_name = args_cli.run_name
|
||
|
|
|
||
|
|
# Create the environment
|
||
|
|
env = gym.make(args_cli.task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None)
|
||
|
|
# Wrap the environment
|
||
|
|
env = RslRlVecEnvWrapper(env)
|
||
|
|
|
||
|
|
# Specify log directory
|
||
|
|
log_root_path = os.path.join("logs", "rsl_rl", agent_cfg.experiment_name)
|
||
|
|
log_root_path = os.path.abspath(log_root_path)
|
||
|
|
print(f"[INFO] Logging experiment in directory: {log_root_path}")
|
||
|
|
# Specify run directory based on timestamp
|
||
|
|
log_dir = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||
|
|
if agent_cfg.run_name:
|
||
|
|
log_dir += f"_{agent_cfg.run_name}"
|
||
|
|
log_dir = os.path.join(log_root_path, log_dir)
|
||
|
|
|
||
|
|
# Create runner
|
||
|
|
runner = OnPolicyRunner(env, agent_cfg.to_dict(), log_dir=log_dir, device=agent_cfg.device)
|
||
|
|
|
||
|
|
# Optional: warm-start from an existing checkpoint (e.g. cross-robot fine-tuning).
|
||
|
|
if args_cli.checkpoint is not None:
|
||
|
|
ckpt_path = os.path.abspath(args_cli.checkpoint)
|
||
|
|
if not os.path.isfile(ckpt_path):
|
||
|
|
raise FileNotFoundError(f"Checkpoint not found: {ckpt_path}")
|
||
|
|
print(f"[INFO] Warm-starting from checkpoint: {ckpt_path}")
|
||
|
|
print(f"[INFO] load_optimizer = {args_cli.load_optimizer}")
|
||
|
|
runner.load(ckpt_path, load_optimizer=args_cli.load_optimizer)
|
||
|
|
|
||
|
|
# Write git state to log
|
||
|
|
runner.add_git_repo_to_log(__file__)
|
||
|
|
# Save configuration
|
||
|
|
dump_yaml(os.path.join(log_dir, "params", "env.yaml"), env_cfg)
|
||
|
|
dump_yaml(os.path.join(log_dir, "params", "agent.yaml"), agent_cfg)
|
||
|
|
dump_pickle(os.path.join(log_dir, "params", "env.pkl"), env_cfg)
|
||
|
|
dump_pickle(os.path.join(log_dir, "params", "agent.pkl"), agent_cfg)
|
||
|
|
|
||
|
|
# Run training
|
||
|
|
runner.learn(num_learning_iterations=agent_cfg.max_iterations, init_at_random_ep_len=True)
|
||
|
|
|
||
|
|
# Close the environment
|
||
|
|
env.close()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
# Run the main function
|
||
|
|
main()
|
||
|
|
# Close simulation
|
||
|
|
simulation_app.close()
|