forked from valeoai/POCO
/
generate.py
596 lines (457 loc) · 22.3 KB
/
generate.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import os
import logging
import numpy as np
from tqdm import tqdm
import math
from skimage import measure
import open3d as o3d
from scipy.spatial import KDTree
import torch_geometric.transforms as T
# torch imports
import torch
import torch.nn.functional as F
# lightconvpoint imports
from lightconvpoint.datasets.dataset import get_dataset
import lightconvpoint.utils.transforms as lcp_T
from lightconvpoint.utils.logs import logs_file
from lightconvpoint.utils.misc import dict_to_device
import networks
import datasets
import utils.argparseFromFile as argparse
def export_mesh_and_refine_vertices_region_growing_v2(
network,latent,
resolution,
padding=0,
mc_value=0,
device=None,
num_pts=50000,
refine_iter=10,
simplification_target=None,
input_points=None,
refine_threshold=None,
out_value=np.nan,
step = None,
dilation_size=2,
whole_negative_component=False,
return_volume=False
):
bmin=input_points.min()
bmax=input_points.max()
if step is None:
step = (bmax-bmin) / (resolution -1)
resolutionX = resolution
resolutionY = resolution
resolutionZ = resolution
else:
bmin = input_points.min(axis=0)
bmax = input_points.max(axis=0)
resolutionX = math.ceil((bmax[0]-bmin[0])/step)
resolutionY = math.ceil((bmax[1]-bmin[1])/step)
resolutionZ = math.ceil((bmax[2]-bmin[2])/step)
bmin_pad = bmin - padding * step
bmax_pad = bmax + padding * step
pts_ids = (input_points - bmin)/step + padding
pts_ids = pts_ids.astype(int)
# create the volume
volume = np.full((resolutionX+2*padding, resolutionY+2*padding, resolutionZ+2*padding), np.nan, dtype=np.float64)
mask_to_see = np.full((resolutionX+2*padding, resolutionY+2*padding, resolutionZ+2*padding), True, dtype=bool)
while(pts_ids.shape[0] > 0):
# print("Pts", pts_ids.shape)
# creat the mask
mask = np.full((resolutionX+2*padding, resolutionY+2*padding, resolutionZ+2*padding), False, dtype=bool)
mask[pts_ids[:,0], pts_ids[:,1], pts_ids[:,2]] = True
# dilation
for i in tqdm(range(pts_ids.shape[0]), ncols=100, disable=True):
xc = int(pts_ids[i,0])
yc = int(pts_ids[i,1])
zc = int(pts_ids[i,2])
mask[max(0,xc-dilation_size):xc+dilation_size,
max(0,yc-dilation_size):yc+dilation_size,
max(0,zc-dilation_size):zc+dilation_size] = True
# get the valid points
valid_points_coord = np.argwhere(mask).astype(np.float32)
valid_points = valid_points_coord * step + bmin_pad
# get the prediction for each valid points
z = []
near_surface_samples_torch = torch.tensor(valid_points, dtype=torch.float, device=device)
for pnts in tqdm(torch.split(near_surface_samples_torch,num_pts,dim=0), ncols=100, disable=True):
latent["pos_non_manifold"] = pnts.unsqueeze(0)
occ_hat = network.from_latent(latent)
# get class and max non class
class_dim = 1
occ_hat = torch.stack([occ_hat[:, class_dim] , occ_hat[:,[i for i in range(occ_hat.shape[1]) if i!=class_dim]].max(dim=1)[0]], dim=1)
occ_hat = F.softmax(occ_hat, dim=1)
occ_hat[:, 0] = occ_hat[:, 0] * (-1)
if class_dim == 0:
occ_hat = occ_hat * (-1)
# occ_hat = -occ_hat.sum(dim=1)
occ_hat = occ_hat.sum(dim=1)
outputs = occ_hat.squeeze(0)
z.append(outputs.detach().cpu().numpy())
z = np.concatenate(z,axis=0)
z = z.astype(np.float64)
# update the volume
volume[mask] = z
# create the masks
mask_pos = np.full((resolutionX+2*padding, resolutionY+2*padding, resolutionZ+2*padding), False, dtype=bool)
mask_neg = np.full((resolutionX+2*padding, resolutionY+2*padding, resolutionZ+2*padding), False, dtype=bool)
# dilation
for i in tqdm(range(pts_ids.shape[0]), ncols=100, disable=True):
xc = int(pts_ids[i,0])
yc = int(pts_ids[i,1])
zc = int(pts_ids[i,2])
mask_to_see[xc,yc,zc] = False
if volume[xc,yc,zc] <= 0:
mask_neg[max(0,xc-dilation_size):xc+dilation_size,
max(0,yc-dilation_size):yc+dilation_size,
max(0,zc-dilation_size):zc+dilation_size] = True
if volume[xc,yc,zc] >= 0:
mask_pos[max(0,xc-dilation_size):xc+dilation_size,
max(0,yc-dilation_size):yc+dilation_size,
max(0,zc-dilation_size):zc+dilation_size] = True
# get the new points
new_mask = (mask_neg & (volume>=0) & mask_to_see) | (mask_pos & (volume<=0) & mask_to_see)
pts_ids = np.argwhere(new_mask).astype(int)
volume[0:padding, :, :] = out_value
volume[-padding:, :, :] = out_value
volume[:, 0:padding, :] = out_value
volume[:, -padding:, :] = out_value
volume[:, :, 0:padding] = out_value
volume[:, :, -padding:] = out_value
# volume[np.isnan(volume)] = out_value
maxi = volume[~np.isnan(volume)].max()
mini = volume[~np.isnan(volume)].min()
if not (maxi > mc_value and mini < mc_value):
return None
if return_volume:
return volume
# compute the marching cubes
verts, faces, _, _ = measure.marching_cubes(
volume=volume.copy(),
level=mc_value,
)
# removing the nan values in the vertices
values = verts.sum(axis=1)
o3d_verts = o3d.utility.Vector3dVector(verts)
o3d_faces = o3d.utility.Vector3iVector(faces)
mesh = o3d.geometry.TriangleMesh(o3d_verts, o3d_faces)
mesh.remove_vertices_by_mask(np.isnan(values))
verts = np.asarray(mesh.vertices)
faces = np.asarray(mesh.triangles)
if refine_iter > 0:
dirs = verts - np.floor(verts)
dirs = (dirs>0).astype(dirs.dtype)
mask = np.logical_and(dirs.sum(axis=1)>0, dirs.sum(axis=1)<2)
v = verts[mask]
dirs = dirs[mask]
# initialize the two values (the two vertices for mc grid)
v1 = np.floor(v)
v2 = v1 + dirs
# get the predicted values for both set of points
v1 = v1.astype(int)
v2 = v2.astype(int)
preds1 = volume[v1[:,0], v1[:,1], v1[:,2]]
preds2 = volume[v2[:,0], v2[:,1], v2[:,2]]
# get the coordinates in the real coordinate system
v1 = v1.astype(np.float32)*step + bmin_pad
v2 = v2.astype(np.float32)*step + bmin_pad
# tmp mask
mask_tmp = np.logical_and(
np.logical_not(np.isnan(preds1)),
np.logical_not(np.isnan(preds2))
)
v = v[mask_tmp]
dirs = dirs[mask_tmp]
v1 = v1[mask_tmp]
v2 = v2[mask_tmp]
mask[mask] = mask_tmp
# initialize the vertices
verts = verts * step + bmin_pad
v = v * step + bmin_pad
# iterate for the refinement step
for iter_id in tqdm(range(refine_iter), ncols=50, disable=True):
# print(f"iter {iter_id}")
preds = []
pnts_all = torch.tensor(v, dtype=torch.float, device=device)
for pnts in tqdm(torch.split(pnts_all,num_pts,dim=0), ncols=100, disable=True):
latent["pos_non_manifold"] = pnts.unsqueeze(0)
occ_hat = network.from_latent(latent)
# get class and max non class
class_dim = 1
occ_hat = torch.stack([occ_hat[:, class_dim] , occ_hat[:,[i for i in range(occ_hat.shape[1]) if i!=class_dim]].max(dim=1)[0]], dim=1)
occ_hat = F.softmax(occ_hat, dim=1)
occ_hat[:, 0] = occ_hat[:, 0] * (-1)
if class_dim == 0:
occ_hat = occ_hat * (-1)
# occ_hat = -occ_hat.sum(dim=1)
occ_hat = occ_hat.sum(dim=1)
outputs = occ_hat.squeeze(0)
# outputs = network.predict_from_latent(latent, pnts.unsqueeze(0), with_sigmoid=True)
# outputs = outputs.squeeze(0)
preds.append(outputs.detach().cpu().numpy())
preds = np.concatenate(preds,axis=0)
mask1 = (preds*preds1)>0
v1[mask1] = v[mask1]
preds1[mask1] = preds[mask1]
mask2 = (preds*preds2)>0
v2[mask2] = v[mask2]
preds2[mask2] = preds[mask2]
v = (v2 + v1)/2
verts[mask] = v
# keep only the points that needs to be refined
if refine_threshold is not None:
mask_vertices = (np.linalg.norm(v2 - v1, axis=1) > refine_threshold)
# print("V", mask_vertices.sum() , "/", v.shape[0])
v = v[mask_vertices]
preds1 = preds1[mask_vertices]
preds2 = preds2[mask_vertices]
v1 = v1[mask_vertices]
v2 = v2[mask_vertices]
mask[mask] = mask_vertices
if v.shape[0] == 0:
break
# print("V", v.shape[0])
else:
verts = verts * step + bmin_pad
o3d_verts = o3d.utility.Vector3dVector(verts)
o3d_faces = o3d.utility.Vector3iVector(faces)
mesh = o3d.geometry.TriangleMesh(o3d_verts, o3d_faces)
if simplification_target is not None and simplification_target > 0:
mesh = o3d.geometry.TriangleMesh.simplify_quadric_decimation(mesh, simplification_target)
return mesh
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def main(config):
config = eval(str(config))
logging.getLogger().setLevel(config["logging"])
disable_log = (config["log_mode"] != "interactive")
device = torch.device(config["device"])
if config["device"] == "cuda":
torch.backends.cudnn.benchmark = True
savedir_root = config["save_dir"]
# create the network
N_LABELS = config["network_n_labels"]
latent_size = config["network_latent_size"]
backbone = config["network_backbone"]
decoder = {'name':config["network_decoder"], 'k': config['network_decoder_k']}
logging.info("Creating the network")
def network_function():
return networks.Network(3, latent_size, N_LABELS, backbone, decoder)
net = network_function()
checkpoint = torch.load(os.path.join(savedir_root, "checkpoint.pth"))
net.load_state_dict(checkpoint["state_dict"])
net.to(device)
net.eval()
logging.info(f"Network -- Number of parameters {count_parameters(net)}")
logging.info("Getting the dataset")
DatasetClass = get_dataset(eval("datasets."+config["dataset_name"]))
test_transform = []
# downsample
if config["manifold_points"] is not None and config["manifold_points"] > 0:
test_transform.append(lcp_T.FixedPoints(config["manifold_points"], item_list=["x", "pos", "normal", "y", "y_object"]))
test_transform.append(lcp_T.FixedPoints(1, item_list=["pos_non_manifold", "occupancies", "y_v", "y_v_object"]))
# add noise to data
if (config["random_noise"] is not None) and (config["random_noise"] > 0):
logging.info("Adding random noise")
test_transform.append(lcp_T.RandomNoiseNormal(sigma=config["random_noise"]))
if config["normals"]:
logging.info("Normals as features")
test_transform.append(lcp_T.FieldAsFeatures(["normal"]))
# operate the permutations
test_transform = test_transform + [
lcp_T.Permutation("pos", [1,0]),
lcp_T.Permutation("pos_non_manifold", [1,0]),
# lcp_T.Permutation("normal", [1,0]),
lcp_T.Permutation("x", [1,0]),
lcp_T.ToDict(),]
test_transform = T.Compose(test_transform)
# build the dataset
gen_dataset = DatasetClass(config["dataset_root"],
split=config["test_split"],
transform=test_transform,
network_function=network_function,
filter_name=config["filter_name"],
num_non_manifold_points=config["non_manifold_points"],
dataset_size=config["num_mesh"]
)
# build the data loaders
gen_loader = torch.utils.data.DataLoader(
gen_dataset,
batch_size=1,
shuffle=False,
num_workers=0,
)
with torch.no_grad():
gen_dir = f"gen_{config['dataset_name']}"
gen_dir += f"_{config['test_split']}"
if config['manifold_points'] <= 0:
gen_dir += f"_allPts"
else:
gen_dir += f"_{config['manifold_points']}"
if "gen_descriptor" in config:
gen_dir += f"_{config['gen_descriptor']}"
savedir_mesh_root = os.path.join(savedir_root, gen_dir)
for data in tqdm(gen_loader, ncols=100):
shape_id = data["shape_id"].item()
category_name = gen_dataset.get_category(shape_id)
object_name = gen_dataset.get_object_name(shape_id)
savedir = gen_dataset.get_save_dir(shape_id)
# print(f"{shape_id} | {category_name} - {object_name} - {data['pos'].shape}")
# create the directories
savedir_points = os.path.join(savedir_mesh_root, "input", savedir)
os.makedirs(savedir_points, exist_ok=True)
savedir_mesh = os.path.join(savedir_mesh_root, "meshes", savedir)
os.makedirs(savedir_mesh, exist_ok=True)
# if resume skip if the file already exists
if config["resume"]:
if os.path.splitext(object_name)[1] == ".ply":
if os.path.isfile(os.path.join(savedir_mesh, object_name)):
continue
else:
if os.path.isfile(os.path.join(savedir_mesh, object_name+".ply")):
continue
data = dict_to_device(data, device)
# if config["normals"]:
# data["x"] = data["normal"]
# # save the input
# pts = data["pos"][0].transpose(1,0).cpu().numpy()
# nls = data["x"][0].transpose(1,0).cpu().numpy()
# pts = np.concatenate([pts, nls], axis=1)
# pts = pts.astype(np.float16)
# np.savetxt(os.path.join(savedir_points, object_name+".xyz"), pts)
# auto scale (for big scenes)
if "gen_autoscale" in config and config["gen_autoscale"]:
logging.info("Autoscale computation")
autoscale_target = config["gen_autoscale_target"] # 0.01 # estimated on shapenet 3000
pos = data["pos"][0].cpu().transpose(0,1).numpy()
tree = KDTree(pos)
mean_dist = tree.query(pos, 2)[0].max(axis=1).mean()
scale = autoscale_target / mean_dist
logging.info(f"Autoscale {scale}")
else:
scale = 1
# scale the points
data["pos"] = data["pos"] * scale
# if too musch points and no subsample iteratively compute the latent vectors
if data["pos"].shape[2] > 100000 and ("gen_subsample_manifold" not in config or config["gen_subsample_manifold"] is None):
# create the KDTree
pos = data["pos"][0].cpu().transpose(0,1).numpy()
tree = KDTree(pos)
# create the latent storage
latent = torch.zeros((pos.shape[0], config["network_latent_size"]), dtype=torch.float)
counts = torch.zeros((pos.shape[0],), dtype=torch.float)
n_views = 3
logging.info(f"Latent computation - {n_views} views")
for current_value in range(0,n_views):
while counts.min() < current_value+1:
valid_ids = np.argwhere(counts.cpu().numpy()==current_value)
# print(valid_ids.shape)
pt_id = torch.randint(0, valid_ids.shape[0], (1,)).item()
pt = pos[valid_ids[pt_id]]
k = 100000
distances, neighbors = tree.query(pt, k=k)
neighbors = neighbors[0]
data_partial = {
"pos": data["pos"][0].transpose(1,0)[neighbors].transpose(1,0).unsqueeze(0),
"x": data["x"][0].transpose(1,0)[neighbors].transpose(1,0).unsqueeze(0)
}
partial_latent = net.get_latent(data_partial, with_correction=False)["latents"]
latent[neighbors] += partial_latent[0].cpu().numpy().transpose(1,0)
counts[neighbors] += 1
latent = latent / counts.unsqueeze(1)
latent = latent.transpose(1,0).unsqueeze(0).to(device)
data["latents"] = latent
latent = data
logging.info("Latent done")
elif "gen_subsample_manifold" in config and config["gen_subsample_manifold"] is not None:
logging.info("Submanifold sampling")
# create the KDTree
pos = data["pos"][0].cpu().transpose(0,1).numpy()
# create the latent storage
latent = torch.zeros((pos.shape[0], config["network_latent_size"]), dtype=torch.float)
counts = torch.zeros((pos.shape[0],), dtype=torch.float)
iteration = 0
for current_value in range(config["gen_subsample_manifold_iter"]):
while counts.min() < current_value+1:
# print("iter", iteration, current_value)
valid_ids = torch.tensor(np.argwhere(counts.cpu().numpy()==current_value)[:,0]).long()
if pos.shape[0] >= config["gen_subsample_manifold"]:
ids = torch.randperm(valid_ids.shape[0])[:config["gen_subsample_manifold"]]
ids = valid_ids[ids]
if ids.shape[0] < config["gen_subsample_manifold"]:
ids = torch.cat([ids, torch.randperm(pos.shape[0])[:config["gen_subsample_manifold"] - ids.shape[0]]], dim=0)
assert(ids.shape[0] == config["gen_subsample_manifold"])
else:
ids = torch.arange(pos.shape[0])
data_partial = {
"pos": data["pos"][0].transpose(1,0)[ids].transpose(1,0).unsqueeze(0),
"x": data["x"][0].transpose(1,0)[ids].transpose(1,0).unsqueeze(0)
}
partial_latent = net.get_latent(data_partial, with_correction=False)["latents"]
latent[ids] += partial_latent[0].cpu().numpy().transpose(1,0)
counts[ids] += 1
iteration += 1
latent = latent / counts.unsqueeze(1)
latent = latent.transpose(1,0).unsqueeze(0).to(device)
data["latents"] = latent
latent = data
else:
# all prediction
latent = net.get_latent(data, with_correction=False)
if "gen_resolution_metric" in config and config["gen_resolution_metric"] is not None:
step = config['gen_resolution_metric'] * scale
resolution = None
elif config["gen_resolution_global"] is not None:
step = None
resolution = config["gen_resolution_global"]
else:
raise ValueError("You must specify either a global resolution or a metric resolution")
# print("POS", data["pos"].shape)
mesh = export_mesh_and_refine_vertices_region_growing_v2(
net, latent,
resolution=resolution,
padding=1,
mc_value=0,
device=device,
input_points=data["pos"][0].cpu().numpy().transpose(1,0),
refine_iter=config["gen_refine_iter"],
out_value=1,
step=step
)
if mesh is not None:
vertices = np.asarray(mesh.vertices)
vertices = vertices / scale
vertices = o3d.utility.Vector3dVector(vertices)
mesh.vertices = vertices
# print(os.path.join(savedir_mesh, object_name))
if os.path.splitext(object_name)[1] == ".ply":
o3d.io.write_triangle_mesh(os.path.join(savedir_mesh, object_name), mesh)
else:
o3d.io.write_triangle_mesh(os.path.join(savedir_mesh, object_name+".ply"), mesh)
else:
logging.warning("mesh is None")
def replace_values_of_config(config, config_update):
for key, value in config_update.items():
if key not in config:
print(f"replace warning unknown key '{key}'")
continue
if isinstance(value, dict):
config[key] = replace_values_of_config(config[key], value)
else:
config[key] = value
return config
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
logging.getLogger("trimesh").setLevel(logging.CRITICAL)
parser = argparse.ArgumentParserFromFile(description='Process some integers.')
parser.add_argument('--config_default', type=str, default="configs/config_default.yaml")
parser.add_argument('--config', type=str, required=True)
parser.add_argument('--num_mesh', type=int, default=None)
parser.add_argument("--gen_refine_iter", type=int, default=10)
parser.update_file_arg_names(["config_default", "config"])
config = parser.parse(use_unknown=True)
logging.getLogger().setLevel(config["logging"])
if config["logging"] == "DEBUG":
config["threads"] = 0
# config["save_dir"] = os.path.dirname(config["config"])
main(config)