Files
lerobot-clone/lerobot/scripts/eval.py

282 lines
10 KiB
Python
Raw Normal View History

2024-03-22 10:26:55 +00:00
"""Evaluate a policy on an environment by running rollouts and computing metrics.
The script may be run in one of two ways:
1. By providing the path to a config file with the --config argument.
2. By providing a HuggingFace Hub ID with the --hub-id argument. You may also provide a revision number with the
--revision argument.
In either case, it is possible to override config arguments by adding a list of config.key=value arguments.
Examples:
You have a specific config file to go with trained model weights, and want to run 10 episodes.
```
python lerobot/scripts/eval.py \
--config PATH/TO/FOLDER/config.yaml \
policy.pretrained_model_path=PATH/TO/FOLDER/weights.pth \
eval_episodes=10
2024-03-22 10:26:55 +00:00
```
You have a HuggingFace Hub ID, you know which revision you want, and want to run 10 episodes (note that in this case,
you don't need to specify which weights to use):
```
python lerobot/scripts/eval.py --hub-id HUB/ID --revision v1.0 eval_episodes=10
```
"""
import argparse
2024-03-22 15:06:57 +00:00
import json
import logging
2024-03-22 10:26:55 +00:00
import os.path as osp
2024-02-24 18:19:18 +00:00
import threading
2024-02-29 23:13:06 +00:00
import time
2024-03-22 10:26:55 +00:00
from datetime import datetime as dt
from pathlib import Path
2024-03-20 09:45:45 +00:00
import einops
import hydra
import imageio
import numpy as np
import torch
2024-02-27 11:44:26 +00:00
import tqdm
2024-03-22 10:26:55 +00:00
from huggingface_hub import snapshot_download
from tensordict.nn import TensorDictModule
2024-03-19 16:02:09 +00:00
from torchrl.envs import EnvBase
from torchrl.envs.batched_envs import BatchedEnvBase
2024-03-02 15:58:21 +00:00
from lerobot.common.datasets.factory import make_offline_buffer
from lerobot.common.envs.factory import make_env
from lerobot.common.logger import log_output_dir
from lerobot.common.policies.abstract import AbstractPolicy
from lerobot.common.policies.factory import make_policy
2024-03-25 17:19:28 +01:00
from lerobot.common.utils import get_safe_torch_device, init_logging, set_global_seed
2024-02-24 18:19:18 +00:00
def write_video(video_path, stacked_frames, fps):
imageio.mimsave(video_path, stacked_frames, fps=fps)
2024-02-24 18:19:18 +00:00
def eval_policy(
env: BatchedEnvBase,
policy: AbstractPolicy,
2024-01-31 13:54:32 +00:00
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-03-20 18:38:55 +01:00
if policy is not None:
policy.eval()
2024-02-29 23:13:06 +00:00
start = time.time()
2024-02-22 12:14:12 +00:00
sum_rewards = []
max_rewards = []
successes = []
2024-03-22 15:06:57 +00:00
seeds = []
threads = [] # for video saving threads
episode_counter = 0 # for saving the correct number of videos
# TODO(alexander-soare): if num_episodes is not evenly divisible by the batch size, this will do more work than
# needed as I'm currently taking a ceil.
for i in tqdm.tqdm(range(-(-num_episodes // env.batch_size[0]))):
ep_frames = []
def maybe_render_frame(env: EnvBase, _):
if save_video or (return_first_video and i == 0): # noqa: B023
2024-02-29 13:37:48 +01:00
ep_frames.append(env.render()) # noqa: B023
2024-03-22 15:06:57 +00:00
# Clear the policy's action queue before the start of a new rollout.
if policy is not None:
policy.clear_action_queue()
2024-03-22 15:32:55 +00:00
if env.is_closed:
env.start() # needed to be able to get the seeds the first time as BatchedEnvs are lazy
2024-03-22 15:06:57 +00:00
seeds.extend(env._next_seed)
with torch.inference_mode():
# TODO(alexander-soare): When `break_when_any_done == False` this rolls out for max_steps even when all
2024-03-19 18:50:04 +00:00
# envs are done the first time. But we only use the first rollout. This is a waste of compute.
rollout = env.rollout(
max_steps=max_steps,
policy=policy,
auto_cast_to_device=True,
callback=maybe_render_frame,
break_when_any_done=env.batch_size[0] == 1,
)
2024-03-22 15:06:57 +00:00
# Figure out where in each rollout sequence the first done condition was encountered (results after
# this won't be included).
2024-03-19 18:50:04 +00:00
# Note: this assumes that the shape of the done key is (batch_size, max_steps, 1).
# Note: this relies on a property of argmax: that it returns the first occurrence as a tiebreaker.
rollout_steps = rollout["next", "done"].shape[1]
done_indices = torch.argmax(rollout["next", "done"].to(int), axis=1) # (batch_size, rollout_steps)
mask = (torch.arange(rollout_steps) <= done_indices).unsqueeze(-1) # (batch_size, rollout_steps, 1)
2024-03-20 09:45:45 +00:00
batch_sum_reward = einops.reduce((rollout["next", "reward"] * mask), "b n 1 -> b", "sum")
batch_max_reward = einops.reduce((rollout["next", "reward"] * mask), "b n 1 -> b", "max")
batch_success = einops.reduce((rollout["next", "success"] * mask), "b n 1 -> b", "any")
sum_rewards.extend(batch_sum_reward.tolist())
max_rewards.extend(batch_max_reward.tolist())
successes.extend(batch_success.tolist())
2024-01-30 23:30:14 +00:00
if save_video or (return_first_video and i == 0):
batch_stacked_frames = np.stack(ep_frames) # (t, b, *)
batch_stacked_frames = batch_stacked_frames.transpose(
1, 0, *range(2, batch_stacked_frames.ndim)
) # (b, t, *)
2024-02-22 12:14:12 +00:00
if save_video:
for stacked_frames, done_index in zip(
batch_stacked_frames, done_indices.flatten().tolist(), strict=False
):
if episode_counter >= num_episodes:
continue
video_dir.mkdir(parents=True, exist_ok=True)
video_path = video_dir / f"eval_episode_{episode_counter}.mp4"
thread = threading.Thread(
target=write_video,
args=(str(video_path), stacked_frames[:done_index], fps),
)
thread.start()
threads.append(thread)
episode_counter += 1
2024-02-22 12:14:12 +00:00
if return_first_video and i == 0:
first_video = batch_stacked_frames[0].transpose(0, 3, 1, 2)
for thread in threads:
thread.join()
2024-02-29 23:13:06 +00:00
info = {
2024-03-22 15:43:45 +00:00
"per_episode": [
2024-03-22 15:06:57 +00:00
{
"episode_ix": i,
"sum_reward": sum_reward,
"max_reward": max_reward,
"success": success,
"seed": seed,
}
for i, (sum_reward, max_reward, success, seed) in enumerate(
zip(
sum_rewards[:num_episodes],
max_rewards[:num_episodes],
successes[:num_episodes],
seeds[:num_episodes],
strict=True,
)
)
],
2024-03-22 15:43:45 +00:00
"aggregated": {
2024-03-22 15:06:57 +00:00
"avg_sum_reward": np.nanmean(sum_rewards[:num_episodes]),
"avg_max_reward": np.nanmean(max_rewards[:num_episodes]),
"pc_success": np.nanmean(successes[:num_episodes]) * 100,
"eval_s": time.time() - start,
"eval_ep_s": (time.time() - start) / num_episodes,
},
}
if return_first_video:
2024-02-29 23:13:06 +00:00
return info, first_video
return info
2024-03-22 10:26:55 +00:00
def eval(cfg: dict, out_dir=None, stats_path=None):
2024-02-22 12:14:12 +00:00
if out_dir is None:
raise NotImplementedError()
init_logging()
2024-03-20 18:38:55 +01:00
# Check device is available
get_safe_torch_device(cfg.device, log=True)
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
2024-03-25 17:19:28 +01:00
set_global_seed(cfg.seed)
log_output_dir(out_dir)
2024-03-22 10:26:55 +00:00
logging.info("Making transforms.")
offline_buffer = make_offline_buffer(cfg, stats_path=stats_path)
2024-03-22 10:26:55 +00:00
logging.info("Making environment.")
2024-03-19 16:02:09 +00:00
env = make_env(cfg, transform=offline_buffer.transform)
2024-02-28 12:35:49 +01:00
if cfg.policy.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
2024-03-22 15:06:57 +00:00
info = eval_policy(
2024-01-30 23:30:14 +00:00
env,
2024-01-31 13:54:32 +00:00
policy=policy,
save_video=True,
2024-02-22 12:14:12 +00:00
video_dir=Path(out_dir) / "eval",
2024-02-25 12:02:29 +00:00
fps=cfg.env.fps,
max_steps=cfg.env.episode_length,
2024-02-22 12:14:12 +00:00
num_episodes=cfg.eval_episodes,
2024-01-30 23:30:14 +00:00
)
2024-03-22 15:43:45 +00:00
print(info["aggregated"])
2024-03-22 15:06:57 +00:00
# Save info
with open(Path(out_dir) / "eval_info.json", "w") as f:
json.dump(info, f, indent=2)
logging.info("End of eval")
2024-03-22 10:26:55 +00:00
def _relative_path_between(path1: Path, path2: Path) -> Path:
"""Returns path1 relative to path2."""
path1 = path1.absolute()
path2 = path2.absolute()
try:
return path1.relative_to(path2)
except ValueError: # most likely because path1 is not a subpath of path2
common_parts = Path(osp.commonpath([path1, path2])).parts
return Path(
"/".join([".."] * (len(path2.parts) - len(common_parts)) + list(path1.parts[len(common_parts) :]))
)
if __name__ == "__main__":
2024-03-22 10:26:55 +00:00
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--config", help="Path to a specific yaml config you want to use.")
group.add_argument("--hub-id", help="HuggingFace Hub ID for a pretrained model.")
parser.add_argument("--revision", help="Optionally provide the HuggingFace Hub revision ID.")
parser.add_argument(
"overrides",
nargs="*",
help="Any key=value arguments to override config values (use dots for.nested=overrides)",
)
args = parser.parse_args()
if args.config is not None:
# Note: For the config_path, Hydra wants a path relative to this script file.
hydra.initialize(
config_path=str(
_relative_path_between(Path(args.config).absolute().parent, Path(__file__).parent)
)
)
cfg = hydra.compose(Path(args.config).stem, args.overrides)
# TODO(alexander-soare): Save and load stats in trained model directory.
stats_path = None
elif args.hub_id is not None:
folder = Path(snapshot_download(args.hub_id, revision=args.revision))
2024-03-22 10:26:55 +00:00
cfg = hydra.initialize(config_path=str(_relative_path_between(folder, Path(__file__).parent)))
cfg = hydra.compose("config", args.overrides)
cfg.policy.pretrained_model_path = folder / "model.pt"
stats_path = folder / "stats.pth"
eval(
cfg,
2024-03-22 12:33:25 +00:00
out_dir=f"outputs/eval/{dt.now().strftime('%Y-%m-%d/%H-%M-%S')}_{cfg.env.name}_{cfg.policy.name}",
2024-03-22 12:58:59 +00:00
stats_path=stats_path,
2024-03-22 10:26:55 +00:00
)