-
Notifications
You must be signed in to change notification settings - Fork 5
/
eval.py
266 lines (238 loc) · 11.1 KB
/
eval.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
import os
import time
import json
import torch
import argparse
import numpy as np
import open3d as o3d
import torch.nn as nn
import MinkowskiEngine as ME
import matplotlib.pyplot as plt
import torch.distributed as dist
from tqdm import tqdm
from copy import deepcopy
from easydict import EasyDict as edict
from diffusers.optimization import get_cosine_schedule_with_warmup
from policy import RISE
from eval_agent import Agent
from utils.constants import *
from utils.training import set_seed
from dataset.projector import Projector
from utils.ensemble import EnsembleBuffer
from utils.transformation import rotation_transform
default_args = edict({
"ckpt": None,
"calib": "calib/",
"num_action": 20,
"num_inference_step": 20,
"voxel_size": 0.005,
"obs_feature_dim": 512,
"hidden_dim": 512,
"nheads": 8,
"num_encoder_layers": 4,
"num_decoder_layers": 1,
"dim_feedforward": 2048,
"dropout": 0.1,
"max_steps": 300,
"seed": 233,
"vis": False,
"discretize_rotation": True,
"ensemble_mode": "new"
})
def create_point_cloud(colors, depths, cam_intrinsics, voxel_size = 0.005):
"""
color, depth => point cloud
"""
h, w = depths.shape
fx, fy = cam_intrinsics[0, 0], cam_intrinsics[1, 1]
cx, cy = cam_intrinsics[0, 2], cam_intrinsics[1, 2]
colors = o3d.geometry.Image(colors.astype(np.uint8))
depths = o3d.geometry.Image(depths.astype(np.float32))
camera_intrinsics = o3d.camera.PinholeCameraIntrinsic(
width = w, height = h, fx = fx, fy = fy, cx = cx, cy = cy
)
rgbd = o3d.geometry.RGBDImage.create_from_color_and_depth(
colors, depths, depth_scale = 1.0, convert_rgb_to_intensity = False
)
cloud = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd, camera_intrinsics)
cloud = cloud.voxel_down_sample(voxel_size)
points = np.array(cloud.points).astype(np.float32)
colors = np.array(cloud.colors).astype(np.float32)
x_mask = ((points[:, 0] >= WORKSPACE_MIN[0]) & (points[:, 0] <= WORKSPACE_MAX[0]))
y_mask = ((points[:, 1] >= WORKSPACE_MIN[1]) & (points[:, 1] <= WORKSPACE_MAX[1]))
z_mask = ((points[:, 2] >= WORKSPACE_MIN[2]) & (points[:, 2] <= WORKSPACE_MAX[2]))
mask = (x_mask & y_mask & z_mask)
points = points[mask]
colors = colors[mask]
# imagenet normalization
colors = (colors - IMG_MEAN) / IMG_STD
# final cloud
cloud_final = np.concatenate([points, colors], axis = -1).astype(np.float32)
return cloud_final
def create_batch(coords, feats):
"""
coords, feats => batch coords, batch feats (batch size = 1)
"""
coords_batch = [coords]
feats_batch = [feats]
coords_batch, feats_batch = ME.utils.sparse_collate(coords_batch, feats_batch)
return coords_batch, feats_batch
def create_input(colors, depths, cam_intrinsics, voxel_size = 0.005):
"""
colors, depths => batch coords, batch feats
"""
cloud = create_point_cloud(colors, depths, cam_intrinsics, voxel_size = voxel_size)
coords = np.ascontiguousarray(cloud[:, :3] / voxel_size, dtype = np.int32)
coords_batch, feats_batch = create_batch(coords, cloud)
return coords_batch, feats_batch, cloud
def unnormalize_action(action):
action[..., :3] = (action[..., :3] + 1) / 2.0 * (TRANS_MAX - TRANS_MIN) + TRANS_MIN
action[..., -1] = (action[..., -1] + 1) / 2.0 * MAX_GRIPPER_WIDTH
return action
def rot_diff(rot1, rot2):
rot1_mat = rotation_transform(
rot1,
from_rep = "rotation_6d",
to_rep = "matrix"
)
rot2_mat = rotation_transform(
rot2,
from_rep = "rotation_6d",
to_rep = "matrix"
)
diff = rot1_mat @ rot2_mat.T
diff = np.diag(diff).sum()
diff = min(max((diff - 1) / 2.0, -1), 1)
return np.arccos(diff)
def discretize_rotation(rot_begin, rot_end, rot_step_size = np.pi / 16):
n_step = int(rot_diff(rot_begin, rot_end) // rot_step_size) + 1
rot_steps = []
for i in range(n_step):
rot_i = rot_begin * (n_step - 1 - i) / n_step + rot_end * (i + 1) / n_step
rot_steps.append(rot_i)
return rot_steps
def evaluate(args_override):
# load default arguments
args = deepcopy(default_args)
for key, value in args_override.items():
args[key] = value
# set up device
set_seed(args.seed)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# policy
print("Loading policy ...")
policy = RISE(
num_action = args.num_action,
input_dim = 6,
obs_feature_dim = args.obs_feature_dim,
action_dim = 10,
hidden_dim = args.hidden_dim,
nheads = args.nheads,
num_encoder_layers = args.num_encoder_layers,
num_decoder_layers = args.num_decoder_layers,
dropout = args.dropout
).to(device)
n_parameters = sum(p.numel() for p in policy.parameters() if p.requires_grad)
print("Number of parameters: {:.2f}M".format(n_parameters / 1e6))
# load checkpoint
assert args.ckpt is not None, "Please provide the checkpoint to evaluate."
policy.load_state_dict(torch.load(args.ckpt, map_location = device), strict = False)
print("Checkpoint {} loaded.".format(args.ckpt))
# evaluation
agent = Agent(
robot_ip = "192.168.2.100",
pc_ip = "192.168.2.35",
gripper_port = "/dev/ttyUSB0",
camera_serial = "750612070851"
)
projector = Projector(args.calib)
ensemble_buffer = EnsembleBuffer(mode = args.ensemble_mode)
if args.discretize_rotation:
last_rot = np.array(agent.ready_rot_6d, dtype = np.float32)
with torch.inference_mode():
policy.eval()
prev_width = None
for t in range(args.max_steps):
if t % args.num_inference_step == 0:
# pre-process inputs
colors, depths = agent.get_observation()
coords, feats, cloud = create_input(
colors,
depths,
cam_intrinsics = agent.intrinsics,
voxel_size = args.voxel_size
)
feats, coords = feats.to(device), coords.to(device)
cloud_data = ME.SparseTensor(feats, coords)
# predict
pred_raw_action = policy(cloud_data, actions = None, batch_size = 1).squeeze(0).cpu().numpy()
# unnormalize predicted actions
action = unnormalize_action(pred_raw_action)
# visualization
if args.vis:
import open3d as o3d
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(cloud[:, :3])
pcd.colors = o3d.utility.Vector3dVector(cloud[:, 3:] * IMG_STD + IMG_MEAN)
tcp_vis_list = []
for raw_tcp in action:
tcp_vis = o3d.geometry.TriangleMesh.create_sphere(0.01).translate(raw_tcp[:3])
tcp_vis_list.append(tcp_vis)
o3d.visualization.draw_geometries([pcd, *tcp_vis_list])
# project action to base coordinate
action_tcp = projector.project_tcp_to_base_coord(action[..., :-1], cam = agent.camera_serial, rotation_rep = "rotation_6d")
action_width = action[..., -1]
# safety insurance
action_tcp[..., :3] = np.clip(action_tcp[..., :3], SAFE_WORKSPACE_MIN + SAFE_EPS, SAFE_WORKSPACE_MAX - SAFE_EPS)
# full actions
action = np.concatenate([action_tcp, action_width[..., np.newaxis]], axis = -1)
# add to ensemble buffer
ensemble_buffer.add_action(action, t)
# get step action from ensemble buffer
step_action = ensemble_buffer.get_action()
if step_action is None: # no action in the buffer => no movement.
continue
step_tcp = step_action[:-1]
step_width = step_action[-1]
# send tcp pose to robot
if args.discretize_rotation:
rot_steps = discretize_rotation(last_rot, step_tcp[3:], np.pi / 16)
last_rot = step_tcp[3:]
for rot in rot_steps:
step_tcp[3:] = rot
agent.set_tcp_pose(
step_tcp,
rotation_rep = "rotation_6d",
blocking = True
)
else:
agent.set_tcp_pose(
step_tcp,
rotation_rep = "rotation_6d",
blocking = True
)
# send gripper width to gripper (thresholding to avoid repeating sending signals to gripper)
if prev_width is None or abs(prev_width - step_width) > GRIPPER_THRESHOLD:
agent.set_gripper_width(step_width, blocking = True)
prev_width = step_width
agent.stop()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--ckpt', action = 'store', type = str, help = 'checkpoint path', required = True)
parser.add_argument('--calib', action = 'store', type = str, help = 'calibration path', required = True)
parser.add_argument('--num_action', action = 'store', type = int, help = 'number of action steps', required = False, default = 20)
parser.add_argument('--num_inference_step', action = 'store', type = int, help = 'number of inference query steps', required = False, default = 20)
parser.add_argument('--voxel_size', action = 'store', type = float, help = 'voxel size', required = False, default = 0.005)
parser.add_argument('--obs_feature_dim', action = 'store', type = int, help = 'observation feature dimension', required = False, default = 512)
parser.add_argument('--hidden_dim', action = 'store', type = int, help = 'hidden dimension', required = False, default = 512)
parser.add_argument('--nheads', action = 'store', type = int, help = 'number of heads', required = False, default = 8)
parser.add_argument('--num_encoder_layers', action = 'store', type = int, help = 'number of encoder layers', required = False, default = 4)
parser.add_argument('--num_decoder_layers', action = 'store', type = int, help = 'number of decoder layers', required = False, default = 1)
parser.add_argument('--dim_feedforward', action = 'store', type = int, help = 'feedforward dimension', required = False, default = 2048)
parser.add_argument('--dropout', action = 'store', type = float, help = 'dropout ratio', required = False, default = 0.1)
parser.add_argument('--max_steps', action = 'store', type = int, help = 'max steps for evaluation', required = False, default = 300)
parser.add_argument('--seed', action = 'store', type = int, help = 'seed', required = False, default = 233)
parser.add_argument('--vis', action = 'store_true', help = 'add visualization during evaluation')
parser.add_argument('--discretize_rotation', action = 'store_true', help = 'whether to discretize rotation process.')
parser.add_argument('--ensemble_mode', action = 'store', type = str, help = 'temporal ensemble mode', required = False, default = 'new')
evaluate(vars(parser.parse_args()))