-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlayers.py
305 lines (247 loc) · 10.5 KB
/
layers.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
"""
License: Apache 2.0
Author: Ashley Gritzman
E-mail: [email protected]
"""
import tensorflow as tf
import numpy as np
# Get logger that has already been created in config.py
import daiquiri
logger = daiquiri.getLogger(__name__)
import utils as utl
import em_routing as em
def conv_caps(activation_in,
pose_in,
kernel,
stride,
ncaps_out,
name='conv_caps',
weights_regularizer=None):
"""Convolutional capsule layer.
"The routing procedure is used between each adjacent pair of capsule layers.
For convolutional capsules, each capsule in layer L + 1 sends feedback only to
capsules within its receptive field in layer L. Therefore each convolutional
instance of a capsule in layer L receives at most kernel size X kernel size
feedback from each capsule type in layer L + 1. The instances closer to the
border of the image receive fewer feedbacks with corner ones receiving only
one feedback per capsule type in layer L + 1."
See Hinton et al. "Matrix Capsules with EM Routing" for detailed description
convolutional capsule layer.
Author:
Ashley Gritzman 27/11/2018
Args:
activation_in:
(batch_size, child_space, child_space, child_caps, 1)
(64, 7, 7, 8, 1)
pose_in:
(batch_size, child_space, child_space, child_caps, 16)
(64, 7, 7, 8, 16)
kernel:
stride:
ncaps_out: depth dimension of parent capsules
Returns:
activation_out:
(batch_size, parent_space, parent_space, parent_caps, 1)
(64, 5, 5, 32, 1)
pose_out:
(batch_size, parent_space, parent_space, parent_caps, 16)
(64, 5, 5, 32, 16)
"""
with tf.variable_scope(name) as scope:
# Get shapes
shape = pose_in.get_shape().as_list()
batch_size = shape[0]
child_space = shape[1]
child_space_2 = int(child_space**2)
child_caps = shape[3]
parent_space = int(np.floor((child_space-kernel)/stride + 1))
parent_space_2 = int(parent_space**2)
parent_caps = ncaps_out
kernel_2 = int(kernel**2)
with tf.variable_scope('votes') as scope:
# Tile poses and activations
# (64, 7, 7, 8, 16) -> (64, 5, 5, 9, 8, 16)
pose_tiled, spatial_routing_matrix = utl.kernel_tile(
pose_in,
kernel=kernel,
stride=stride)
activation_tiled, _ = utl.kernel_tile(
activation_in,
kernel=kernel,
stride=stride)
# Check dimensions of spatial_routing_matrix
assert spatial_routing_matrix.shape == (child_space_2, parent_space_2)
# Unroll along batch_size and parent_space_2
# (64, 5, 5, 9, 8, 16) -> (64*5*5, 9*8, 16)
pose_unroll = tf.reshape(
pose_tiled,
shape=[batch_size * parent_space_2, kernel_2 * child_caps, 16])
activation_unroll = tf.reshape(
activation_tiled,
shape=[batch_size * parent_space_2, kernel_2 * child_caps, 1])
# (64*5*5, 9*8, 16) -> (64*5*5, 9*8, 32, 16)
votes = utl.compute_votes(
pose_unroll,
parent_caps,
weights_regularizer,
tag=True)
logger.info(name + ' votes shape: {}'.format(votes.get_shape()))
with tf.variable_scope('routing') as scope:
# votes (64*5*5, 9*8, 32, 16)
# activations (64*5*5, 9*8, 1)
# pose_out: (N, OH, OW, o, 4x4)
# activation_out: (N, OH, OW, o, 1)
pose_out, activation_out = em.em_routing(votes,
activation_unroll,
batch_size,
spatial_routing_matrix)
logger.info(name + ' pose_out shape: {}'.format(pose_out.get_shape()))
logger.info(name + ' activation_out shape: {}'
.format(activation_out.get_shape()))
tf.summary.histogram(name + "activation_out", activation_out)
return activation_out, pose_out
def fc_caps(activation_in,
pose_in,
ncaps_out,
name='class_caps',
weights_regularizer=None):
"""Fully connected capsule layer.
"The last layer of convolutional capsules is connected to the final capsule
layer which has one capsule per output class." We call this layer 'fully
connected' because it fits these characteristics, although Hinton et al. do
not use this teminology in the paper.
See Hinton et al. "Matrix Capsules with EM Routing" for detailed description.
Author:
Ashley Gritzman 27/11/2018
Args:
activation_in:
(batch_size, child_space, child_space, child_caps, 1)
(64, 7, 7, 8, 1)
pose_in:
(batch_size, child_space, child_space, child_caps, 16)
(64, 7, 7, 8, 16)
ncaps_out: number of class capsules
name:
weights_regularizer:
Returns:
activation_out:
score for each output class
(batch_size, ncaps_out)
(64, 5)
pose_out:
pose for each output class capsule
(batch_size, ncaps_out, 16)
(64, 5, 16)
"""
with tf.variable_scope(name) as scope:
# Get shapes
shape = pose_in.get_shape().as_list()
batch_size = shape[0]
child_space = shape[1]
child_caps = shape[3]
with tf.variable_scope('v') as scope:
# In the class_caps layer, we apply same multiplication to every spatial
# location, so we unroll along the batch and spatial dimensions
# (64, 5, 5, 32, 16) -> (64*5*5, 32, 16)
pose = tf.reshape(
pose_in,
shape=[batch_size * child_space * child_space, child_caps, 16])
activation = tf.reshape(
activation_in,
shape=[batch_size * child_space * child_space, child_caps, 1],
name="activation")
# (64*5*5, 32, 16) -> (65*5*5, 32, 5, 16)
votes = utl.compute_votes(pose, ncaps_out, weights_regularizer)
# (65*5*5, 32, 5, 16)
assert (
votes.get_shape() ==
[batch_size * child_space * child_space, child_caps, ncaps_out, 16])
logger.info('class_caps votes original shape: {}'
.format(votes.get_shape()))
with tf.variable_scope('coord_add') as scope:
# (64*5*5, 32, 5, 16)
votes = tf.reshape(
votes,
[batch_size, child_space, child_space, child_caps, ncaps_out,
votes.shape[-1]])
votes = coord_addition(votes)
with tf.variable_scope('routing') as scope:
# Flatten the votes:
# Combine the 4 x 4 spacial dimensions to appear as one spacial dimension # with many capsules.
# [64*5*5, 16, 5, 16] -> [64, 5*5*16, 5, 16]
votes_flat = tf.reshape(
votes,
shape=[batch_size, child_space * child_space * child_caps,
ncaps_out, votes.shape[-1]])
activation_flat = tf.reshape(
activation,
shape=[batch_size, child_space * child_space * child_caps, 1])
spatial_routing_matrix = utl.create_routing_map(child_space=1, k=1, s=1)
logger.info('class_caps votes in to routing shape: {}'
.format(votes_flat.get_shape()))
pose_out, activation_out = em.em_routing(votes_flat,
activation_flat,
batch_size,
spatial_routing_matrix)
activation_out = tf.squeeze(activation_out, name="activation_out")
pose_out = tf.squeeze(pose_out, name="pose_out")
logger.info('class_caps activation shape: {}'
.format(activation_out.get_shape()))
logger.info('class_caps pose shape: {}'.format(pose_out.get_shape()))
tf.summary.histogram("activation_out", activation_out)
return activation_out, pose_out
def coord_addition(votes):
"""Coordinate addition for connecting the last convolutional capsule layer to the final layer.
"When connecting the last convolutional capsule layer to the final layer we do
not want to throw away information about the location of the convolutional
capsules but we also want to make use of the fact that all capsules of the
same type are extracting the same entity at different positions. We therefore share the transformation matrices between different positions of the same
capsule type and add the scaled coordinate (row, column) of the center of the receptive field of each capsule to the first two elements of the right-hand
column of its vote matrix. We refer to this technique as Coordinate Addition. This should encourage the shared final transformations to produce values for
those two elements that represent the fine position of the entity relative to the center of the capsule’s receptive field."
In Suofei's implementation, they add x and y coordinates as two new dimensions to the pose matrix i.e. from 16 to 18 dimensions. The paper seems to say that the values are added to existing dimensions.
See Hinton et al. "Matrix Capsules with EM Routing" for detailed description
coordinate addition.
Author:
Ashley Gritzman 27/11/2018
Credit:
Based on Jonathan Hui's implementation:
https://jhui.github.io/2017/11/14/Matrix-Capsules-with-EM-routing-
Capsule-Network/
Args:
votes:
(batch_size, child_space, child_space, child_caps, n_output_capsules, 16)
(64, 5, 5, 32, 5, 16)
Returns:
votes:
same size as input, with coordinate encoding added to first two elements
of right hand column of vote matrix
(batch_size, parent_space, parent_space, parent_caps, 1)
(64, 5, 5, 32, 16)
"""
# get spacial dimension of votes
height = votes.get_shape().as_list()[1]
width = votes.get_shape().as_list()[2]
dims = votes.get_shape().as_list()[-1]
# Generate offset coordinates
# The difference here is that the coordinate won't be exactly in the middle of
# the receptive field, but will be evenly spread out
w_offset_vals = (np.arange(width) + 0.50)/float(width)
h_offset_vals = (np.arange(height) + 0.50)/float(height)
w_offset = np.zeros([width, dims]) # (5, 16)
w_offset[:,3] = w_offset_vals
# (1, 1, 5, 1, 1, 16)
w_offset = np.reshape(w_offset, [1, 1, width, 1, 1, dims])
h_offset = np.zeros([height, dims])
h_offset[:,7] = h_offset_vals
# (1, 5, 1, 1, 1, 16)
h_offset = np.reshape(h_offset, [1, height, 1, 1, 1, dims])
# Combine w and h offsets using broadcasting
# w is (1, 1, 5, 1, 1, 16)
# h is (1, 5, 1, 1, 1, 16)
# together (1, 5, 5, 1, 1, 16)
offset = w_offset + h_offset
# Convent from numpy to tensor
offset = tf.constant(offset, dtype=tf.float32)
votes = tf.add(votes, offset, name="votes_with_coord_add")
return votes