-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_modules.py
435 lines (354 loc) · 18.2 KB
/
tree_modules.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
import tensorflow as tf
import tree_common
class neuralNode_splitter(tf.Module) :
def __init__(self) :
super(neuralNode_splitter, self).__init__()
self.nX = tf.constant([1.0, 0.0, 0.0], dtype=tf.float32)
self.nY = tf.constant([0.0, 1.0, 0.0], dtype=tf.float32)
self.nZ = tf.constant([0.0, 0.0, 1.0], dtype=tf.float32)
self.gen_fn = self.gen_nodes
@tf.function
def tight_bounds(self, point_clouds, node_bounds) :
mask = tf.stop_gradient(tree_common.build_mask(point_clouds, node_bounds))
masked_pc = tf.stop_gradient(tf.einsum('bij, bik -> bij', point_clouds, mask))
N = tf.reduce_sum(mask, axis=1)
xyz_axis = tf.expand_dims(masked_pc, axis=-1)
xyz_min = tf.reduce_min(tf.abs(xyz_axis - 1.0), axis=1, keepdims=True) + 1.0
xyz_max = tf.reduce_max(xyz_axis, axis=1, keepdims=True)
xyz_min = tf.math.minimum(xyz_min, xyz_max)
tight_node_bmin = tf.squeeze(xyz_min)
tight_node_bmax = tf.squeeze(xyz_max)
node_bmin = node_bounds[:, 0:3]
node_bmax = node_bounds[:, 3:6]
diag = node_bmax - node_bmin
theta_bmin = tf.stop_gradient(tf.math.divide_no_nan(tight_node_bmin - node_bmin, diag))
theta_bmax = tf.stop_gradient(tf.math.divide_no_nan(tight_node_bmax - node_bmin, diag))
return \
tf.where(tf.greater(N, 0), node_bmin + theta_bmin * diag, node_bmin), \
tf.where(tf.greater(N, 0), node_bmin + theta_bmax * diag, node_bmax)
@tf.function
def gen_nodes(self, normal, theta, node_bounds, point_clouds) :
node_bmin = node_bounds[:, 0:3]
node_bmax = node_bounds[:, 3:6]
b0 = tf.einsum('bk, k -> b', node_bmin, normal)[..., tf.newaxis]
b1 = tf.einsum('bk, k -> b', node_bmax, normal)[..., tf.newaxis]
theta_offset = b0 + theta * (b1 - b0)
right_bmin_temp = tf.einsum('bk, k -> bk', node_bmin, 1.0 - normal) + tf.einsum('bi, k -> bk', theta_offset, normal)
right_bmax_temp = node_bmax
right_bmin = tf.minimum(right_bmin_temp, right_bmax_temp)
right_bmax = tf.maximum(right_bmin_temp, right_bmax_temp)
left_bmin_temp = node_bmin
left_bmax_temp = tf.einsum('bk, k -> bk', node_bmax, 1.0 - normal) + tf.einsum('bi, k -> bk', theta_offset, normal)
left_bmin = tf.minimum(left_bmin_temp, left_bmax_temp)
left_bmax = tf.maximum(left_bmin_temp, left_bmax_temp)
left_bbox = tf.concat([left_bmin, left_bmax], axis=-1)
right_bbox = tf.concat([right_bmin, right_bmax], axis=-1)
return theta_offset, left_bbox, right_bbox
@tf.function
def __call__(self, node_bounds, thetas, point_clouds=None) :
thetas_X = thetas[:, 0:1]
thetas_Y = thetas[:, 1:2]
thetas_Z = thetas[:, 2:3]
offset_X, left_bbox_X, right_bbox_X = self.gen_fn(self.nX, thetas_X, node_bounds, point_clouds)
offset_Y, left_bbox_Y, right_bbox_Y = self.gen_fn(self.nY, thetas_Y, node_bounds, point_clouds)
offset_Z, left_bbox_Z, right_bbox_Z = self.gen_fn(self.nZ, thetas_Z, node_bounds, point_clouds)
return (offset_X, offset_Y, offset_Z), \
(left_bbox_X, right_bbox_X, left_bbox_Y, right_bbox_Y, left_bbox_Z, right_bbox_Z)
class sah_eval(tf.Module) :
def __init__(self) :
super(sah_eval, self).__init__()
@tf.function
def area(self, bounds) :
bmin = bounds[:, 0:3]
bmax = bounds[:, 3:6]
diag = bmax - bmin
x = diag[:, 0:1]
y = diag[:, 1:2]
z = diag[:, 2:3]
return 2.0 * (x * y + x * z + y * z)
@tf.function
def __call__(self, parent_bounds, bounds, point_clouds) :
parent_area = self.area(parent_bounds)
self_area = self.area(bounds)
return self_area / parent_area
class vh_eval(tf.Module) :
def __init__(self, r_eps=1.e-4) :
super(vh_eval, self).__init__()
self.r = r_eps
@tf.function
def volume(self, bounds) :
bmin = bounds[:, 0:3] - self.r
bmax = bounds[:, 3:6] + self.r
diag = bmax - bmin
x = diag[:, 0:1]
y = diag[:, 1:2]
z = diag[:, 2:3]
return (x * y * z)
@tf.function
def __call__(self, parent_bounds, bounds, point_clouds) :
parent_vol = self.volume(parent_bounds)
self_vol = self.volume(bounds)
return self_vol / parent_vol
class gr_q_eval(tf.Module) :
def __init__(self, t) :
super(gr_q_eval, self).__init__()
self.t_cost = t
@tf.function
def __call__(self, point_clouds, parent_normal, parent_offset, parent_bounds, bounds) :
parent_mask = tf.stop_gradient(tree_common.build_mask(point_clouds, bounds))
N = tf.reduce_sum(parent_mask, axis=1)
return N * self.t_cost
class q_eval(tf.Module) :
def __init__(self, t, beta) :
super(q_eval, self).__init__()
self.t_cost = t
self.count_fn = qL_fn
self.beta = beta
@tf.function
def __call__(self, point_clouds, parent_normal, parent_offset, parent_bounds, parent_mask) :
axis_points = tf.einsum('bijk, j -> bik', point_clouds[..., tf.newaxis], parent_normal)
parent_minmax = tf.einsum('bij, j -> bi', tf.reshape(parent_bounds, [-1, 2, 3]), parent_normal)
N = tf.stop_gradient(tf.reduce_sum(parent_mask, axis=1))
nL = self.count_fn(self.beta, axis_points, parent_mask, parent_minmax[..., 0:1], parent_minmax[..., 1:], parent_offset)
return nL * self.t_cost, (N - nL) * self.t_cost
class p_eval(tf.Module) :
def __init__(self, t) :
super(p_eval, self).__init__()
self.t_cost = t
@tf.function
def __call__(self, point_clouds, parent_normal, parent_offset, parent_bounds, bounds) :
return self.t_cost
@tf.function
@tf.custom_gradient
def qL_fn(beta, axis_points, parent_mask, parent_min, parent_max, offset) :
offset = offset[..., tf.newaxis]
node_mask = tf.einsum('bij, bij -> bij', parent_mask, tf.cast(axis_points <= offset, tf.float32))
N = tf.reduce_sum(node_mask, axis=1)
@tf.function
def next_step(mask, b) :
mask_after_offset = tf.einsum('bij, bij -> bij', mask, tf.cast(axis_points > b, tf.float32))
axisR = tf.einsum('bij, bij -> bij', axis_points, mask_after_offset)
offset_above = tf.reduce_min(tf.abs(axisR - beta), axis=1, keepdims=True) + beta
axis_max = tf.reduce_max(axisR, axis=1, keepdims=True)
offset_above = tf.math.minimum(offset_above, axis_max)
N1 = tf.reduce_sum(tf.einsum('bij, bij -> bij', mask, tf.cast(axis_points <= offset_above, tf.float32)), axis=1)
return offset_above, N1
@tf.function
def grad(upstream) :
offset_above, N1 = next_step(parent_mask, offset)
slope = tf.math.divide_no_nan(N1 - N, offset_above[..., 0] - offset[..., 0])
stepGrad = tf.clip_by_value(slope, 0.0, 1.0 / 0.0001)
upstream_grad = stepGrad
upstream_grad = tf.einsum('bi, bi -> bi', upstream, upstream_grad)
upstream_grad = tf.einsum('bi, bi -> bi', upstream_grad, tf.cast(offset[..., 0] >= parent_min, tf.float32))
upstream_grad = tf.einsum('bi, bi -> bi', upstream_grad, tf.cast(offset[..., 0] <= parent_max, tf.float32))
return None, None, None, None, None, upstream_grad
return N, grad
@tf.function
@tf.custom_gradient
def soft_min4(v0, v1, v2, v3, t) :
vals = tf.concat([v0, v1, v2, v3], axis=-1)
ret = tf.reduce_min(vals, axis=-1, keepdims=True)
@tf.function
def grad(upstream) :
x = -t * vals
x -= tf.reduce_max(x, axis=-1, keepdims=True)
x = tf.nn.softmax(x, axis=-1)
upstream_grad = upstream * x
return upstream_grad[:, 0:1], upstream_grad[:, 1:2], upstream_grad[:, 2:3], upstream_grad[:, 3:4], None
return ret, grad
@tf.function
@tf.custom_gradient
def soft_min3(v0, v1, v2, v3, t) :
vals = tf.concat([v0, v1, v2], axis=-1)
ret = tf.reduce_min(vals, axis=-1, keepdims=True)
@tf.function
def grad(upstream) :
x = -t * vals
x -= tf.reduce_max(x, axis=-1, keepdims=True)
x = tf.nn.softmax(x, axis=-1)
upstream_grad = upstream * x
return upstream_grad[:, 0:1], upstream_grad[:, 1:2], upstream_grad[:, 2:3], None, None
return ret, grad
@tf.function
def hard_min4(v0, v1, v2, v3) :
vals = tf.concat([v0, v1, v2, v3], axis=-1)
return vals, tf.reduce_min(vals, axis=-1, keepdims=True)
@tf.function
def hard_min3(v0, v1, v2, v3) :
vals = tf.concat([v0, v1, v2], axis=-1)
return vals, tf.reduce_min(vals, axis=-1, keepdims=True)
class pool_treelet(tf.Module) :
def __init__(self, t, num_splits, p_eval, q_eval, gr_q_eval, w_eval, normFactor) :
super(pool_treelet, self).__init__()
self.t = t
self.num_splits = num_splits
self.soft_min = soft_min3 if num_splits == 3 else soft_min4
self.hard_min = hard_min3 if num_splits == 3 else hard_min4
self.p_eval = p_eval
self.q_eval = q_eval
self.w_eval = w_eval
self.gr_q_eval = gr_q_eval
self.normFactor = 1.0 / normFactor
self.nX = tf.constant([1.0, 0.0, 0.0], dtype=tf.float32)
self.nY = tf.constant([0.0, 1.0, 0.0], dtype=tf.float32)
self.nZ = tf.constant([0.0, 0.0, 1.0], dtype=tf.float32)
self.isect_cost = self.q_eval.t_cost
self.beta = self.q_eval.beta
@tf.function
def get_pred_branch_from_leaves(self, cost_xyz, offsets_xyz) :
batch_size = tf.shape(cost_xyz)[0]
diag_eye = tf.eye(num_rows=self.num_splits, batch_shape=[batch_size])
pred_axis = tf.argmin(cost_xyz, axis=1, output_type=tf.int32)
pred_normal = tf.gather(diag_eye, pred_axis, axis=1, batch_dims=1)
pred_offset = tf.gather(offsets_xyz, pred_axis, axis=1, batch_dims=1)[..., tf.newaxis]
pred_planes = tf.concat([pred_normal, pred_offset], axis=-1)
return pred_planes[:, tf.newaxis, :]
@tf.function
def get_pred_branch_from_interior(self, cost_xyz, offsets_xyz, subtree_splits) :
batch_size = tf.shape(cost_xyz)[0]
diag_eye = tf.eye(num_rows=self.num_splits, batch_shape=[batch_size])
pred_axis = tf.argmin(cost_xyz, axis=1, output_type=tf.int32)
pred_normal = tf.gather(diag_eye, pred_axis, axis=1, batch_dims=1)
pred_offset = tf.gather(offsets_xyz, pred_axis, axis=1, batch_dims=1)[..., tf.newaxis]
pred_planes = tf.concat([pred_normal, pred_offset], axis=-1)[:, tf.newaxis, :]
pred_splits = tf.gather(subtree_splits, pred_axis, axis=1, batch_dims=1)
pred_planes = tf.concat([pred_planes, pred_splits], axis=1)
return pred_planes
@tf.function
def eval_leaves(self, point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
offsetX, offsetY, offsetZ,
xL_bounds, xR_bounds, yL_bounds, yR_bounds, zL_bounds, zR_bounds,
flag) :
Cnode = \
self.p_eval(point_clouds, parent_normal, parent_offset, parent_bounds, node_bounds) * \
self.w_eval(root_bounds, node_bounds, point_clouds)
node_mask = tf.stop_gradient(tree_common.build_mask(point_clouds, node_bounds))
qL_X, qR_X = self.q_eval(point_clouds, self.nX, offsetX, node_bounds, node_mask)
qL_Y, qR_Y = self.q_eval(point_clouds, self.nY, offsetY, node_bounds, node_mask)
qL_Z, qR_Z = self.q_eval(point_clouds, self.nZ, offsetZ, node_bounds, node_mask)
CxL = qL_X * self.w_eval(root_bounds, xL_bounds, point_clouds)
CxR = qR_X * self.w_eval(root_bounds, xR_bounds, point_clouds)
CyL = qL_Y * self.w_eval(root_bounds, yL_bounds, point_clouds)
CyR = qR_Y * self.w_eval(root_bounds, yR_bounds, point_clouds)
CzL = qL_Z * self.w_eval(root_bounds, zL_bounds, point_clouds)
CzR = qR_Z * self.w_eval(root_bounds, zR_bounds, point_clouds)
parent_mask = tf.stop_gradient(tree_common.build_mask(point_clouds, parent_bounds))
QleafL, QleafR = self.q_eval(point_clouds, parent_normal, parent_offset, parent_bounds, parent_mask)
QleafRoot = tf.zeros_like(qL_X) * self.normFactor
Qleaf = QleafRoot * flag[0] + QleafL * flag[1] + QleafR * flag[2]
Cleaf = Qleaf * self.w_eval(root_bounds, node_bounds, point_clouds)
return Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf
@tf.function
def eval_interior(self, point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds) :
Cnode = \
self.p_eval(point_clouds, parent_normal, parent_offset, parent_bounds, node_bounds) * \
self.w_eval(root_bounds, node_bounds, point_clouds)
return Cnode
@tf.function
def pool_leaves_soft(self, flag, point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
offsetX, offsetY, offsetZ,
xL_bounds, xR_bounds, yL_bounds, yR_bounds, zL_bounds, zR_bounds) :
Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf = self.eval_leaves(point_clouds, \
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
offsetX, offsetY, offsetZ,
xL_bounds, xR_bounds, yL_bounds, yR_bounds, zL_bounds, zR_bounds,
flag)
return (self.pool_soft(Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf), Cleaf)
@tf.function
def pool_leaves(self, flag, point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
offsetX, offsetY, offsetZ,
xL_bounds, xR_bounds, yL_bounds, yR_bounds, zL_bounds, zR_bounds) :
Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf = self.eval_leaves(point_clouds, \
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
offsetX, offsetY, offsetZ,
xL_bounds, xR_bounds, yL_bounds, yR_bounds, zL_bounds, zR_bounds,
flag)
return (Cnode + CxL + CxR, Cnode + CyL + CyR, Cnode + CzL + CzR)
@tf.function
def pool_interior_soft(self, flag, point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
CxL, CxR, CyL, CyR, CzL, CzR,
CxL_leaf, CxR_leaf,
CyL_leaf, CyR_leaf,
CzL_leaf, CzR_leaf) :
Cnode = self.eval_interior(point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds)
parent_mask = tf.stop_gradient(tree_common.build_mask(point_clouds, parent_bounds))
QleafL, QleafR = self.q_eval(point_clouds, parent_normal, parent_offset, parent_bounds, parent_mask)
QleafRoot = tf.ones_like(QleafL) * self.normFactor
Qleaf = QleafRoot * flag[0] + QleafL * flag[1] + QleafR * flag[2]
Cleaf = Qleaf * self.w_eval(root_bounds, node_bounds, point_clouds)
return self.pool_soft(Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf)
@tf.function
def pool_soft(self, Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf) :
cost_x = Cnode + CxL + CxR
cost_y = Cnode + CyL + CyR
cost_z = Cnode + CzL + CzR
treelet_cost = self.soft_min(cost_x, cost_y, cost_z, Cleaf, self.t)
return treelet_cost
@tf.function
def pool_leaves_hard(self, flag, point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
offsetX, offsetY, offsetZ,
xL_bounds, xR_bounds, yL_bounds, yR_bounds, zL_bounds, zR_bounds) :
Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf = self.eval_leaves(point_clouds, \
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
offsetX, offsetY, offsetZ,
xL_bounds, xR_bounds, yL_bounds, yR_bounds, zL_bounds, zR_bounds,
flag)
return self.pool_structure_leaves(Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf, offsetX, offsetY, offsetZ)
@tf.function
def pool_interior_hard(self, flag, point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds,
branch_xL, branch_xR, branch_yL, branch_yR, branch_zL, branch_zR,
offsetX, offsetY, offsetZ) :
Cnode = self.eval_interior(point_clouds,
root_bounds, parent_bounds, parent_normal, parent_offset, node_bounds)
parent_mask = tf.stop_gradient(tree_common.build_mask(point_clouds, parent_bounds))
QleafL, QleafR = self.q_eval(point_clouds, parent_normal, parent_offset, parent_bounds, parent_mask)
QleafRoot = tf.ones_like(QleafL) * self.normFactor
Qleaf = QleafRoot * flag[0] + QleafL * flag[1] + QleafR * flag[2]
Cleaf = Qleaf * self.w_eval(root_bounds, node_bounds, point_clouds)
return self.pool_structure_interior(Cnode,
branch_xL, branch_xR, branch_yL, branch_yR, branch_zL, branch_zR,
Cleaf, offsetX, offsetY, offsetZ)
@tf.function
def pool_structure_leaves(self, Cnode, CxL, CxR, CyL, CyR, CzL, CzR, Cleaf, offsetX, offsetY, offsetZ) :
cost_x = Cnode + CxL + CxR
cost_y = Cnode + CyL + CyR
cost_z = Cnode + CzL + CzR
cost_xyz, min_cost = self.hard_min(cost_x, cost_y, cost_z, Cleaf)
return min_cost, \
self.get_pred_branch_from_leaves(cost_xyz,
tf.concat([offsetX, offsetY, offsetZ, tf.ones_like(offsetZ)], axis=-1))
@tf.function
def pool_structure_interior(self, Cnode,
branch_xL, branch_xR, branch_yL, branch_yR, branch_zL, branch_zR,
Cleaf, offsetX, offsetY, offsetZ) :
CxL, planes_xL = branch_xL
CxR, planes_xR = branch_xR
CyL, planes_yL = branch_yL
CyR, planes_yR = branch_yR
CzL, planes_zL = branch_zL
CzR, planes_zR = branch_zR
plane_x = tf.concat([planes_xL, planes_xR], axis=1)
plane_y = tf.concat([planes_yL, planes_yR], axis=1)
plane_z = tf.concat([planes_zL, planes_zR], axis=1)
split_planes = tf.concat([
plane_x[:, tf.newaxis, ...],
plane_y[:, tf.newaxis, ...],
plane_z[:, tf.newaxis, ...],
], axis=1)
cost_x = Cnode + CxL + CxR
cost_y = Cnode + CyL + CyR
cost_z = Cnode + CzL + CzR
cost_xyz, min_cost = self.hard_min(cost_x, cost_y, cost_z, Cleaf)
return min_cost, \
self.get_pred_branch_from_interior(cost_xyz,
tf.concat([offsetX, offsetY, offsetZ, tf.ones_like(offsetZ)], axis=-1),
split_planes)