This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
visualize.py
368 lines (324 loc) · 11.3 KB
/
visualize.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""Visualization code for point clouds and 3D bounding boxes with mayavi.
Modified by Charles R. Qi
Date: September 2017
"""
import argparse
import os
import mayavi.mlab as mlab
import numpy as np
import open3d as o3d
import torch
from torchsparse import SparseTensor
from torchsparse.utils.quantize import sparse_quantize
from model_zoo import minkunet, spvcnn, spvnas_specialized
def process_point_cloud(input_point_cloud, input_labels=None, voxel_size=0.05):
input_point_cloud[:, 3] = input_point_cloud[:, 3]
pc_ = np.round(input_point_cloud[:, :3] / voxel_size)
pc_ -= pc_.min(0, keepdims=1)
label_map = create_label_map()
if input_labels is not None:
labels_ = label_map[input_labels & 0xFFFF].astype(
np.int64) # semantic labels
else:
labels_ = np.zeros(pc_.shape[0], dtype=np.int64)
feat_ = input_point_cloud
if input_labels is not None:
out_pc = input_point_cloud[labels_ != labels_.max(), :3]
pc_ = pc_[labels_ != labels_.max()]
feat_ = feat_[labels_ != labels_.max()]
labels_ = labels_[labels_ != labels_.max()]
else:
out_pc = input_point_cloud
pc_ = pc_
coords_, inds, inverse_map = sparse_quantize(pc_,
return_index=True,
return_inverse=True)
pc = np.zeros((inds.shape[0], 4))
pc[:, :3] = pc_[inds]
feat = feat_[inds]
labels = labels_[inds]
lidar = SparseTensor(
torch.from_numpy(feat).float(),
torch.from_numpy(pc).int())
return {
'pc': out_pc,
'lidar': lidar,
'targets': labels,
'targets_mapped': labels_,
'inverse_map': inverse_map
}
mlab.options.offscreen = True
def create_label_map(num_classes=19):
name_label_mapping = {
'unlabeled': 0,
'outlier': 1,
'car': 10,
'bicycle': 11,
'bus': 13,
'motorcycle': 15,
'on-rails': 16,
'truck': 18,
'other-vehicle': 20,
'person': 30,
'bicyclist': 31,
'motorcyclist': 32,
'road': 40,
'parking': 44,
'sidewalk': 48,
'other-ground': 49,
'building': 50,
'fence': 51,
'other-structure': 52,
'lane-marking': 60,
'vegetation': 70,
'trunk': 71,
'terrain': 72,
'pole': 80,
'traffic-sign': 81,
'other-object': 99,
'moving-car': 252,
'moving-bicyclist': 253,
'moving-person': 254,
'moving-motorcyclist': 255,
'moving-on-rails': 256,
'moving-bus': 257,
'moving-truck': 258,
'moving-other-vehicle': 259
}
for k in name_label_mapping:
name_label_mapping[k] = name_label_mapping[k.replace('moving-', '')]
train_label_name_mapping = {
0: 'car',
1: 'bicycle',
2: 'motorcycle',
3: 'truck',
4: 'other-vehicle',
5: 'person',
6: 'bicyclist',
7: 'motorcyclist',
8: 'road',
9: 'parking',
10: 'sidewalk',
11: 'other-ground',
12: 'building',
13: 'fence',
14: 'vegetation',
15: 'trunk',
16: 'terrain',
17: 'pole',
18: 'traffic-sign'
}
label_map = np.zeros(260) + num_classes
for i in range(num_classes):
cls_name = train_label_name_mapping[i]
label_map[name_label_mapping[cls_name]] = min(num_classes, i)
return label_map.astype(np.int64)
cmap = np.array([
[245, 150, 100, 255],
[245, 230, 100, 255],
[150, 60, 30, 255],
[180, 30, 80, 255],
[255, 0, 0, 255],
[30, 30, 255, 255],
[200, 40, 255, 255],
[90, 30, 150, 255],
[255, 0, 255, 255],
[255, 150, 255, 255],
[75, 0, 75, 255],
[75, 0, 175, 255],
[0, 200, 255, 255],
[50, 120, 255, 255],
[0, 175, 0, 255],
[0, 60, 135, 255],
[80, 240, 150, 255],
[150, 240, 255, 255],
[0, 0, 255, 255],
])
cmap = cmap[:, [2, 1, 0, 3]] # convert bgra to rgba
def draw_lidar(pc,
color=None,
fig=None,
bgcolor=(1, 1, 1),
pts_scale=0.06,
pts_mode='2dcircle',
pts_color=None):
if fig is None:
fig = mlab.figure(figure=None,
bgcolor=bgcolor,
fgcolor=None,
engine=None,
size=(800, 500))
if color is None:
color = pc[:, 2]
pts = mlab.points3d(pc[:, 0],
pc[:, 1],
pc[:, 2],
color,
mode=pts_mode,
scale_factor=pts_scale,
figure=fig)
pts.glyph.scale_mode = 'scale_by_vector'
pts.glyph.color_mode = 'color_by_scalar' # Color by scalar
pts.module_manager.scalar_lut_manager.lut.table = cmap
pts.module_manager.scalar_lut_manager.lut.number_of_colors = cmap.shape[0]
mlab.view(azimuth=180,
elevation=70,
focalpoint=[12.0909996, -1.04700089, -2.03249991],
distance=62,
figure=fig)
return fig
# visualize by open3d
label_name_mapping = {
0: 'unlabeled',
1: 'outlier',
10: 'car',
11: 'bicycle',
13: 'bus',
15: 'motorcycle',
16: 'on-rails',
18: 'truck',
20: 'other-vehicle',
30: 'person',
31: 'bicyclist',
32: 'motorcyclist',
40: 'road',
44: 'parking',
48: 'sidewalk',
49: 'other-ground',
50: 'building',
51: 'fence',
52: 'other-structure',
60: 'lane-marking',
70: 'vegetation',
71: 'trunk',
72: 'terrain',
80: 'pole',
81: 'traffic-sign',
99: 'other-object',
252: 'moving-car',
253: 'moving-bicyclist',
254: 'moving-person',
255: 'moving-motorcyclist',
256: 'moving-on-rails',
257: 'moving-bus',
258: 'moving-truck',
259: 'moving-other-vehicle'
}
kept_labels = [
'road', 'sidewalk', 'parking', 'other-ground', 'building', 'car', 'truck',
'bicycle', 'motorcycle', 'other-vehicle', 'vegetation', 'trunk', 'terrain',
'person', 'bicyclist', 'motorcyclist', 'fence', 'pole', 'traffic-sign'
]
class BinVisualizer:
def __init__(self):
self.points = np.zeros((0, 3), dtype=np.float32)
self.sem_label = np.zeros((0, 1), dtype=np.uint32) # [m, 1]: label
self.sem_label_color = np.zeros((0, 3),
dtype=np.float32) # [m ,3]: color
# label map
reverse_label_name_mapping = {}
self.label_map = np.zeros(260)
cnt = 0
for label_id in label_name_mapping:
if label_id > 250:
if label_name_mapping[label_id].replace('moving-',
'') in kept_labels:
self.label_map[label_id] = reverse_label_name_mapping[
label_name_mapping[label_id].replace('moving-', '')]
else:
self.label_map[label_id] = 255
elif label_id == 0:
self.label_map[label_id] = 255
else:
if label_name_mapping[label_id] in kept_labels:
self.label_map[label_id] = cnt
reverse_label_name_mapping[
label_name_mapping[label_id]] = cnt
cnt += 1
else:
self.label_map[label_id] = 255
self.reverse_label_name_mapping = reverse_label_name_mapping
def read_pc_label(self, points, label):
assert points.shape[0] == label.shape[0]
label = label.reshape(-1)
self.sem_label = label
self.points = points[:, :3]
def show_cloud(self, window_name='open3d'):
# make color table
color_dict = {}
for i in range(19):
color_dict[i] = cmap[i, :]
color_dict[255] = [0, 0, 0, 255]
pc = o3d.geometry.PointCloud()
pc.points = o3d.utility.Vector3dVector(self.points)
cloud_color = [color_dict[i] for i in list(self.sem_label)]
self.sem_label_color = np.array(cloud_color).reshape(
(-1, 4))[:, :3] / 255
pc.colors = o3d.utility.Vector3dVector(self.sem_label_color)
o3d.visualization.draw_geometries([pc], window_name)
def run_visualize(self, points, label, window_name):
self.read_pc_label(points, label)
self.show_cloud(window_name)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--velodyne-dir', type=str, default='sample_data')
parser.add_argument('--model',
type=str,
default='SemanticKITTI_val_SPVNAS@65GMACs')
parser.add_argument('--visualize_backend',
type=str,
default='open3d',
help='visualization beckend, default=open3d')
args = parser.parse_args()
output_dir = os.path.join(args.velodyne_dir, 'outputs')
os.makedirs(output_dir, exist_ok=True)
if torch.cuda.is_available():
device = 'cuda:0'
else:
device = 'cpu'
if 'MinkUNet' in args.model:
model = minkunet(args.model, pretrained=True)
elif 'SPVCNN' in args.model:
model = spvcnn(args.model, pretrained=True)
elif 'SPVNAS' in args.model:
model = spvnas_specialized(args.model, pretrained=True)
else:
raise NotImplementedError
model = model.to(device)
input_point_clouds = sorted(os.listdir(args.velodyne_dir))
for point_cloud_name in input_point_clouds:
if not point_cloud_name.endswith('.bin'):
continue
label_file_name = point_cloud_name.replace('.bin', '.label')
vis_file_name = point_cloud_name.replace('.bin', '.png')
gt_file_name = point_cloud_name.replace('.bin', '_GT.png')
pc = np.fromfile(f'{args.velodyne_dir}/{point_cloud_name}',
dtype=np.float32).reshape(-1, 4)
if os.path.exists(f'{args.velodyne_dir}/{label_file_name}'):
label = np.fromfile(f'{args.velodyne_dir}/{label_file_name}',
dtype=np.int32)
else:
label = None
feed_dict = process_point_cloud(pc, label)
inputs = feed_dict['lidar'].to(device)
outputs = model(inputs)
predictions = outputs.argmax(1).cpu().numpy()
predictions = predictions[feed_dict['inverse_map']]
if args.visualize_backend == 'mayavi':
fig = draw_lidar(feed_dict['pc'], predictions.astype(np.int32))
mlab.savefig(f'{output_dir}/{vis_file_name}')
if label is not None:
fig = draw_lidar(feed_dict['pc'], feed_dict['targets_mapped'])
mlab.savefig(f'{output_dir}/{gt_file_name}')
elif args.visualize_backend == 'open3d':
# visualize prediction
bin_vis = BinVisualizer()
bin_vis.run_visualize(feed_dict['pc'], predictions.astype(np.int32),
'Predictions')
if label is not None:
bin_vis = BinVisualizer()
bin_vis.run_visualize(feed_dict['pc'],
feed_dict['targets_mapped'],
'Ground turth')
else:
raise NotImplementedError