-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_syn_data.py
341 lines (276 loc) · 10.6 KB
/
create_syn_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
334
335
336
337
338
339
340
341
import numpy as np
import matplotlib.pyplot as plt
import itertools
import pickle
from pathlib import Path
import multiprocessing
import time
import json
import cv2
import os
import collections
import sys
sys.path.append('../')
import renderer
import co
from commons import get_patterns,get_rotation_matrix
def get_objs(shapenet_dir, obj_classes, num_perclass=100):
shapenet = {'chair': '03001627',
'airplane': '02691156',
'car': '02958343',
'watercraft': '04530566',
'camera': '02942699'}
obj_paths = []
for cls in obj_classes:
if cls not in shapenet.keys():
raise Exception('unknown class name')
ids = shapenet[cls]
obj_path = sorted(Path(f'{shapenet_dir}/{ids}').glob('**/models/*.obj'))
obj_paths += obj_path[:num_perclass]
print(f'found {len(obj_paths)} object paths')
objs = []
for obj_path in obj_paths:
if (obj_path.parent / "extractedn.npy").exists():
print(f'load {obj_path.parent / "extractedv.npy"}')
v = np.load(obj_path.parent / "extractedv.npy")
f = np.load(obj_path.parent / "extractedf.npy")
c = np.load(obj_path.parent / "extractedc.npy")
n = np.load(obj_path.parent / "extractedn.npy")
else:
print(f'load {obj_path}')
v, f, c, n = co.io3d.read_obj(obj_path)
diffs = v.max(axis=0) - v.min(axis=0)
v /= (0.5 * diffs.max())
v -= (v.min(axis=0) + 1)
f = f.astype(np.int32)
np.save(obj_path.parent / "extractedv.npy",v)
np.save(obj_path.parent / "extractedf.npy", f)
np.save(obj_path.parent / "extractedc.npy", c)
np.save(obj_path.parent / "extractedn.npy", n)
obj = (v,f,c, n)
objs.append(obj)
print(f'loaded {len(objs)} objects')
return objs
def get_mesh2(rng, rng_clr, ref_len, min_z=0):
# set up background board
verts, faces, normals, colors = [], [], [], []
v, f, n = co.geometry.xyplane(z=0, interleaved=True)
v[:,2] += -v[:,2].min() + rng.uniform(2,7)
v[:,:2] *= 5e2
v[:,2] = np.mean(v[:,2]) + (v[:,2] - np.mean(v[:,2])) * 5e2
c = np.full(v.shape[0], rng_clr.randint(0, ref_len))
verts.append(v)
faces.append(f)
normals.append(n)
colors.append(c)
# randomly sample 4 foreground objects for each scene
for shape_idx in range(4):
v, f, c, n = objs[rng.randint(0,len(objs))]
v, f, c, n = v.copy(), f.copy(), c.copy(), n.copy()
s = rng.uniform(0.25, 1)
v *= s
R = co.geometry.rotm_from_quat(co.geometry.quat_random(rng=rng))
v = v @ R.T
n = n @ R.T
v[:,2] += -v[:,2].min() + min_z + rng.uniform(0.5, 3)
v[:,:2] += rng.uniform(-0.5, 0.5, size=(1,2))
c = np.full(v.shape[0], rng_clr.randint(0, ref_len))
verts.append(v.astype(np.float32))
faces.append(f)
normals.append(n)
colors.append(c)
verts, faces = co.geometry.stack_mesh(verts, faces)
normals = np.vstack(normals).astype(np.float32)
colors = np.vstack(colors).astype(np.float32)
return verts, faces, colors, normals
def get_mesh(rng, rng_clr, ref_len, x, y, min_z=0):
# set up background board
verts, faces, normals, colors = [], [], [], []
v, f, n = co.geometry.xyplane(z=0, interleaved=True)
v[:,2] += -v[:,2].min() + rng.uniform(0.53,0.58)
v[:,:2] *= 5e2
v[:,2] = np.mean(v[:,2]) + (v[:,2] - np.mean(v[:,2])) * 5e2
c = np.full(v.shape[0], rng_clr.randint(0, ref_len))
verts.append(v)
faces.append(f)
normals.append(n)
colors.append(c)
# randomly sample 4 foreground objects for each scene
for shape_idx in range(4):
objidx = rng.randint(0,len(objs))
v, f, c, n = objs[objidx]
v, f, c, n = v.copy(), f.copy(), c.copy(), n.copy()
s = rng.uniform(0.3, 0.33)
v *= s
R = co.geometry.rotm_from_quat(co.geometry.quat_random(rng=rng))
v = v @ R.T
n = n @ R.T
v[:,2] += -v[:,2].min() + min_z + rng.uniform(0.40, 0.45)
v[:,0] += v[:,2]*x+rng.uniform(-0.15, 0.15)
v[:,1] += v[:,2]*y+rng.uniform(-0.15, 0.15)
c_idx=rng_clr.randint(ref_len, size=(c.max()+1))#np.full(v.shape[0], )
c = c_idx[c]
verts.append(v.astype(np.float32))
faces.append(f)
normals.append(n)
colors.append(c)
verts, faces = co.geometry.stack_mesh(verts, faces)
normals = np.vstack(normals).astype(np.float32)
colors = np.hstack(colors).astype(np.int32)
return verts, faces, colors, normals
def create_data(out_root, idx, n_samples, imsize, patterns, reflectance, camerasensitivity, illumination, wavelength, K, shiftcamera, shiftpattern, baseline, blend_im, noise, maxdisp, mindisp, track_length=4):
tic = time.time()
rng = np.random.RandomState()
rng_clr = np.random.RandomState()
rng.seed(idx)
rng_clr.seed(idx)
x_center=(imsize[1]/2-K[0,2]+shiftcamera)/K[0,0]
y_center=(imsize[0]/2-K[1,2])/K[1,1]
verts, faces, colors, normals = get_mesh(rng, rng_clr, reflectance.shape[0],x_center,y_center)
data = renderer.PyRenderInput(verts=verts.copy(), colors=colors.copy(), normals=normals.copy(), faces=faces.copy())
print(f'loading mesh for sample {idx+1}/{n_samples} took {time.time()-tic}[s]')
# let the camera point to the center
center = np.array([0,0,0.4], dtype=np.float32)
basevec = np.array([-baseline,0,0], dtype=np.float32)
unit = np.array([0,0,1],dtype=np.float32)
cam_x_ = rng.uniform(-0.05,0.05)
cam_y_ = rng.uniform(-0.05,0.05)
cam_z_ = rng.uniform(0,0)
ret = collections.defaultdict(list)
blend_im_rnd = np.clip(blend_im + rng.uniform(-0.1,0.1), 0,1)
# capture the same static scene from different view points as a track
for ind in range(track_length):
cam_x = cam_x_
cam_y = cam_y_
cam_z = cam_z_
tcam = np.array([cam_x, cam_y, cam_z], dtype=np.float32)
if np.linalg.norm(tcam[0:2])<1e-9:
Rcam = np.eye(3, dtype=np.float32)
else:
Rcam = get_rotation_matrix(center, center-tcam)
tproj = tcam + basevec
Rproj = Rcam
ret['R'].append(Rcam)
ret['t'].append(tcam)
cams = []
projs = []
# render the scene at multiple scales
scales = [1, 0.5, 0.25, 0.125]
for scale in scales:
fx = K[0,0] * scale
fy = K[1,1] * scale
pxcam = (K[0,2]-shiftcamera) * scale
pxpro = (K[0,2]-shiftpattern) * scale
py = K[1,2] * scale
im_height = imsize[0] * scale
im_width = imsize[1] * scale
cams.append( renderer.PyCamera(fx,fy,pxcam,py, Rcam, tcam, im_width, im_height) )
projs.append( renderer.PyCamera(fx,fy,pxpro,py, Rproj, tproj, im_width, im_height) )
for s, cam, proj, pattern in zip(itertools.count(), cams, projs, patterns):
fl = K[0,0] / (2**s)
shiftcameras=shiftcamera/ (2**s)
shiftpatterns=shiftpattern/ (2**s)
shader = renderer.PyShader(0,1.5,0.0,10)
pyrenderer = renderer.PyRenderer(cam, shader, wavelength=wavelength, engine='gpu')
pyrenderer.mesh_proj(data, proj, pattern, reflectance, camerasensitivity, illumination, d_alpha=0, d_beta=0.35)
# get the reflected laser pattern $R$
im = pyrenderer.color().copy()
refimg = pyrenderer.reflectance().copy()
depth = pyrenderer.depth().copy()
disp = baseline * fl / depth - shiftcameras + shiftpatterns
mask = depth > 0
# get the ambient image $A$
ambient = pyrenderer.normal().copy()
# get the noise free IR image $J$
if s==0:
ret[f'ambient{s}'].append( ambient[None].astype(np.float32) )
ret[f'im{s}'].append( im[None].astype(np.float32))
ret[f'refimg{s}'].append( refimg[None].astype(np.float32))
dmax=disp.max()
dmin=disp.min()
maxdisp = max(maxdisp,dmax)
mindisp = min(mindisp,dmin)
ret[f'disp{s}'].append(disp[None].astype(np.float32))
print(f'Disp Min: {dmin}; Disp Max: {dmax}. Whole Min: {mindisp}; Whole Max: {maxdisp}')
for key in ret.keys():
ret[key] = np.stack(ret[key], axis=0)
# save to files
out_dir = out_root / f'{idx:08d}'
out_dir.mkdir(exist_ok=True, parents=True)
for k,val in ret.items():
for tidx in range(track_length):
v = val[tidx]
out_path = out_dir / f'{k}_{tidx}.npy'
np.save(out_path, v)
np.save( str(out_dir /'blend_im.npy'), blend_im_rnd)
print(f'create sample {idx+1}/{n_samples} took {time.time()-tic}[s]')
return maxdisp, mindisp
if __name__=='__main__':
np.random.seed(42)
# output directory
with open('../para/config.json') as fp:
config = json.load(fp)
data_root = Path(config['DATA_ROOT'])
shapenet_root = config['SHAPENET_ROOT']
data_type = 'syn'
out_root = data_root / f'{data_type}'
out_root.mkdir(parents=True, exist_ok=True)
# load shapenet models
# obj_classes = ['chair','car']
obj_classes = ['airplane','watercraft','camera']
objs = get_objs(shapenet_root, obj_classes)
# camera parameters
imsize = (480, 640)
imsizes = [(imsize[0]//(2**s), imsize[1]//(2**s)) for s in range(4)]
with open(str('../para/campara.pkl'), 'rb') as f:
campara = pickle.load(f)
K = campara['K']
baseline = campara['baseline']
shiftcamera= campara['shiftcamera']
shiftpattern= campara['shiftpattern']
print(K)
focal_lengths = [K[0,0]/(2**s) for s in range(4)]
blend_im = 0.6
noise = 0
# capture the same static scene from different view points as a track
track_length = 1#4
# load pattern image
pattern_path = '../para/pattern_croped.png'
pattern_crop = True
patterns = get_patterns(pattern_path, imsizes, pattern_crop)
# load reflectance
reflectance = np.loadtxt('../para/reflectance.txt', dtype=np.float32, delimiter=',')
wavelength = reflectance.shape[1]
# load camera sensitivity
camerasensitivity = np.loadtxt('../para/camerasensitivity.txt', dtype=np.float32, delimiter=',')
# load illumination
illumination = np.loadtxt('../para/illumination.txt', dtype=np.float32, delimiter=',')
patterns_illu=[]
for index, value in enumerate(patterns):
value=value.astype(np.float16)/255
ill_pat = value@illumination[:3,:]
print(ill_pat.shape)
patterns_illu.append(ill_pat)
settings = {
'imsizes': imsizes,
'patterns_3ch': patterns,
'patterns_illu':patterns_illu,
'camerasensitivity':camerasensitivity,
'focal_lengths': focal_lengths,
'baseline': baseline,
'K': K,
'shiftcamera': shiftcamera,
'shiftpattern': shiftpattern,
}
out_path = out_root / f'settings.pkl'
print(f'write settings to {out_path}')
with open(str(out_path), 'wb') as f:
pickle.dump(settings, f, pickle.HIGHEST_PROTOCOL)
maxdisp=0
mindisp=sys.float_info.max
# start the job
n_samples = 2**10 + 2**13
#n_samples = 2**8# + 2**13
for idx in range(n_samples):
args = (out_root, idx, n_samples, imsize, patterns, reflectance, camerasensitivity, illumination, wavelength, K, shiftcamera, shiftpattern, baseline, blend_im, noise, maxdisp, mindisp, track_length)
maxdisp, mindisp = create_data(*args)