-
Notifications
You must be signed in to change notification settings - Fork 40
/
semantic3d_seg.py
422 lines (332 loc) · 14.3 KB
/
semantic3d_seg.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Semantic3D Example with ConvPoint
# add the parent folder to the python path to access convpoint library
import sys
sys.path.append('../../')
import numpy as np
import argparse
from datetime import datetime
import os
import random
from tqdm import tqdm
import torch
import torch.utils.data
import torch.nn.functional as F
from torchvision import transforms
from sklearn.metrics import confusion_matrix
import time
import utils.metrics as metrics
import convpoint.knn.lib.python.nearest_neighbors as nearest_neighbors
from PIL import Image
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# wrap blue / green
def wblue(str):
return bcolors.OKBLUE+str+bcolors.ENDC
def wgreen(str):
return bcolors.OKGREEN+str+bcolors.ENDC
def nearest_correspondance(pts_src, pts_dest, data_src, K=1):
print(pts_dest.shape)
indices = nearest_neighbors.knn(pts_src.copy(), pts_dest.copy(), K, omp=True)
print(indices.shape)
if K==1:
indices = indices.ravel()
data_dest = data_src[indices]
else:
data_dest = data_src[indices].mean(1)
return data_dest
def rotate_point_cloud_z(batch_data):
""" Randomly rotate the point clouds to augument the dataset
rotation is per shape based along up direction
Input:
BxNx3 array, original batch of point clouds
Return:
BxNx3 array, rotated batch of point clouds
"""
rotation_angle = np.random.uniform() * 2 * np.pi
cosval = np.cos(rotation_angle)
sinval = np.sin(rotation_angle)
rotation_matrix = np.array([[cosval, sinval, 0],
[-sinval, cosval, 0],
[0, 0, 1],])
return np.dot(batch_data, rotation_matrix)
# Part dataset only for training / validation
class PartDataset():
def __init__ (self, filelist, folder,
training=False,
iteration_number = None,
block_size=8,
npoints = 8192,
nocolor=False):
self.folder = folder
self.training = training
self.filelist = filelist
self.bs = block_size
self.nocolor = nocolor
self.npoints = npoints
self.iterations = iteration_number
self.verbose = False
self.transform = transforms.ColorJitter(
brightness=0.4,
contrast=0.4,
saturation=0.4)
def __getitem__(self, index):
# load the data
index = random.randint(0, len(self.filelist)-1)
pts = np.load(os.path.join(self.folder, self.filelist[index]))
# get the features
fts = pts[:,3:6]
# get the labels
lbs = pts[:, 6].astype(int)-1 # the generation script label starts at 1
# get the point coordinates
pts = pts[:, :3]
# pick a random point
pt_id = random.randint(0, pts.shape[0]-1)
pt = pts[pt_id]
# create the mask
mask_x = np.logical_and(pts[:,0]<pt[0]+self.bs/2, pts[:,0]>pt[0]-self.bs/2)
mask_y = np.logical_and(pts[:,1]<pt[1]+self.bs/2, pts[:,1]>pt[1]-self.bs/2)
mask = np.logical_and(mask_x, mask_y)
pts = pts[mask]
lbs = lbs[mask]
fts = fts[mask]
# random selection
choice = np.random.choice(pts.shape[0], self.npoints, replace=True)
pts = pts[choice]
lbs = lbs[choice]
fts = fts[choice]
# data augmentation
if self.training:
# random rotation
pts = rotate_point_cloud_z(pts)
# random jittering
fts = fts.astype(np.uint8)
fts = np.array(self.transform( Image.fromarray(np.expand_dims(fts, 0)) ))
fts = np.squeeze(fts, 0)
fts = fts.astype(np.float32)
fts = fts / 255 - 0.5
if self.nocolor:
fts = np.ones((pts.shape[0], 1))
pts = torch.from_numpy(pts).float()
fts = torch.from_numpy(fts).float()
lbs = torch.from_numpy(lbs).long()
return pts, fts, lbs
def __len__(self):
return self.iterations
class PartDatasetTest():
def compute_mask(self, pt, bs):
# build the mask
mask_x = np.logical_and(self.xyzrgb[:,0]<pt[0]+bs/2, self.xyzrgb[:,0]>pt[0]-bs/2)
mask_y = np.logical_and(self.xyzrgb[:,1]<pt[1]+bs/2, self.xyzrgb[:,1]>pt[1]-bs/2)
mask = np.logical_and(mask_x, mask_y)
return mask
def __init__ (self, filename, folder,
block_size=8,
npoints = 8192,
test_step=0.8, nocolor=False):
self.folder = folder
self.bs = block_size
self.npoints = npoints
self.verbose = False
self.nocolor = nocolor
self.filename = filename
# load the points
self.xyzrgb = np.load(os.path.join(self.folder, self.filename))
step = test_step
discretized = ((self.xyzrgb[:,:2]).astype(float)/step).astype(int)
self.pts = np.unique(discretized, axis=0)
self.pts = self.pts.astype(np.float)*step
def __getitem__(self, index):
# get the data
mask = self.compute_mask(self.pts[index], self.bs)
pts = self.xyzrgb[mask]
# choose right number of points
choice = np.random.choice(pts.shape[0], self.npoints, replace=True)
pts = pts[choice]
# labels will contain indices in the original point cloud
lbs = np.where(mask)[0][choice]
# separate between features and points
if self.nocolor:
fts = np.ones((pts.shape[0], 1))
else:
fts = pts[:,3:6]
fts = fts.astype(np.float32)
fts = fts / 255 - 0.5
pts = pts[:, :3].copy()
pts = torch.from_numpy(pts).float()
fts = torch.from_numpy(fts).float()
lbs = torch.from_numpy(lbs).long()
return pts, fts, lbs
def __len__(self):
return len(self.pts)
def get_model(model_name, input_channels, output_channels, args):
if model_name == "SegBig":
from networks.network_seg import SegBig as Net
return Net(input_channels, output_channels, args=args)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--rootdir', '-s', help='Path to data folder')
parser.add_argument("--savedir", type=str, default="./results")
parser.add_argument('--block_size', help='Block size', type=float, default=8)
parser.add_argument("--epochs", type=int, default=100)
parser.add_argument("--batch_size", "-b", type=int, default=16)
parser.add_argument("--iter", "-i", type=int, default=1000)
parser.add_argument("--npoints", "-n", type=int, default=8192)
parser.add_argument("--threads", type=int, default=4)
parser.add_argument("--nocolor", action="store_true")
parser.add_argument("--test", action="store_true")
parser.add_argument("--savepts", action="store_true")
parser.add_argument("--test_step", default=0.8, type=float)
parser.add_argument("--model", default="SegBig", type=str)
parser.add_argument("--drop", default=0.5, type=float)
args = parser.parse_args()
time_string = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
root_folder = os.path.join(args.savedir, "{}_{}_nocolor{}_drop{}_{}".format(
args.model, args.npoints, args.nocolor, args.drop, time_string))
filelist_train=[
"bildstein_station1_xyz_intensity_rgb_voxels.npy",
"bildstein_station3_xyz_intensity_rgb_voxels.npy",
"bildstein_station5_xyz_intensity_rgb_voxels.npy",
"domfountain_station1_xyz_intensity_rgb_voxels.npy",
"domfountain_station2_xyz_intensity_rgb_voxels.npy",
"domfountain_station3_xyz_intensity_rgb_voxels.npy",
"neugasse_station1_xyz_intensity_rgb_voxels.npy",
"sg27_station1_intensity_rgb_voxels.npy",
"sg27_station2_intensity_rgb_voxels.npy",
"sg27_station4_intensity_rgb_voxels.npy",
"sg27_station5_intensity_rgb_voxels.npy",
"sg27_station9_intensity_rgb_voxels.npy",
"sg28_station4_intensity_rgb_voxels.npy",
"untermaederbrunnen_station1_xyz_intensity_rgb_voxels.npy",
"untermaederbrunnen_station3_xyz_intensity_rgb_voxels.npy",
]
filelist_test = [
"birdfountain_station1_xyz_intensity_rgb_voxels.npy",
"castleblatten_station1_intensity_rgb_voxels.npy",
"castleblatten_station5_xyz_intensity_rgb_voxels.npy",
"marketplacefeldkirch_station1_intensity_rgb_voxels.npy",
"marketplacefeldkirch_station4_intensity_rgb_voxels.npy",
"marketplacefeldkirch_station7_intensity_rgb_voxels.npy",
"sg27_station10_intensity_rgb_voxels.npy",
"sg27_station3_intensity_rgb_voxels.npy",
"sg27_station6_intensity_rgb_voxels.npy",
"sg27_station8_intensity_rgb_voxels.npy",
"sg28_station2_intensity_rgb_voxels.npy",
"sg28_station5_xyz_intensity_rgb_voxels.npy",
"stgallencathedral_station1_intensity_rgb_voxels.npy",
"stgallencathedral_station3_intensity_rgb_voxels.npy",
"stgallencathedral_station6_intensity_rgb_voxels.npy",
]
N_CLASSES = 8
# create model
print("Creating the network...", end="", flush=True)
if args.nocolor:
net = get_model(args.model, input_channels=1, output_channels=N_CLASSES, args=args)
else:
net = get_model(args.model, input_channels=3, output_channels=N_CLASSES, args=args)
if args.test:
net.load_state_dict(torch.load(os.path.join(args.savedir, "state_dict.pth")))
net.cuda()
print("Done")
##### TRAIN
if not args.test:
print("Create the datasets...", end="", flush=True)
ds = PartDataset(filelist_train, args.rootdir,
training=True, block_size=args.block_size,
iteration_number=args.batch_size*args.iter,
npoints=args.npoints,
nocolor=args.nocolor)
train_loader = torch.utils.data.DataLoader(ds, batch_size=args.batch_size, shuffle=True,
num_workers=args.threads)
print("Done")
print("Create optimizer...", end="", flush=True)
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)
print("Done")
# create the root folder
os.makedirs(root_folder, exist_ok=True)
# create the log file
logs = open(os.path.join(root_folder, "log.txt"), "w")
# iterate over epochs
for epoch in range(args.epochs):
#######
# training
net.train()
train_loss = 0
cm = np.zeros((N_CLASSES, N_CLASSES))
t = tqdm(train_loader, ncols=100, desc="Epoch {}".format(epoch))
for pts, features, seg in t:
features = features.cuda()
pts = pts.cuda()
seg = seg.cuda()
optimizer.zero_grad()
outputs = net(features, pts)
loss = F.cross_entropy(outputs.view(-1, N_CLASSES), seg.view(-1))
loss.backward()
optimizer.step()
output_np = np.argmax(outputs.cpu().detach().numpy(), axis=2).copy()
target_np = seg.cpu().numpy().copy()
cm_ = confusion_matrix(target_np.ravel(), output_np.ravel(), labels=list(range(N_CLASSES)))
cm += cm_
oa = f"{metrics.stats_overall_accuracy(cm):.5f}"
aa = f"{metrics.stats_accuracy_per_class(cm)[0]:.5f}"
iou = f"{metrics.stats_iou_per_class(cm)[0]:.5f}"
train_loss += loss.detach().cpu().item()
t.set_postfix(OA=wblue(oa), AA=wblue(aa), IOU=wblue(iou), LOSS=wblue(f"{train_loss/cm.sum():.4e}"))
# save the model
torch.save(net.state_dict(), os.path.join(root_folder, "state_dict.pth"))
# write the logs
logs.write(f"{epoch} {oa} {aa} {iou}\n")
logs.flush()
logs.close()
##### TEST
else:
net.eval()
for filename in filelist_test:
print(filename)
ds = PartDatasetTest(filename, args.rootdir,
block_size=args.block_size,
npoints= args.npoints,
test_step=args.test_step,
nocolor=args.nocolor
)
loader = torch.utils.data.DataLoader(ds, batch_size=args.batch_size, shuffle=False,
num_workers=args.threads
)
xyzrgb = ds.xyzrgb[:,:3]
scores = np.zeros((xyzrgb.shape[0], N_CLASSES))
with torch.no_grad():
t = tqdm(loader, ncols=80)
for pts, features, indices in t:
features = features.cuda()
pts = pts.cuda()
outputs = net(features, pts)
outputs_np = outputs.cpu().numpy().reshape((-1, N_CLASSES))
scores[indices.cpu().numpy().ravel()] += outputs_np
mask = np.logical_not(scores.sum(1)==0)
scores = scores[mask]
pts_src = xyzrgb[mask]
# create the scores for all points
scores = nearest_correspondance(pts_src.astype(np.float32), xyzrgb.astype(np.float32), scores, K=1)
# compute softmax
scores = scores - scores.max(axis=1)[:,None]
scores = np.exp(scores) / np.exp(scores).sum(1)[:,None]
scores = np.nan_to_num(scores)
os.makedirs(os.path.join(args.savedir, "results"), exist_ok=True)
# saving labels
save_fname = os.path.join(args.savedir, "results", filename)
scores = scores.argmax(1)
np.savetxt(save_fname,scores,fmt='%d')
if args.savepts:
save_fname = os.path.join(args.savedir, "results", f"{filename}_pts.txt")
xyzrgb = np.concatenate([xyzrgb, np.expand_dims(scores,1)], axis=1)
np.savetxt(save_fname,xyzrgb,fmt=['%.4f','%.4f','%.4f','%d'])
# break
if __name__ == '__main__':
main()
print('{}-Done.'.format(datetime.now()))