forked from tonyzhaozh/act
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imitate_episodes.py
529 lines (447 loc) · 18.8 KB
/
imitate_episodes.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import argparse
from copy import deepcopy
import os
import pickle
from constants import DT
from constants import PUPPET_GRIPPER_JOINT_OPEN
from einops import rearrange
import IPython
import matplotlib.pyplot as plt
import numpy as np
from policy import ACTPolicy
from policy import CNNMLPPolicy
from policy import PiPolicy
from sim_env import BOX_POSE
import torch
from tqdm import tqdm
from utils import compute_dict_mean # robot functions # helper functions
from utils import detach_dict # robot functions # helper functions
from utils import load_data # robot functions # helper functions # data functions
from utils import sample_box_pose # robot functions # helper functions
from utils import sample_insertion_pose # robot functions # helper functions
from utils import set_seed # robot functions # helper functions
from visualize_episodes import save_videos
e = IPython.embed
_POLICY_CLASS_ACT = "ACT"
_POLICY_CLASS_CNNMLP = "CNNMLP"
_POLICY_CLASS_PI = "PI"
def main(args):
set_seed(1)
# command line parameters
is_eval = args["eval"]
ckpt_dir = args["ckpt_dir"]
policy_class = args["policy_class"]
onscreen_render = args["onscreen_render"]
task_name = args["task_name"]
batch_size_train = args["batch_size"]
batch_size_val = args["batch_size"]
num_epochs = args["num_epochs"]
# get task parameters
is_sim = task_name[:4] == "sim_"
if is_sim:
from constants import SIM_TASK_CONFIGS
task_config = SIM_TASK_CONFIGS[task_name]
else:
from aloha_scripts.constants import TASK_CONFIGS
task_config = TASK_CONFIGS[task_name]
dataset_dir = task_config["dataset_dir"]
num_episodes = task_config["num_episodes"]
episode_len = task_config["episode_len"]
camera_names = task_config["camera_names"]
# fixed parameters
state_dim = 14
lr_backbone = 1e-5
backbone = "resnet18"
if policy_class == _POLICY_CLASS_ACT:
enc_layers = 4
dec_layers = 7
nheads = 8
policy_config = {
"lr": args["lr"],
"num_queries": args["chunk_size"],
"kl_weight": args["kl_weight"],
"hidden_dim": args["hidden_dim"],
"dim_feedforward": args["dim_feedforward"],
"lr_backbone": lr_backbone,
"backbone": backbone,
"enc_layers": enc_layers,
"dec_layers": dec_layers,
"nheads": nheads,
"camera_names": camera_names,
}
elif policy_class == _POLICY_CLASS_CNNMLP or policy_class == _POLICY_CLASS_PI:
policy_config = {
"lr": args["lr"],
"lr_backbone": lr_backbone,
"backbone": backbone,
"num_queries": 1,
"camera_names": camera_names,
}
else:
raise NotImplementedError
config = {
"num_epochs": num_epochs,
"ckpt_dir": ckpt_dir,
"episode_len": episode_len,
"state_dim": state_dim,
"lr": args["lr"],
"policy_class": policy_class,
"onscreen_render": onscreen_render,
"render_height": args["render_height"],
"render_width": args["render_width"],
"policy_config": policy_config,
"task_name": task_name,
"seed": args["seed"],
"temporal_agg": args["temporal_agg"],
"camera_names": camera_names,
"real_robot": not is_sim,
}
if is_eval:
ckpt_names = ["policy_best.ckpt"]
results = []
for ckpt_name in ckpt_names:
success_rate, avg_return = eval_bc(config, ckpt_name, save_episode=True)
results.append([ckpt_name, success_rate, avg_return])
for ckpt_name, success_rate, avg_return in results:
print(f"{ckpt_name}: {success_rate=} {avg_return=}")
print()
exit()
train_dataloader, val_dataloader, stats, _ = load_data(
dataset_dir, num_episodes, camera_names, batch_size_train, batch_size_val
)
# save dataset stats
if not os.path.isdir(ckpt_dir):
os.makedirs(ckpt_dir)
stats_path = os.path.join(ckpt_dir, "dataset_stats.pkl")
with open(stats_path, "wb") as f:
pickle.dump(stats, f)
best_ckpt_info = train_bc(train_dataloader, val_dataloader, config)
best_epoch, min_val_loss, best_state_dict = best_ckpt_info
# save best checkpoint
ckpt_path = os.path.join(ckpt_dir, "policy_best.ckpt")
torch.save(best_state_dict, ckpt_path)
print(f"Best ckpt, val loss {min_val_loss:.6f} @ epoch{best_epoch}")
def make_policy(policy_class, policy_config):
if policy_class == _POLICY_CLASS_ACT:
policy = ACTPolicy(policy_config)
elif policy_class == _POLICY_CLASS_CNNMLP:
policy = CNNMLPPolicy(policy_config)
elif policy_class == _POLICY_CLASS_PI:
policy = PiPolicy()
else:
raise NotImplementedError
return policy
def make_optimizer(policy_class, policy):
if policy_class == _POLICY_CLASS_ACT or policy_class == _POLICY_CLASS_CNNMLP:
optimizer = policy.configure_optimizers()
else:
raise NotImplementedError
return optimizer
def get_image(ts, camera_names, use_gpu: bool = True):
curr_images = []
for cam_name in camera_names:
curr_image = rearrange(ts.observation["images"][cam_name], "h w c -> c h w")
curr_images.append(curr_image)
curr_image = np.stack(curr_images, axis=0) / 255.0
if use_gpu:
curr_image = torch.from_numpy(curr_image).float().cuda().unsqueeze(0)
return curr_image
def eval_bc(config, ckpt_name, save_episode=True):
set_seed(1000)
ckpt_dir = config["ckpt_dir"]
real_robot = config["real_robot"]
policy_class = config["policy_class"]
onscreen_render = config["onscreen_render"]
render_height = config["render_height"]
render_width = config["render_width"]
policy_config = config["policy_config"]
max_timesteps = config["episode_len"]
task_name = config["task_name"]
onscreen_cam = "angle"
# load policy
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
policy = make_policy(policy_class, policy_config)
if policy_class != _POLICY_CLASS_PI:
loading_status = policy.load_state_dict(torch.load(ckpt_path))
print(loading_status)
policy.cuda()
policy.eval()
print(f"Loaded: {ckpt_path}")
policy_func = _make_policy_func(config, policy)
# load environment
if real_robot:
from aloha_scripts.real_env import make_real_env # requires aloha
from aloha_scripts.robot_utils import move_grippers # requires aloha
env = make_real_env(init_node=True)
env_max_reward = 0
else:
from sim_env import make_sim_env
env = make_sim_env(task_name, render_height, render_width)
env_max_reward = env.task.max_reward
max_timesteps = int(max_timesteps * 1) # may increase for real-world tasks
num_rollouts = 50
episode_returns = []
highest_rewards = []
for rollout_id in range(num_rollouts):
rollout_id += 0
### set task
if "sim_transfer_cube" in task_name:
BOX_POSE[0] = sample_box_pose() # used in sim reset
elif "sim_insertion" in task_name:
BOX_POSE[0] = np.concatenate(sample_insertion_pose()) # used in sim reset
ts = env.reset()
### onscreen render
if onscreen_render:
ax = plt.subplot()
plt_img = ax.imshow(env._physics.render(height=render_height, width=render_width, camera_id=onscreen_cam))
plt.ion()
### evaluation loop
image_list = [] # for visualization
qpos_list = []
target_qpos_list = []
rewards = []
with torch.inference_mode():
for t in range(max_timesteps):
### update onscreen render and wait for DT
if onscreen_render:
image = env._physics.render(height=render_height, width=render_width, camera_id=onscreen_cam)
plt_img.set_data(image)
plt.pause(DT)
### process previous timestep to get qpos and image_list
obs = ts.observation
if "images" in obs:
image_list.append(obs["images"])
else:
image_list.append({"main": obs["image"]})
curr_image = get_image(ts, config["camera_names"], use_gpu=policy_class != _POLICY_CLASS_PI)
qpos_numpy = np.array(obs["qpos"])
### query policy
target_qpos = policy_func(qpos=qpos_numpy, curr_image=curr_image, t=t)
### step the environment
ts = env.step(target_qpos)
### for visualization
qpos_list.append(qpos_numpy)
target_qpos_list.append(target_qpos)
rewards.append(ts.reward)
plt.close()
if real_robot:
move_grippers(
[env.puppet_bot_left, env.puppet_bot_right],
[PUPPET_GRIPPER_JOINT_OPEN] * 2,
move_time=0.5,
) # open
rewards = np.array(rewards)
episode_return = np.sum(rewards[rewards is not None])
episode_returns.append(episode_return)
episode_highest_reward = np.max(rewards)
highest_rewards.append(episode_highest_reward)
print(
f"Rollout {rollout_id}\n{episode_return=}, {episode_highest_reward=}, {env_max_reward=}, Success: {episode_highest_reward==env_max_reward}"
)
if save_episode:
save_videos(
image_list,
DT,
video_path=os.path.join(ckpt_dir, f"video{rollout_id}.mp4"),
)
success_rate = np.mean(np.array(highest_rewards) == env_max_reward)
avg_return = np.mean(episode_returns)
summary_str = f"\nSuccess rate: {success_rate}\nAverage return: {avg_return}\n\n"
for r in range(env_max_reward + 1):
more_or_equal_r = (np.array(highest_rewards) >= r).sum()
more_or_equal_r_rate = more_or_equal_r / num_rollouts
summary_str += f"Reward >= {r}: {more_or_equal_r}/{num_rollouts} = {more_or_equal_r_rate*100}%\n"
print(summary_str)
# save success rate to txt
result_file_name = "result_" + ckpt_name.split(".")[0] + ".txt"
with open(os.path.join(ckpt_dir, result_file_name), "w") as f:
f.write(summary_str)
f.write(repr(episode_returns))
f.write("\n\n")
f.write(repr(highest_rewards))
return success_rate, avg_return
def _make_policy_func(config, policy):
if config["policy_class"] == _POLICY_CLASS_ACT:
max_timesteps = config["episode_len"]
temporal_agg = config["temporal_agg"]
query_frequency = config["policy_config"]["num_queries"]
num_queries = config["policy_config"]["num_queries"]
state_dim = config["state_dim"]
if temporal_agg:
query_frequency = 1
if temporal_agg:
all_time_actions = torch.zeros([max_timesteps, max_timesteps + num_queries, state_dim]).cuda()
def policy_func(qpos, curr_image, t):
qpos = torch.from_numpy(qpos).float().cuda().unsqueeze(0)
if t % query_frequency == 0:
all_actions = policy(qpos, curr_image)
if temporal_agg:
all_time_actions[[t], t : t + num_queries] = all_actions
actions_for_curr_step = all_time_actions[:, t]
actions_populated = torch.all(actions_for_curr_step != 0, axis=1)
actions_for_curr_step = actions_for_curr_step[actions_populated]
k = 0.01
exp_weights = np.exp(-k * np.arange(len(actions_for_curr_step)))
exp_weights = exp_weights / exp_weights.sum()
exp_weights = torch.from_numpy(exp_weights).cuda().unsqueeze(dim=1)
raw_action = (actions_for_curr_step * exp_weights).sum(dim=0, keepdim=True)
else:
raw_action = all_actions[:, t % query_frequency]
return raw_action.squeeze(0).cpu().numpy()
policy_func = _normalize_to_dataset_stats_wrapper(policy_func, config)
elif config["policy_class"] == _POLICY_CLASS_CNNMLP:
def policy_func(qpos, curr_image, t):
qpos = torch.from_numpy(qpos).float().cuda().unsqueeze(0)
raw_action = policy(qpos, curr_image)
return raw_action.squeeze(0).cpu().numpy()
policy_func = _normalize_to_dataset_stats_wrapper(policy_func, config)
elif config["policy_class"] == _POLICY_CLASS_PI:
def policy_func(qpos, curr_image, t):
raw_action = policy(qpos, curr_image)
return raw_action
else:
raise NotImplementedError
return policy_func
def _normalize_to_dataset_stats_wrapper(policy_func, config):
ckpt_dir = config["ckpt_dir"]
stats_path = os.path.join(ckpt_dir, "dataset_stats.pkl")
with open(stats_path, "rb") as f:
stats = pickle.load(f)
def pre_process(s_qpos):
return (s_qpos - stats["qpos_mean"]) / stats["qpos_std"]
def post_process(a):
return a * stats["action_std"] + stats["action_mean"]
def inner(qpos, curr_image, t):
qpos = pre_process(qpos)
raw_action = policy_func(qpos, curr_image, t)
return post_process(raw_action)
return inner
def forward_pass(data, policy):
image_data, qpos_data, action_data, is_pad = data
image_data, qpos_data, action_data, is_pad = (
image_data.cuda(),
qpos_data.cuda(),
action_data.cuda(),
is_pad.cuda(),
)
return policy(qpos_data, image_data, action_data, is_pad) # TODO remove None
def train_bc(train_dataloader, val_dataloader, config):
num_epochs = config["num_epochs"]
ckpt_dir = config["ckpt_dir"]
seed = config["seed"]
policy_class = config["policy_class"]
policy_config = config["policy_config"]
set_seed(seed)
policy = make_policy(policy_class, policy_config)
policy.cuda()
optimizer = make_optimizer(policy_class, policy)
train_history = []
validation_history = []
min_val_loss = np.inf
best_ckpt_info = None
for epoch in tqdm(range(num_epochs)):
print(f"\nEpoch {epoch}")
# validation
with torch.inference_mode():
policy.eval()
epoch_dicts = []
for batch_idx, data in enumerate(val_dataloader):
forward_dict = forward_pass(data, policy)
epoch_dicts.append(forward_dict)
epoch_summary = compute_dict_mean(epoch_dicts)
validation_history.append(epoch_summary)
epoch_val_loss = epoch_summary["loss"]
if epoch_val_loss < min_val_loss:
min_val_loss = epoch_val_loss
best_ckpt_info = (epoch, min_val_loss, deepcopy(policy.state_dict()))
print(f"Val loss: {epoch_val_loss:.5f}")
summary_string = ""
for k, v in epoch_summary.items():
summary_string += f"{k}: {v.item():.3f} "
print(summary_string)
# training
policy.train()
optimizer.zero_grad()
for batch_idx, data in enumerate(train_dataloader):
forward_dict = forward_pass(data, policy)
# backward
loss = forward_dict["loss"]
loss.backward()
optimizer.step()
optimizer.zero_grad()
train_history.append(detach_dict(forward_dict))
epoch_summary = compute_dict_mean(train_history[(batch_idx + 1) * epoch : (batch_idx + 1) * (epoch + 1)])
epoch_train_loss = epoch_summary["loss"]
print(f"Train loss: {epoch_train_loss:.5f}")
summary_string = ""
for k, v in epoch_summary.items():
summary_string += f"{k}: {v.item():.3f} "
print(summary_string)
if epoch % 100 == 0:
ckpt_path = os.path.join(ckpt_dir, f"policy_epoch_{epoch}_seed_{seed}.ckpt")
torch.save(policy.state_dict(), ckpt_path)
plot_history(train_history, validation_history, epoch, ckpt_dir, seed)
ckpt_path = os.path.join(ckpt_dir, "policy_last.ckpt")
torch.save(policy.state_dict(), ckpt_path)
best_epoch, min_val_loss, best_state_dict = best_ckpt_info
ckpt_path = os.path.join(ckpt_dir, f"policy_epoch_{best_epoch}_seed_{seed}.ckpt")
torch.save(best_state_dict, ckpt_path)
print(f"Training finished:\nSeed {seed}, val loss {min_val_loss:.6f} at epoch {best_epoch}")
# save training curves
plot_history(train_history, validation_history, num_epochs, ckpt_dir, seed)
return best_ckpt_info
def plot_history(train_history, validation_history, num_epochs, ckpt_dir, seed):
# save training curves
for key in train_history[0]:
plot_path = os.path.join(ckpt_dir, f"train_val_{key}_seed_{seed}.png")
plt.figure()
train_values = [summary[key].item() for summary in train_history]
val_values = [summary[key].item() for summary in validation_history]
plt.plot(
np.linspace(0, num_epochs - 1, len(train_history)),
train_values,
label="train",
)
plt.plot(
np.linspace(0, num_epochs - 1, len(validation_history)),
val_values,
label="validation",
)
# plt.ylim([-0.1, 1])
plt.tight_layout()
plt.legend()
plt.title(key)
plt.savefig(plot_path)
print(f"Saved plots to {ckpt_dir}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--eval", action="store_true")
parser.add_argument("--onscreen_render", action="store_true")
parser.add_argument("--render_height", action="store", type=int, default=480)
parser.add_argument("--render_width", action="store", type=int, default=640)
parser.add_argument("--ckpt_dir", action="store", type=str, help="ckpt_dir", required=True)
parser.add_argument(
"--policy_class",
action="store",
type=str,
help="policy_class, capitalize",
required=True,
)
parser.add_argument("--task_name", action="store", type=str, help="task_name", required=True)
parser.add_argument("--batch_size", action="store", type=int, help="batch_size", required=True)
parser.add_argument("--seed", action="store", type=int, help="seed", required=True)
parser.add_argument("--num_epochs", action="store", type=int, help="num_epochs", required=True)
parser.add_argument("--lr", action="store", type=float, help="lr", required=True)
# for ACT
parser.add_argument("--kl_weight", action="store", type=int, help="KL Weight", required=False)
parser.add_argument("--chunk_size", action="store", type=int, help="chunk_size", required=False)
parser.add_argument("--hidden_dim", action="store", type=int, help="hidden_dim", required=False)
parser.add_argument(
"--dim_feedforward",
action="store",
type=int,
help="dim_feedforward",
required=False,
)
parser.add_argument("--temporal_agg", action="store_true")
main(vars(parser.parse_args()))