forked from facebookresearch/DeepSDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_data.py
executable file
·268 lines (208 loc) · 7.91 KB
/
preprocess_data.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
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserved.
import argparse
import concurrent.futures
import json
import logging
import os
import subprocess
import deep_sdf
import deep_sdf.workspace as ws
def filter_classes_glob(patterns, classes):
import fnmatch
passed_classes = set()
for pattern in patterns:
passed_classes = passed_classes.union(
set(filter(lambda x: fnmatch.fnmatch(x, pattern), classes))
)
return list(passed_classes)
def filter_classes_regex(patterns, classes):
import re
passed_classes = set()
for pattern in patterns:
regex = re.compile(pattern)
passed_classes = passed_classes.union(set(filter(regex.match, classes)))
return list(passed_classes)
def filter_classes(patterns, classes):
if patterns[0] == "glob":
return filter_classes_glob(patterns, classes[1:])
elif patterns[0] == "regex":
return filter_classes_regex(patterns, classes[1:])
else:
return filter_classes_glob(patterns, classes)
def process_mesh(mesh_filepath, target_filepath, executable, additional_args):
logging.info(mesh_filepath + " --> " + target_filepath)
command = [executable, "-m", mesh_filepath, "-o", target_filepath] + additional_args
subproc = subprocess.Popen(command, stdout=subprocess.DEVNULL)
subproc.wait()
def append_data_source_map(data_dir, name, source):
data_source_map_filename = ws.get_data_source_map_filename(data_dir)
print("data sources stored to " + data_source_map_filename)
data_source_map = {}
if os.path.isfile(data_source_map_filename):
with open(data_source_map_filename, "r") as f:
data_source_map = json.load(f)
if name in data_source_map:
if not data_source_map[name] == os.path.abspath(source):
raise RuntimeError(
"Cannot add data with the same name and a different source."
)
else:
data_source_map[name] = os.path.abspath(source)
with open(data_source_map_filename, "w") as f:
json.dump(data_source_map, f, indent=2)
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="Pre-processes data from a data source and append the results to "
+ "a dataset.",
)
arg_parser.add_argument(
"--data_dir",
"-d",
dest="data_dir",
required=True,
help="The directory which holds all preprocessed data.",
)
arg_parser.add_argument(
"--source",
"-s",
dest="source_dir",
required=True,
help="The directory which holds the data to preprocess and append.",
)
arg_parser.add_argument(
"--name",
"-n",
dest="source_name",
default=None,
help="The name to use for the data source. If unspecified, it defaults to the "
+ "directory name.",
)
arg_parser.add_argument(
"--split",
dest="split_filename",
required=True,
help="A split filename defining the shapes to be processed.",
)
arg_parser.add_argument(
"--skip",
dest="skip",
default=False,
action="store_true",
help="If set, previously-processed shapes will be skipped",
)
arg_parser.add_argument(
"--threads",
dest="num_threads",
default=8,
help="The number of threads to use to process the data.",
)
arg_parser.add_argument(
"--test",
"-t",
dest="test_sampling",
default=False,
action="store_true",
help="If set, the script will produce SDF samplies for testing",
)
arg_parser.add_argument(
"--surface",
dest="surface_sampling",
default=False,
action="store_true",
help="If set, the script will produce mesh surface samples for evaluation. "
+ "Otherwise, the script will produce SDF samples for training.",
)
deep_sdf.add_common_args(arg_parser)
args = arg_parser.parse_args()
deep_sdf.configure_logging(args)
additional_general_args = []
deepsdf_dir = os.path.dirname(os.path.abspath(__file__))
if args.surface_sampling:
executable = os.path.join(deepsdf_dir, "bin/SampleVisibleMeshSurface")
subdir = ws.surface_samples_subdir
extension = ".ply"
else:
executable = os.path.join(deepsdf_dir, "bin/PreprocessMesh")
subdir = ws.sdf_samples_subdir
extension = ".npz"
if args.test_sampling:
additional_general_args += ["-t"]
with open(args.split_filename, "r") as f:
split = json.load(f)
if args.source_name is None:
args.source_name = os.path.basename(os.path.normpath(args.source_dir))
dest_dir = os.path.join(args.data_dir, subdir, args.source_name)
logging.info(
"Preprocessing data from "
+ args.source_dir
+ " and placing the results in "
+ dest_dir
)
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
if args.surface_sampling:
normalization_param_dir = os.path.join(
args.data_dir, ws.normalization_param_subdir, args.source_name
)
if not os.path.isdir(normalization_param_dir):
os.makedirs(normalization_param_dir)
append_data_source_map(args.data_dir, args.source_name, args.source_dir)
class_directories = split[args.source_name]
meshes_targets_and_specific_args = []
for class_dir in class_directories:
class_path = os.path.join(args.source_dir, class_dir)
instance_dirs = class_directories[class_dir]
logging.debug(
"Processing " + str(len(instance_dirs)) + " instances of class " + class_dir
)
target_dir = os.path.join(dest_dir, class_dir)
if not os.path.isdir(target_dir):
os.mkdir(target_dir)
for instance_dir in instance_dirs:
shape_dir = os.path.join(class_path, instance_dir)
processed_filepath = os.path.join(target_dir, instance_dir + extension)
if args.skip and os.path.isfile(processed_filepath):
logging.debug("skipping " + processed_filepath)
continue
try:
mesh_filename = deep_sdf.data.find_mesh_in_directory(shape_dir)
specific_args = []
if args.surface_sampling:
normalization_param_target_dir = os.path.join(
normalization_param_dir, class_dir
)
if not os.path.isdir(normalization_param_target_dir):
os.mkdir(normalization_param_target_dir)
normalization_param_filename = os.path.join(
normalization_param_target_dir, instance_dir + ".npz"
)
specific_args = ["-n", normalization_param_filename]
meshes_targets_and_specific_args.append(
(
os.path.join(shape_dir, mesh_filename),
processed_filepath,
specific_args,
)
)
except deep_sdf.data.NoMeshFileError:
logging.warning("No mesh found for instance " + instance_dir)
except deep_sdf.data.MultipleMeshFileError:
logging.warning("Multiple meshes found for instance " + instance_dir)
with concurrent.futures.ThreadPoolExecutor(
max_workers=int(args.num_threads)
) as executor:
for (
mesh_filepath,
target_filepath,
specific_args,
) in meshes_targets_and_specific_args:
executor.submit(
process_mesh,
mesh_filepath,
target_filepath,
executable,
specific_args + additional_general_args,
)
executor.shutdown()