-
Notifications
You must be signed in to change notification settings - Fork 1
/
opt.py
296 lines (245 loc) · 11.6 KB
/
opt.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
"""Optimization functions."""
import jax
import jax.numpy as jnp
import numpy as np
import mesher
import transforms
import sdf_ops
import projections
import constraints
def unnormalize_latent_coordns(x: jnp.ndarray,
low: jnp.ndarray,
high: jnp.ndarray)-> jnp.ndarray:
"""
Unnormalize latent coordinates from [0, 1] to the specified range.
Args:
x: Array of (num_shapes, latent_dim) with values in [0,1]
low: Array of (latent_dim,)
high: Array of (latent_dim,)
Returns: Array of (num_shape, latent_dim)
"""
nwax = np.newaxis
range = high - low
return low[nwax, :] + range[nwax, :]*x
def normalize_latent_coordns(x: jnp.ndarray,
low: jnp.ndarray,
high: jnp.ndarray)-> jnp.ndarray:
"""
Normalize latent coordinates to the range [0, 1].
Args:
x: Array of (num_shapes, latent_dim) with values in [0,1]
low: Array of (latent_dim,)
high: Array of (latent_dim,)
Returns: Array of (num_shape, latent_dim)
"""
nwax = np.newaxis
range = high - low
return (x - low[nwax, :])/range[nwax, :]
def compute_transforms_and_latent_coordn_from_opt_params(
opt_params: jnp.ndarray,
num_stamps: int,
transform_extent: transforms.TransformExtent,
num_latent_params: int,
latent_dim: int,
min_encoded_coordn: float,
max_encoded_coordn: float
)->transforms.Transform:
"""
Compute transforms and latent coordinates from optimization parameters.
Args:
opt_params: Optimization parameters array of shape (num_elems, 6).
num_stamps: Number of stamps.
transform_extent: Transformation extent object.
num_latent_params: Number of latent parameters.
latent_dim: Latent dimension.
min_encoded_coordn: Minimum encoded coordinate.
max_encoded_coordn: Maximum encoded coordinate.
Returns:
Tuple of predicted stamp latent coordinates and shape transforms.
"""
norm_latent_params = opt_params[:num_latent_params].reshape((num_stamps, latent_dim))
norm_shape_params = opt_params[num_latent_params:]
pred_stamp_latent_coordns = unnormalize_latent_coordns(norm_latent_params,
min_encoded_coordn,
max_encoded_coordn)
shape_transforms = transforms.Transform.from_normalized_array(
norm_shape_params,
num_stamps,
transform_extent)
return pred_stamp_latent_coordns, shape_transforms
def compute_shape_sdfs(sdf_net,
sdf_net_params,
dom_mesh: mesher.Mesher,
shape_transforms: transforms.Transform,
pred_stamp_latent_coordns:jnp.ndarray,
stamp_bbox: mesher.BoundingBox):
"""
Compute signed distance fields (SDFs) for shape instances.
Args:
sdf_net: SDF network.
sdf_net_params: SDF network parameters.
dom_mesh: Mesh object containing mesh information.
shape_transforms: Shape transforms.
pred_stamp_latent_coordns: Predicted stamp latent coordinates.
stamp_bbox: Bounding box for stamping.
Returns:
Array of shape SDFs.
"""
num_objects = shape_transforms.num_objects
trans_mesh_xy = transforms.transform_coordinates(dom_mesh.elem_centers,
shape_transforms)
sdfs = jnp.zeros((num_objects, dom_mesh.num_elems))
trans_xy_nograd = jax.lax.stop_gradient(trans_mesh_xy)
# compute the stamped indices
stamped_indices = mesher.compute_point_indices_in_box(
trans_xy_nograd,
stamp_bbox)
unstamped_indices = jnp.logical_not(stamped_indices)
# compute the SDF and density of each object
for obj in range(num_objects):
stamp_xy = trans_mesh_xy[obj, stamped_indices[obj,:],:]
unstamp_xy = trans_mesh_xy[obj, unstamped_indices[obj,:],:]
scale = shape_transforms.scale[obj]
# compute the stamped sdf
stamp_sdf = sdf_net.apply({'params': sdf_net_params},
pred_stamp_latent_coordns[obj,:].reshape((1, -1)),
stamp_xy,
method='call_decoder'
)*scale
sdfs = sdfs.at[obj, stamped_indices[obj,:]].set(stamp_sdf.reshape(-1))
# compute the unstamped approx sdf
sdfs = sdfs.at[obj, unstamped_indices[obj,:]].set(
sdf_ops.compute_circ_sdf(unstamp_xy)*scale)
return sdfs
def compute_objective(opt_params: jnp.ndarray,
transform_extent: transforms.TransformExtent,
challenge,
dom_mesh: mesher.Mesher,
sdf_net,
sdf_net_params,
stamp_bbox,
dens_array,
num_stamps: int,
num_latent_params: int,
latent_dim: int,
min_encoded_coordn: float,
max_encoded_coordn: float
):
"""
Compute the constraints for optimization.
Args:
opt_params: Optimization parameters of shape (num_elems, 6).
min_separation: Minimum separation constraint.
transform_extent: Transformation extent object.
dom_mesh: Mesh object containing mesh information.
sdf_net: SDF network.
sdf_net_params: SDF network parameters.
stamp_bbox: Bounding box for stamping.
num_latent_params: Number of latent parameters.
latent_dim: Latent dimension.
min_encoded_coordn: Minimum encoded coordinate.
max_encoded_coordn: Maximum encoded coordinate.
encoded_z: Encoded latent coordinates.
num_stamps: Number of stamps.
epoch: Current epoch of the optimization.
Returns:
Tuple of constraints and their gradients.
"""
def objective_wrapper(opt_params)->float:
(pred_stamp_latent_coordns,
shape_transforms) = compute_transforms_and_latent_coordn_from_opt_params(
opt_params,
num_stamps,
transform_extent,
num_latent_params,
latent_dim,
min_encoded_coordn,
max_encoded_coordn)
shape_sdfs = compute_shape_sdfs(sdf_net,
sdf_net_params,
dom_mesh,
shape_transforms,
pred_stamp_latent_coordns,
stamp_bbox)
shape_densities = sdf_ops.project_sdf_to_density(shape_sdfs, dom_mesh)
density = sdf_ops.compute_union_density_fields(shape_densities).reshape((
dom_mesh.nelx,
dom_mesh.nely))
density = projections.threshold_filter(density)
dens_array.array = density
response, aux = challenge.component.response(dens_array)
performance_loss = jnp.log(challenge.loss(response))
distance = challenge.distance_to_target(response)
metrics = challenge.metrics(response, dens_array, aux)
return performance_loss, (shape_transforms, pred_stamp_latent_coordns, density,
response, distance, metrics, aux)
(obj, auxs), grad_obj = jax.value_and_grad(objective_wrapper,
has_aux=True)(opt_params.reshape(-1))
return jnp.array([obj]), grad_obj.reshape((-1, 1)), auxs
def compute_constraint(opt_params: jnp.ndarray,
min_separation: float,
transform_extent: transforms.TransformExtent,
dom_mesh: mesher.Mesher,
sdf_net,
sdf_net_params,
stamp_bbox,
num_latent_params,
latent_dim,
min_encoded_coordn,
max_encoded_coordn,
encoded_z,
num_stamps: int,
epoch: int):
"""
Compute signed distance fields (SDFs) for shapes.
Args:
sdf_net: SDF network.
sdf_net_params: SDF network parameters.
dom_mesh: Mesh object containing mesh information.
shape_transforms: Shape transforms.
pred_stamp_latent_coordns: Predicted stamp latent coordinates.
stamp_bbox: Bounding box for stamping.
Returns:
Array of shape SDFs.
"""
def sep_constraint_wrapper(opt_params: jnp.ndarray)->float:
(pred_stamp_latent_coordns,
shape_transforms) = compute_transforms_and_latent_coordn_from_opt_params(
opt_params,
num_stamps,
transform_extent,
num_latent_params,
latent_dim,
min_encoded_coordn,
max_encoded_coordn)
shape_sdfs = compute_shape_sdfs(sdf_net,
sdf_net_params,
dom_mesh,
shape_transforms,
pred_stamp_latent_coordns,
stamp_bbox)
min_sep_cons = constraints.compute_min_separation_constraint(shape_sdfs,
dom_mesh,
min_separation)
return min_sep_cons
sep_cons, grad_sep_cons = jax.value_and_grad(sep_constraint_wrapper)(opt_params.reshape(-1))
def latent_cons_wrapper(opt_params):
(pred_stamp_latent_coordns,
shape_transforms) = compute_transforms_and_latent_coordn_from_opt_params(
opt_params,
num_stamps,
transform_extent,
num_latent_params,
latent_dim,
min_encoded_coordn,
max_encoded_coordn)
d = max(0.02, 4. - 0.08*epoch) # HARDCODING
latent_space_cons = constraints.latent_distance_constraint(
pred_stamp_latent_coordns,
encoded_z,
max_allowed_latent_dist=d)
return latent_space_cons
lat_cons, grad_lat_cons = jax.value_and_grad(latent_cons_wrapper)(opt_params.reshape(-1))
cons = jnp.array([sep_cons, lat_cons]).reshape((-1, 1))
grad_cons = jnp.stack((grad_sep_cons, grad_lat_cons), axis=-1)
return cons, grad_cons.T