-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdata_prep.py
452 lines (355 loc) · 14 KB
/
data_prep.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
import os
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(BASE_DIR + '/scripts/')
import urllib.request
from multiprocessing import Pool
import binvox_rw
import scipy.io as sio
import numpy as np
from tqdm import tqdm
from glob import glob
import random
import shutil
from PIL import Image
import argparse
from scipy import ndimage
from subprocess import call
import torch
import utils
from time import time
parser = argparse.ArgumentParser(description='Dataset prep for image to 3D object super resolution')
parser.add_argument('-no','--num_objects', default=500, help='number of objects to be converted', type = int)
args = parser.parse_args()
#labels for the union of the core shapenet classes and the ikea dataset classes
labels = {'04379243':'table','03211117':'monitor','04401088':'cellphone','04530566': 'watercraft', '03001627' : 'chair','03636649' : 'lamp', '03691459': 'speaker' , '02828884':'bench',
'02691156': 'plane', '02808440': 'bathtub', '02871439': 'bookcase',
'02773838': 'bag', '02801938': 'basket', '02828884' : 'bench','02880940': 'bowl' ,
'02924116': 'bus', '02933112': 'cabinet', '02942699': 'camera', '02958343': 'car', '03207941': 'dishwasher',
'03337140': 'file', '03624134': 'knife', '03642806': 'laptop', '03710193': 'mailbox',
'03761084': 'microwave', '03928116': 'piano', '03938244':'pillow', '03948459': 'pistol', '04004475': 'printer',
'04099429': 'rocket', '04256520': 'sofa', '04554684': 'washer', '04090263': 'rifle'}
objects = ['bench','cabinet','car','cellphone','chair','lamp','monitor','plane','rifle','sofa','speaker','table','watercraft']
wanted_classes=[]
for l in labels:
if labels[l] in objects:
wanted_classes.append(l)
debug_mode = False # change to make all of the called scripts print their errors and warnings
if debug_mode:
io_redirect = ''
else:
io_redirect = ' > /dev/null 2>&1'
# make data directories
if not os.path.exists('data/objects/'):
os.makedirs('data/objects/')
# download .obj obect files
def download():
with open('scripts/binvox_file_locations.txt','rb') as f: # location of all the binvoxes for shapenet's core classes
content = f.readlines()
# make data sub-directories for each class
for s in wanted_classes:
obj = 'data/objects/' + labels[s]+'/'
if not os.path.exists(obj):
os.makedirs(obj)
# search object for correct object classes
binvox_urls = []
obj_urls = []
for file in content:
file = str(file)
current_class = file.split('/')
if current_class[1] in wanted_classes:
if '_' in current_class[3]: continue
if 'presolid' in current_class[3]: continue
obj_urls.append(['http://shapenet.cs.stanford.edu/shapenet/obj-zip/ShapeNetCore.v1/'+file.split('/')[1]+'/'+file.split('/')[2]+'/model.obj', 'data/objects/'+labels[current_class[1]]+ '/'+ current_class[2]+'.obj'])
# get randomized sample from each object class of correct size
random.shuffle(obj_urls)
final_urls = []
dictionary = {}
for o in obj_urls:
obj_class = o[1].split('/')[-2]
if obj_class in dictionary:
dictionary[obj_class] += 1
if dictionary[obj_class]> args.num_objects:
continue
else:
dictionary[obj_class] = 1
final_urls.append(o)
# parallel downloading of object .obj files
pool = Pool(processes=16)
pbar = tqdm(pool.imap_unordered(down, final_urls), total=len(final_urls))
pbar.set_description(f"Downloading Meshes")
for _ in pbar:
pass
# this take object files and makes then a more managable size
# this is only done for training the latent loss
# it makes it far quicker to load the object during training
def manage_objects():
commands = []
for s in wanted_classes:
# final all downloaded objects from the class
objs = glob('data/objects/' + labels[s]+'/*.obj')
location_meshinfo = 'data/mesh_info/' + labels[s]+'/'
location_obj = 'data/managable_objects/' + labels[s]+'/'
if not os.path.exists(location_meshinfo):
os.makedirs(location_meshinfo)
if not os.path.exists(location_obj):
os.makedirs(location_obj)
l = 0
for o in objs:
name = o.split('/')[-1][:-4]
file_name_mesh = location_meshinfo+ name
file_name_new_obj = location_obj + name + '.obj'
cmd = 'blender scripts/manage.blend -b -P scripts/blender_convert.py -- %s %s %s' %( o, file_name_mesh,file_name_new_obj )
commands.append(cmd)
random.shuffle(commands)
pool = Pool(processes=16)
pbar = tqdm(pool.imap_unordered(call, commands), total=len(commands))
pbar.set_description(f"Downscaling meshes")
for _ in pbar:
pass
message = 'The blender commands failed. Please check why using the following command: '
assert len(glob('data/managable_objects/' + labels[s]+'/*.obj')) > 0, message + cmd
# converts obj files to binvox, is an intermediary for voxel computation
def binvox():
commands =[]
for s in wanted_classes:
dirs = glob('data/managable_objects/' + labels[s]+'/*.obj')
count = 0
for d in (dirs):
command = 'scripts/binvox ' + d + ' -d ' + str(32)+ ' -pb -cb -c -e' # this executable can be found at http://www.patrickmin.com/binvox/ ,
# -d x idicates resoltuion will be x by x by x , -pb is to stop the visualization, the rest of the commnads are to help make the object water tight
commands.append(command)
random.shuffle(commands)
pool = Pool(processes=16)
pbar = tqdm(pool.imap_unordered(call, commands), total=len(commands))
pbar.set_description(f"converting meshes to small binvoxes")
for _ in pbar:
pass
message = 'The binvox executable failed. Please check its permissions, and that it can run properly from the commandline using the following command: '
assert len(glob('data/managable_objects/' + labels[s]+'/*.binvox')) > 0, message + command
# converts binvox files to voxel files
def convert_bin():
models = []
for s in wanted_classes:
directory = 'data/voxels/'+labels[s] +'/'
# find all binvoxes
models += glob('data/managable_objects/'+labels[s]+'/*.binvox')
if not os.path.exists(directory):
os.makedirs(directory)
cur_class = ''
random.shuffle(models)
pbar = tqdm(models)
pbar.set_description(f"Converting binvoxes to voxels")
for m in pbar:
with open(m, 'rb') as f:
try:
model = binvox_rw.read_as_3d_array(f).data
except ValueError:
continue
directory = 'data/voxels/' + m.split('/')[-2] + '/'
# remove internals from models
# I think this makes it easier to learn
positions = np.where(model != 0 )
new_mod = np.zeros(model.shape)
for i, j, k in zip(*positions):
# identifies if current voxel has an exposed face
if np.sum(model[i-1:i+2, j-1:j+2, k-1:k+2]) < 27:
new_mod[i,j,k] = 1
# save as np array
sio.savemat(directory +m.split('/')[-1][:-7], {'model': new_mod.astype(np.uint8)})
# these are two simple functions for parallel processing
# down() downloads , and call() calls functions
def down(url):
urllib.request.urlretrieve(url[0], url[1])
def call(command):
os.system('%s %s' % (command, io_redirect))
# calculates the surface of an object
def surface_computation(data):
dim = data.shape[0]
a,b,c = np.where(data == 1)
large = int(dim *1.5)
big_list = [[[[-1,large]for j in range(dim)] for i in range(dim)] for k in range(3)]
# over the whole object extract for each face the first and last occurance of a voxel at each pixel
# we take highest for convinience
for i,j,k in zip(a,b,c):
big_list[0][i][j][0] = (max(k,big_list[0][i][j][0]))
big_list[0][i][j][1] = (min(k,big_list[0][i][j][1]))
big_list[1][i][k][0] = (max(j,big_list[1][i][k][0]))
big_list[1][i][k][1] = (min(j,big_list[1][i][k][1]))
big_list[2][j][k][0] = (max(i,big_list[2][j][k][0]))
big_list[2][j][k][1] = (min(i,big_list[2][j][k][1]))
faces = np.zeros((6,dim,dim)) # will hold odms
for i in range(dim):
for j in range(dim):
faces[0,i,j] = dim -1 - big_list[0][i][j][0] if big_list[0][i][j][0] > -1 else dim
# we subtract from the (dimension -1) as we computed the last occurance, instead of the first for half of the faces
faces[1,i,j] = big_list[0][i][j][1] if big_list[0][i][j][1] < large else dim
faces[2,i,j] = dim -1 - big_list[1][i][j][0] if big_list[1][i][j][0] > -1 else dim
faces[3,i,j] = big_list[1][i][j][1] if big_list[1][i][j][1] < large else dim
faces[4,i,j] = dim -1 - big_list[2][i][j][0] if big_list[2][i][j][0] > -1 else dim
faces[5,i,j] = big_list[2][i][j][1] if big_list[2][i][j][1] < large else dim
return faces
def calc_surface():
dims = 128
# first render as voxel array at high resolution
models = []
for s in wanted_classes:
models += glob('data/objects/' + labels[s]+'/*.obj')
commands =[]
random.shuffle(models)
for m in models:
command = 'scripts/binvox ' + m + ' -d ' + str(dims)+ ' -pb -cb -c -e' # this executable can be found at http://www.patrickmin.com/binvox/ ,
# -d x idicates resoltuion will be x by x by x , -pb is to stop the visualization, the rest of the commnads are to help make the object water tight
commands.append(command)
pool = Pool(processes=16)
pbar = tqdm(pool.imap_unordered(call, commands), total=len(commands))
pbar.set_description(f"Making large binvoxes from meshes")
for _ in pbar:
pass
message = 'The binvox executable failed. Please check its permissions, and that it can run properly from the commandline using the following command: '
assert len(glob('data/objects/' + labels[s]+'/*.binvox')) > 0, message + command
models = []
for s in wanted_classes:
models += glob('data/objects/'+labels[s]+'/*.binvox')
location = 'data/surfaces/'+labels[s] +'/'
if not os.path.exists(location):
os.makedirs(location)
random.shuffle(models)
pbar = tqdm(models)
pbar.set_description(f"Extracting point cloud from converted objects")
for m in pbar:
location = 'data/surfaces/'+m.split('/')[-2] +'/'
with open(m, 'rb') as f:
try:
model = binvox_rw.read_as_3d_array(f).data
except ValueError:
continue
faces = surface_computation(model)
# calculate low resolution version, to make watertight
high, low = dims, 32
down = high // low
a,b,c = np.where(model==1)
low_model = np.zeros((low,low,low))
for x,y,z in zip(a,b,c):
low_model[ x//down, y//down, z//down] =1
# fill internals
low_model[ndimage.binary_fill_holes(low_model)] = 1
# obtain surface projections
corrected = np.zeros((high,high,high))
for i in range(low):
for j in range(low):
for k in range(low):
corrected[i*down: (i+1)*down, j*down:(j+1)*down, k*down:(k+1)*down] = low_model[i,j,k]
# carve away from low res model
for i in range(high):
for j in range(high):
if faces[0,i,j] <high:
corrected[i,j,int((high - faces[0,i,j])):high]=0
else:
corrected[i,j,:] =0
if faces[1,i,j] <high:
corrected[i,j,0:int(faces[1,i,j])]=0
else:
corrected[i,j,:] =0
if faces[2,i,j] <high:
corrected[i,int((high - faces[2,i,j])):high, j] =0
else:
corrected[i,:,j] =0
if faces[3,i,j] <high:
corrected[i,0:int(faces[3,i,j]), j] =0
else:
corrected[i,:,j] =0
if faces[4,i,j] <high:
corrected[int((high - faces[4,i,j])):high,i,j] =0
else:
corrected[:,i,j] =0
if faces[5,i,j] <high:
corrected[0:int(faces[5,i,j]),i,j] =0
else:
corrected[:,i,j] =0
corrected[ndimage.binary_fill_holes(corrected)] = 1
positions = np.where(corrected != 0 )
new_mod = np.zeros(corrected.shape)
points = []
# get only surface objects
for i, j, k in zip(*positions):
# identifies if current voxel has an exposed face
if np.sum(corrected[i-1:i+2, j-1:j+2, k-1:k+2]) < 27:
points.append([i,j,k])
voxel_points = np.array(points).astype(float)
obj = m[:-7] + '.obj'
try:
obj = utils.ObjLoader(obj)
except ValueError:
continue
voxel_points = np.array(voxel_points)
mesh_points = np.array(obj.vertices)
# make computed surface be same size as origional obejct
xx = np.amax(mesh_points[:,0]) - np.amin(mesh_points[:,0])
xx_v = np.amax(voxel_points[:,0]) - np.amin(voxel_points[:,0])
x_diff = xx/xx_v
yy = np.amax(mesh_points[:,1]) - np.amin(mesh_points[:,1])
yy_v = np.amax(voxel_points[:,1]) - np.amin(voxel_points[:,1])
y_diff = yy/yy_v
zz = np.amax(mesh_points[:,2]) - np.amin(mesh_points[:,2])
zz_v = np.amax(voxel_points[:,2]) - np.amin(voxel_points[:,2])
z_diff = zz/zz_v
voxel_points*=[x_diff, y_diff, z_diff]
xx = np.amax(mesh_points[:,0])
xx_v = np.amax(voxel_points[:,0])
x_diff = xx-xx_v
yy = np.amax(mesh_points[:,1])
yy_v = np.amax(voxel_points[:,1])
y_diff = yy-yy_v
zz = np.amax(mesh_points[:,2])
zz_v = np.amax(voxel_points[:,2])
z_diff = zz-zz_v
voxel_points+=[x_diff, y_diff, z_diff]
while voxel_points.shape[0] < 10000:
voxel_points = np.concatenate((voxel_points, voxel_points))
sio.savemat(location + m.split('/')[-1][:-7] , {'points': voxel_points})
def download_images():
print('downloading shapenet images')
command = 'wget http://cvgl.stanford.edu/data2/ShapeNetRendering.tgz -O data/images.tgz'
os.system(command)
print('extracting shapenet images')
command = 'tar -xzf data/images.tgz --directory data/'
os.system(command)
os.rename('data/ShapeNetRendering','data/images' )
print('Splitting dataset in training, valudation, and tests.')
folders = glob('data/images/*/')
for folder in tqdm(folders):
im_fol = glob( folder + '*')
if not os.path.exists(folder + 'train'):
os.mkdir(folder + 'train')
os.mkdir(folder + 'valid')
os.mkdir(folder + 'test')
first = (7*len(im_fol))//10
second = (8*len(im_fol))//10
train = im_fol[:first]
valid = im_fol[first:second]
test = im_fol[second:]
for ex in train:
source = ex
dest = folder + 'train/' + ex.split('/')[-1]
shutil.move(source, dest)
for ex in valid:
source = ex
dest = folder + 'valid/' + ex.split('/')[-1]
shutil.move(source, dest)
for ex in test:
source = ex
dest = folder + 'test/' + ex.split('/')[-1]
shutil.move(source, dest)
for folder in folders:
class_num = folder.split('/')[-2]
class_obj = labels[class_num]
os.rename(folder, folder[:-len(class_num)-1] + class_obj)
download()
manage_objects()
binvox()
convert_bin()
calc_surface()
download_images()
print ('finished eratin')