-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpose_inference.py
276 lines (218 loc) · 9.91 KB
/
pose_inference.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
import os
import sys
import torch
import numpy as np
import argparse
from eval import inference, load_model, get_args, infer_from_points
from NOCS_tools.utils import draw_detections, backproject
import DualSDFHandler
from mmdet.apis import inference_detector, init_detector
from mmdet.registry import VISUALIZERS
from mmengine.model import revert_sync_batchnorm
import cv2
import open3d as o3d
import pyzed.sl as sl
def init_model_ins(config, checkpoint, device='cuda:0'):
model_ins = init_detector(config, checkpoint, device=device)
if device == 'cpu':
model_ins = revert_sync_batchnorm(model_ins)
ins_visualizer = VISUALIZERS.build(model_ins.cfg.visualizer)
# the dataset_meta is loaded from the checkpoint and
# then pass to the model in init_detector
ins_visualizer.dataset_meta = model_ins.dataset_meta
print("Instance Model initialized")
return model_ins
# Parse arguments for pose estimation
parser = argparse.ArgumentParser( description='eval nocs')
parser.add_argument('config', type=str,help='The configuration file.')
parser.add_argument('--pretrained', default=None, type=str,help='pretrained model checkpoint')
parser.add_argument('--data', type=str, help="val/real_test", default='real_test')
parser.add_argument('--draw', dest='draw', action='store_true', help="whether draw and save detection visualization")
args = parser.parse_args()
data = args.data
# Configurations for instance segmentation
ins_seg_config = '/home/lxt/mmdetection/configs/rtmdet/rtmdet-ins_x_8xb16-300e_coco.py'
ins_seg_checkpoint = '/home/lxt/mmdetection/ckpt/rtmdet-ins_x_8xb16-300e_coco_20221124_111313-33d4595b.pth'
torch.multiprocessing.set_start_method('spawn')
torch.backends.cudnn.benchmark = True
device = torch.device('cuda:0')
cfg = get_args(args)
model_ins_seg = init_model_ins(ins_seg_config, ins_seg_checkpoint, device)
# Configurations for zed camera
zed = sl.Camera()
init_params = sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD720
init_params.depth_mode = sl.DEPTH_MODE.NEURAL
init_params.coordinate_units = sl.UNIT.METER
init_params.camera_fps = 15
init_params.sdk_verbose = 1
tracking_parameters = sl.PositionalTrackingParameters()
zed.enable_positional_tracking(tracking_parameters)
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
print("Failed to open ZED camera")
exit(1)
image = sl.Mat()
depth = sl.Mat()
scene_point_cloud = sl.Mat()
# sensors_data = sl.SensorsData()
if zed.grab() == sl.ERROR_CODE.SUCCESS:
# zed.get_sensors_data(sensors_data, sl.TIME_REFERENCE.IMAGE)
zed.retrieve_image(image, sl.VIEW.LEFT)
zed.retrieve_measure(depth, sl.MEASURE.DEPTH)
zed.retrieve_measure(scene_point_cloud, sl.MEASURE.XYZRGBA)
calibration_params = zed.get_camera_information().camera_configuration.calibration_parameters
# Get intrinsics
left_cam = calibration_params.left_cam
fx = left_cam.fx
fy = left_cam.fy
cx = left_cam.cx
cy = left_cam.cy
intrinsics = [[fx, 0, cx], [0, fy, cy], [0, 0, 1]]
# Get image and depth in OpenCV format
image_ocv = image.get_data()
image_ocv = cv2.cvtColor(image_ocv, cv2.COLOR_BGR2RGB)
colors = image_ocv[:,:,:3].reshape(-1, 3)/255.
depth_ocv = depth.get_data()
depth_ocv_normalized = cv2.normalize(depth_ocv, None, 0, 255, cv2.NORM_MINMAX)
depth_ocv_normalized = depth_ocv_normalized.astype(np.uint8)
# Get 3D points and colors for the scene
scene_points_3d = scene_point_cloud.get_data()[:, :, :3].reshape(-1, 3)
valid_mask = ~np.isnan(scene_points_3d).any(axis=1)
scene_points_3d = scene_points_3d[valid_mask]
colors = colors[valid_mask]
# TEST CODE
# image_ocv = cv2.imread('/home/lxt/research-assignment/gcasp/0000_color.png')
# colors = image_ocv[:,:,:3].reshape(-1, 3)/255.
# depth_ocv = cv2.imread('/home/lxt/research-assignment/gcasp/0000_depth.png', -1)
# fake_mask = np.ones_like(depth_ocv)
# scene_points_3d, _ = backproject(depth_ocv, intrinsics, fake_mask)
# valid_mask = (depth_ocv > 0).flatten()
# colors = colors[valid_mask]
#Instance Segmentation
result_ins = inference_detector(model_ins_seg, image_ocv)
masks = result_ins.pred_instances.masks.cpu().numpy()
labels = result_ins.pred_instances.labels.cpu().numpy()
scores = result_ins.pred_instances.scores.cpu().numpy()
bboxes = result_ins.pred_instances.bboxes.cpu().numpy()
score_threshold = 0.3
keep = scores > score_threshold
masks = masks[keep]
labels = labels[keep]
scores = scores[keep]
bboxes = bboxes[keep]
# Get 3D points, colors and labels for each object
objects = {}
objects['points_3d'] = []
objects['label'] = []
objects['color'] = []
for mask, label in zip(masks, labels):
indices = np.where(mask == True)
points_3d = []
color = []
for v, u in zip(indices[0], indices[1]):
color.append(image_ocv[v, u]/255)
point3D = scene_point_cloud.get_data()[v, u,:3]
points_3d.append(point3D)
color = np.array(color)
points_3d = np.array(points_3d)
objects['points_3d'].append(points_3d)
objects['label'].append(label)
objects['color'].append(color)
# Load experimental settings
num_segs = cfg.data.num_segs
synset_names = ['BG', #0
'bottle', #1
'bowl', #2
'camera', #3
'can', #4
'laptop',#5
'mug'#6
]
class_map = {
'bottle': 'bottle',
'bowl':'bowl',
'cup':'mug', # or can ?
'laptop': 'laptop',
}
to_eval_ids = [] # check which category to evaluate, default: all
shape_handlers = [] # load dualsdf pretrained models
for eval_name in cfg.data.names:
for i,name in enumerate(synset_names):
if(name == eval_name):
to_eval_ids.append(i)
break
for eval_name in cfg.data.names:
for i,name in enumerate(synset_names):
if(name == eval_name):
shape_handlers.append(DualSDFHandler.get_instance({
'config': f"/home/lxt/research-assignment/gcasp/config/dualsdf{num_segs}.yaml",
'pretrained': f"/home/lxt/research-assignment/gcasp/DualSDF_ckpts/{eval_name}/{num_segs}/epoch_9999.pth"
}))
break
model = load_model(args, cfg)
result = {}
result['pred_class_ids'] = []
result['pred_bboxes'] = []
result['pred_RTs'] = []
result['pred_scales'] = []
result['pred_scores'] = []
result['class_ids'] = []
result['masks'] = []
for i, eval_id in enumerate(to_eval_ids):
for j,label in enumerate(objects['label']):
print(f"Object {model_ins_seg.dataset_meta['classes'][label]}")
if model_ins_seg.dataset_meta['classes'][label] == synset_names[eval_id] or (model_ins_seg.dataset_meta['classes'][label] == 'cup' and synset_names[eval_id] == 'mug'): #if the object is the same as the one we want to evaluate
# remove NaN points
valid_mask = ~np.isnan(objects['points_3d'][j]).any(axis=1)
objects['points_3d'][j] = objects['points_3d'][j][valid_mask]
# denoise
clean_pc = o3d.geometry.PointCloud()
clean_pc.points = o3d.utility.Vector3dVector(objects['points_3d'][j])
clean_pc = clean_pc.voxel_down_sample(voxel_size=0.01)
clean_pc.remove_statistical_outlier(nb_neighbors=20, std_ratio=1.0)
points = np.asarray(clean_pc.points)
# inference, s is the scale, R is the rotation matrix, t is the translation vector, T is the transformation matrix
s, R, t, T, pred_scale,shapecode, points = infer_from_points(model, points, shape_handlers[i], eval_id, num_segs)
if points is None:
continue
o3d_pc = o3d.geometry.PointCloud()
object_colors = np.zeros((points.shape[0], 3))
object_colors[:, 0] = 1.0
object_center = np.mean(points, axis=0)
# add scene points
o3d_pc.points = o3d.utility.Vector3dVector(scene_points_3d)
o3d_pc.colors = o3d.utility.Vector3dVector(colors)
o3d_pc = o3d_pc.voxel_down_sample(voxel_size=0.01)
o3d_pc, ind = o3d_pc.remove_statistical_outlier(nb_neighbors=20, std_ratio=2.0)
# create a coordinate frame
axis = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1)
z_180_RT = np.zeros((4, 4), dtype=np.float32)
z_180_RT[:3, :3] = np.diag([-1, -1, 1])
z_180_RT[3, 3] = 1
axis.rotate(z_180_RT[:3, :3])
axis.rotate(T[:3,:3])
axis.translate(object_center)
o3d.visualization.draw_geometries([o3d_pc, axis])
# transform the object to canonical pose and save it
normalized_object_pc = o3d.geometry.PointCloud()
normalized_object_pc.points = o3d.utility.Vector3dVector(objects['points_3d'][j])
normalized_object_pc = normalized_object_pc.rotate(np.linalg.inv(T[:3,:3]))
normalized_object_pc = normalized_object_pc.translate(-T[:3,3])
normalized_object_pc = normalized_object_pc.rotate(z_180_RT[:3, :3])
normalized_object_pc.colors = o3d.utility.Vector3dVector(object_colors)
axis = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.5)
o3d.visualization.draw_geometries([normalized_object_pc, axis])
o3d.io.write_point_cloud(f"/home/lxt/research-assignment/PoinTr/data/{synset_names[eval_id]}_{j}.pcd", normalized_object_pc)
print(f"Object {synset_names[eval_id]}: ")
# print(f"s: {s}")
# print(f"R: {R}")
# print(f"t: {t}")
# print(f"object_center: {object_center}")
# print(f"T: {T}")
# print(f"pred_scale: {pred_scale}")
# print(f"shapecode: {shapecode}")
zed.close()
# draw_detections(image_ocv, './', data, 'image', intrinsics, synset_names, True,
# 0, 0, 0, 0, 0, 0, 0,
# result['pred_bboxes'], result['class_ids'], result['masks'], 0, np.array(result['pred_RTs']), result['pred_scores'], np.array(result['pred_scales']))