| | |
| | |
| | |
| | |
| |
|
| | """Script to play and evaluate a trained policy from robomimic. |
| | |
| | This script loads a robomimic policy and plays it in an Isaac Lab environment. |
| | |
| | Args: |
| | task: Name of the environment. |
| | checkpoint: Path to the robomimic policy checkpoint. |
| | horizon: If provided, override the step horizon of each rollout. |
| | num_rollouts: If provided, override the number of rollouts. |
| | seed: If provided, overeride the default random seed. |
| | norm_factor_min: If provided, minimum value of the action space normalization factor. |
| | norm_factor_max: If provided, maximum value of the action space normalization factor. |
| | """ |
| |
|
| | """Launch Isaac Sim Simulator first.""" |
| |
|
| |
|
| | import argparse |
| |
|
| | from isaaclab.app import AppLauncher |
| |
|
| | |
| | parser = argparse.ArgumentParser(description="Evaluate robomimic policy for Isaac Lab environment.") |
| | parser.add_argument( |
| | "--disable_fabric", action="store_true", default=False, help="Disable fabric and use USD I/O operations." |
| | ) |
| | parser.add_argument("--task", type=str, default=None, help="Name of the task.") |
| | parser.add_argument("--checkpoint", type=str, default=None, help="Pytorch model checkpoint to load.") |
| | parser.add_argument("--horizon", type=int, default=800, help="Step horizon of each rollout.") |
| | parser.add_argument("--num_rollouts", type=int, default=1, help="Number of rollouts.") |
| | parser.add_argument("--seed", type=int, default=101, help="Random seed.") |
| | parser.add_argument( |
| | "--norm_factor_min", type=float, default=None, help="Optional: minimum value of the normalization factor." |
| | ) |
| | parser.add_argument( |
| | "--norm_factor_max", type=float, default=None, help="Optional: maximum value of the normalization factor." |
| | ) |
| | parser.add_argument("--enable_pinocchio", default=False, action="store_true", help="Enable Pinocchio.") |
| |
|
| |
|
| | |
| | AppLauncher.add_app_launcher_args(parser) |
| | |
| | args_cli = parser.parse_args() |
| |
|
| | if args_cli.enable_pinocchio: |
| | |
| | |
| | |
| | import pinocchio |
| |
|
| | |
| | app_launcher = AppLauncher(args_cli) |
| | simulation_app = app_launcher.app |
| |
|
| | """Rest everything follows.""" |
| |
|
| | import copy |
| | import random |
| |
|
| | import gymnasium as gym |
| | import numpy as np |
| | import robomimic.utils.file_utils as FileUtils |
| | import robomimic.utils.torch_utils as TorchUtils |
| | import torch |
| |
|
| | if args_cli.enable_pinocchio: |
| | import isaaclab_tasks.manager_based.locomanipulation.pick_place |
| | import isaaclab_tasks.manager_based.manipulation.pick_place |
| |
|
| | from isaaclab_tasks.utils import parse_env_cfg |
| |
|
| |
|
| | def rollout(policy, env, success_term, horizon, device): |
| | """Perform a single rollout of the policy in the environment. |
| | |
| | Args: |
| | policy: The robomimicpolicy to play. |
| | env: The environment to play in. |
| | horizon: The step horizon of each rollout. |
| | device: The device to run the policy on. |
| | |
| | Returns: |
| | terminated: Whether the rollout terminated. |
| | traj: The trajectory of the rollout. |
| | """ |
| | policy.start_episode() |
| | obs_dict, _ = env.reset() |
| | traj = dict(actions=[], obs=[], next_obs=[]) |
| |
|
| | for i in range(horizon): |
| | |
| | obs = copy.deepcopy(obs_dict["policy"]) |
| | for ob in obs: |
| | obs[ob] = torch.squeeze(obs[ob]) |
| |
|
| | |
| | if hasattr(env.cfg, "image_obs_list"): |
| | |
| | for image_name in env.cfg.image_obs_list: |
| | if image_name in obs_dict["policy"].keys(): |
| | |
| | image = torch.squeeze(obs_dict["policy"][image_name]) |
| | image = image.permute(2, 0, 1).clone().float() |
| | image = image / 255.0 |
| | image = image.clip(0.0, 1.0) |
| | obs[image_name] = image |
| |
|
| | traj["obs"].append(obs) |
| |
|
| | |
| | actions = policy(obs) |
| |
|
| | |
| | if args_cli.norm_factor_min is not None and args_cli.norm_factor_max is not None: |
| | actions = ( |
| | (actions + 1) * (args_cli.norm_factor_max - args_cli.norm_factor_min) |
| | ) / 2 + args_cli.norm_factor_min |
| |
|
| | actions = torch.from_numpy(actions).to(device=device).view(1, env.action_space.shape[1]) |
| |
|
| | |
| | obs_dict, _, terminated, truncated, _ = env.step(actions) |
| | obs = obs_dict["policy"] |
| |
|
| | |
| | traj["actions"].append(actions.tolist()) |
| | traj["next_obs"].append(obs) |
| |
|
| | |
| | if bool(success_term.func(env, **success_term.params)[0]): |
| | return True, traj |
| | elif terminated or truncated: |
| | return False, traj |
| |
|
| | return False, traj |
| |
|
| |
|
| | def main(): |
| | """Run a trained policy from robomimic with Isaac Lab environment.""" |
| | |
| | env_cfg = parse_env_cfg(args_cli.task, device=args_cli.device, num_envs=1, use_fabric=not args_cli.disable_fabric) |
| |
|
| | |
| | env_cfg.observations.policy.concatenate_terms = False |
| |
|
| | |
| | env_cfg.terminations.time_out = None |
| |
|
| | |
| | env_cfg.recorders = None |
| |
|
| | |
| | success_term = env_cfg.terminations.success |
| | env_cfg.terminations.success = None |
| |
|
| | |
| | env = gym.make(args_cli.task, cfg=env_cfg).unwrapped |
| |
|
| | |
| | torch.manual_seed(args_cli.seed) |
| | np.random.seed(args_cli.seed) |
| | random.seed(args_cli.seed) |
| | env.seed(args_cli.seed) |
| |
|
| | |
| | device = TorchUtils.get_torch_device(try_to_use_cuda=True) |
| |
|
| | |
| | results = [] |
| | for trial in range(args_cli.num_rollouts): |
| | print(f"[INFO] Starting trial {trial}") |
| | policy, _ = FileUtils.policy_from_checkpoint(ckpt_path=args_cli.checkpoint, device=device) |
| | terminated, traj = rollout(policy, env, success_term, args_cli.horizon, device) |
| | results.append(terminated) |
| | print(f"[INFO] Trial {trial}: {terminated}\n") |
| |
|
| | print(f"\nSuccessful trials: {results.count(True)}, out of {len(results)} trials") |
| | print(f"Success rate: {results.count(True) / len(results)}") |
| | print(f"Trial Results: {results}\n") |
| |
|
| | env.close() |
| |
|
| |
|
| | if __name__ == "__main__": |
| | |
| | main() |
| | |
| | simulation_app.close() |
| |
|