lerobot/lerobot/scripts/eval.py

137 lines
3.8 KiB
Python
Raw Normal View History

2024-02-25 02:19:18 +08:00
import threading
from pathlib import Path
import hydra
import imageio
import numpy as np
import torch
2024-02-27 19:44:26 +08:00
import tqdm
from tensordict.nn import TensorDictModule
from termcolor import colored
2024-01-31 21:54:32 +08:00
from torchrl.envs import EnvBase
from lerobot.common.envs.factory import make_env
from lerobot.common.policies.factory import make_policy
from lerobot.common.utils import set_seed
2024-02-25 02:19:18 +08:00
def write_video(video_path, stacked_frames, fps):
imageio.mimsave(video_path, stacked_frames, fps=fps)
2024-02-25 02:19:18 +08:00
def eval_policy(
2024-01-31 21:54:32 +08:00
env: EnvBase,
policy: TensorDictModule = None,
num_episodes: int = 10,
max_steps: int = 30,
save_video: bool = False,
video_dir: Path = None,
fps: int = 15,
return_first_video: bool = False,
):
2024-02-22 20:14:12 +08:00
sum_rewards = []
max_rewards = []
successes = []
threads = []
2024-02-27 19:44:26 +08:00
for i in tqdm.tqdm(range(num_episodes)):
tensordict = env.reset()
ep_frames = []
if save_video or (return_first_video and i == 0):
def rendering_callback(env, td=None):
ep_frames.append(env.render())
# render first frame before rollout
rendering_callback(env)
else:
rendering_callback = None
with torch.inference_mode():
rollout = env.rollout(
max_steps=max_steps,
policy=policy,
callback=rendering_callback,
auto_reset=False,
tensordict=tensordict,
auto_cast_to_device=True,
)
# print(", ".join([f"{x:.3f}" for x in rollout["next", "reward"][:,0].tolist()]))
2024-02-22 20:14:12 +08:00
ep_sum_reward = rollout["next", "reward"].sum()
ep_max_reward = rollout["next", "reward"].max()
ep_success = rollout["next", "success"].any()
2024-02-22 20:14:12 +08:00
sum_rewards.append(ep_sum_reward.item())
max_rewards.append(ep_max_reward.item())
successes.append(ep_success.item())
2024-01-31 07:30:14 +08:00
if save_video or (return_first_video and i == 0):
2024-02-22 20:14:12 +08:00
stacked_frames = np.stack(ep_frames)
if save_video:
video_dir.mkdir(parents=True, exist_ok=True)
video_path = video_dir / f"eval_episode_{i}.mp4"
thread = threading.Thread(
target=write_video,
args=(str(video_path), stacked_frames, fps),
)
thread.start()
threads.append(thread)
2024-02-22 20:14:12 +08:00
if return_first_video and i == 0:
first_video = stacked_frames.transpose(0, 3, 1, 2)
for thread in threads:
thread.join()
metrics = {
2024-02-22 20:14:12 +08:00
"avg_sum_reward": np.nanmean(sum_rewards),
"avg_max_reward": np.nanmean(max_rewards),
"pc_success": np.nanmean(successes) * 100,
}
if return_first_video:
return metrics, first_video
return metrics
@hydra.main(version_base=None, config_name="default", config_path="../configs")
2024-02-22 20:14:12 +08:00
def eval_cli(cfg: dict):
eval(cfg, out_dir=hydra.core.hydra_config.HydraConfig.get().runtime.output_dir)
def eval(cfg: dict, out_dir=None):
if out_dir is None:
raise NotImplementedError()
assert torch.cuda.is_available()
torch.backends.cudnn.benchmark = True
set_seed(cfg.seed)
2024-02-22 20:14:12 +08:00
print(colored("Log dir:", "yellow", attrs=["bold"]), out_dir)
env = make_env(cfg)
if cfg.pretrained_model_path:
policy = make_policy(cfg)
policy = TensorDictModule(
policy,
in_keys=["observation", "step_count"],
out_keys=["action"],
)
else:
# when policy is None, rollout a random policy
policy = None
metrics = eval_policy(
2024-01-31 07:30:14 +08:00
env,
2024-01-31 21:54:32 +08:00
policy=policy,
save_video=True,
2024-02-22 20:14:12 +08:00
video_dir=Path(out_dir) / "eval",
2024-02-25 20:02:29 +08:00
fps=cfg.env.fps,
max_steps=cfg.env.episode_length,
2024-02-22 20:14:12 +08:00
num_episodes=cfg.eval_episodes,
2024-01-31 07:30:14 +08:00
)
print(metrics)
if __name__ == "__main__":
2024-02-22 20:14:12 +08:00
eval_cli()