-
Notifications
You must be signed in to change notification settings - Fork 15
/
initialSP_prepare_S3DIS.py
178 lines (154 loc) · 6.28 KB
/
initialSP_prepare_S3DIS.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
from pclpy import pcl
import pclpy
import numpy as np
from scipy import stats
import os
from os.path import join, exists, dirname, abspath
import sys, glob
BASE_DIR = dirname(abspath(__file__))
ROOT_DIR = dirname(BASE_DIR)
sys.path.append(BASE_DIR)
sys.path.append(ROOT_DIR)
from lib.helper_ply import read_ply, write_ply
import time
import MinkowskiEngine as ME
import matplotlib.pyplot as plt
from concurrent.futures import ProcessPoolExecutor
from pathlib import Path
colormap = []
for _ in range(1000):
for k in range(12):
colormap.append(plt.cm.Set3(k))
for k in range(9):
colormap.append(plt.cm.Set1(k))
for k in range(8):
colormap.append(plt.cm.Set2(k))
colormap.append((0, 0, 0, 0))
colormap = np.array(colormap)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--input_path', type=str, default='data/S3DIS/input', help='raw data path')
parser.add_argument('--sp_path', type=str, default='data/S3DIS/initial_superpoints')
args = parser.parse_args()
ignore_label = 12
voxel_size = 0.05
vis = True
def supervoxel_clustering(coords, rgb=None):
pc = pcl.PointCloud.PointXYZRGBA(coords, rgb)
normals = pc.compute_normals(radius=3, num_threads=2)
vox = pcl.segmentation.SupervoxelClustering.PointXYZRGBA(voxel_resolution=1, seed_resolution=10)
vox.setInputCloud(pc)
vox.setNormalCloud(normals)
vox.setSpatialImportance(0.4)
vox.setNormalImportance(1)
vox.setColorImportance(0.2)
output = pcl.vectors.map_uint32t_PointXYZRGBA()
vox.extract(output)
return list(output.items())
def region_growing_simple(coords):
pc = pcl.PointCloud.PointXYZ(coords)
normals = pc.compute_normals(radius=3, num_threads=2)
clusters = pclpy.region_growing(pc, normals=normals, min_size=1, max_size=100000, n_neighbours=15,
smooth_threshold=3, curvature_threshold=1, residual_threshold=1)
return clusters, normals.normals
def construct_superpoints(path):
f = Path(path)
data = read_ply(f)
coords = np.vstack((data['x'], data['y'], data['z'])).T.copy()
feats = np.vstack((data['red'], data['green'], data['blue'])).T.copy()
labels = data['class'].copy()
coords = coords.astype(np.float32)
coords -= coords.mean(0)
time_start = time.time()
'''Voxelize'''
scale = 1 / voxel_size
coords = np.floor(coords * scale)
coords, feats, labels, unique_map, inverse_map = ME.utils.sparse_quantize(np.ascontiguousarray(coords),
feats, labels=labels, ignore_label=-1, return_index=True, return_inverse=True)
coords = coords.numpy().astype(np.float32)
'''VCCS'''
out = supervoxel_clustering(coords, feats)
voxel_idx = -np.ones_like(labels)
voxel_num = 0
for voxel in range(len(out)):
if out[voxel][1].voxels_.xyz.shape[0] >= 0:
for xyz_voxel in out[voxel][1].voxels_.xyz:
index_colum = np.where((xyz_voxel == coords).all(1))
voxel_idx[index_colum] = voxel_num
voxel_num += 1
'''Region Growing'''
clusters = region_growing_simple(coords)[0]
region_idx = -1 * np.ones_like(labels)
for region in range(len(clusters)):
for point_idx in clusters[region].indices:
region_idx[point_idx] = region
'''Merging'''
merged = -np.ones_like(labels)
voxel_idx[voxel_idx != -1] += len(clusters)
for v in np.unique(voxel_idx):
if v != -1:
voxel_mask = v == voxel_idx
voxel2region = region_idx[voxel_mask] ### count which regions are appeared in current voxel
dominant_region = stats.mode(voxel2region)[0][0]
if (dominant_region == voxel2region).sum() > voxel2region.shape[0] * 0.5:
merged[voxel_mask] = dominant_region
else:
merged[voxel_mask] = v
'''Make Superpoint Labels Continuous'''
sp_labels = -np.ones_like(merged)
count_num = 0
for m in np.unique(merged):
if m != -1:
sp_labels[merged == m] = count_num
count_num += 1
'''ReProject to Input Point Cloud'''
out_sp_labels = sp_labels[inverse_map]
out_coords = np.vstack((data['x'], data['y'], data['z'])).T
out_labels = data['class'].squeeze()
#
if not exists(args.sp_path):
os.makedirs(args.sp_path)
np.save(args.sp_path + '/' + f.name[:-4] + '_superpoint.npy', out_sp_labels)
if vis:
vis_path = args.sp_path +'/vis/'
if not os.path.exists(vis_path):
os.makedirs(vis_path)
colors = np.zeros_like(out_coords)
for p in range(colors.shape[0]):
colors[p] = 255 * (colormap[out_sp_labels[p].astype(np.int32)])[:3]
colors = colors.astype(np.uint8)
write_ply(vis_path + '/' + f.name, [out_coords, colors], ['x', 'y', 'z', 'red', 'green', 'blue'])
sp2gt = -np.ones_like(out_labels)
for sp in np.unique(out_sp_labels):
if sp != -1:
sp_mask = sp == out_sp_labels
sp2gt[sp_mask] = stats.mode(out_labels[sp_mask])[0][0]
print('completed scene: {}, used time: {:.2f}s'.format(f.name, time.time() - time_start))
return (out_labels, sp2gt)
print('start constructing initial superpoints')
path_list = []
folders = sorted(glob.glob(args.input_path + '/*.ply'))
for _, file in enumerate(folders):
path_list.append(file)
pool = ProcessPoolExecutor(max_workers=10)
result = list(pool.map(construct_superpoints, path_list))
print('end constructing initial superpoints')
all_labels, all_sp2gt = [], []
for (labels, sp2gt) in result:
mask = (sp2gt != -1) & (sp2gt != ignore_label)
labels, sp2gt = labels[mask].astype(np.int32), sp2gt[mask].astype(np.int32)
all_labels.append(labels), all_sp2gt.append(sp2gt)
all_labels, all_sp2gt = np.concatenate(all_labels), np.concatenate(all_sp2gt)
sem_num = 12
mask = (all_labels >= 0) & (all_labels < sem_num)
histogram = np.bincount(sem_num * all_labels[mask] + all_sp2gt[mask], minlength=sem_num ** 2).reshape(sem_num, sem_num)
o_Acc = histogram[range(sem_num), range(sem_num)].sum() / histogram.sum()
tp = np.diag(histogram)
fp = np.sum(histogram, 0) - tp
fn = np.sum(histogram, 1) - tp
IoUs = tp / (tp + fp + fn + 1e-8)
m_IoU = np.nanmean(IoUs)
s = '| mIoU {:5.2f} | '.format(100 * m_IoU)
for IoU in IoUs:
s += '{:5.2f} '.format(100 * IoU)
print(' Acc: {:.5f} Test IoU'.format(o_Acc), s)