-
Notifications
You must be signed in to change notification settings - Fork 132
/
eval_real.py
581 lines (516 loc) · 27.1 KB
/
eval_real.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"""
Usage:
(umi): python scripts_real/eval_real_umi.py -i data/outputs/2023.10.26/02.25.30_train_diffusion_unet_timm_umi/checkpoints/latest.ckpt -o data_local/cup_test_data
================ Human in control ==============
Robot movement:
Move your SpaceMouse to move the robot EEF (locked in xy plane).
Press SpaceMouse right button to unlock z axis.
Press SpaceMouse left button to enable rotation axes.
Recording control:
Click the opencv window (make sure it's in focus).
Press "C" to start evaluation (hand control over to policy).
Press "Q" to exit program.
================ Policy in control ==============
Make sure you can hit the robot hardware emergency-stop button quickly!
Recording control:
Press "S" to stop evaluation and gain control back.
"""
# %%
import os
import pathlib
import time
from multiprocessing.managers import SharedMemoryManager
import av
import click
import cv2
import yaml
import dill
import hydra
import numpy as np
import scipy.spatial.transform as st
import torch
from omegaconf import OmegaConf
import json
from diffusion_policy.common.replay_buffer import ReplayBuffer
from diffusion_policy.common.cv2_util import (
get_image_transform
)
from umi.common.cv_util import (
parse_fisheye_intrinsics,
FisheyeRectConverter
)
from diffusion_policy.common.pytorch_util import dict_apply
from diffusion_policy.workspace.base_workspace import BaseWorkspace
from umi.common.precise_sleep import precise_wait
from umi.real_world.bimanual_umi_env import BimanualUmiEnv
from umi.real_world.keystroke_counter import (
KeystrokeCounter, Key, KeyCode
)
from umi.real_world.real_inference_util import (get_real_obs_dict,
get_real_obs_resolution,
get_real_umi_obs_dict,
get_real_umi_action)
from umi.real_world.spacemouse_shared_memory import Spacemouse
from umi.common.pose_util import pose_to_mat, mat_to_pose
OmegaConf.register_new_resolver("eval", eval, replace=True)
def solve_table_collision(ee_pose, gripper_width, height_threshold):
finger_thickness = 25.5 / 1000
keypoints = list()
for dx in [-1, 1]:
for dy in [-1, 1]:
keypoints.append((dx * gripper_width / 2, dy * finger_thickness / 2, 0))
keypoints = np.asarray(keypoints)
rot_mat = st.Rotation.from_rotvec(ee_pose[3:6]).as_matrix()
transformed_keypoints = np.transpose(rot_mat @ np.transpose(keypoints)) + ee_pose[:3]
delta = max(height_threshold - np.min(transformed_keypoints[:, 2]), 0)
ee_pose[2] += delta
def solve_sphere_collision(ee_poses, robots_config):
num_robot = len(robots_config)
this_that_mat = np.identity(4)
this_that_mat[:3, 3] = np.array([0, 0.89, 0]) # TODO: very hacky now!!!!
for this_robot_idx in range(num_robot):
for that_robot_idx in range(this_robot_idx + 1, num_robot):
this_ee_mat = pose_to_mat(ee_poses[this_robot_idx][:6])
this_sphere_mat_local = np.identity(4)
this_sphere_mat_local[:3, 3] = np.asarray(robots_config[this_robot_idx]['sphere_center'])
this_sphere_mat_global = this_ee_mat @ this_sphere_mat_local
this_sphere_center = this_sphere_mat_global[:3, 3]
that_ee_mat = pose_to_mat(ee_poses[that_robot_idx][:6])
that_sphere_mat_local = np.identity(4)
that_sphere_mat_local[:3, 3] = np.asarray(robots_config[that_robot_idx]['sphere_center'])
that_sphere_mat_global = this_that_mat @ that_ee_mat @ that_sphere_mat_local
that_sphere_center = that_sphere_mat_global[:3, 3]
distance = np.linalg.norm(that_sphere_center - this_sphere_center)
threshold = robots_config[this_robot_idx]['sphere_radius'] + robots_config[that_robot_idx]['sphere_radius']
# print(that_sphere_center, this_sphere_center)
if distance < threshold:
print('avoid collision between two arms')
half_delta = (threshold - distance) / 2
normal = (that_sphere_center - this_sphere_center) / distance
this_sphere_mat_global[:3, 3] -= half_delta * normal
that_sphere_mat_global[:3, 3] += half_delta * normal
ee_poses[this_robot_idx][:6] = mat_to_pose(this_sphere_mat_global @ np.linalg.inv(this_sphere_mat_local))
ee_poses[that_robot_idx][:6] = mat_to_pose(np.linalg.inv(this_that_mat) @ that_sphere_mat_global @ np.linalg.inv(that_sphere_mat_local))
@click.command()
@click.option('--input', '-i', required=True, help='Path to checkpoint')
@click.option('--output', '-o', required=True, help='Directory to save recording')
@click.option('--robot_config', '-rc', required=True, help='Path to robot_config yaml file')
@click.option('--match_dataset', '-m', default=None, help='Dataset used to overlay and adjust initial condition')
@click.option('--match_episode', '-me', default=None, type=int, help='Match specific episode from the match dataset')
@click.option('--match_camera', '-mc', default=0, type=int)
@click.option('--camera_reorder', '-cr', default='0')
@click.option('--vis_camera_idx', default=0, type=int, help="Which RealSense camera to visualize.")
@click.option('--init_joints', '-j', is_flag=True, default=False, help="Whether to initialize robot joint configuration in the beginning.")
@click.option('--steps_per_inference', '-si', default=6, type=int, help="Action horizon for inference.")
@click.option('--max_duration', '-md', default=2000000, help='Max duration for each epoch in seconds.')
@click.option('--frequency', '-f', default=10, type=float, help="Control frequency in Hz.")
@click.option('--command_latency', '-cl', default=0.01, type=float, help="Latency between receiving SapceMouse command to executing on Robot in Sec.")
@click.option('-nm', '--no_mirror', is_flag=True, default=False)
@click.option('-sf', '--sim_fov', type=float, default=None)
@click.option('-ci', '--camera_intrinsics', type=str, default=None)
@click.option('--mirror_swap', is_flag=True, default=False)
def main(input, output, robot_config,
match_dataset, match_episode, match_camera,
camera_reorder,
vis_camera_idx, init_joints,
steps_per_inference, max_duration,
frequency, command_latency,
no_mirror, sim_fov, camera_intrinsics, mirror_swap):
max_gripper_width = 0.09
gripper_speed = 0.2
# load robot config file
robot_config_data = yaml.safe_load(open(os.path.expanduser(robot_config), 'r'))
# load left-right robot relative transform
tx_left_right = np.array(robot_config_data['tx_left_right'])
tx_robot1_robot0 = tx_left_right
robots_config = robot_config_data['robots']
grippers_config = robot_config_data['grippers']
# load checkpoint
ckpt_path = input
if not ckpt_path.endswith('.ckpt'):
ckpt_path = os.path.join(ckpt_path, 'checkpoints', 'latest.ckpt')
payload = torch.load(open(ckpt_path, 'rb'), map_location='cpu', pickle_module=dill)
cfg = payload['cfg']
print("model_name:", cfg.policy.obs_encoder.model_name)
print("dataset_path:", cfg.task.dataset.dataset_path)
# setup experiment
dt = 1/frequency
obs_res = get_real_obs_resolution(cfg.task.shape_meta)
# load fisheye converter
fisheye_converter = None
if sim_fov is not None:
assert camera_intrinsics is not None
opencv_intr_dict = parse_fisheye_intrinsics(
json.load(open(camera_intrinsics, 'r')))
fisheye_converter = FisheyeRectConverter(
**opencv_intr_dict,
out_size=obs_res,
out_fov=sim_fov
)
print("steps_per_inference:", steps_per_inference)
with SharedMemoryManager() as shm_manager:
with Spacemouse(shm_manager=shm_manager) as sm, \
KeystrokeCounter() as key_counter, \
BimanualUmiEnv(
output_dir=output,
robots_config=robots_config,
grippers_config=grippers_config,
frequency=frequency,
obs_image_resolution=obs_res,
obs_float32=True,
camera_reorder=[int(x) for x in camera_reorder],
init_joints=init_joints,
enable_multi_cam_vis=True,
# latency
camera_obs_latency=0.17,
# obs
camera_obs_horizon=cfg.task.shape_meta.obs.camera0_rgb.horizon,
robot_obs_horizon=cfg.task.shape_meta.obs.robot0_eef_pos.horizon,
gripper_obs_horizon=cfg.task.shape_meta.obs.robot0_gripper_width.horizon,
no_mirror=no_mirror,
fisheye_converter=fisheye_converter,
mirror_swap=mirror_swap,
# action
max_pos_speed=2.0,
max_rot_speed=6.0,
shm_manager=shm_manager) as env:
cv2.setNumThreads(2)
print("Waiting for camera")
time.sleep(1.0)
# load match_dataset
episode_first_frame_map = dict()
match_replay_buffer = None
if match_dataset is not None:
match_dir = pathlib.Path(match_dataset)
match_zarr_path = match_dir.joinpath('replay_buffer.zarr')
match_replay_buffer = ReplayBuffer.create_from_path(str(match_zarr_path), mode='r')
match_video_dir = match_dir.joinpath('videos')
for vid_dir in match_video_dir.glob("*/"):
episode_idx = int(vid_dir.stem)
match_video_path = vid_dir.joinpath(f'{match_camera}.mp4')
if match_video_path.exists():
img = None
with av.open(str(match_video_path)) as container:
stream = container.streams.video[0]
for frame in container.decode(stream):
img = frame.to_ndarray(format='rgb24')
break
episode_first_frame_map[episode_idx] = img
print(f"Loaded initial frame for {len(episode_first_frame_map)} episodes")
# creating model
# have to be done after fork to prevent
# duplicating CUDA context with ffmpeg nvenc
cls = hydra.utils.get_class(cfg._target_)
workspace = cls(cfg)
workspace: BaseWorkspace
workspace.load_payload(payload, exclude_keys=None, include_keys=None)
policy = workspace.model
if cfg.training.use_ema:
policy = workspace.ema_model
policy.num_inference_steps = 16 # DDIM inference iterations
obs_pose_rep = cfg.task.pose_repr.obs_pose_repr
action_pose_repr = cfg.task.pose_repr.action_pose_repr
print('obs_pose_rep', obs_pose_rep)
print('action_pose_repr', action_pose_repr)
device = torch.device('cuda')
policy.eval().to(device)
print("Warming up policy inference")
obs = env.get_obs()
episode_start_pose = list()
for robot_id in range(len(robots_config)):
pose = np.concatenate([
obs[f'robot{robot_id}_eef_pos'],
obs[f'robot{robot_id}_eef_rot_axis_angle']
], axis=-1)[-1]
episode_start_pose.append(pose)
with torch.no_grad():
policy.reset()
obs_dict_np = get_real_umi_obs_dict(
env_obs=obs, shape_meta=cfg.task.shape_meta,
obs_pose_repr=obs_pose_rep,
tx_robot1_robot0=tx_robot1_robot0,
episode_start_pose=episode_start_pose)
obs_dict = dict_apply(obs_dict_np,
lambda x: torch.from_numpy(x).unsqueeze(0).to(device))
result = policy.predict_action(obs_dict)
action = result['action_pred'][0].detach().to('cpu').numpy()
assert action.shape[-1] == 10 * len(robots_config)
action = get_real_umi_action(action, obs, action_pose_repr)
assert action.shape[-1] == 7 * len(robots_config)
del result
print('Ready!')
while True:
# ========= human control loop ==========
print("Human in control!")
robot_states = env.get_robot_state()
target_pose = np.stack([rs['TargetTCPPose'] for rs in robot_states])
gripper_states = env.get_gripper_state()
gripper_target_pos = np.asarray([gs['gripper_position'] for gs in gripper_states])
control_robot_idx_list = [0]
t_start = time.monotonic()
iter_idx = 0
while True:
# calculate timing
t_cycle_end = t_start + (iter_idx + 1) * dt
t_sample = t_cycle_end - command_latency
t_command_target = t_cycle_end + dt
# pump obs
obs = env.get_obs()
# visualize
episode_id = env.replay_buffer.n_episodes
vis_img = obs[f'camera{match_camera}_rgb'][-1]
match_episode_id = episode_id
if match_episode is not None:
match_episode_id = match_episode
if match_episode_id in episode_first_frame_map:
match_img = episode_first_frame_map[match_episode_id]
ih, iw, _ = match_img.shape
oh, ow, _ = vis_img.shape
tf = get_image_transform(
input_res=(iw, ih),
output_res=(ow, oh),
bgr_to_rgb=False)
match_img = tf(match_img).astype(np.float32) / 255
vis_img = (vis_img + match_img) / 2
obs_left_img = obs['camera0_rgb'][-1]
obs_right_img = obs['camera0_rgb'][-1]
vis_img = np.concatenate([obs_left_img, obs_right_img, vis_img], axis=1)
text = f'Episode: {episode_id}'
cv2.putText(
vis_img,
text,
(10,20),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.5,
lineType=cv2.LINE_AA,
thickness=3,
color=(0,0,0)
)
cv2.putText(
vis_img,
text,
(10,20),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.5,
thickness=1,
color=(255,255,255)
)
cv2.imshow('default', vis_img[...,::-1])
_ = cv2.pollKey()
press_events = key_counter.get_press_events()
start_policy = False
for key_stroke in press_events:
if key_stroke == KeyCode(char='q'):
# Exit program
env.end_episode()
exit(0)
elif key_stroke == KeyCode(char='c'):
# Exit human control loop
# hand control over to the policy
start_policy = True
elif key_stroke == KeyCode(char='e'):
# Next episode
if match_episode is not None:
match_episode = min(match_episode + 1, env.replay_buffer.n_episodes-1)
elif key_stroke == KeyCode(char='w'):
# Prev episode
if match_episode is not None:
match_episode = max(match_episode - 1, 0)
elif key_stroke == KeyCode(char='m'):
# move the robot
duration = 3.0
ep = match_replay_buffer.get_episode(match_episode_id)
for robot_idx in range(1):
pos = ep[f'robot{robot_idx}_eef_pos'][0]
rot = ep[f'robot{robot_idx}_eef_rot_axis_angle'][0]
grip = ep[f'robot{robot_idx}_gripper_width'][0]
pose = np.concatenate([pos, rot])
env.robots[robot_idx].servoL(pose, duration=duration)
env.grippers[robot_idx].schedule_waypoint(grip, target_time=time.time() + duration)
target_pose[robot_idx] = pose
gripper_target_pos[robot_idx] = grip
time.sleep(duration)
elif key_stroke == Key.backspace:
if click.confirm('Are you sure to drop an episode?'):
env.drop_episode()
key_counter.clear()
elif key_stroke == KeyCode(char='a'):
control_robot_idx_list = list(range(target_pose.shape[0]))
elif key_stroke == KeyCode(char='1'):
control_robot_idx_list = [0]
elif key_stroke == KeyCode(char='2'):
control_robot_idx_list = [1]
if start_policy:
break
precise_wait(t_sample)
# get teleop command
sm_state = sm.get_motion_state_transformed()
# print(sm_state)
dpos = sm_state[:3] * (0.5 / frequency)
drot_xyz = sm_state[3:] * (1.5 / frequency)
drot = st.Rotation.from_euler('xyz', drot_xyz)
for robot_idx in control_robot_idx_list:
target_pose[robot_idx, :3] += dpos
target_pose[robot_idx, 3:] = (drot * st.Rotation.from_rotvec(
target_pose[robot_idx, 3:])).as_rotvec()
dpos = 0
if sm.is_button_pressed(0):
# close gripper
dpos = -gripper_speed / frequency
if sm.is_button_pressed(1):
dpos = gripper_speed / frequency
for robot_idx in control_robot_idx_list:
gripper_target_pos[robot_idx] = np.clip(gripper_target_pos[robot_idx] + dpos, 0, max_gripper_width)
# solve collision with table
for robot_idx in control_robot_idx_list:
solve_table_collision(
ee_pose=target_pose[robot_idx],
gripper_width=gripper_target_pos[robot_idx],
height_threshold=robots_config[robot_idx]['height_threshold'])
# solve collison between two robots
solve_sphere_collision(
ee_poses=target_pose,
robots_config=robots_config
)
action = np.zeros((7 * target_pose.shape[0],))
for robot_idx in range(target_pose.shape[0]):
action[7 * robot_idx + 0: 7 * robot_idx + 6] = target_pose[robot_idx]
action[7 * robot_idx + 6] = gripper_target_pos[robot_idx]
# execute teleop command
env.exec_actions(
actions=[action],
timestamps=[t_command_target-time.monotonic()+time.time()],
compensate_latency=False)
precise_wait(t_cycle_end)
iter_idx += 1
# ========== policy control loop ==============
try:
# start episode
policy.reset()
start_delay = 1.0
eval_t_start = time.time() + start_delay
t_start = time.monotonic() + start_delay
env.start_episode(eval_t_start)
# get current pose
obs = env.get_obs()
episode_start_pose = list()
for robot_id in range(len(robots_config)):
pose = np.concatenate([
obs[f'robot{robot_id}_eef_pos'],
obs[f'robot{robot_id}_eef_rot_axis_angle']
], axis=-1)[-1]
episode_start_pose.append(pose)
# wait for 1/30 sec to get the closest frame actually
# reduces overall latency
frame_latency = 1/60
precise_wait(eval_t_start - frame_latency, time_func=time.time)
print("Started!")
iter_idx = 0
perv_target_pose = None
while True:
# calculate timing
t_cycle_end = t_start + (iter_idx + steps_per_inference) * dt
# get obs
obs = env.get_obs()
obs_timestamps = obs['timestamp']
print(f'Obs latency {time.time() - obs_timestamps[-1]}')
# run inference
with torch.no_grad():
s = time.time()
obs_dict_np = get_real_umi_obs_dict(
env_obs=obs, shape_meta=cfg.task.shape_meta,
obs_pose_repr=obs_pose_rep,
tx_robot1_robot0=tx_robot1_robot0,
episode_start_pose=episode_start_pose)
obs_dict = dict_apply(obs_dict_np,
lambda x: torch.from_numpy(x).unsqueeze(0).to(device))
result = policy.predict_action(obs_dict)
raw_action = result['action_pred'][0].detach().to('cpu').numpy()
action = get_real_umi_action(raw_action, obs, action_pose_repr)
print('Inference latency:', time.time() - s)
# convert policy action to env actions
this_target_poses = action
assert this_target_poses.shape[1] == len(robots_config) * 7
for target_pose in this_target_poses:
for robot_idx in range(len(robots_config)):
solve_table_collision(
ee_pose=target_pose[robot_idx * 7: robot_idx * 7 + 6],
gripper_width=target_pose[robot_idx * 7 + 6],
height_threshold=robots_config[robot_idx]['height_threshold']
)
# solve collison between two robots
solve_sphere_collision(
ee_poses=target_pose.reshape([len(robots_config), -1]),
robots_config=robots_config
)
# deal with timing
# the same step actions are always the target for
action_timestamps = (np.arange(len(action), dtype=np.float64)
) * dt + obs_timestamps[-1]
print(dt)
action_exec_latency = 0.01
curr_time = time.time()
is_new = action_timestamps > (curr_time + action_exec_latency)
if np.sum(is_new) == 0:
# exceeded time budget, still do something
this_target_poses = this_target_poses[[-1]]
# schedule on next available step
next_step_idx = int(np.ceil((curr_time - eval_t_start) / dt))
action_timestamp = eval_t_start + (next_step_idx) * dt
print('Over budget', action_timestamp - curr_time)
action_timestamps = np.array([action_timestamp])
else:
this_target_poses = this_target_poses[is_new]
action_timestamps = action_timestamps[is_new]
# execute actions
env.exec_actions(
actions=this_target_poses,
timestamps=action_timestamps,
compensate_latency=True
)
print(f"Submitted {len(this_target_poses)} steps of actions.")
# visualize
episode_id = env.replay_buffer.n_episodes
obs_left_img = obs['camera0_rgb'][-1]
obs_right_img = obs['camera0_rgb'][-1]
vis_img = np.concatenate([obs_left_img, obs_right_img], axis=1)
text = 'Episode: {}, Time: {:.1f}'.format(
episode_id, time.monotonic() - t_start
)
cv2.putText(
vis_img,
text,
(10,20),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.5,
thickness=1,
color=(255,255,255)
)
cv2.imshow('default', vis_img[...,::-1])
_ = cv2.pollKey()
press_events = key_counter.get_press_events()
stop_episode = False
for key_stroke in press_events:
if key_stroke == KeyCode(char='s'):
# Stop episode
# Hand control back to human
print('Stopped.')
stop_episode = True
t_since_start = time.time() - eval_t_start
if t_since_start > max_duration:
print("Max Duration reached.")
stop_episode = True
if stop_episode:
env.end_episode()
break
# wait for execution
precise_wait(t_cycle_end - frame_latency)
iter_idx += steps_per_inference
except KeyboardInterrupt:
print("Interrupted!")
# stop robot.
env.end_episode()
print("Stopped.")
# %%
if __name__ == '__main__':
main()