forked from playertr/tsgrasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_grasps.py
306 lines (245 loc) · 9.92 KB
/
predict_grasps.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
"""
Implementation of grasp_synthesis.
Predicts 6DOF grasps on input point clouds.
Author: Tim Player, Marc Micatka
Note: torch.backends.cudnn.benchmark=True
makes a big difference on FPS for some PTS_PER_FRAME values,
but seems to increase memory usage and can result in OOM errors.
"""
# Standard Library
import os
import warnings
from typing import Optional, Tuple
import numpy as np
import torch
# Third-party
from omegaconf import OmegaConf
try:
from .tsgrasp.net.lit_tsgraspnet import LitTSGraspNet
from .tsgrasp_utils import (
build_6dof_grasps,
downsample_xyz,
eul_to_rotm,
generate_color_lookup,
infer_grasps,
)
except ImportError:
from tsgrasp.net.lit_tsgraspnet import LitTSGraspNet # type: ignore
from tsgrasp_utils import ( # type: ignore
build_6dof_grasps,
downsample_xyz,
eul_to_rotm,
generate_color_lookup,
infer_grasps,
)
# Suppresses a userwarning from kornia
warnings.filterwarnings("ignore", category=UserWarning)
class GraspPredictor:
"""
Node implementing tsgrasp network
"""
def __init__(self, model_metadata: dict, verbose: bool, pkg_root: str) -> None:
self.device = torch.device("cuda")
if model_metadata is None:
print("No Metadata Loaded!")
# Default params, if not in metadata
self.gripper_depth = 0.090
self.queue_len = 1
self.outlier_threshold = 0.00005
self.pts_per_frame = 45000
self.verbose = verbose
try:
model_path = os.path.join(pkg_root, model_metadata["ckpt_path"])
assert os.path.isfile(model_path)
# Other model params from YAML:
self.queue_len = model_metadata["queue_len"]
self.outlier_threshold = model_metadata["outlier_threshold"]
self.pts_per_frame = model_metadata["pts_per_frame"]
model_cfg = OmegaConf.create(model_metadata["model"]["model_cfg"])
training_cfg = OmegaConf.create(model_metadata["training"])
except KeyError as ex:
print(f"Key Error: {ex}")
self.color_lookup = generate_color_lookup()
self.pc_input_msg = None
self.py_grasps = None
self.tf_trans = [0, 0, 0]
self.tf_rot = [0, 0, np.pi / 2]
self.pl_model = LitTSGraspNet(model_cfg=model_cfg, training_cfg=training_cfg)
self.pl_model.load_state_dict(torch.load(model_path)["state_dict"])
# # load Pytorch Lightning network
self.pl_model.to(self.device)
self.pl_model.eval()
@torch.inference_mode()
def detect(self, pointcloud: np.array) -> Optional[Tuple]:
"""
Run grasp prediction on a single input
Args:
pointcloud (np.array): input pointcloud
Returns:
Tuple(np.array, np.array): Tuple of grasps pointcloud and a heatmap pointcloud (np.array)
"""
grasps_array: Optional[np.array] = None
cm_array: Optional[np.array] = None
try:
orig_pts = [pointcloud]
pts = [
torch.from_numpy(pt.astype(np.float32)).to(self.device)
for pt in orig_pts
]
except ValueError as ex:
print(f"Is this error because there are fewer than 300x300 points? - {ex}")
return (None, None)
pts = downsample_xyz(pts, self.pts_per_frame)
if pts is None or any(len(pcl) == 0 for pcl in pts):
if self.verbose:
print("No points found after downsampling!")
return (None, None)
all_grasps, all_confs, all_widths = self.identify_grasps(pts)
try:
all_grasps = self.ensure_grasp_y_axis_upward(all_grasps)
all_grasps = self.transform_to_eq_pose(all_grasps)
(grasps_array, cm_array) = self.generate_pc_data(
pts, all_grasps, all_confs, all_widths
)
return (grasps_array, cm_array)
except RuntimeError as ex:
print(f"Encountered Runtime Error! {ex}")
return (None, None)
def identify_grasps(self, pts):
"""
Identify grasps in point cloud pts
Args:
pts (torch.Tensor): _description_
Returns:
_type_: _description_
"""
try:
outputs = infer_grasps(
self.pl_model, pts, grid_size=self.pl_model.model.grid_size
)
class_logits, baseline_dir, approach_dir, grasp_offset, positions = outputs
grasps = build_6dof_grasps(
positions,
baseline_dir,
approach_dir,
grasp_offset,
gripper_depth=self.gripper_depth,
)
confs = torch.sigmoid(class_logits)
return grasps, confs, grasp_offset
except Exception as ex:
print(f"{ex}")
return None, None, None
def ensure_grasp_y_axis_upward(self, grasps: torch.Tensor) -> torch.Tensor:
"""
Flip grasps with their Y-axis pointing downwards by 180 degrees about the wrist (z) axis,
because we have mounted the camera on the wrist in the direction of the Y axis and don't
want it to be scraped off on the table.
Args:
grasps (torch.Tensor): (N, 4, 4) grasp pose tensor
Returns:
torch.Tensor: (N, 4, 4) grasp pose tensor with some grasps flipped
"""
ngrasps = len(grasps)
# The strategy here is to create a Boolean tensor for whether
# to flip the grasp. From the way we mounted our camera, we know that
# we'd prefer grasps with X axes that point up in the camera frame
# (along the -Y axis). Therefore, we flip the rotation matrices of the
# grasp poses that don't do that.
# For speed, the flipping is done by allocating two (N, 4, 4) transformation
# matrices: one for flipping (flips) and one for do-nothing (eyes). We select
# between them with torch.where and perform matrix multiplication. This avoids
# a for loop (~100X speedup) at the expense of a bit of memory and obfuscation.
y_axis = torch.tensor([0, 1, 0], dtype=torch.float32).to(self.device)
flip_about_z = torch.tensor(
[[-1, 0, 0], [0, -1, 0], [0, 0, 1]], dtype=torch.float32
).to(self.device)
needs_flipping = grasps[:, :3, 1] @ y_axis > 0
needs_flipping = needs_flipping.reshape(ngrasps, 1, 1).expand(ngrasps, 3, 3)
eyes = torch.eye(3).repeat((ngrasps, 1, 1)).to(self.device)
flips = flip_about_z.repeat((ngrasps, 1, 1)).to(self.device)
tfs = torch.where(needs_flipping, flips, eyes)
grasps[:, :3, :3] = torch.bmm(grasps[:, :3, :3], tfs)
return grasps
def transform_to_eq_pose(self, poses):
"""
Apply the static frame transformation between the network output and the
input expected by the servoing logic at /panda/cartesian_impendance_controller/equilibrium_pose.
The servoing pose is at the gripper pads, and is rotated, while the network output is at the wrist.
This is an *intrinsic* pose transformation, where each grasp pose moves a fixed amount relative to
its initial pose, so we right-multiply instead of left-multiply.
"""
roll, pitch, yaw = self.tf_rot
x, y, z = self.tf_trans
tf = torch.cat(
[
torch.cat(
[
eul_to_rotm(roll, pitch, yaw),
torch.Tensor([x, y, z]).reshape(3, 1),
],
dim=1,
),
torch.Tensor([0, 0, 0, 1]).reshape(1, 4),
],
dim=0,
).to(poses.device)
return poses @ tf
def generate_pc_data(
self, pts, all_grasps, all_confs, all_widths
) -> Tuple[np.array, np.array]:
"""
Returns point cloud of the grasps with confidences colormapped
Also includes pc with all the grasps and widths.
Args:
pts (torch.Tensor): x, y, z points
all_grasps (torch.Tensor): 4x4 pose matrix for each grasp
all_confs (torch.Tensor): Confidence float values for each grasp
all_widths (torch.Tensor): Width float values for each grasp
Returns:
Tuple[np.array, np.array]: pointcloud containing grasp information, colormapped pc
"""
cloud_points = pts[-1]
confs = all_confs.cpu().numpy()
widths = all_widths.cpu().numpy()
points = cloud_points.cpu().numpy()
npoints = len(points)
int_confs = np.round(confs * 255).astype(np.uint8).squeeze()
colors = self.color_lookup[int_confs]
cm_array = np.zeros(
(npoints,),
dtype=[
("x", np.float32),
("y", np.float32),
("z", np.float32),
("r", np.float32),
("g", np.float32),
("b", np.float32),
("a", np.float32),
],
)
cm_array["x"] = points[:, 0]
cm_array["y"] = points[:, 1]
cm_array["z"] = points[:, 2]
cm_array["r"] = colors[:, 0]
cm_array["g"] = colors[:, 1]
cm_array["b"] = colors[:, 2]
cm_array["a"] = colors[:, 3]
grasps_array = np.zeros(
(npoints,),
dtype=[
("x", np.float32),
("y", np.float32),
("z", np.float32),
("grasps", np.float32, (4, 4)),
("confidences", np.float32),
("widths", np.float32),
],
)
grasps_array["x"] = points[:, 0]
grasps_array["y"] = points[:, 1]
grasps_array["z"] = points[:, 2]
grasps_array["grasps"] = all_grasps.cpu().numpy()
grasps_array["confidences"] = confs[:, 0]
grasps_array["widths"] = widths[:, 0]
return (grasps_array, cm_array)