File size: 10,530 Bytes
d18db74 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | import numpy as np
import torch
import cv2
try:
from pynput import keyboard
except Exception: # rsm_rise patch: headless nodes
keyboard = None
from collections import deque
from sim_pipeline.eval.rollout_env import PossibleVecEnv
from sim_pipeline.eval.rollout_policies.rollout_policy import RolloutPolicy
from sim_pipeline.eval.rollout_stats import RolloutStats
from sim_pipeline.utils.logger import Video
from sim_pipeline.utils.logger import Logger
from sim_pipeline.utils.rollout_utils import _render_frame, stack_last_n_obs, add_batch_dim
def rollout(
policy: RolloutPolicy,
eval_envs: PossibleVecEnv,
rollout_stats: RolloutStats,
reset_state: np.ndarray | None = None,
logger: Logger | None = None,
total_steps: int | None = 200,
render_mode: str | None = 'rgb_array',
log_video: bool = True,
video_writers: list[cv2.VideoWriter] | None = None,
render_dims: tuple[int, int] | None = None,
video_skip: int = 5,
return_trajectory: bool = False,
end_on_success: bool = True,
tag: str = 'eval'
) -> None | dict[str, list[np.ndarray | float | bool] | list[list[np.ndarray | float | bool]]]:
"""
General function for rolling out a policy in an environment.
Args:
policy: supports .predict(action)
eval_envs: must have gymnasium api, so `step` returns (obs, rew, term, trunc, info) and `reset` returns (obs, info)
rollout_stats: RolloutStats object to store episode statistics
logger: supports .record(key, value)
total_steps : number of steps to rollout. If None, roll out
until done=True (for all envs) and assumes all environments terminate at the same time.
render_mode:
'human' - render to screen. no videos can be saved. `eval_envs` can be
a single environment, or if vectorized, only the first environment is rendered.
'rgb_array' - `eval_envs` is a vectorized environment. videos are logged if `log_video`
is True.
None - no rendering
log_video: whether to save videos of the rollout. If video_writer is not None, writes to video_writer,
and if logger is not None, logs the video to the logger.
video_writer: a cv2.VideoWriter object to write the video to. Must have `log_video` set to True to save
videos.
render_dims: dimensions to render the video in. If None, uses the default camera size of the environment.
Ignored if vectorized environment.
return_trajectory: whether to return the trajectory of the rollout.
Returns:
if `return_trajectory` is True, returns a dictionary:
{
'states': list of states,
'actions': list of actions,
'rewards': list of rewards,
'dones': list of dones
}
if `eval_envs` is a vectorized environment, each list is a list of lists, where each inner list
corresponds to a different env's rollout. Otherwise, each list is a list of the rollout trajectory.
else, returns None
"""
assert isinstance(policy, RolloutPolicy)
render = torch.cuda.device_count() > 0 and render_mode is not None
eval_envs.render_mode = render_mode
if eval_envs.is_sb_vec_env:
# not correct
camera_height = eval_envs.get_camera_sizes()[0][0][0]
else:
camera_height = eval_envs.get_camera_sizes()[0][0]
if return_trajectory:
if eval_envs.is_vec_env:
trajectory = {
"states": [[] for _ in range(eval_envs.num_envs)],
"actions": [[] for _ in range(eval_envs.num_envs)],
"rewards": [[] for _ in range(eval_envs.num_envs)],
"dones": [[] for _ in range(eval_envs.num_envs)],
}
trajectory = {
"states": [],
"actions": [],
"rewards": [],
"dones": [],
"obs": [],
}
########################################
# obs shapes:
# SB3:
# non-vectorized: (D, ) flat
# obs: (N, D) flat
# policy handles either
# robomimic:
# non-vectorized: dict -> (d,)
# vectorized: dict -> (N, d)
# -> does not support vectorized eval b/c robomimic policy auto-pads batch dim
# diffusion:
# non-vectorized: dict -> (d, )
# vectorized: dict -> (N, d)
# policy:
# lowdim expects (N, H, D)
# rgb expects dict -> (N, H, d)
# H - obs horizon, D - total obs dim, d - obs key dim, N - num envs
########################################
if reset_state is not None and not eval_envs.is_vec_env:
eval_envs.reset()
obs = eval_envs.reset_to({'states': reset_state})
else:
obs = eval_envs.reset()
policy.reset()
if return_trajectory:
# doesn't work when eval_envs is VecEnv
state_dict = eval_envs.get_state()
state = state_dict['states']
trajectory['initial_state_dict'] = state_dict
dones = False
if render_mode == 'rgb_array' and log_video:
frames: list[np.ndarray] = []
if render:
frame = _render_frame(eval_envs, render_mode, log_video, camera_height, video_writers=video_writers, dims=render_dims)
if render_mode == 'rgb_array' and log_video and video_writers is None:
frames.append(frame)
# TODO: Don't assume all eval_envs terminate at the same time. If VecEnv,
# steps is not specified, and a env has done = True before others, will error out.
step_count = 0
# for diffusion policies/policies that do action chunking, execute one action at a time
action_queue = deque()
if not eval_envs.is_vec_env:
obs = add_batch_dim(obs)
if policy.n_obs_steps is not None:
obs_stack: deque[np.ndarray | dict[str, np.ndarray]] = deque(maxlen=policy.n_obs_steps+1)
obs_stack.append(obs)
else:
obs_stack = None
global should_stop, random_sample
should_stop = False
random_sample = False
def on_press(key):
global should_stop, random_sample
try:
if key.char == 'q':
should_stop = True
elif key.char == 'r':
random_sample = True
except AttributeError:
pass
listener = None
if keyboard is not None:
try:
listener = keyboard.Listener(on_press=on_press)
listener.start()
except Exception:
listener = None
while (total_steps is None and not np.all(dones)) or (total_steps is not None and step_count < total_steps):
if should_stop:
break
obs_input = obs if policy.n_obs_steps is None else stack_last_n_obs(obs_stack, policy.n_obs_steps)
if policy.action_chunking:
if not action_queue:
actions = policy.predict(obs_input, random_sample=random_sample)
# actions has dim (n_envs, execution_horizon, action_dim)
for i in range(actions.shape[1]):
action_queue.append(actions[:, i])
actions = action_queue.popleft()
else:
# actions has dim (n_envs, action_dim)
actions = policy.predict(obs_input, random_sample=random_sample)
if policy.is_composite:
switched = policy.switch_policy(obs, actions, eval_envs)
# empty action queue b/c the new policy might not have action chunking
if switched:
action_queue.clear()
if not eval_envs.is_vec_env:
# for non vectorized envs, remove batch dim
actions = actions[0]
next_obs, rewards, dones, truncated, infos = eval_envs.step(actions)
if not eval_envs.is_vec_env:
next_obs = add_batch_dim(next_obs)
if obs_stack is not None:
obs_stack.append(next_obs)
success: list[bool] | bool = eval_envs.get_success()
rollout_stats.add_step(
reward=rewards,
success=success,
)
if return_trajectory:
if eval_envs.is_vec_env:
for i in range(eval_envs.num_envs):
trajectory['states'][i].append(state[i])
trajectory['actions'][i].append(actions[i])
trajectory['rewards'][i].append(rewards[i])
trajectory['dones'][i].append(dones[i])
else:
trajectory['states'].append(state)
trajectory['actions'].append(actions)
trajectory['rewards'].append(rewards)
trajectory['dones'].append(dones)
trajectory['obs'].append(obs)
state = eval_envs.get_state()['states']
if render:
if step_count % video_skip == 0:
frame = _render_frame(eval_envs, render_mode, log_video, camera_height, video_writers=video_writers, dims=render_dims)
if render_mode == 'rgb_array' and log_video and video_writers is None:
frames.append(frame)
if end_on_success:
if isinstance(success, list | tuple) and all(success):
break
elif isinstance(success, bool) and success:
break
obs = next_obs
step_count += 1
if listener is not None:
try:
listener.stop()
except Exception:
pass
# Record episode statistics
rollout_stats.finalize_episode()
if logger is not None:
logger.record(f"{tag}/batch_avg_return", rollout_stats.avg_ep_reward)
logger.record(f"{tag}/success", rollout_stats.avg_ep_success)
logger.record(f"{tag}/final_success", rollout_stats.avg_ep_final_success)
# Log videos
if render and render_mode == 'rgb_array' and log_video:
if logger is not None:
# -> (b, t, 3, h, w) + downsample
video: np.ndarray[np.uint8] = np.stack(frames).transpose(1, 0, 4, 2, 3)[..., ::2, ::2]
logger.record(
f"{tag}/trajectory/env/camera",
Video(video, fps=20),
exclude=["stdout"],
)
if return_trajectory:
return trajectory |