-
Notifications
You must be signed in to change notification settings - Fork 408
/
build_metadata.py
270 lines (249 loc) · 12.5 KB
/
build_metadata.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
import os
import shutil
import sys
import time
import importlib
import argparse
import numpy as np
import pandas as pd
from tqdm import tqdm
from easydict import EasyDict as edict
from concurrent.futures import ThreadPoolExecutor
import utils3d
def get_first_directory(path):
with os.scandir(path) as it:
for entry in it:
if entry.is_dir():
return entry.name
return None
def need_process(key):
return key in opt.field or opt.field == ['all']
if __name__ == '__main__':
dataset_utils = importlib.import_module(f'datasets.{sys.argv[1]}')
parser = argparse.ArgumentParser()
parser.add_argument('--output_dir', type=str, required=True,
help='Directory to save the metadata')
parser.add_argument('--field', type=str, default='all',
help='Fields to process, separated by commas')
parser.add_argument('--from_file', action='store_true',
help='Build metadata from file instead of from records of processings.' +
'Useful when some processing fail to generate records but file already exists.')
dataset_utils.add_args(parser)
opt = parser.parse_args(sys.argv[2:])
opt = edict(vars(opt))
os.makedirs(opt.output_dir, exist_ok=True)
os.makedirs(os.path.join(opt.output_dir, 'merged_records'), exist_ok=True)
opt.field = opt.field.split(',')
timestamp = str(int(time.time()))
# get file list
if os.path.exists(os.path.join(opt.output_dir, 'metadata.csv')):
print('Loading previous metadata...')
metadata = pd.read_csv(os.path.join(opt.output_dir, 'metadata.csv'))
else:
metadata = dataset_utils.get_metadata(**opt)
metadata.set_index('sha256', inplace=True)
# merge downloaded
df_files = [f for f in os.listdir(opt.output_dir) if f.startswith('downloaded_') and f.endswith('.csv')]
df_parts = []
for f in df_files:
try:
df_parts.append(pd.read_csv(os.path.join(opt.output_dir, f)))
except:
pass
if len(df_parts) > 0:
df = pd.concat(df_parts)
df.set_index('sha256', inplace=True)
if 'local_path' in metadata.columns:
metadata.update(df, overwrite=True)
else:
metadata = metadata.join(df, on='sha256', how='left')
for f in df_files:
shutil.move(os.path.join(opt.output_dir, f), os.path.join(opt.output_dir, 'merged_records', f'{timestamp}_{f}'))
# detect models
image_models = []
if os.path.exists(os.path.join(opt.output_dir, 'features')):
image_models = os.listdir(os.path.join(opt.output_dir, 'features'))
latent_models = []
if os.path.exists(os.path.join(opt.output_dir, 'latents')):
latent_models = os.listdir(os.path.join(opt.output_dir, 'latents'))
ss_latent_models = []
if os.path.exists(os.path.join(opt.output_dir, 'ss_latents')):
ss_latent_models = os.listdir(os.path.join(opt.output_dir, 'ss_latents'))
print(f'Image models: {image_models}')
print(f'Latent models: {latent_models}')
print(f'Sparse Structure latent models: {ss_latent_models}')
if 'rendered' not in metadata.columns:
metadata['rendered'] = [False] * len(metadata)
if 'voxelized' not in metadata.columns:
metadata['voxelized'] = [False] * len(metadata)
if 'num_voxels' not in metadata.columns:
metadata['num_voxels'] = [0] * len(metadata)
if 'cond_rendered' not in metadata.columns:
metadata['cond_rendered'] = [False] * len(metadata)
for model in image_models:
if f'feature_{model}' not in metadata.columns:
metadata[f'feature_{model}'] = [False] * len(metadata)
for model in latent_models:
if f'latent_{model}' not in metadata.columns:
metadata[f'latent_{model}'] = [False] * len(metadata)
for model in ss_latent_models:
if f'ss_latent_{model}' not in metadata.columns:
metadata[f'ss_latent_{model}'] = [False] * len(metadata)
# merge rendered
df_files = [f for f in os.listdir(opt.output_dir) if f.startswith('rendered_') and f.endswith('.csv')]
df_parts = []
for f in df_files:
try:
df_parts.append(pd.read_csv(os.path.join(opt.output_dir, f)))
except:
pass
if len(df_parts) > 0:
df = pd.concat(df_parts)
df.set_index('sha256', inplace=True)
metadata.update(df, overwrite=True)
for f in df_files:
shutil.move(os.path.join(opt.output_dir, f), os.path.join(opt.output_dir, 'merged_records', f'{timestamp}_{f}'))
# merge voxelized
df_files = [f for f in os.listdir(opt.output_dir) if f.startswith('voxelized_') and f.endswith('.csv')]
df_parts = []
for f in df_files:
try:
df_parts.append(pd.read_csv(os.path.join(opt.output_dir, f)))
except:
pass
if len(df_parts) > 0:
df = pd.concat(df_parts)
df.set_index('sha256', inplace=True)
metadata.update(df, overwrite=True)
for f in df_files:
shutil.move(os.path.join(opt.output_dir, f), os.path.join(opt.output_dir, 'merged_records', f'{timestamp}_{f}'))
# merge cond_rendered
df_files = [f for f in os.listdir(opt.output_dir) if f.startswith('cond_rendered_') and f.endswith('.csv')]
df_parts = []
for f in df_files:
try:
df_parts.append(pd.read_csv(os.path.join(opt.output_dir, f)))
except:
pass
if len(df_parts) > 0:
df = pd.concat(df_parts)
df.set_index('sha256', inplace=True)
metadata.update(df, overwrite=True)
for f in df_files:
shutil.move(os.path.join(opt.output_dir, f), os.path.join(opt.output_dir, 'merged_records', f'{timestamp}_{f}'))
# merge features
for model in image_models:
df_files = [f for f in os.listdir(opt.output_dir) if f.startswith(f'feature_{model}_') and f.endswith('.csv')]
df_parts = []
for f in df_files:
try:
df_parts.append(pd.read_csv(os.path.join(opt.output_dir, f)))
except:
pass
if len(df_parts) > 0:
df = pd.concat(df_parts)
df.set_index('sha256', inplace=True)
metadata.update(df, overwrite=True)
for f in df_files:
shutil.move(os.path.join(opt.output_dir, f), os.path.join(opt.output_dir, 'merged_records', f'{timestamp}_{f}'))
# merge latents
for model in latent_models:
df_files = [f for f in os.listdir(opt.output_dir) if f.startswith(f'latent_{model}_') and f.endswith('.csv')]
df_parts = []
for f in df_files:
try:
df_parts.append(pd.read_csv(os.path.join(opt.output_dir, f)))
except:
pass
if len(df_parts) > 0:
df = pd.concat(df_parts)
df.set_index('sha256', inplace=True)
metadata.update(df, overwrite=True)
for f in df_files:
shutil.move(os.path.join(opt.output_dir, f), os.path.join(opt.output_dir, 'merged_records', f'{timestamp}_{f}'))
# merge sparse structure latents
for model in ss_latent_models:
df_files = [f for f in os.listdir(opt.output_dir) if f.startswith(f'ss_latent_{model}_') and f.endswith('.csv')]
df_parts = []
for f in df_files:
try:
df_parts.append(pd.read_csv(os.path.join(opt.output_dir, f)))
except:
pass
if len(df_parts) > 0:
df = pd.concat(df_parts)
df.set_index('sha256', inplace=True)
metadata.update(df, overwrite=True)
for f in df_files:
shutil.move(os.path.join(opt.output_dir, f), os.path.join(opt.output_dir, 'merged_records', f'{timestamp}_{f}'))
# build metadata from files
if opt.from_file:
with ThreadPoolExecutor(max_workers=os.cpu_count()) as executor, \
tqdm(total=len(metadata), desc="Building metadata") as pbar:
def worker(sha256):
try:
if need_process('rendered') and metadata.loc[sha256, 'rendered'] == False and \
os.path.exists(os.path.join(opt.output_dir, 'renders', sha256, 'transforms.json')):
metadata.loc[sha256, 'rendered'] = True
if need_process('voxelized') and metadata.loc[sha256, 'rendered'] == True and metadata.loc[sha256, 'voxelized'] == False and \
os.path.exists(os.path.join(opt.output_dir, 'voxels', f'{sha256}.ply')):
try:
pts = utils3d.io.read_ply(os.path.join(opt.output_dir, 'voxels', f'{sha256}.ply'))[0]
metadata.loc[sha256, 'voxelized'] = True
metadata.loc[sha256, 'num_voxels'] = len(pts)
except Exception as e:
pass
if need_process('cond_rendered') and metadata.loc[sha256, 'cond_rendered'] == False and \
os.path.exists(os.path.join(opt.output_dir, 'renders_cond', sha256, 'transforms.json')):
metadata.loc[sha256, 'cond_rendered'] = True
for model in image_models:
if need_process(f'feature_{model}') and \
metadata.loc[sha256, f'feature_{model}'] == False and \
metadata.loc[sha256, 'rendered'] == True and \
metadata.loc[sha256, 'voxelized'] == True and \
os.path.exists(os.path.join(opt.output_dir, 'features', model, f'{sha256}.npz')):
metadata.loc[sha256, f'feature_{model}'] = True
for model in latent_models:
if need_process(f'latent_{model}') and \
metadata.loc[sha256, f'latent_{model}'] == False and \
metadata.loc[sha256, 'rendered'] == True and \
metadata.loc[sha256, 'voxelized'] == True and \
os.path.exists(os.path.join(opt.output_dir, 'latents', model, f'{sha256}.npz')):
metadata.loc[sha256, f'latent_{model}'] = True
for model in ss_latent_models:
if need_process(f'ss_latent_{model}') and \
metadata.loc[sha256, f'ss_latent_{model}'] == False and \
metadata.loc[sha256, 'voxelized'] == True and \
os.path.exists(os.path.join(opt.output_dir, 'ss_latents', model, f'{sha256}.npz')):
metadata.loc[sha256, f'ss_latent_{model}'] = True
pbar.update()
except Exception as e:
print(f'Error processing {sha256}: {e}')
pbar.update()
executor.map(worker, metadata.index)
executor.shutdown(wait=True)
# statistics
metadata.to_csv(os.path.join(opt.output_dir, 'metadata.csv'))
num_downloaded = metadata['local_path'].count() if 'local_path' in metadata.columns else 0
with open(os.path.join(opt.output_dir, 'statistics.txt'), 'w') as f:
f.write('Statistics:\n')
f.write(f' - Number of assets: {len(metadata)}\n')
f.write(f' - Number of assets downloaded: {num_downloaded}\n')
f.write(f' - Number of assets rendered: {metadata["rendered"].sum()}\n')
f.write(f' - Number of assets voxelized: {metadata["voxelized"].sum()}\n')
if len(image_models) != 0:
f.write(f' - Number of assets with image features extracted:\n')
for model in image_models:
f.write(f' - {model}: {metadata[f"feature_{model}"].sum()}\n')
if len(latent_models) != 0:
f.write(f' - Number of assets with latents extracted:\n')
for model in latent_models:
f.write(f' - {model}: {metadata[f"latent_{model}"].sum()}\n')
if len(ss_latent_models) != 0:
f.write(f' - Number of assets with sparse structure latents extracted:\n')
for model in ss_latent_models:
f.write(f' - {model}: {metadata[f"ss_latent_{model}"].sum()}\n')
f.write(f' - Number of assets with captions: {metadata["captions"].count()}\n')
f.write(f' - Number of assets with image conditions: {metadata["cond_rendered"].sum()}\n')
with open(os.path.join(opt.output_dir, 'statistics.txt'), 'r') as f:
print(f.read())