forked from gaoyuezhou/dino_wm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan.py
507 lines (451 loc) · 16.7 KB
/
plan.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import os
import gym
import json
import hydra
import random
import torch
import pickle
import wandb
import logging
import warnings
import numpy as np
import submitit
from itertools import product
from pathlib import Path
from einops import rearrange
from omegaconf import OmegaConf, open_dict
from env.venv import SubprocVectorEnv
from custom_resolvers import replace_slash
from preprocessor import Preprocessor
from planning.evaluator import PlanEvaluator
from utils import cfg_to_dict, seed
warnings.filterwarnings("ignore")
log = logging.getLogger(__name__)
ALL_MODEL_KEYS = [
"encoder",
"predictor",
"decoder",
"proprio_encoder",
"action_encoder",
]
def planning_main_in_dir(working_dir, cfg_dict):
os.chdir(working_dir)
return planning_main(cfg_dict=cfg_dict)
def launch_plan_jobs(
epoch,
cfg_dicts,
plan_output_dir,
):
with submitit.helpers.clean_env():
jobs = []
for cfg_dict in cfg_dicts:
subdir_name = f"{cfg_dict['planner']['name']}_goal_source={cfg_dict['goal_source']}_goal_H={cfg_dict['goal_H']}_alpha={cfg_dict['objective']['alpha']}"
subdir_path = os.path.join(plan_output_dir, subdir_name)
executor = submitit.AutoExecutor(
folder=subdir_path, slurm_max_num_timeout=20
)
executor.update_parameters(
**{
k: v
for k, v in cfg_dict["hydra"]["launcher"].items()
if k != "submitit_folder"
}
)
cfg_dict["saved_folder"] = subdir_path
cfg_dict["wandb_logging"] = False # don't init wandb
job = executor.submit(planning_main_in_dir, subdir_path, cfg_dict)
jobs.append((epoch, subdir_name, job))
print(
f"Submitted evaluation job for checkpoint: {subdir_path}, job id: {job.job_id}"
)
return jobs
def build_plan_cfg_dicts(
plan_cfg_path="",
ckpt_base_path="",
model_name="",
model_epoch="final",
planner=["gd", "cem"],
goal_source=["dset"],
goal_H=[1, 5, 10],
alpha=[0, 0.1, 1],
):
"""
Return a list of plan overrides, for model_path, add a key in the dict {"model_path": model_path}.
"""
config_path = os.path.dirname(plan_cfg_path)
overrides = [
{
"planner": p,
"goal_source": g_source,
"goal_H": g_H,
"ckpt_base_path": ckpt_base_path,
"model_name": model_name,
"model_epoch": model_epoch,
"objective": {"alpha": a},
}
for p, g_source, g_H, a in product(planner, goal_source, goal_H, alpha)
]
cfg = OmegaConf.load(plan_cfg_path)
cfg_dicts = []
for override_args in overrides:
planner = override_args["planner"]
planner_cfg = OmegaConf.load(
os.path.join(config_path, f"planner/{planner}.yaml")
)
cfg["planner"] = OmegaConf.merge(cfg.get("planner", {}), planner_cfg)
override_args.pop("planner")
cfg = OmegaConf.merge(cfg, OmegaConf.create(override_args))
cfg_dict = OmegaConf.to_container(cfg)
cfg_dict["planner"]["horizon"] = cfg_dict["goal_H"] # assume planning horizon equals to goal horizon
cfg_dicts.append(cfg_dict)
return cfg_dicts
class PlanWorkspace:
def __init__(
self,
cfg_dict: dict,
wm: torch.nn.Module,
dset,
env: SubprocVectorEnv,
env_name: str,
frameskip: int,
wandb_run: wandb.run,
):
self.cfg_dict = cfg_dict
self.wm = wm
self.dset = dset
self.env = env
self.env_name = env_name
self.frameskip = frameskip
self.wandb_run = wandb_run
self.device = next(wm.parameters()).device
# have different seeds for each planning instances
self.eval_seed = [cfg_dict["seed"] * n + 1 for n in range(cfg_dict["n_evals"])]
print("eval_seed: ", self.eval_seed)
self.n_evals = cfg_dict["n_evals"]
self.goal_source = cfg_dict["goal_source"]
self.goal_H = cfg_dict["goal_H"]
self.action_dim = self.dset.action_dim * self.frameskip
self.debug_dset_init = cfg_dict["debug_dset_init"]
objective_fn = hydra.utils.call(
cfg_dict["objective"],
)
self.data_preprocessor = Preprocessor(
action_mean=self.dset.action_mean,
action_std=self.dset.action_std,
state_mean=self.dset.state_mean,
state_std=self.dset.state_std,
proprio_mean=self.dset.proprio_mean,
proprio_std=self.dset.proprio_std,
transform=self.dset.transform,
)
if self.cfg_dict["goal_source"] == "file":
self.prepare_targets_from_file(cfg_dict["goal_file_path"])
else:
self.prepare_targets()
self.evaluator = PlanEvaluator(
obs_0=self.obs_0,
obs_g=self.obs_g,
state_0=self.state_0,
state_g=self.state_g,
env=self.env,
wm=self.wm,
frameskip=self.frameskip,
seed=self.eval_seed,
preprocessor=self.data_preprocessor,
n_plot_samples=self.cfg_dict["n_plot_samples"],
)
if self.wandb_run is None or isinstance(
self.wandb_run, wandb.sdk.lib.disabled.RunDisabled
):
self.wandb_run = DummyWandbRun()
self.log_filename = "logs.json" # planner and final eval logs are dumped here
self.planner = hydra.utils.instantiate(
self.cfg_dict["planner"],
wm=self.wm,
env=self.env, # only for mpc
action_dim=self.action_dim,
objective_fn=objective_fn,
preprocessor=self.data_preprocessor,
evaluator=self.evaluator,
wandb_run=self.wandb_run,
log_filename=self.log_filename,
)
# optional: assume planning horizon equals to goal horizon
from planning.mpc import MPCPlanner
if isinstance(self.planner, MPCPlanner):
self.planner.sub_planner.horizon = cfg_dict["goal_H"]
self.planner.n_taken_actions = cfg_dict["goal_H"]
else:
self.planner.horizon = cfg_dict["goal_H"]
self.dump_targets()
def prepare_targets(self):
states = []
actions = []
observations = []
if self.goal_source == "random_state":
# update env config from val trajs
observations, states, actions, env_info = (
self.sample_traj_segment_from_dset(traj_len=2)
)
self.env.update_env(env_info)
# sample random states
rand_init_state, rand_goal_state = self.env.sample_random_init_goal_states(
self.eval_seed
)
if self.env_name == "deformable_env": # take rand init state from dset for deformable envs
rand_init_state = np.array([x[0] for x in states])
obs_0, state_0 = self.env.prepare(self.eval_seed, rand_init_state)
obs_g, state_g = self.env.prepare(self.eval_seed, rand_goal_state)
# add dim for t
for k in obs_0.keys():
obs_0[k] = np.expand_dims(obs_0[k], axis=1)
obs_g[k] = np.expand_dims(obs_g[k], axis=1)
self.obs_0 = obs_0
self.obs_g = obs_g
self.state_0 = rand_init_state # (b, d)
self.state_g = rand_goal_state
self.gt_actions = None
else:
# update env config from val trajs
observations, states, actions, env_info = (
self.sample_traj_segment_from_dset(traj_len=self.frameskip * self.goal_H + 1)
)
self.env.update_env(env_info)
# get states from val trajs
init_state = [x[0] for x in states]
init_state = np.array(init_state)
actions = torch.stack(actions)
if self.goal_source == "random_action":
actions = torch.randn_like(actions)
wm_actions = rearrange(actions, "b (t f) d -> b t (f d)", f=self.frameskip)
exec_actions = self.data_preprocessor.denormalize_actions(actions)
# replay actions in env to get gt obses
rollout_obses, rollout_states = self.env.rollout(
self.eval_seed, init_state, exec_actions.numpy()
)
self.obs_0 = {
key: np.expand_dims(arr[:, 0], axis=1)
for key, arr in rollout_obses.items()
}
self.obs_g = {
key: np.expand_dims(arr[:, -1], axis=1)
for key, arr in rollout_obses.items()
}
self.state_0 = init_state # (b, d)
self.state_g = rollout_states[:, -1] # (b, d)
self.gt_actions = wm_actions
def sample_traj_segment_from_dset(self, traj_len):
states = []
actions = []
observations = []
env_info = []
# Check if any trajectory is long enough
valid_traj = [
self.dset[i][0]["visual"].shape[0]
for i in range(len(self.dset))
if self.dset[i][0]["visual"].shape[0] >= traj_len
]
if len(valid_traj) == 0:
raise ValueError("No trajectory in the dataset is long enough.")
# sample init_states from dset
for i in range(self.n_evals):
max_offset = -1
while max_offset < 0: # filter out traj that are not long enough
traj_id = random.randint(0, len(self.dset) - 1)
obs, act, state, e_info = self.dset[traj_id]
max_offset = obs["visual"].shape[0] - traj_len
state = state.numpy()
offset = random.randint(0, max_offset)
obs = {
key: arr[offset : offset + traj_len]
for key, arr in obs.items()
}
state = state[offset : offset + traj_len]
act = act[offset : offset + self.frameskip * self.goal_H]
actions.append(act)
states.append(state)
observations.append(obs)
env_info.append(e_info)
return observations, states, actions, env_info
def prepare_targets_from_file(self, file_path):
with open(file_path, "rb") as f:
data = pickle.load(f)
self.obs_0 = data["obs_0"]
self.obs_g = data["obs_g"]
self.state_0 = data["state_0"]
self.state_g = data["state_g"]
self.gt_actions = data["gt_actions"]
self.goal_H = data["goal_H"]
def dump_targets(self):
with open("plan_targets.pkl", "wb") as f:
pickle.dump(
{
"obs_0": self.obs_0,
"obs_g": self.obs_g,
"state_0": self.state_0,
"state_g": self.state_g,
"gt_actions": self.gt_actions,
"goal_H": self.goal_H,
},
f,
)
file_path = os.path.abspath("plan_targets.pkl")
print(f"Dumped plan targets to {file_path}")
def perform_planning(self):
if self.debug_dset_init:
actions_init = self.gt_actions
else:
actions_init = None
actions, action_len = self.planner.plan(
obs_0=self.obs_0,
obs_g=self.obs_g,
actions=actions_init,
)
logs, successes, _, _ = self.evaluator.eval_actions(
actions.detach(), action_len, save_video=True, filename="output_final"
)
logs = {f"final_eval/{k}": v for k, v in logs.items()}
self.wandb_run.log(logs)
logs_entry = {
key: (
value.item()
if isinstance(value, (np.float32, np.int32, np.int64))
else value
)
for key, value in logs.items()
}
with open(self.log_filename, "a") as file:
file.write(json.dumps(logs_entry) + "\n")
return logs
def load_ckpt(snapshot_path, device):
with snapshot_path.open("rb") as f:
payload = torch.load(f, map_location=device)
loaded_keys = []
result = {}
for k, v in payload.items():
if k in ALL_MODEL_KEYS:
loaded_keys.append(k)
result[k] = v.to(device)
result["epoch"] = payload["epoch"]
return result
def load_model(model_ckpt, train_cfg, num_action_repeat, device):
result = {}
if model_ckpt.exists():
result = load_ckpt(model_ckpt, device)
print(f"Resuming from epoch {result['epoch']}: {model_ckpt}")
if "encoder" not in result:
result["encoder"] = hydra.utils.instantiate(
train_cfg.encoder,
)
if "predictor" not in result:
raise ValueError("Predictor not found in model checkpoint")
if train_cfg.has_decoder and "decoder" not in result:
base_path = os.path.dirname(os.path.abspath(__file__))
if train_cfg.env.decoder_path is not None:
decoder_path = os.path.join(base_path, train_cfg.env.decoder_path)
ckpt = torch.load(decoder_path)
if isinstance(ckpt, dict):
result["decoder"] = ckpt["decoder"]
else:
result["decoder"] = torch.load(decoder_path)
else:
raise ValueError(
"Decoder path not found in model checkpoint \
and is not provided in config"
)
elif not train_cfg.has_decoder:
result["decoder"] = None
model = hydra.utils.instantiate(
train_cfg.model,
encoder=result["encoder"],
proprio_encoder=result["proprio_encoder"],
action_encoder=result["action_encoder"],
predictor=result["predictor"],
decoder=result["decoder"],
proprio_dim=train_cfg.proprio_emb_dim,
action_dim=train_cfg.action_emb_dim,
concat_dim=train_cfg.concat_dim,
num_action_repeat=num_action_repeat,
num_proprio_repeat=train_cfg.num_proprio_repeat,
)
model.to(device)
return model
class DummyWandbRun:
def __init__(self):
self.mode = "disabled"
def log(self, *args, **kwargs):
pass
def watch(self, *args, **kwargs):
pass
def config(self, *args, **kwargs):
pass
def finish(self):
pass
def planning_main(cfg_dict):
output_dir = cfg_dict["saved_folder"]
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if cfg_dict["wandb_logging"]:
wandb_run = wandb.init(
project=f"plan_{cfg_dict['planner']['name']}", config=cfg_dict
)
wandb.run.name = "{}".format(output_dir.split("plan_outputs/")[-1])
else:
wandb_run = None
ckpt_base_path = cfg_dict["ckpt_base_path"]
model_path = f"{ckpt_base_path}/outputs/{cfg_dict['model_name']}/"
with open(os.path.join(model_path, "hydra.yaml"), "r") as f:
model_cfg = OmegaConf.load(f)
seed(cfg_dict["seed"])
_, dset = hydra.utils.call(
model_cfg.env.dataset,
num_hist=model_cfg.num_hist,
num_pred=model_cfg.num_pred,
frameskip=model_cfg.frameskip,
)
dset = dset["valid"]
num_action_repeat = model_cfg.num_action_repeat
model_ckpt = (
Path(model_path) / "checkpoints" / f"model_{cfg_dict['model_epoch']}.pth"
)
model = load_model(model_ckpt, model_cfg, num_action_repeat, device=device)
# use dummy vector env for wall and deformable envs
if model_cfg.env.name == "wall" or model_cfg.env.name == "deformable_env":
from env.serial_vector_env import SerialVectorEnv
env = SerialVectorEnv(
[
gym.make(
model_cfg.env.name, *model_cfg.env.args, **model_cfg.env.kwargs
)
for _ in range(cfg_dict["n_evals"])
]
)
else:
env = SubprocVectorEnv(
[
lambda: gym.make(
model_cfg.env.name, *model_cfg.env.args, **model_cfg.env.kwargs
)
for _ in range(cfg_dict["n_evals"])
]
)
plan_workspace = PlanWorkspace(
cfg_dict=cfg_dict,
wm=model,
dset=dset,
env=env,
env_name=model_cfg.env.name,
frameskip=model_cfg.frameskip,
wandb_run=wandb_run,
)
logs = plan_workspace.perform_planning()
return logs
@hydra.main(config_path="conf", config_name="plan")
def main(cfg: OmegaConf):
with open_dict(cfg):
cfg["saved_folder"] = os.getcwd()
log.info(f"Planning result saved dir: {cfg['saved_folder']}")
cfg_dict = cfg_to_dict(cfg)
cfg_dict["wandb_logging"] = True
planning_main(cfg_dict)
if __name__ == "__main__":
main()