forked from THU-VCLab/HGGD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
311 lines (276 loc) · 13.6 KB
/
demo.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
import argparse
import random
import numpy as np
import open3d as o3d
import torch
import torch.nn.functional as F
from matplotlib import pyplot as plt
from PIL import Image
from dataset.evaluation import anchor_output_process, collision_detect, detect_2d_grasp, detect_6d_grasp_multi
from dataset.pc_dataset_tools import data_process, feature_fusion
from dataset.utils import PointCloudHelper
from models.anchornet import AnchorGraspNet, Backbone, AnchorNetOriginal
from models.localgraspnet import LocalNetOriginal, PointMultiGraspNet
from models.pointnet import PointNetfeat
parser = argparse.ArgumentParser()
parser.add_argument('--checkpoint-path', default='resources/checkpoint')
parser.add_argument('--random-seed', type=int, default=1)
# image input
parser.add_argument('--rgb-path', default='resources/demo_rgb.png')
parser.add_argument('--depth-path', default='resources/demo_depth.png')
parser.add_argument('--input-h', type=int, default=160) # target height of input image
parser.add_argument('--input-w', type=int, default=320) # target width of input image
parser.add_argument('--export-onnx', type=bool, default=False) # export .onnx files
# 2d grasping
parser.add_argument('--sigma', type=int, default=10)
parser.add_argument('--ratio', type=int, default=8) # grasp-attributes prediction downsample ratio, must be 2^N
parser.add_argument('--anchor-k', type=int, default=6) # in-plane rotation anchor number
parser.add_argument('--anchor-w', type=float, default=50.0) # grasp width anchor size
parser.add_argument('--anchor-z', type=float, default=20.0) # grasp depth anchor size
parser.add_argument('--grid-size', type=int, default=8) # grid size for grid-based center sampling
parser.add_argument('--feature-dim', type=int, default=128) # feature dimension for anchornet
# 6d grasping
parser.add_argument('--heatmap-thres', type=float, default=0.01) # heatmap threshold
parser.add_argument('--local-k', type=int, default=10) # grasp detection number in each local region (localnet)
parser.add_argument('--depth-thres', type=float, default=0.02) # depth threshold for collision detection
parser.add_argument('--max-points', type=int, default=25600) # downsampled max number of points in pc
parser.add_argument('--anchor-num', type=int, default=7) # spatial rotation anchor number
parser.add_argument('--center-num', type=int, default=64) # sampled local center/region number (how many grasps are predicted)
parser.add_argument('--group-num', type=int, default=512) # local region pc number
args = parser.parse_args()
# --------------------------------------------------------------------------- #
if __name__ == '__main__':
# set torch and gpu setting
np.set_printoptions(precision=4, suppress=True)
torch.set_printoptions(precision=4, sci_mode=False)
gpu = torch.cuda.is_available()
if gpu:
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
# random seed
random.seed(args.random_seed)
np.random.seed(args.random_seed)
torch.manual_seed(args.random_seed)
# init the model
ori_anchormodel = AnchorNetOriginal(feature_dim=args.feature_dim, ratio=args.ratio, anchor_k=args.anchor_k, in_dim=4, mode='34')
ori_localmodel = LocalNetOriginal(info_size=3, k_cls=args.anchor_num**2, feature_len=3)
resnet = Backbone(in_dim=4, planes=args.feature_dim//16, mode='34')
anchornet = AnchorGraspNet(feature_dim=args.feature_dim, ratio=args.ratio, anchor_k=args.anchor_k)
pointnet = PointNetfeat(feature_len=3)
localnet = PointMultiGraspNet(info_size=3, k_cls=args.anchor_num**2)
if gpu:
resnet = resnet.cuda()
anchornet = anchornet.cuda()
pointnet = pointnet.cuda()
localnet = localnet.cuda()
# load weights from original checkpoint
checkpoint = torch.load(args.checkpoint_path, weights_only=True, map_location=torch.device('cpu'))
ori_anchormodel.load_state_dict(checkpoint['anchor'])
ori_localmodel.load_state_dict(checkpoint['local'])
# transfer weights to new models
resnet.load_state_dict(ori_anchormodel.backbone.state_dict())
anchornet.load_state_dict(
ori_anchormodel.trconv.state_dict(prefix='trconv.') |
ori_anchormodel.hmap.state_dict(prefix='hmap.') |
ori_anchormodel.cls_mask_conv.state_dict(prefix='cls_mask_conv.') |
ori_anchormodel.theta_offset_conv.state_dict(prefix='theta_offset_conv.') |
ori_anchormodel.width_offset_conv.state_dict(prefix='width_offset_conv.') |
ori_anchormodel.depth_offset_conv.state_dict(prefix='depth_offset_conv.')
)
pointnet.load_state_dict(ori_localmodel.pointnet.state_dict())
localnet.load_state_dict(
ori_localmodel.point_layer.state_dict(prefix='point_layer.') |
ori_localmodel.info_layer.state_dict(prefix='info_layer.') |
ori_localmodel.anchor_mlp.state_dict(prefix='anchor_mlp.') |
ori_localmodel.offset_mlp.state_dict(prefix='offset_mlp.')
)
# load anchors
basic_ranges = torch.linspace(-1, 1, args.anchor_num + 1)
if gpu:
basic_ranges = basic_ranges.cuda()
basic_anchors = (basic_ranges[1:] + basic_ranges[:-1]) / 2
anchors = {'gamma': basic_anchors, 'beta': basic_anchors}
anchors['gamma'] = checkpoint['gamma']
anchors['beta'] = checkpoint['beta']
print('-> loaded checkpoint %s ' % (args.checkpoint_path))
# network eval mode
resnet.eval()
anchornet.eval()
pointnet.eval()
localnet.eval()
# read image
ori_depth = np.array(Image.open(args.depth_path))
ori_rgb = np.array(Image.open(args.rgb_path)) / 255.0
ori_depth = np.clip(ori_depth, 0, 1000)
ori_rgb = torch.from_numpy(ori_rgb).permute(2, 1, 0)[None]
ori_rgb = ori_rgb.to(device='cuda' if gpu else 'cpu', dtype=torch.float32)
ori_depth = torch.from_numpy(ori_depth).T[None]
ori_depth = ori_depth.to(device='cuda' if gpu else 'cpu', dtype=torch.float32)
# create pc for 3d visualization
pc_helper = PointCloudHelper(args.max_points, (args.input_w, args.input_h))
view_points, _, _ = pc_helper.to_scene_points(ori_rgb, ori_depth)
xyzs = pc_helper.to_xyz_maps(ori_depth)
# generate 2d input (downscale image and normalize depth)
rgb = F.interpolate(ori_rgb, (args.input_w, args.input_h))
depth = F.interpolate(ori_depth[None], (args.input_w, args.input_h))[0]
depth = depth / 1000.0
depth = torch.clip((depth - depth.mean()), -1, 1)
x = torch.concat([depth[None], rgb], 1)
x = x.to(device='cuda' if gpu else 'cpu', dtype=torch.float32)
# inference
with torch.no_grad():
# use ResNet to get downscaled features
xs = resnet(x)
# 2d prediction (heatmap + 2d grasp attributes)
pred_2d, perpoint_features = anchornet(xs)
loc_map, cls_mask, theta_offset, height_offset, width_offset = anchor_output_process(*pred_2d, sigma=args.sigma)
# detect 2d grasps
rect_gg = detect_2d_grasp(loc_map,
cls_mask,
theta_offset,
height_offset,
width_offset,
ratio=args.ratio,
anchor_k=args.anchor_k,
anchor_w=args.anchor_w,
anchor_z=args.anchor_z,
mask_thre=args.heatmap_thres,
center_num=args.center_num,
grid_size=args.grid_size,
grasp_nms=args.grid_size)
print("# 2d grasps found:", rect_gg.size)
# check if a grasp is found
assert rect_gg.size != 0, "No 2d grasp found"
# show heatmap
rgb_t = x[0, 1:].cpu().numpy().squeeze().transpose(2, 1, 0)
depth_t = ori_depth.cpu().numpy().squeeze().T / 1000.0
resized_rgb = Image.fromarray((rgb_t * 255.0).astype(np.uint8))
resized_rgb = np.array(resized_rgb.resize((args.input_w, args.input_h))) / 255.0
rect_rgb = rect_gg.plot_rect_grasp_group(resized_rgb, 0).clip(0, 1)
plt.subplot(221)
plt.imshow(rgb_t) # original image
plt.subplot(222)
plt.imshow(depth_t) # depth image
plt.subplot(223)
plt.imshow(loc_map.squeeze().T, cmap='jet') # heatmap
plt.subplot(224)
plt.imshow(rect_rgb) # grasps
plt.tight_layout()
plt.show()
# -------------------------------- #
# wait for user to close window... #
# -------------------------------- #
if args.export_onnx:
torch.onnx.export(
resnet,
x, # model input
"resnet.onnx", # file to save
export_params=True, # store the trained parameter weights inside the onnx file
opset_version=11, # ONNX opset version
input_names=["rgbd_image"],
output_names=[
"layer_1_features",
"layer_2_features",
"layer_3_features",
"layer_4_features",
"layer_5_features"
]
)
print("resnet.onnx saved")
torch.onnx.export(
anchornet,
xs, # model input
"anchornet.onnx", # file to save
export_params=True, # store the trained parameter weights inside the onnx file
opset_version=11, # ONNX opset version
input_names=[
"layer_1_features",
"layer_2_features",
"layer_3_features",
"layer_4_features",
"layer_5_features"
],
output_names=[
"loc_map",
"cls_mask",
"theta_offset",
"depth_offset",
"width_offset",
"perpoint_features"
]
)
print("anchornet.onnx saved")
# fuse heatmap and 2d grasp attributes with point cloud
points_all = feature_fusion(view_points[..., :3], perpoint_features, xyzs)
rect_ggs = [rect_gg]
pc_group, valid_local_centers = data_process(
points_all,
ori_depth,
rect_ggs,
args.center_num,
args.group_num,
(args.input_w, args.input_h),
min_points=32,
is_training=False)
rect_gg = rect_ggs[0] # overwrite rect_gg to update valid mask
points_all = points_all.squeeze()
pc_group = pc_group.transpose(1, 2) # change structure for localnet
# get 2d grasp info (not grasp itself) for trainning
grasp_info = np.zeros((0, 3), dtype=np.float32)
g_thetas = rect_gg.thetas[None]
g_ws = rect_gg.widths[None]
g_ds = rect_gg.depths[None]
cur_info = np.vstack([g_thetas, g_ws, g_ds])
grasp_info = np.vstack([grasp_info, cur_info.T])
grasp_info = torch.from_numpy(grasp_info).to(dtype=torch.float32, device='cuda' if gpu else 'cpu')
# pointnet for feature extraction
features = pointnet(pc_group)
# localnet (6d grasp prediction)
pred, offset = localnet(features, grasp_info)
# detect 6d grasp from 2d output and 6d output
_, pred_rect_gg = detect_6d_grasp_multi(rect_gg,
pred,
offset,
valid_local_centers,
(args.input_w, args.input_h),
anchors,
k=args.local_k)
# collision detect
pred_grasp_from_rect = pred_rect_gg.to_6d_grasp_group(depth=args.depth_thres)
pred_gg, _ = collision_detect(points_all, pred_grasp_from_rect, mode='graspnet')
pred_gg = pred_gg.nms() # remove redundant grasps
print("# 6d grasps found:", pred_gg.size)
# show grasps in 3d
grasp_geo = pred_gg.to_open3d_geometry_list()
points = view_points[..., :3].cpu().numpy().squeeze()
colors = view_points[..., 3:6].cpu().numpy().squeeze()
vispc = o3d.geometry.PointCloud()
vispc.points = o3d.utility.Vector3dVector(points)
vispc.colors = o3d.utility.Vector3dVector(colors)
o3d.visualization.draw_geometries([vispc] + grasp_geo)
if args.export_onnx:
torch.onnx.export(
pointnet,
pc_group, # model input
"pointnet.onnx", # file to save
export_params=True, # store the trained parameter weights inside the onnx file
opset_version=11, # ONNX opset version
input_names=["pointcloud"],
output_names=[
"pointnet_features"
]
)
print("pointnet.onnx saved")
torch.onnx.export(
localnet,
(features, grasp_info), # model input
"localnet.onnx", # file to save
export_params=True, # store the trained parameter weights inside the onnx file
opset_version=11, # ONNX opset version
input_names=["pointnet_features", "grasp_info"],
output_names=[
"anchor_pred",
"offset_pred"
]
)
print("localnet.onnx saved")