-
Notifications
You must be signed in to change notification settings - Fork 5
/
tf_util.py
436 lines (378 loc) · 16 KB
/
tf_util.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
import tensorflow as tf
from pc_distance import tf_nndistance, tf_approxmatch
from tf_ops.grouping.tf_grouping import knn_point
def mlp(features, layer_dims, bn=None, bn_params=None):
for i, num_outputs in enumerate(layer_dims[:-1]):
features = tf.contrib.layers.fully_connected(
features, num_outputs,
normalizer_fn=bn,
normalizer_params=bn_params,
scope='fc_%d' % i)
outputs = tf.contrib.layers.fully_connected(
features, layer_dims[-1],
activation_fn=None,
scope='fc_%d' % (len(layer_dims) - 1))
return outputs
def mlp_conv(inputs, layer_dims, bn=None, bn_params=None):
for i, num_out_channel in enumerate(layer_dims[:-1]):
inputs = tf.contrib.layers.conv1d(
inputs, num_out_channel,
kernel_size=1,
normalizer_fn=bn,
normalizer_params=bn_params,
scope='conv_%d' % i)
outputs = tf.contrib.layers.conv1d(
inputs, layer_dims[-1],
kernel_size=1,
activation_fn=None,
scope='conv_%d' % (len(layer_dims) - 1))
return outputs
def mlp_conv2(inputs, layer_dims, bn=None, bn_params=None):
for i, num_out_channel in enumerate(layer_dims[:-1]):
inputs = tf.contrib.layers.conv1d(
inputs, num_out_channel,
kernel_size=1,
normalizer_fn=bn,
normalizer_params=bn_params,
scope='conv2_%d' % i)
outputs = tf.contrib.layers.conv1d(
inputs, layer_dims[-1],
kernel_size=1,
activation_fn=None,
scope='conv2_%d' % (len(layer_dims) - 1))
return outputs
def chamfer(pcd1, pcd2):
dist1, _, dist2, _ = tf_nndistance.nn_distance(pcd1, pcd2)
dist1 = tf.reduce_mean(tf.sqrt(dist1))
dist2 = tf.reduce_mean(tf.sqrt(dist2))
return (dist1 + dist2) / 2
def earth_mover(pcd1, pcd2):
assert pcd1.shape[1] == pcd2.shape[1]
num_points = tf.cast(pcd1.shape[1], tf.float32)
match = tf_approxmatch.approx_match(pcd1, pcd2)
cost = tf_approxmatch.match_cost(pcd1, pcd2, match)
return tf.reduce_mean(cost / num_points)
def add_train_summary(name, value):
tf.summary.scalar(name, value, collections=['train_summary'])
def add_valid_summary(name, value):
avg, update = tf.metrics.mean(value)
tf.summary.scalar(name, avg, collections=['valid_summary'])
return update
def get_modified_cd_loss(pred, gt, forward_weight=1.0, threshold=None):
"""
pred: BxNxC,
label: BxN,
forward_weight: relative weight for forward_distance
"""
with tf.name_scope("cd_loss"):
dists_forward, _, dists_backward, _ = tf_nndistance.nn_distance(gt, pred)
if threshold is not None:
forward_threshold = tf.reduce_mean(dists_forward, keepdims=True, axis=1) * threshold
backward_threshold = tf.reduce_mean(dists_backward, keepdims=True, axis=1) * threshold
# only care about distance within threshold (ignore strong outliers)
dists_forward = tf.where(dists_forward < forward_threshold, dists_forward, tf.zeros_like(dists_forward))
dists_backward = tf.where(dists_backward < backward_threshold, dists_backward, tf.zeros_like(dists_backward))
# dists_forward is for each element in gt, the closest distance to this element
dists_forward = tf.reduce_mean(dists_forward, axis=1)
dists_backward = tf.reduce_mean(dists_backward, axis=1)
CD_dist = forward_weight * dists_forward + dists_backward
# CD_dist_norm = CD_dist/radius
cd_loss = tf.reduce_mean(CD_dist)
return cd_loss
def dense_conv(feature, growth_rate, n, k, scope, idx=None, **kwargs):
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
y, idx = get_edge_feature(feature, k=k, idx=idx) # [B N K 2*C]
for i in range(n):
if i == 0:
y = tf.concat([
conv2d(y, growth_rate, [1, 1], padding='VALID', scope='l%d' % i, **kwargs),
tf.tile(tf.expand_dims(feature, axis=2), [1, 1, k, 1])], axis=-1)
else:
y = tf.concat([
conv2d(y, growth_rate, [1, 1], padding='VALID', scope='l%d' % i, **kwargs),
y], axis=-1)
y = tf.reduce_max(y, axis=-2)
return y, idx
def dense_conv1(feature, growth_rate, n, k, scope, idx=None, **kwargs):
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
y, idx = get_edge_feature(feature, k=k, idx=idx) # [B N K 2*C]
for i in range(n):
if i == 0:
y = tf.concat([
conv2d(y, growth_rate, [1, 1], padding='VALID', scope='l%d' % i, **kwargs),
tf.tile(tf.expand_dims(feature, axis=2), [1, 1, k, 1])], axis=-1)
elif i == n-1:
y = tf.concat([
conv2d(y, growth_rate, [1, 1], padding='VALID', scope='l%d' % i, activation_fn=None, **kwargs),
y], axis=-1)
else:
y = tf.concat([
conv2d(y, growth_rate, [1, 1], padding='VALID', scope='l%d' % i, **kwargs),
y], axis=-1)
y = tf.reduce_max(y, axis=-2)
return y, idx
def get_edge_feature(point_cloud, k=20, idx=None):
"""Construct edge feature for each point
Args:
point_cloud: (batch_size, num_points, 1, num_dims)
nn_idx: (batch_size, num_points, k, 2)
k: int
Returns:
edge features: (batch_size, num_points, k, num_dims)
"""
if idx is None:
_, idx = knn_point(k+1, point_cloud, point_cloud, unique=True, sort=True)
idx = idx[:, :, 1:, :]
# [N, P, K, Dim]
point_cloud_neighbors = tf.gather_nd(point_cloud, idx)
point_cloud_central = tf.expand_dims(point_cloud, axis=-2)
point_cloud_central = tf.tile(point_cloud_central, [1, 1, k, 1])
edge_feature = tf.concat(
[point_cloud_central, point_cloud_neighbors - point_cloud_central], axis=-1)
return edge_feature, idx
def conv2d(inputs,
num_output_channels,
kernel_size,
scope=None,
stride=[1, 1],
padding='SAME',
use_xavier=True,
stddev=1e-3,
weight_decay=0.00001,
activation_fn=tf.nn.relu,
bn=False,
ibn=False,
bn_decay=None,
use_bias=True,
is_training=None,
reuse=None):
""" 2D convolution with non-linear operation.
Args:
inputs: 4-D tensor variable BxHxWxC
num_output_channels: int
kernel_size: a list of 2 ints
scope: string
stride: a list of 2 ints
padding: 'SAME' or 'VALID'
use_xavier: bool, use xavier_initializer if true
stddev: float, stddev for truncated_normal init
weight_decay: float
activation_fn: function
bn: bool, whether to use batch norm
bn_decay: float or float tensor variable in [0,1]
is_training: bool Tensor variable
Returns:
Variable tensor
"""
with tf.variable_scope(scope, reuse=reuse):
if use_xavier:
initializer = tf.contrib.layers.xavier_initializer()
else:
initializer = tf.truncated_normal_initializer(stddev=stddev)
outputs = tf.layers.conv2d(inputs, num_output_channels, kernel_size, stride, padding,
kernel_initializer=initializer,
kernel_regularizer=tf.contrib.layers.l2_regularizer(
weight_decay),
bias_regularizer=tf.contrib.layers.l2_regularizer(
weight_decay),
use_bias=use_bias, reuse=None)
assert not (bn and ibn)
if bn:
outputs = tf.layers.batch_normalization(
outputs, momentum=bn_decay, training=is_training, renorm=False, fused=True)
# outputs = tf.contrib.layers.batch_norm(outputs,is_training=is_training)
if ibn:
outputs = instance_norm(outputs, is_training)
if activation_fn is not None:
outputs = activation_fn(outputs)
return outputs
def instance_norm(net, train=True, weight_decay=0.00001):
batch, rows, cols, channels = [i.value for i in net.get_shape()]
var_shape = [channels]
mu, sigma_sq = tf.nn.moments(net, [1, 2], keepdims=True)
shift = tf.get_variable('shift', shape=var_shape,
initializer=tf.zeros_initializer,
regularizer=tf.contrib.layers.l2_regularizer(weight_decay))
scale = tf.get_variable('scale', shape=var_shape,
initializer=tf.ones_initializer,
regularizer=tf.contrib.layers.l2_regularizer(weight_decay))
epsilon = 1e-3
normalized = (net - mu) / tf.square(sigma_sq + epsilon)
return scale * normalized + shift
def conv1d(inputs,
num_output_channels,
kernel_size,
scope=None,
stride=1,
padding='SAME',
use_xavier=True,
stddev=1e-3,
weight_decay=0.00001,
activation_fn=tf.nn.relu,
bn=False,
ibn=False,
bn_decay=None,
use_bias=True,
is_training=None,
reuse=None):
""" 1D convolution with non-linear operation.
Args:
inputs: 3-D tensor variable BxHxWxC
num_output_channels: int
kernel_size: int
scope: string
stride: a list of 2 ints
padding: 'SAME' or 'VALID'
use_xavier: bool, use xavier_initializer if true
stddev: float, stddev for truncated_normal init
weight_decay: float
activation_fn: function
bn: bool, whether to use batch norm
bn_decay: float or float tensor variable in [0,1]
is_training: bool Tensor variable
Returns:
Variable tensor
"""
with tf.variable_scope(scope, reuse=reuse):
if use_xavier:
initializer = tf.contrib.layers.xavier_initializer()
else:
initializer = tf.truncated_normal_initializer(stddev=stddev)
outputs = tf.layers.conv1d(inputs, num_output_channels, kernel_size, stride, padding,
kernel_initializer=initializer,
kernel_regularizer=tf.contrib.layers.l2_regularizer(
weight_decay),
bias_regularizer=tf.contrib.layers.l2_regularizer(
weight_decay),
use_bias=use_bias, reuse=None)
assert not (bn and ibn)
if bn:
outputs = tf.layers.batch_normalization(
outputs, momentum=bn_decay, training=is_training, renorm=False, fused=True)
# outputs = tf.contrib.layers.batch_norm(outputs,is_training=is_training)
if ibn:
outputs = instance_norm(outputs, is_training)
if activation_fn is not None:
outputs = activation_fn(outputs)
return outputs
def fully_connected(inputs,
num_outputs,
scope,
use_xavier=True,
stddev=1e-3,
weight_decay=None,
activation_fn=tf.nn.relu,
bn=False,
bn_decay=None,
is_training=None):
""" Fully connected layer with non-linear operation.
Args:
inputs: 2-D tensor BxN
num_outputs: int
Returns:
Variable tensor of size B x num_outputs.
"""
with tf.variable_scope(scope) as sc:
num_input_units = inputs.get_shape()[-1].value
weights = _variable_with_weight_decay('weights',
shape=[num_input_units, num_outputs],
use_xavier=use_xavier,
stddev=stddev,
wd=weight_decay)
outputs = tf.matmul(inputs, weights)
biases = _variable_on_cpu('biases', [num_outputs],
tf.constant_initializer(0.0))
outputs = tf.nn.bias_add(outputs, biases)
if bn:
outputs = batch_norm_for_fc(outputs, is_training, bn_decay, 'bn')
if activation_fn is not None:
outputs = activation_fn(outputs)
return outputs
def max_pool2d(inputs,
kernel_size,
scope,
stride=[2, 2],
padding='VALID'):
""" 2D max pooling.
Args:
inputs: 4-D tensor BxHxWxC
kernel_size: a list of 2 ints
stride: a list of 2 ints
Returns:
Variable tensor
"""
with tf.variable_scope(scope) as sc:
kernel_h, kernel_w = kernel_size
stride_h, stride_w = stride
outputs = tf.nn.max_pool(inputs,
ksize=[1, kernel_h, kernel_w, 1],
strides=[1, stride_h, stride_w, 1],
padding=padding,
name=sc.name)
return outputs
def _variable_on_cpu(name, shape, initializer, use_fp16=False):
"""Helper to create a Variable stored on CPU memory.
Args:
name: name of the variable
shape: list of ints
initializer: initializer for Variable
Returns:
Variable Tensor
"""
with tf.device("/cpu:0"):
dtype = tf.float16 if use_fp16 else tf.float32
var = tf.get_variable(name, shape, initializer=initializer, dtype=dtype)
return var
def _variable_with_weight_decay(name, shape, stddev, wd, use_xavier=True):
"""Helper to create an initialized Variable with weight decay.
Note that the Variable is initialized with a truncated normal distribution.
A weight decay is added only if one is specified.
Args:
name: name of the variable
shape: list of ints
stddev: standard deviation of a truncated Gaussian
wd: add L2Loss weight decay multiplied by this float. If None, weight
decay is not added for this Variable.
use_xavier: bool, whether to use xavier initializer
Returns:
Variable Tensor
"""
if use_xavier:
initializer = tf.contrib.layers.xavier_initializer()
else:
initializer = tf.truncated_normal_initializer(stddev=stddev)
var = _variable_on_cpu(name, shape, initializer)
if wd is not None:
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def batch_norm_for_fc(inputs, is_training, bn_decay, scope):
""" Batch normalization on FC data.
Args:
inputs: Tensor, 2D BxC input
is_training: boolean tf.Varialbe, true indicates training phase
bn_decay: float or float tensor variable, controling moving average weight
scope: string, variable scope
Return:
normed: batch-normalized maps
"""
return batch_norm_template(inputs, is_training, scope, [0, ], bn_decay)
def batch_norm_template(inputs, is_training, scope, moments_dims_unused, bn_decay, data_format='NHWC'):
""" Batch normalization on convolutional maps and beyond...
Ref.: http://stackoverflow.com/questions/33949786/how-could-i-use-batch-normalization-in-tensorflow
Args:
inputs: Tensor, k-D input ... x C could be BC or BHWC or BDHWC
is_training: boolean tf.Varialbe, true indicates training phase
scope: string, variable scope
moments_dims: a list of ints, indicating dimensions for moments calculation
bn_decay: float or float tensor variable, controling moving average weight
data_format: 'NHWC' or 'NCHW'
Return:
normed: batch-normalized maps
"""
bn_decay = bn_decay if bn_decay is not None else 0.9
return tf.contrib.layers.batch_norm(inputs,
center=True, scale=True,
is_training=is_training, decay=bn_decay, updates_collections=None,
scope=scope,
data_format=data_format)