-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_dataset_3d.py
394 lines (344 loc) · 12.5 KB
/
make_dataset_3d.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
import fix_dead_command_line
import cleanup_when_killed
import h5py
import numpy as np
import torch
import torch.nn as nn
import os
import math
from argparse import ArgumentParser
from which_device import get_compute_device
from simulation_description import SimulationDescription
from h5ds import H5DS
from dataset3d import WaveDataset3d
from current_simulation_description import (
make_random_obstacles,
make_simulation_description,
minimum_x_units,
Nx,
Ny,
Nz,
)
from assert_eq import assert_eq
from kwave_util import make_ball
from signals_and_geometry import obstacle_map_to_sdf
def make_inner_outer_partitions():
f = math.sqrt(0.5)
f_lo = 0.5 - 0.5 * f
f_hi = 0.5 + 0.5 * f
x_min = minimum_x_units
y_min = 0
z_min = 0
x_extent = Nx - minimum_x_units
y_extent = Ny
z_extent = Nz
x_lo = x_min
x_hi = x_min + x_extent
y_lo = y_min + round(y_extent * f_lo)
y_hi = y_min + round(y_extent * f_hi)
z_lo = z_min + round(z_extent * f_lo)
z_hi = z_min + round(z_extent * f_hi)
assert (
abs(
(
((x_hi - x_lo) * (y_hi - y_lo) * (z_hi - z_lo))
/ ((x_extent * y_extent * z_extent))
)
- 0.5
)
< 0.02 # NOTE: I know this error margin is a bit lousy, but it's the best I can do right now.
)
return (x_lo, x_hi, y_lo, y_hi, z_lo, z_hi)
def middle_is_empty(obstacles):
if isinstance(obstacles, np.ndarray):
obstacles = torch.tensor(obstacles)
assert isinstance(obstacles, torch.Tensor)
assert_eq(obstacles.shape, (Nx, Ny, Nz))
assert_eq(obstacles.dtype, torch.bool)
x_lo, x_hi, y_lo, y_hi, z_lo, z_hi = make_inner_outer_partitions()
return torch.all(obstacles[x_lo:x_hi, y_lo:y_hi, z_lo:z_hi] == 0)
def outside_is_empty(obstacles):
if isinstance(obstacles, np.ndarray):
obstacles = torch.tensor(obstacles)
assert isinstance(obstacles, torch.Tensor)
assert_eq(obstacles.shape, (Nx, Ny, Nz))
assert_eq(obstacles.dtype, torch.bool)
x_lo, x_hi, y_lo, y_hi, z_lo, z_hi = make_inner_outer_partitions()
# front
# ########
# ########
# ########
# ########
#
# middle
# ######## <- middle top
# ## ## <- middle left and right
# ######## <- middle bottom
#
# back
# ########
# ########
# ########
# ########
return (
torch.all(obstacles[:x_lo, :, :] == 0) # front
and torch.all(obstacles[x_hi:, :, :] == 0) # back
and torch.all(obstacles[x_lo:x_hi, :y_lo, :] == 0) # middle bottom
and torch.all(obstacles[x_lo:x_hi, y_hi:, :] == 0) # middle top
and torch.all(obstacles[x_lo:x_hi, y_lo:y_hi, :z_lo] == 0) # middle left
and torch.all(obstacles[x_lo:x_hi, y_lo:y_hi, z_hi:] == 0) # middle right
)
def resample_echo4ch_obstacles(echo4ch_obstacles, description):
echo4ch_grid_size = 64 # grid units
echo4ch_spatial_extent = 0.64 # m
assert isinstance(echo4ch_obstacles, np.ndarray)
assert_eq(echo4ch_obstacles.shape, (64, 64, 64))
assert isinstance(description, SimulationDescription)
# HACK
# echo4ch_obstacles[...] = 1.0
sim_x_grid_size = Nx - minimum_x_units # grid units
sim_y_grid_size = Ny # grid units
sim_z_grid_size = Nz # grid units
sim_x_spatial_extent = sim_x_grid_size * description.dx # m
sim_y_spatial_extent = sim_y_grid_size * description.dy # m
sim_z_spatial_extent = sim_z_grid_size * description.dz # m
sim_to_echo4ch_x_ratio = sim_x_spatial_extent / echo4ch_spatial_extent
sim_to_echo4ch_y_ratio = sim_y_spatial_extent / echo4ch_spatial_extent
sim_to_echo4ch_z_ratio = sim_z_spatial_extent / echo4ch_spatial_extent
sim_to_echo4ch_x_min_index = -sim_to_echo4ch_x_ratio
sim_to_echo4ch_y_min_index = -sim_to_echo4ch_y_ratio
sim_to_echo4ch_z_min_index = -sim_to_echo4ch_z_ratio
sim_to_echo4ch_x_max_index = +sim_to_echo4ch_x_ratio
sim_to_echo4ch_y_max_index = +sim_to_echo4ch_y_ratio
sim_to_echo4ch_z_max_index = +sim_to_echo4ch_z_ratio
ls_x = torch.linspace(
sim_to_echo4ch_x_min_index, sim_to_echo4ch_x_max_index, sim_x_grid_size
)
ls_y = torch.linspace(
sim_to_echo4ch_y_min_index, sim_to_echo4ch_y_max_index, sim_y_grid_size
)
ls_z = torch.linspace(
sim_to_echo4ch_z_min_index, sim_to_echo4ch_z_max_index, sim_z_grid_size
)
gx, gy, gz = torch.meshgrid([ls_x, ls_y, ls_z])
assert_eq(
gx.shape,
(
sim_x_grid_size,
sim_y_grid_size,
sim_z_grid_size,
),
)
assert_eq(
gy.shape,
(
sim_x_grid_size,
sim_y_grid_size,
sim_z_grid_size,
),
)
assert_eq(
gz.shape,
(
sim_x_grid_size,
sim_y_grid_size,
sim_z_grid_size,
),
)
all_locations = torch.stack([gx, gy, gz], dim=-1)
assert_eq(
all_locations.shape, (sim_x_grid_size, sim_y_grid_size, sim_z_grid_size, 3)
)
# for grid_sample: batch, features, output depth, output height, output width, xyz coordinates
t_grid = all_locations.reshape(
1, # N
sim_x_grid_size, # D_out
sim_y_grid_size, # H_out
sim_z_grid_size, # W_out
3, # yep, just 3
)
input_tensor = (
torch.tensor(echo4ch_obstacles)
.permute(1, 2, 0)
.reshape(
1, # N
1, # C
echo4ch_grid_size, # D_in
echo4ch_grid_size, # H_in
echo4ch_grid_size, # W_in
)
.float()
)
values = nn.functional.grid_sample(
input=input_tensor,
grid=t_grid,
mode="bilinear",
padding_mode="zeros",
align_corners=False,
)
assert_eq(values.shape, (1, 1, sim_x_grid_size, sim_y_grid_size, sim_z_grid_size))
values = values.reshape(sim_x_grid_size, sim_y_grid_size, sim_z_grid_size)
return values > 0.0
def main():
mode_random = "random"
mode_random_outer = "random-outer"
mode_random_inner = "random-inner"
mode_orbiting_sphere = "orbiting-sphere"
mode_echo4ch = "echo4ch"
parser = ArgumentParser()
parser.add_argument("--numworkers", type=int, dest="numworkers", required=True)
parser.add_argument("--count", type=int, dest="count", required=False)
parser.add_argument("--workerindex", type=int, dest="workerindex", required=True)
parser.add_argument(
"--mode",
type=str,
choices=[
mode_random,
mode_random_inner,
mode_random_outer,
mode_orbiting_sphere,
mode_echo4ch,
],
dest="mode",
required=True,
)
parser.add_argument("--verbose", dest="verbose", default=False, action="store_true")
parser.add_argument("--append", dest="append", default=False, action="store_true")
args = parser.parse_args()
numworkers = args.numworkers
count = args.count
workerindex = args.workerindex
mode = args.mode
append = args.append
assert numworkers >= 1
assert workerindex >= 0 and workerindex < numworkers
desc = make_simulation_description()
desc.print_summary()
dataset_path = os.environ.get("DATASET_OUTPUT")
if dataset_path is None:
raise Exception(
"Please set the DATASET_OUTPUT environment variable to point to the HDF5 dataset file that should be created"
)
if mode in [mode_random, mode_random_inner, mode_random_outer]:
if count is None:
raise Exception(
"Please specify the --count argument when using random mode"
)
def filter(obs):
if mode == mode_random_inner:
return outside_is_empty(obs)
elif mode == mode_random_outer:
return middle_is_empty(obs)
return True
def obstacle_generator():
for i in range(count):
attempts = 0
while True:
attempts += 1
obs = make_random_obstacles(desc)
if filter(obs):
break
if args.verbose:
print(
f"Found valid random obstacles after {attempts} attempt{'' if attempts == 1 else 's'}"
)
yield obs, mode
elif mode == mode_orbiting_sphere:
radius = math.ceil(0.05 / desc.dz)
margin = 4
min_x = minimum_x_units + margin + radius
min_y = margin + radius
min_z = margin + radius
max_x = desc.Nx - margin - radius
max_y = desc.Ny - margin - radius
max_z = desc.Nz - margin - radius
def obstacle_generator():
i = 0
while i < count:
t = i / count
print(t)
tx = 0.5 - 0.5 * math.sin(2.0 * math.pi * t)
ty = 0.5 - 0.5 * math.sin(4.0 * math.pi * t)
tz = 0.5 + 0.5 * math.cos(6.0 * math.pi * t)
ix = min_x + tx * (max_x - min_x)
iy = min_y + ty * (max_y - min_y)
iz = min_z + tz * (max_z - min_z)
obs = make_ball(desc.Nx, desc.Ny, desc.Nz, ix, iy, iz, radius)
yield obs, f"orbiting-sphere {i}/{count}"
i += 1
# HACK
for i, f in enumerate(obstacle_generator()):
pass
exit(0)
elif mode == mode_echo4ch:
echo4ch_obstacle_path = os.environ.get("ECHO4CH_OBSTACLES")
if echo4ch_obstacle_path is None:
raise Exception(
"Please set the ECHO4CH_OBSTACLES environment variable to point to the ECHO4CH obstacles HDF5 file"
)
def obstacle_generator():
with h5py.File(echo4ch_obstacle_path, "r") as obstacles_h5file:
obstacles_ds = H5DS(
name="obstacles",
dtype=np.bool8,
shape=(64, 64, 64),
extensible=True,
)
assert obstacles_ds.exists(obstacles_h5file)
i = 0
obstacles_ds_size = obstacles_ds.count(obstacles_h5file)
N = obstacles_ds_size
if count is not None:
N = min(count, N)
magic_prime_number = 7919
assert (obstacles_ds_size % magic_prime_number) != 0
while i < N:
i_permuted = (i * magic_prime_number) % obstacles_ds_size
echo4ch_obstacles = obstacles_ds.read(
obstacles_h5file, index=i_permuted
)
sim_roi_obstacles = resample_echo4ch_obstacles(
echo4ch_obstacles, desc
)
sim_obstacles = np.zeros(
(desc.Nx, desc.Ny, desc.Nz), dtype=np.bool8
)
sim_obstacles[minimum_x_units:, :, :] = sim_roi_obstacles
yield sim_obstacles, f"ECHO4CH obstacles {i} permuted to {i_permuted}"
i += 1
else:
raise Exception(f'Unrecognized dataset mode: "{mode}"')
def obstacles_subset():
try:
obstacles_all = obstacle_generator()
for _ in range(workerindex):
next(obstacles_all)
while True:
yield next(obstacles_all)
for _ in range(numworkers - 1):
next(obstacles_all)
except StopIteration:
pass
if os.path.exists(dataset_path) and not append:
print(
f"Error: attempted to create a dataset file at {dataset_path} but it already exists. Please provide a different path for DATASET_OUTPUT or use the --append flag."
)
exit(-1)
with WaveDataset3d(desc, dataset_path, write=True) as dataset:
for i, (o, s) in enumerate(obstacles_subset()):
print(f'{i} - Creating dataset example "{s}"')
if dataset.contains(o):
print(
"Warning: duplicate obstacles found, this example is being skipped"
)
continue
desc.set_obstacles(o)
results = desc.run(verbose=args.verbose)
sdf = (
obstacle_map_to_sdf(torch.tensor(o).to(get_compute_device()), desc)
.cpu()
.numpy()
)
dataset.append_to_dataset(obstacles=o, recordings=results, sdf=sdf)
if __name__ == "__main__":
main()