-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgeneration_iterator.py
82 lines (66 loc) · 2.55 KB
/
generation_iterator.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
import os
import traceback
import numpy as np
from tqdm import tqdm
from multiprocessing import Pool
# def gen_iterator(out_path, dataset, gen_p , buff_p, start,end):
def gen_iterator(out_path, dataset, gen_p, shuffle=True):
global gen
gen = gen_p
if not os.path.exists(out_path):
os.makedirs(out_path)
print(out_path)
# can be run on multiple machines if dataset is shuffled (already generated objects are skipped)
loader = dataset.get_loader(shuffle=shuffle)
data_tupels = []
for i, data in tqdm(enumerate(loader)):
path = os.path.normpath(data['path'][0])
path_end = path.split(os.sep)[-1]
splits = path_end.split('.')
path_end = splits[0]
export_path = out_path + '/generation/{}/{}/'.format(path.split(os.sep)[-2], path_end)
# check if existing file is empty
if os.path.exists(export_path + 'surface_reconstruction.off'):
file = open(export_path + 'surface_reconstruction.off', "r")
line_count = 0
for line in file:
line_count += 1
if line_count > 5:
break
if line_count > 5:
print('Path exists - skip! {}'.format(export_path))
continue
if line_count < 5:
print('Empty mesh detected! {}'.format(export_path))
try:
if len(data_tupels) >= 20:
create_meshes(data_tupels)
data_tupels = []
logits = gen.generate_mesh(data)
if 'input_pc' in data:
data['inputs'] = data['input_pc']
del data['input_pc']
data_tupels.append((logits, data, export_path, ''))
except Exception as err:
print('Error with {}: {}'.format(data['path'][0], traceback.format_exc()))
try:
create_meshes(data_tupels)
except Exception as err:
print('Error with {}: {}'.format(data['path'][0], traceback.format_exc()))
def save_mesh(data_tupel):
logits, data, export_path, name = data_tupel
if gen.method == 'mcubes':
mesh = gen.mesh_from_logits_mcubes(logits)
else:
mesh = gen.mesh_from_logits_MISE(logits)
if not os.path.exists(export_path):
os.makedirs(export_path)
exp_path = export_path + name + 'surface_reconstruction.off'
mesh.export(exp_path)
if not os.path.exists(export_path + 'data.npz'):
np.savez(export_path + 'data.npz', data)
def create_meshes(data_tupels):
p = Pool(10)
p.map(save_mesh, data_tupels)
p.close()
p.join()