-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils_real.py
139 lines (116 loc) · 5.74 KB
/
utils_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
import time
from utils import preprocess_pcd,preprocess_pcd_from_canon, postprocess_pcd, unpad_points
import numpy as np
import wandb
def rollout_policy_real(env, policy, urdf, cfg, render=True, from_state=True, expert_policy=None):
policy.eval()
actions = []
states = []
joints = []
cont_actions = []
all_pcds_points = []
all_pcds_colors = []
all_pcds_points_full = []
all_pcds_colors_full = []
expert_actions = []
images = []
debug = True
state = env.reset()
joint = env.get_robot_joints()
start_demo =time.time()
for t in range(cfg['max_path_length']):
start_timestep = time.time()
if render:
start = time.time()
img, pcd = env.render_image(cfg["sensors"])
print("Rendering pcd image", time.time()-start)
points, colors = pcd
points = points.reshape((1, -1, 3))
colors = colors.reshape((1, -1, 3))
start_prc = time.time()
# check last joint
if cfg["presample_arm_pcd"]:
# this is faster (doesn't re-sample the points from the mesh each time)
pcd_processed_points = preprocess_pcd_from_canon(pcd, joint, urdf, urdf.canonical_link_pcds, cfg)
else:
pcd_processed_points = preprocess_pcd(pcd, joint, urdf, cfg)
print("End proc ", time.time() - start_prc)
start_prc = time.time()
pcd_processed_points_full, pcd_processed_colors_full = postprocess_pcd(pcd_processed_points, cfg)
print("end proc 2", time.time() - start_prc)
print("state", state)
# pcd_processed_points_full, pcd_processed_colors_full, pcd_processed_points, pcd_processed_colors = process_pcd((points, colors), joint, urdf, cfg)
# import IPython
# IPython.embed()
if t == 0:
import open3d as o3d
pcd1 = o3d.geometry.PointCloud()
pcd1.points = o3d.utility.Vector3dVector(unpad_points(pcd_processed_points_full[0]))
o3d.visualization.draw_geometries([pcd1])
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd1)
vis.remove_geometry(pcd1)
pcd1 = o3d.geometry.PointCloud()
pcd1.points = o3d.utility.Vector3dVector(unpad_points(pcd_processed_points_full[0]))
vis.add_geometry(pcd1)
vis.poll_events()
vis.update_renderer()
if from_state:
observation = env.observation(state)
action = policy.act_vectorized(observation, observation)
else:
state = state.reshape(1, -1)
if cfg["rnn"]:
with torch.no_grad():
pcd_processed_points_full_par, pcd_processed_colors_full_par, state_par = np.expand_dims(pcd_processed_points_full, axis=1), np.expand_dims(pcd_processed_colors_full, axis=1), state.unsqueeze(1)
action = policy((pcd_processed_points_full_par, pcd_processed_colors_full_par, state_par), init_belief=(t==0)).detach().squeeze().argmax(dim=1).cpu().numpy()
else:
action = policy((pcd_processed_points_full, pcd_processed_colors_full, state)).argmax(dim=1).cpu().numpy()
if expert_policy:
expert_action = expert_policy.act_vectorized(observation, observation)
expert_actions.append(expert_action)
if render:
all_pcds_points.append(pcd_processed_points)
all_pcds_colors.append([])
all_pcds_points_full.append(pcd_processed_points_full)
all_pcds_colors_full.append(pcd_processed_colors_full)
images.append(img)
if expert_policy and np.random.random() < cfg["sampling_expert"] :
action = expert_action
actions.append(action)
states.append(state)
joints.append(joint.detach().cpu().numpy())
print("Forward pass", time.time() - start_timestep)
state, _, done , info = env.step(action)
joint = info["robot_joints"]
if cfg["reset_if_open"] and action == 12:
print("resetting because open")
break
print("sum actions open", np.sum(np.array(actions) == 12))
if action == 12 and np.sum(np.array(actions) == 12) > cfg["nb_open"]:
print("resetting because too ,many open")
break
# cont_action = info["cont_action"].detach().cpu().numpy()
# cont_actions.append(cont_action)
# success = env.base_env._env.get_success().detach().cpu().numpy()
print(f"Trajectory took {time.time() - start_demo}")
actions = np.array(actions).transpose(1,0)
# cont_actions = np.array(cont_actions).transpose(1,0,2)
# states = np.array(states).transpose(1,0,2)
# joints = np.array(joints).transpose(1,0,2)
if expert_policy:
expert_actions = np.array(expert_actions).transpose(1,0)
else:
expert_actions = None
if render:
all_pcd_points = np.array(all_pcds_points).transpose(1,0,2,3) # TODO TRANSPOSE
all_pcd_colors = np.ones_like(all_pcd_points)
all_pcd_colors = all_pcd_colors[...,0]
all_pcd_colors = all_pcd_colors[...,None]
all_pcd_points_full = np.array(all_pcds_points_full).transpose(1,0,2,3) # TODO TRANSPOSE
all_pcd_colors_full = np.array(all_pcds_colors_full).transpose(1,0,2,3) # TODO TRANSPOSE
images = np.array(images).transpose(1,0,2,3,4)
success = np.array([False])
wandb.log({"Success": np.mean(success), "Time/Trajectory": time.time() - start_demo})
return actions, cont_actions, states, joints, all_pcd_points_full, all_pcd_colors_full, all_pcd_points, all_pcd_colors, images, expert_actions, success