在预建USD场景中部署训练好的机器人策略
该教程介绍如何用RSL RL库把Humanoid Rough Terrain任务的训练策略部署到预建USD仓库场景,通过play_mode覆盖训练配置,并用jit导出的检查点进行推理。
## USD环境的政策推论[#](<https://isaac-sim.github.io/IsaacLab/main/source/how-to/policy_inference_in_usd.html#policy-inference-in-usd-environment>) 这个部署例子在预建的USD场景中,使用训练任务的观察,动作和机器人配置来运行一个训练有素的政策. 在这个教程中,我们将使用 RSL RL 库 和经过训练的政策 来自Humanoid Rough Terrain `Isaac-Velocity-Rough-H1` 任务在一个简单的仓库 USD 中。 ## 教学法[#](<https://isaac-sim.github.io/IsaacLab/main/source/how-to/policy_inference_in_usd.html#the-tutorial-code>) 校对:Soup 脚本解析 `H1RoughEnvCfg` 与 `parse_env_cfg`,包括命令行上通过的任何物理预设。 `play_mode` 方法在训练配置的顶部应用游戏/推论超载(如环境减少和观测噪音失效). 为了使用一个预建的USD环境而不是指定的地形生成器,我们先对配置进行以下修改,然后再将其传递到 `ManagerBasedRLEnv`. policy_inference_in_usd .py的代码 ``` 1# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). 2# All rights reserved. 3# 4# SPDX-License-Identifier: BSD-3-Clause 5 6""" 7This script demonstrates policy inference in a prebuilt USD environment. 8 9In this example, we use a locomotion policy to control the H1 robot. The robot was trained 10using Isaac-Velocity-Rough-H1. The robot is commanded to move forward at a constant velocity. 11 12.. code-block:: bash 13 14 # Run the script 15 uv run python scripts/tutorials/03_envs/policy_inference_in_usd.py --checkpoint /path/to/jit/checkpoint.pt 16 17""" 18 19import argparse 20 21from isaaclab.app import add_launcher_args, launch_simulation 22from isaaclab.utils import instantiate 23 24# add argparse arguments 25parser = argparse.ArgumentParser(description="Tutorial on inferencing a policy on an H1 robot in a warehouse.") 26parser.add_argument("--checkpoint", type=str, help="Path to model checkpoint exported as jit.", required=True) 27 28add_launcher_args(parser) 29# parse the arguments, forwarding unrecognized ones as Hydra-style task config overrides 30args_cli, hydra_overrides = parser.parse_known_args() 31 32import os 33 34import torch 35 36from isaaclab.terrains import TerrainImporterCfg 37from isaaclab.utils.assets import ISAAC_NUCLEUS_DIR, read_file 38 39from isaaclab_tasks.utils import parse_env_cfg 40 41 42def main(): 43 """Main function.""" 44 # load the trained jit policy 45 policy_path = os.path.abspath(args_cli.checkpoint) 46 file = read_file(policy_path) 47 policy = torch.jit.load(file, map_location=args_cli.device) 48 49 # setup environment 50 env_cfg = parse_env_cfg("Isaac-Velocity-Rough-H1", device=args_cli.device, num_envs=1, overrides=hydra_overrides) 51 env_cfg.play_mode() 52 env_cfg.curriculum = None 53 env_cfg.scene.terrain = TerrainImporterCfg( 54 prim_path="/World/ground", 55 terrain_type="usd", 56 usd_path=f"{ISAAC_NUCLEUS_DIR}/Environments/Simple_Warehouse/warehouse.usd", 57 ) 58 # The warehouse is enclosed: start the height-scan rays below its roof so they hit the floor. 59 env_cfg.scene.height_scanner.offset.pos = (0.0, 0.0, 2.0) 60 if args_cli.device == "cpu": 61 env_cfg.sim.use_fabric = False 62 63 with launch_simulation(env_cfg, args_cli): 64 # create environment 65 env = instantiate(env_cfg) 66 67 # run inference with the policy 68 obs, _ = env.reset() 69 with torch.inference_mode(): 70 while env.sim.is_running(): 71 action = policy(obs["policy"]) 72 obs, _, _, _, _ = env.step(action) 73 env.close() 74 75 76if __name__ == "__main__": 77 main() ``` 脚本使用 `--device` 用于策略加载和模拟。它仅在Fabric `--device cpu` 明确选择。高度扫描仪从仓库屋顶下方开始,因此其下行射线测量地板。 为训练、导出和推论保留相同的物理预设。下面的命令使用 Newton MJWarp 与 `newton` 瞄准器和不需要Isaac Sim。对于与Isaac Sim一起训练的 PhysX 检查站,请使用 `physics=isaacsim_physx` 整个并安装 Isaac Sim; 牛顿查看器仍然可以使用。 交叉后端政策传输需要额外的验证; 见 [在 PhysX 和牛顿之间传输政策](<https://isaac-sim.github.io/IsaacLab/main/source/how-to/transfer_policies_between_physx_and_newton.html>). ## 代码执行[#](<https://isaac-sim.github.io/IsaacLab/main/source/how-to/policy_inference_in_usd.html#the-code-execution>) 首先,我们需要训练 `Isaac-Velocity-Rough-H1` 运行下列任务: uv(建议) ``` uv run isaaclab train --rl_library rsl_rl --task Isaac-Velocity-Rough-H1 physics=newton_mjwarp ``` 当训练完成后,我们可以用以下命令来视觉效果。要停止模拟,可以关闭窗口,也可以按 `Ctrl+C` 在你开始模拟的终端。 uv(建议) ``` uv run isaaclab play --rl_library rsl_rl --task Isaac-Velocity-Rough-H1 physics=newton_mjwarp --num_envs 64 --checkpoint logs/rsl_rl/h1_rough/EXPERIMENT_NAME/POLICY_FILE.pt --viz newton ``` 运行播放脚本后, 策略会导出到 jit 和 onnx 文件的实验日志目录下。 请注意, 并非所有学习库都支持将策略导出到 jit 或 onnx 文件。 对于目前不支持此功能的图书馆, 请参见相应的 `play.py` 文库的脚本可以学习如何初始化政策. 然后,我们可以装入仓库资产,并使用输出的jit政策对H1机器人进行推论(`policy.pt` 文档中 `exported/` 目录). uv(建议) ``` uv run python scripts/tutorials/03_envs/policy_inference_in_usd.py --checkpoint logs/rsl_rl/h1_rough/EXPERIMENT_NAME/exported/policy.pt physics=newton_mjwarp --viz newton ```  在这个教程中,我们学会了如何对现有的环境配置进行一些小的修改,以便在一个预先建成的用药环境中进行政策推论。
查看原始来源