-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathASPCaps.py
393 lines (340 loc) · 17.1 KB
/
ASPCaps.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
"""
__author__ = 'Cimy Wang'
__mtime__ = '2021/9/26'
If necessary, please contact us. e-mail: jinping_wang@foxmail.com
"""
from keras import backend as K
import tensorflow as tf
import numpy as np
from keras import layers, initializers
from keras.utils import conv_utils
from keras.layers import InputSpec
from keras.backend import ndim, expand_dims
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
cf = K.image_data_format() == '..'
useGPU = True
def own_batch_dot(x, y, axes=None):
"""Batchwise dot product.
`batch_dot` is used to compute dot product of `x` and `y` when
`x` and `y` are data in batch, i.e. in a shape of
`(batch_size, :)`.
`batch_dot` results in a tensor or variable with less dimensions
than the input. If the number of dimensions is reduced to 1,
we use `expand_dims` to make sure that ndim is at least 2.
Arguments:
x: Keras tensor or variable with `ndim >= 2`.
y: Keras tensor or variable with `ndim >= 2`.
axes: list of (or single) int with target dimensions.
The lengths of `axes[0]` and `axes[1]` should be the same.
Returns:
A tensor with shape equal to the concatenation of `x`'s shape
(less the dimension that was summed over) and `y`'s shape
(less the batch dimension and the dimension that was summed over).
If the final rank is 1, we reshape it to `(batch_size, 1)`.
Examples:
Assume `x = [[1, 2], [3, 4]]` and `y = [[5, 6], [7, 8]]`
`batch_dot(x, y, axes=1) = [[17, 53]]` which is the main diagonal
of `x.dot(y.T)`, although we never have to calculate the off-diagonal
elements.
Shape inference:
Let `x`'s shape be `(100, 20)` and `y`'s shape be `(100, 30, 20)`.
If `axes` is (1, 2), to find the output shape of resultant tensor,
loop through each dimension in `x`'s shape and `y`'s shape:
* `x.shape[0]` : 100 : append to output shape
* `x.shape[1]` : 20 : do not append to output shape,
dimension 1 of `x` has been summed over. (`dot_axes[0]` = 1)
* `y.shape[0]` : 100 : do not append to output shape,
always ignore first dimension of `y`
* `y.shape[1]` : 30 : append to output shape
* `y.shape[2]` : 20 : do not append to output shape,
dimension 2 of `y` has been summed over. (`dot_axes[1]` = 2)
`output_shape` = `(100, 30)`
```python
>>> x_batch = K.ones(shape=(32, 20, 1))
>>> y_batch = K.ones(shape=(32, 30, 20))
>>> xy_batch_dot = K.batch_dot(x_batch, y_batch, axes=[1, 2])
>>> K.int_shape(xy_batch_dot)
(32, 1, 30)
```
"""
if isinstance(axes, int):
axes = (axes, axes)
x_ndim = ndim(x)
y_ndim = ndim(y)
if axes is None:
# behaves like tf.batch_matmul as default
axes = [x_ndim - 1, y_ndim - 2]
if x_ndim > y_ndim:
diff = x_ndim - y_ndim
y = array_ops.reshape(y,
array_ops.concat(
[array_ops.shape(y), [1] * (diff)], axis=0))
elif y_ndim > x_ndim:
diff = y_ndim - x_ndim
x = array_ops.reshape(x,
array_ops.concat(
[array_ops.shape(x), [1] * (diff)], axis=0))
else:
diff = 0
if ndim(x) == 2 and ndim(y) == 2:
if axes[0] == axes[1]:
out = math_ops.reduce_sum(math_ops.multiply(x, y), axes[0])
else:
out = math_ops.reduce_sum(
math_ops.multiply(array_ops.transpose(x, [1, 0]), y), axes[1])
else:
adj_x = None if axes[0] == ndim(x) - 1 else True
adj_y = True if axes[1] == ndim(y) - 1 else None
out = math_ops.matmul(x, y, adjoint_a=adj_x, adjoint_b=adj_y)
if diff:
if x_ndim > y_ndim:
idx = x_ndim + y_ndim - 3
else:
idx = x_ndim - 1
out = array_ops.squeeze(out, list(range(idx, idx + diff)))
if ndim(out) == 1:
out = expand_dims(out, 1)
return out
def squeeze(s):
sq = K.sum(K.square(s), axis=-1, keepdims=True)
return (sq / (1 + sq)) * (s / K.sqrt(sq + K.epsilon()))
class ConvertToCaps(layers.Layer):
def __init__(self, **kwargs):
super(ConvertToCaps, self).__init__(**kwargs)
self.input_spec = InputSpec(min_ndim=2)
def compute_output_shape(self, input_shape):
output_shape = list(input_shape)
output_shape.insert(1 if cf else len(output_shape), 1)
return tuple(output_shape)
def call(self, inputs):
return K.expand_dims(inputs, 1 if cf else -1)
def get_config(self):
config = {
'input_spec': 5
}
base_config = super(ConvertToCaps, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class FlattenCaps(layers.Layer):
def __init__(self, **kwargs):
super(FlattenCaps, self).__init__(**kwargs)
self.input_spec = InputSpec(min_ndim=4)
def compute_output_shape(self, input_shape):
if not all(input_shape[1:]):
# if tf.cond(input_shape[1:]):
raise ValueError('The shape of the input to "FlattenCaps" '
'is not fully defined '
'(got ' + str(input_shape[1:]) + '. '
'Make sure to pass a complete "input_shape" '
'or "batch_input_shape" argument to the first '
'layer in your model.')
return (input_shape[0], np.prod(input_shape[1:-1]), input_shape[-1])
def call(self, inputs):
shape = K.int_shape(inputs)
return K.reshape(inputs, (-1, np.prod(shape[1:-1]), shape[-1]))
class CapsToScalars(layers.Layer):
def __init__(self, **kwargs):
super(CapsToScalars, self).__init__(**kwargs)
self.input_spec = InputSpec(min_ndim=3)
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[1])
def call(self, inputs):
return K.sqrt(K.sum(K.square(inputs + K.epsilon()), axis=-1))
class ASPCaps(layers.Layer):
def __init__(self, ch_j=32, n_j=4,
kernel_size=(3, 3),
strides=(1, 1),
dilation_rate=(1, 1),
padding='same',
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
**kwargs):
super(ASPCaps, self).__init__(**kwargs)
self.ch_j = ch_j # Number of capsules in layer J
self.n_j = n_j # Number of neurons in a capsule in J
self.filters = self.ch_j * self.n_j
self.kernel_size = kernel_size
self.strides = strides
self.padding = padding
self.dilation_rate = (1, 1)
self.deformable_groups = 1
self.use_bias = use_bias
self.kernel_initializer = kernel_initializer
self.bias_initializer = bias_initializer
self.kernel_regularizer = kernel_regularizer
self.bias_regularizer = bias_regularizer
def build(self, input_shape):
self.h_i, self.w_i, self.ch_i, self.n_i = input_shape[1:5]
self.h_j, self.w_j = [conv_utils.conv_output_length(input_shape[i + 1],
self.kernel_size[i],
padding=self.padding,
stride=self.strides[i],
dilation=self.dilation_rate[i]) for i in (0, 1)]
self.kernel_size = (self.kernel_size[0], self.kernel_size[1],
self.ch_i * self.n_i, self.ch_j * self.n_j)
self.kernel = self.add_weight(
name='kernel',
shape=self.kernel_size,
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
trainable=True,
dtype='float32',
)
if self.use_bias:
self.bias = self.add_weight(
name='bias',
shape=(self.filters,),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
trainable=True,
dtype='float32',
)
# [kh, kw, ic, 3 * groups * kh, kw]--->3 * groups * kh * kw = oc [output channels]
self.offset_kernel = self.add_weight(
name='offset_kernel',
shape=(self.kernel_size[0], self.kernel_size[1], self.kernel_size[2],
3 * self.deformable_groups * self.kernel_size[0] * self.kernel_size[1]),
initializer='zeros',
trainable=True,
dtype='float32')
self.offset_bias = self.add_weight(
name='offset_bias',
shape=(3 * self.kernel_size[0] * self.kernel_size[1] * self.deformable_groups,),
initializer='zeros',
trainable=True,
dtype='float32',
)
self.ks = self.kernel_size[0] * self.kernel_size[1]
self.ph, self.pw = (self.kernel_size[0] - 1) // 2, (self.kernel_size[1] - 1) // 2
self.phw = tf.constant([self.ph, self.pw], dtype='int32')
self.patch_yx = tf.stack(
tf.meshgrid(tf.range(-self.phw[1], self.phw[1] + 1), tf.range(-self.phw[0], self.phw[0] + 1))[::-1],
axis=-1)
self.patch_yx = tf.reshape(self.patch_yx, [-1, 2])
self.built = True
super(ASPCaps, self).build(input_shape)
def call(self, inputs):
x = K.reshape(inputs, (-1, self.h_i, self.w_i, self.ch_i * self.n_i))
offset = tf.nn.conv2d(x, self.offset_kernel, strides=[1, self.strides[0], self.strides[1], 1], padding='SAME',
dilations=[1, 1, 1, 1])
offset += self.offset_bias
bs, ih, iw, ic = [v.value for v in offset.shape]
bs = tf.shape(x)[0]
oyox, mask = offset[..., :2 * self.ks], offset[..., 2 * self.ks:]
mask = tf.nn.sigmoid(mask)
grid_yx = tf.stack(tf.meshgrid(tf.range(iw), tf.range(ih))[::-1], axis=-1)
grid_yx = tf.reshape(grid_yx, [1, ih, iw, 1, 2]) + self.phw + self.patch_yx
grid_yx = tf.cast(grid_yx, 'float32') + tf.reshape(oyox, [bs, ih, iw, -1, 2])
grid_iy0ix0 = tf.floor(grid_yx)
grid_iy1ix1 = tf.clip_by_value(grid_iy0ix0 + 1, 0, tf.constant([ih + 1, iw + 1], dtype='float32'))
grid_iy1, grid_ix1 = tf.split(grid_iy1ix1, 2, axis=4)
grid_iy0ix0 = tf.clip_by_value(grid_iy0ix0, 0, tf.constant([ih + 1, iw + 1], dtype='float32'))
grid_iy0, grid_ix0 = tf.split(grid_iy0ix0, 2, axis=4)
grid_yx = tf.clip_by_value(grid_yx, 0, tf.constant([ih + 1, iw + 1], dtype='float32'))
batch_index = tf.tile(tf.reshape(tf.range(bs), [bs, 1, 1, 1, 1, 1]), [1, ih, iw, self.ks, 4, 1])
grid = tf.reshape(tf.concat([grid_iy1ix1, grid_iy1, grid_ix0, grid_iy0, grid_ix1, grid_iy0ix0], axis=-1),
[bs, ih, iw, self.ks, 4, 2])
grid = tf.concat([batch_index, tf.cast(grid, 'int32')], axis=-1)
delta = tf.reshape(tf.concat([grid_yx - grid_iy0ix0, grid_iy1ix1 - grid_yx], axis=-1),
[bs, ih, iw, self.ks, 2, 2])
w = tf.expand_dims(delta[..., 0], axis=-1) * tf.expand_dims(delta[..., 1], axis=-2)
x = tf.pad(x, [[0, 0], [int(self.ph), int(self.ph)], [int(self.pw), int(self.pw)], [0, 0]])
map_sample = tf.gather_nd(x, grid)
map_bilinear = tf.reduce_sum(tf.reshape(w, [bs, ih, iw, self.ks, 4, 1]) * map_sample, axis=-2) * tf.expand_dims(
mask, axis=-1)
map_all = tf.reshape(map_bilinear, [bs, ih, iw, map_bilinear.shape[3] * map_bilinear.shape[4]])
output = tf.nn.conv2d(map_all, tf.reshape(self.kernel, [1, 1, -1, self.filters]), strides=[1, 1, 1, 1],
padding='SAME')
if self.use_bias:
output += self.bias
outputs = squeeze(K.reshape(output, ((-1, self.h_j, self.w_j,
self.ch_j, self.n_j))))
return outputs
def compute_output_shape(self, input_shape):
return (input_shape[0], self.h_j, self.w_j, self.ch_j, self.n_j)
class CapsuleLayer(layers.Layer):
def __init__(self, num_capsule=16, dim_capsule=16, channels=0, routings=3,
kernel_initializer='glorot_uniform',
**kwargs):
super(CapsuleLayer, self).__init__(**kwargs)
self.num_capsule = num_capsule
self.dim_capsule = dim_capsule
self.routings = routings
self.channels = channels
self.kernel_initializer = initializers.get(kernel_initializer)
def build(self, input_shape):
assert len(input_shape) >= 3, "The input Tensor should have shape=[None, input_num_capsule, input_dim_capsule]"
self.input_num_capsule = input_shape[1]
self.input_dim_capsule = input_shape[2]
if (self.channels != 0):
assert int(self.input_num_capsule / self.channels) / (self.input_num_capsule / self.channels) == 1, "error"
self.W = self.add_weight(shape=[self.num_capsule, self.channels,
self.dim_capsule, self.input_dim_capsule],
initializer=self.kernel_initializer,
name='W')
self.B = self.add_weight(shape=[self.num_capsule, self.dim_capsule],
initializer=self.kernel_initializer,
name='B')
else:
self.W = self.add_weight(shape=[self.num_capsule, self.input_num_capsule,
self.dim_capsule, self.input_dim_capsule],
initializer=self.kernel_initializer,
name='W')
self.B = self.add_weight(shape=[self.num_capsule, self.dim_capsule],
initializer=self.kernel_initializer,
name='B')
self.built = True
def call(self, inputs, training=None):
# inputs.shape=[None, input_num_capsule, input_dim_capsule]
# inputs_expand.shape=[None, 1, input_num_capsule, input_dim_capsule]
inputs_expand = K.expand_dims(inputs, 1)
# Replicate num_capsule dimension to prepare being multiplied by W
# inputs_tiled.shape=[None, num_capsule, input_num_capsule, input_dim_capsule]
inputs_tiled = K.tile(inputs_expand, [1, self.num_capsule, 1, 1])
if (self.channels != 0):
W2 = K.repeat_elements(self.W, int(self.input_num_capsule / self.channels), 1)
else:
W2 = self.W
# Compute `inputs * W` by scanning inputs_tiled on dimension 0.
# x.shape=[num_capsule, input_num_capsule, input_dim_capsule]
# W.shape=[num_capsule, input_num_capsule, dim_capsule, input_dim_capsule]
# Regard the first two dimensions as `batch` dimension,
# then matmul: [input_dim_capsule] x [dim_capsule, input_dim_capsule]^T -> [dim_capsule].
# inputs_hat.shape = [None, num_capsule, input_num_capsule, dim_capsule]
inputs_hat = K.map_fn(lambda x: own_batch_dot(x, W2, [2, 3]), elems=inputs_tiled)
# Begin: Routing algorithm ---------------------------------------------------------------------#
# The prior for coupling coefficient, initialized as zeros.
# b.shape = [None, self.num_capsule, self.input_num_capsule].
b = tf.zeros(shape=[K.shape(inputs_hat)[0], self.num_capsule, self.input_num_capsule])
assert self.routings > 0, 'The routings should be > 0.'
for i in range(self.routings):
# c.shape=[batch_size, num_capsule, input_num_capsule]
c = tf.nn.softmax(b, dim=1)
# c.shape = [batch_size, num_capsule, input_num_capsule]
# inputs_hat.shape=[None, num_capsule, input_num_capsule, dim_capsule]
# The first two dimensions as `batch` dimension,
# then matmal: [input_num_capsule] x [input_num_capsule, dim_capsule] -> [dim_capsule].
# outputs.shape=[None, num_capsule, dim_capsule]
outputs = squash(own_batch_dot(c, inputs_hat, [2, 2]) + self.B) # [None, 10, 16]
if i < self.routings - 1:
# outputs.shape = [None, num_capsule, dim_capsule]
# inputs_hat.shape=[None, num_capsule, input_num_capsule, dim_capsule]
# The first two dimensions as `batch` dimension,
# then matmal: [dim_capsule] x [input_num_capsule, dim_capsule]^T -> [input_num_capsule].
# b.shape=[batch_size, num_capsule, input_num_capsule]
b += own_batch_dot(outputs, inputs_hat, [2, 3])
# End: Routing algorithm -----------------------------------------------------------------------#
return outputs
def compute_output_shape(self, input_shape):
return tuple([None, self.num_capsule, self.dim_capsule])
def _squash(input_tensor):
norm = tf.norm(input_tensor, axis=-1, keep_dims=True)
norm_squared = norm * norm
return (input_tensor / norm) * (norm_squared / (1 + norm_squared))
def squash(vectors, axis=-1):
s_squared_norm = K.sum(K.square(vectors), axis, keepdims=True)
scale = s_squared_norm / (1 + s_squared_norm) / K.sqrt(s_squared_norm)
return scale * vectors