Repository navigation
Expand file tree
/
Copy pathrecord_walker_rollouts.py
More file actions
243 lines (198 loc) · 7.98 KB
/
Copy pathrecord_walker_rollouts.py
File metadata and controls
243 lines (198 loc) · 7.98 KB
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
import os
import random
import math
from typing import Optional
import gym
import d4rl # noqa: F401
import d4rl.gym_mujoco # noqa: F401
import hydra
import numpy as np
import torch
from omegaconf import OmegaConf
from stable_baselines3 import SAC, DSRL
from stable_baselines3.common.vec_env import DummyVecEnv
from env_utils import (
ObservationWrapperGym,
ObservationWrapperRobomimic,
TraitWrapperGym,
ActionChunkWrapper,
make_robomimic_env,
)
from utils import load_base_policy
# register the same OmegaConf resolvers as in train_dsrl.py
OmegaConf.register_new_resolver("eval", eval, replace=True)
OmegaConf.register_new_resolver("round_up", math.ceil)
OmegaConf.register_new_resolver("round_down", math.floor)
base_path = os.path.dirname(os.path.abspath(__file__))
def make_single_env(cfg):
"""
Create a single environment with the same wrappers as used during training.
This mirrors `make_env` in `train_dsrl.py` so that the loaded checkpoint's
observation_space matches the eval env (including traits).
"""
if cfg.env_name in ["halfcheetah-medium-v2", "hopper-medium-v2", "walker2d-medium-v2"]:
env = gym.make(cfg.env_name)
env = ObservationWrapperGym(env, cfg.normalization_path)
traits_cfg = getattr(cfg, "traits", None)
if traits_cfg is not None and getattr(traits_cfg, "enabled", False):
env = TraitWrapperGym(env, traits_cfg)
elif cfg.env_name in ["lift", "can", "square", "transport"]:
env = make_robomimic_env(
env=cfg.env_name,
normalization_path=cfg.normalization_path,
low_dim_keys=cfg.env.wrappers.robomimic_lowdim.low_dim_keys,
dppo_path=cfg.dppo_path,
)
env = ObservationWrapperRobomimic(env, reward_offset=cfg.env.reward_offset)
else:
raise ValueError(f"Unsupported env_name: {cfg.env_name}")
env = ActionChunkWrapper(env, cfg, max_episode_steps=cfg.env.max_episode_steps)
return env
def _get_render_env(vec_env):
"""
Get the underlying (non-vector) env used for rendering.
Should work with plain DummyVecEnv or a VecEnvWrapper on top of it.
"""
base = vec_env
# unwrap one level if this is a VecEnvWrapper
if hasattr(base, "venv"):
base = base.venv
# DummyVecEnv exposes envs list
return base.envs[0]
def _get_trait_wrapper(vec_env):
base = _get_render_env(vec_env)
env = base
while hasattr(env, "env"):
if isinstance(env, TraitWrapperGym):
return env
env = env.env
if isinstance(env, TraitWrapperGym):
return env
return None
def record_episodes(
model,
vec_env,
cfg,
total_episodes: int = 10,
save_first_last: int = 2,
output_dir: Optional[str] = None,
):
"""
Run episodes and save the first and last `save_first_last` as MP4 videos.
Videos are saved as:
<output_dir>/<env_tag>_ep{idx}.mp4
where env_tag is derived from cfg.env_name (e.g., walker2d-medium-v2 -> walker2d_medium_v2).
"""
import imageio
if output_dir is None:
output_dir = os.path.join(os.getcwd(), "videoswalkertrainedgapsteps")
os.makedirs(output_dir, exist_ok=True)
# Choose which episodes to save: first N and last N
total_episodes = max(total_episodes, save_first_last * 2)
indices_to_save = set(
list(range(save_first_last))
+ list(range(total_episodes - save_first_last, total_episodes))
)
render_env = _get_render_env(vec_env)
env_tag = getattr(cfg, "env_name", "env").replace("-", "_")
for ep in range(total_episodes):
obs = vec_env.reset()
done = np.array([False])
frames = []
steps = 0
while not done[0] and steps < cfg.env.max_episode_steps:
frame = render_env.render(mode="rgb_array")
if ep in indices_to_save:
frames.append(frame)
if cfg.algorithm == "dsrl_sac":
action, _ = model.predict(obs, deterministic=True)
else:
action, _ = model.predict_diffused(obs, deterministic=True)
obs, rewards, dones, infos = vec_env.step(action)
done = dones
steps += 1
if ep in indices_to_save and len(frames) > 0:
video_path = os.path.join(output_dir, f"{env_tag}_ep{ep}.mp4")
with imageio.get_writer(video_path, fps=30) as writer:
for f in frames:
writer.append_data(f)
print(f"[record_rollouts] Saved episode {ep} to {video_path}")
@hydra.main(
config_path=os.path.join(base_path, "cfg/gym"),
config_name="dsrl_walker.yaml", # <-- switch to walker config
version_base=None,
)
def main(cfg: OmegaConf):
"""
Example usage for Walker:
python record_hopper_rollouts.py \\
model_path=/home/aneeshe/projects/diffusion-trait-steering/logs/gym-dsrl/gym_walker_dsrl_YYYY-MM-DD_HH-MM-SS_1/ \\
YYYY-MM-DD_HH-MM-SS_1/checkpoint/ft_policy_10000_steps.zip \\
record_episodes=4
This will save MP4s for episodes 0,1 and last two episodes into a `videos/`
folder inside the current Hydra run directory (or whatever output_dir you set).
"""
OmegaConf.resolve(cfg)
# make config mutable for extra CLI keys like model_path, record_episodes
cfg._set_flag("struct", False)
random.seed(cfg.seed)
np.random.seed(cfg.seed)
torch.manual_seed(cfg.seed)
vec_env = DummyVecEnv([lambda: make_single_env(cfg)])
trait_wrapper = _get_trait_wrapper(vec_env)
if trait_wrapper is not None:
trait_values = cfg.get("trait_values", None)
trait_mask = cfg.get("trait_mask", None)
if trait_values is not None or trait_mask is not None:
trait_wrapper.set_traits(values=trait_values, mask=trait_mask)
# Re-instantiate the diffusion base policy just like in training, so we
# can reattach it to the loaded DSRL model if it fails to deserialize.
base_policy = load_base_policy(cfg)
algo = cfg.algorithm
if algo == "dsrl_sac":
ModelCls = SAC
elif algo == "dsrl_na":
ModelCls = DSRL
else:
raise ValueError(f"Unsupported algorithm: {algo}")
# Get path to trained DSRL checkpoint from CLI / config
model_path = cfg.get("model_path", "")
if not model_path:
raise ValueError(
"You must pass a trained checkpoint path via CLI, e.g. "
"'model_path=/abs/path/to/checkpoint.zip'"
)
if not os.path.isabs(model_path):
model_path = os.path.abspath(model_path)
if not os.path.exists(model_path):
raise FileNotFoundError(f"model_path does not exist: {model_path}")
print(f"[record_rollouts] Loading model from: {model_path}")
# for DSRL, some constructor-only kwargs (like diffusion_act_dim) are not
# always stored correctly in the checkpoint. We override them from cfg here
load_kwargs = {}
if algo == "dsrl_na":
load_kwargs.update(
diffusion_act_dim=(cfg.act_steps, cfg.action_dim),
noise_critic_grad_steps=cfg.train.noise_critic_grad_steps,
critic_backup_combine_type=cfg.train.critic_backup_combine_type,
)
# Load the model on the same device as training (e.g., cuda:0)
load_device = getattr(cfg, "device", "auto")
model = ModelCls.load(model_path, env=vec_env, device=load_device, **load_kwargs)
# If diffusion_policy failed to deserialize, reattach the base policy
if algo == "dsrl_na" and getattr(model, "diffusion_policy", None) is None:
model.diffusion_policy = base_policy
# where to place videos
output_dir = cfg.get("video_dir", os.path.join(os.getcwd(), "videoswalkertrainedgapsteps"))
total_episodes = int(cfg.get("record_episodes", 10))
save_first_last = int(cfg.get("save_first_last", 2))
record_episodes(
model=model,
vec_env=vec_env,
cfg=cfg,
total_episodes=total_episodes,
save_first_last=save_first_last,
output_dir=output_dir,
)
if __name__ == "__main__":
main()