-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_input_data.py
333 lines (290 loc) · 11.5 KB
/
load_input_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
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
import glob
import json
import multiprocessing
import os
import platform
import random
import subprocess
import tempfile
import time
import zipfile
from functools import partial
from typing import Any, Dict, List, Literal, Optional, Union
import fsspec
import GPUtil
import pandas as pd
from loguru import logger
import objaverse.xl as oxl
from objaverse.utils import get_uid_from_str
RED = "\033[91m"
ENDC = "\033[0m"
def log_processed_object(csv_filename: str, *args) -> None:
args = ",".join([str(arg) for arg in args])
dirname = os.path.expanduser("logs")
os.makedirs(dirname, exist_ok=True)
with open(os.path.join(dirname, csv_filename), "a", encoding="utf-8") as f:
f.write(f"{time.time()},{args}\n")
def zipdir(path: str, ziph: zipfile.ZipFile) -> None:
for root, dirs, files in os.walk(path):
for file in files:
arcname = os.path.join(os.path.basename(root), file)
ziph.write(os.path.join(root, file), arcname=arcname)
def handle_new_object(
local_path: str,
file_identifier: str,
sha256: str,
metadata: Dict[str, Any],
log_file: str = "handle-new-object.csv",
) -> None:
log_processed_object(log_file, file_identifier, sha256)
def handle_modified_object(
local_path: str,
file_identifier: str,
new_sha256: str,
old_sha256: str,
metadata: Dict[str, Any],
num_renders: int,
render_dir: str,
only_northern_hemisphere: bool,
gpu_devices: Union[int, List[int]],
render_timeout: int,
) -> None:
success = handle_found_object(
local_path=local_path,
file_identifier=file_identifier,
sha256=new_sha256,
metadata=metadata,
num_renders=num_renders,
render_dir=render_dir,
only_northern_hemisphere=only_northern_hemisphere,
gpu_devices=gpu_devices,
render_timeout=render_timeout,
successful_log_file=None,
failed_log_file=None,
)
if success:
log_processed_object(
"handle-modified-object-successful.csv",
file_identifier,
old_sha256,
new_sha256,
)
else:
log_processed_object(
"handle-modified-object-failed.csv",
file_identifier,
old_sha256,
new_sha256,
)
def handle_missing_object(
file_identifier: str,
sha256: str,
metadata: Dict[str, Any],
log_file: str = "handle-missing-object.csv",
) -> None:
log_processed_object(log_file, file_identifier, sha256)
def save_file(local_path: str,
file_identifier: str,
sha256: str,
metadata: Dict[str, Any],
num_renders: int,
render_dir: str,
only_northern_hemisphere: bool,
gpu_devices: Union[int, List[int]],
render_timeout: int,
successful_log_file: Optional[str] = "handle-found-object-successful.csv",
failed_log_file: Optional[str] = "handle-found-object-failed.csv",
) -> bool:
file_name = os.path.basename(file_identifier)
obj_file_path = os.path.join("data", file_name)
with open(local_path, "rb") as f:
file_data = f.read()
with open(obj_file_path, "wb") as f:
f.write(file_data)
return True
def handle_found_object(
local_path: str,
file_identifier: str,
sha256: str,
metadata: Dict[str, Any],
num_renders: int,
render_dir: str,
only_northern_hemisphere: bool,
gpu_devices: Union[int, List[int]],
render_timeout: int,
successful_log_file: Optional[str] = "handle-found-object-successful.csv",
failed_log_file: Optional[str] = "handle-found-object-failed.csv",
) -> bool:
save_uid = get_uid_from_str(file_identifier)
args = f"--object_path '{local_path}' --num_renders {num_renders}"
using_gpu: bool = True
gpu_i = 0
if isinstance(gpu_devices, int) and gpu_devices > 0:
num_gpus = gpu_devices
gpu_i = random.randint(0, num_gpus - 1)
elif isinstance(gpu_devices, list):
gpu_i = random.choice(gpu_devices)
elif isinstance(gpu_devices, int) and gpu_devices == 0:
using_gpu = False
else:
raise ValueError(
f"gpu_devices must be an int > 0, 0, or a list of ints. Got {gpu_devices}."
)
with tempfile.TemporaryDirectory() as temp_dir:
target_directory = os.path.join(temp_dir, save_uid)
os.makedirs(target_directory, exist_ok=True)
args += f" --output_dir {target_directory}"
if platform.system() == "Linux" and using_gpu:
args += " --engine BLENDER_EEVEE"
elif platform.system() == "Darwin" or (
platform.system() == "Linux" and not using_gpu
):
args += " --engine CYCLES"
else:
raise NotImplementedError(f"Platform {platform.system()} is not supported.")
if only_northern_hemisphere:
args += " --only_northern_hemisphere"
command = f"blender --background --python blender_scripts/dataset_rendering.py -- {args}"
if using_gpu:
command = f"export DISPLAY=:0.{gpu_i} && {command}"
result = subprocess.run(
["bash", "-c", command],
timeout=render_timeout,
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
)
if result.returncode != 0:
logger.error(result.stderr.decode())
logger.info(command)
png_files = glob.glob(os.path.join(target_directory, "*.png"))
metadata_files = glob.glob(os.path.join(target_directory, "*.json"))
npy_files = glob.glob(os.path.join(target_directory, "*.npy"))
logger.info(f"png_files: {len(png_files)} | npy_files: {len(npy_files)} | metadata: {len(metadata_files)}")
if (
(len(png_files) != num_renders)
or (len(npy_files) != num_renders)
or (len(metadata_files) != 1)
):
logger.error(
f"Found object {file_identifier} was not rendered successfully!"
)
if failed_log_file is not None:
log_processed_object(
failed_log_file,
file_identifier,
sha256,
)
return False
metadata_path = os.path.join(target_directory, "metadata.json")
with open(metadata_path, "r", encoding="utf-8") as f:
metadata_file = json.load(f)
metadata_file["sha256"] = sha256
metadata_file["file_identifier"] = file_identifier
metadata_file["save_uid"] = save_uid
metadata_file["metadata"] = metadata
with open(metadata_path, "w", encoding="utf-8") as f:
json.dump(metadata_file, f, indent=2, sort_keys=True)
# Keeps the {save_uid} directory structure when unzipped
with zipfile.ZipFile(
f"{target_directory}.zip", "w", zipfile.ZIP_DEFLATED
) as ziph:
zipdir(target_directory, ziph)
fs, path = fsspec.core.url_to_fs(render_dir)
fs.makedirs(os.path.join(path, "renders"), exist_ok=True)
fs.put(
os.path.join(f"{target_directory}.zip"),
os.path.join(path, "renders", f"{save_uid}.zip"),
)
if successful_log_file is not None:
logger.success(
f"Found object {file_identifier} rendered successfully!"
)
log_processed_object(successful_log_file, file_identifier, sha256)
return True
def sample_annotations(source, n_samples):
annotations = oxl.get_annotations(download_dir="data/annotations_data")
github_annotations = annotations[annotations['source'] == source]
github_annotations.sample(n_samples).to_csv("sample_df", index=False)
del annotations
del github_annotations
def get_example_objects() -> pd.DataFrame:
return pd.read_csv("3D_data.csv")
def render_objects(
render_dir: str = "data/input_data/",
download_dir: Optional[str] = "data/downloaded_data",
num_renders: int = 32,
processes: Optional[int] = None,
save_repo_format: Optional[Literal["zip", "tar", "tar.gz", "files"]] = "zip",
only_northern_hemisphere: bool = False,
render_timeout: int = 1000,
gpu_devices: Optional[Union[int, List[int]]] = None,
) -> None:
if platform.system() not in ["Linux", "Darwin"]:
raise NotImplementedError(
f"Platform {platform.system()} is not supported. Use Linux or MacOS."
)
if download_dir is None and save_repo_format is not None:
raise ValueError(
f"If {save_repo_format=} is not None, {download_dir=} must be specified."
)
if download_dir is not None and save_repo_format is None:
logger.warning(
f"GitHub repos will not save. While {download_dir=} is specified, {save_repo_format=} None."
)
parsed_gpu_devices: Union[int, List[int]] = 0
if gpu_devices is None:
parsed_gpu_devices = len(GPUtil.getGPUs())
logger.info(f"Using {parsed_gpu_devices} GPU devices for rendering.")
if processes is None:
processes = multiprocessing.cpu_count() * 3
objects = get_example_objects()
# assert objects.iloc[0]["fileType"] == "obj", (
# RED + "Currently only renders the .obj file, although just only few lines "
# "of replacement needed to make it work for all formats but "
# "developer is lazy" + ENDC)
objects = objects.copy()
logger.info(f"Provided {len(objects)} objects to render.")
fs, path = fsspec.core.url_to_fs(render_dir)
try:
zip_files = fs.glob(os.path.join(path, "renders", "*.zip"), refresh=True)
except TypeError:
zip_files = fs.glob(os.path.join(path, "renders", "*.zip"))
saved_ids = set(zip_file.split("/")[-1].split(".")[0] for zip_file in zip_files)
logger.info(f"Found {len(saved_ids)} objects already rendered.")
objects["saveUid"] = objects["fileIdentifier"].apply(get_uid_from_str)
objects = objects[~objects["saveUid"].isin(saved_ids)]
objects = objects.reset_index(drop=True)
logger.info(f"Rendering {len(objects)} new objects.")
objects = objects.sample(frac=1).reset_index(drop=True)
oxl.download_objects(
objects=objects,
processes=processes,
save_repo_format=save_repo_format,
download_dir=download_dir,
handle_found_object=partial(
handle_found_object,
render_dir=render_dir,
num_renders=num_renders,
only_northern_hemisphere=only_northern_hemisphere,
gpu_devices=parsed_gpu_devices,
render_timeout=render_timeout,
),
handle_new_object=handle_new_object,
handle_modified_object=partial(
handle_modified_object,
render_dir=render_dir,
num_renders=num_renders,
only_northern_hemisphere=only_northern_hemisphere,
gpu_devices=parsed_gpu_devices,
render_timeout=render_timeout,
),
handle_missing_object=handle_missing_object,
)
if __name__ == "__main__":
identifier = "https://github.com/mlivesu/LoopyCuts/blob/c36b81154a03e79208f83725b9f4542f30ee4285/test_data/impeller/impeller.obj"
annotations = oxl.get_annotations(download_dir="data/annotations_data")
annotations = annotations[annotations['source'] == "github"]
# annotations = annotations[(annotations['fileType'] == "obj") & (annotations['source'] == "github")][:10]
annotations.to_csv("3D_data.csv", index=False)
render_objects()