-
Notifications
You must be signed in to change notification settings - Fork 198
/
dihedral.py
387 lines (289 loc) · 13.8 KB
/
dihedral.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
import numpy as np
import theano.tensor as T
import lasagne as nn
# some helper functions that rotate arrays on the trailing axes.
# these should work for both Theano expressions and numpy arrays.
def array_tf_0(arr):
return arr
def array_tf_90(arr):
axes_order = range(arr.ndim - 2) + [arr.ndim - 1, arr.ndim - 2]
slices = [slice(None) for _ in range(arr.ndim - 2)] + [slice(None), slice(None, None, -1)]
return arr[tuple(slices)].transpose(axes_order)
def array_tf_180(arr):
slices = [slice(None) for _ in range(arr.ndim - 2)] + [slice(None, None, -1), slice(None, None, -1)]
return arr[tuple(slices)]
def array_tf_270(arr):
axes_order = range(arr.ndim - 2) + [arr.ndim - 1, arr.ndim - 2]
slices = [slice(None) for _ in range(arr.ndim - 2)] + [slice(None, None, -1), slice(None)]
return arr[tuple(slices)].transpose(axes_order)
def array_tf_0f(arr): # horizontal flip
slices = [slice(None) for _ in range(arr.ndim - 2)] + [slice(None), slice(None, None, -1)]
return arr[tuple(slices)]
def array_tf_90f(arr):
axes_order = range(arr.ndim - 2) + [arr.ndim - 1, arr.ndim - 2]
slices = [slice(None) for _ in range(arr.ndim - 2)] + [slice(None), slice(None)]
# slicing does nothing here, technically I could get rid of it.
return arr[tuple(slices)].transpose(axes_order)
def array_tf_180f(arr):
slices = [slice(None) for _ in range(arr.ndim - 2)] + [slice(None, None, -1), slice(None)]
return arr[tuple(slices)]
def array_tf_270f(arr):
axes_order = range(arr.ndim - 2) + [arr.ndim - 1, arr.ndim - 2]
slices = [slice(None) for _ in range(arr.ndim - 2)] + [slice(None, None, -1), slice(None, None, -1)]
return arr[tuple(slices)].transpose(axes_order)
# c01b versions of the helper functions
def array_tf_0_c01b(arr):
return arr
def array_tf_90_c01b(arr):
axes_order = [0, 2, 1, 3]
slices = [slice(None), slice(None), slice(None, None, -1), slice(None)]
return arr[tuple(slices)].transpose(axes_order)
def array_tf_180_c01b(arr):
slices = [slice(None), slice(None, None, -1), slice(None, None, -1), slice(None)]
return arr[tuple(slices)]
def array_tf_270_c01b(arr):
axes_order = [0, 2, 1, 3]
slices = [slice(None), slice(None, None, -1), slice(None), slice(None)]
return arr[tuple(slices)].transpose(axes_order)
def array_tf_0f_c01b(arr): # horizontal flip
slices = [slice(None), slice(None), slice(None, None, -1), slice(None)]
return arr[tuple(slices)]
def array_tf_90f_c01b(arr):
axes_order = [0, 2, 1, 3]
slices = [slice(None), slice(None), slice(None), slice(None)]
# slicing does nothing here, technically I could get rid of it.
return arr[tuple(slices)].transpose(axes_order)
def array_tf_180f_c01b(arr):
slices = [slice(None), slice(None, None, -1), slice(None), slice(None)]
return arr[tuple(slices)]
def array_tf_270f_c01b(arr):
axes_order = [0, 2, 1, 3]
slices = [slice(None), slice(None, None, -1), slice(None, None, -1), slice(None)]
return arr[tuple(slices)].transpose(axes_order)
class CyclicSliceLayer(nn.layers.Layer):
"""
This layer stacks rotations of 0, 90, 180, and 270 degrees of the input
along the batch dimension.
If the input has shape (batch_size, num_channels, r, c),
then the output will have shape (4 * batch_size, num_channels, r, c).
Note that the stacking happens on axis 0, so a reshape to
(4, batch_size, num_channels, r, c) will separate the slice axis.
"""
def __init__(self, input_layer):
super(CyclicSliceLayer, self).__init__(input_layer)
def get_output_shape_for(self, input_shape):
return (4 * input_shape[0],) + input_shape[1:]
def get_output_for(self, input, *args, **kwargs):
return nn.utils.concatenate([
array_tf_0(input),
array_tf_90(input),
array_tf_180(input),
array_tf_270(input),
], axis=0)
class DihedralSliceLayer(nn.layers.Layer):
"""
This layer stacks rotations of 0, 90, 180, and 270 degrees of the input,
as well as their horizontal flips, along the batch dimension.
If the input has shape (batch_size, num_channels, r, c),
then the output will have shape (8 * batch_size, num_channels, r, c).
Note that the stacking happens on axis 0, so a reshape to
(8, batch_size, num_channels, r, c) will separate the slice axis.
"""
def __init__(self, input_layer):
super(DihedralSliceLayer, self).__init__(input_layer)
def get_output_shape_for(self, input_shape):
return (8 * input_shape[0],) + input_shape[1:]
def get_output_for(self, input, *args, **kwargs):
return nn.utils.concatenate([
array_tf_0(input),
array_tf_90(input),
array_tf_180(input),
array_tf_270(input),
array_tf_0f(input),
array_tf_90f(input),
array_tf_180f(input),
array_tf_270f(input),
], axis=0)
class CyclicRollLayer(nn.layers.Layer):
"""
This layer turns (n_views * batch_size, num_features) into
(n_views * batch_size, n_views * num_features) by rolling
and concatenating feature maps.
"""
def __init__(self, input_layer):
super(CyclicRollLayer, self).__init__(input_layer)
self.compute_permutation_matrix()
def compute_permutation_matrix(self):
map_identity = np.arange(4)
map_rot90 = np.array([1, 2, 3, 0])
valid_maps = []
current_map = map_identity
for k in xrange(4):
valid_maps.append(current_map)
current_map = current_map[map_rot90]
self.perm_matrix = np.array(valid_maps)
def get_output_shape_for(self, input_shape):
return (input_shape[0], 4*input_shape[1])
def get_output_for(self, input, *args, **kwargs):
s = input.shape
input_unfolded = input.reshape((4, s[0] // 4, s[1]))
permuted_inputs = []
for p in self.perm_matrix:
input_permuted = input_unfolded[p].reshape(s)
permuted_inputs.append(input_permuted)
return nn.utils.concatenate(permuted_inputs, axis=1) # concatenate long the channel axis
class DihedralRollLayer(nn.layers.Layer):
"""
This layer turns (n_views * batch_size, num_features) into
(n_views * batch_size, n_views * num_features) by rolling
and concatenating feature maps.
"""
def __init__(self, input_layer):
super(DihedralRollLayer, self).__init__(input_layer)
self.compute_permutation_matrix()
def compute_permutation_matrix(self):
map_identity = np.arange(8)
map_rot90 = np.array([1, 2, 3, 0, 5, 6, 7, 4]) # 7, 4, 5, 6]) # CORRECTED
map_flip = np.array([4, 5, 6, 7, 0, 1, 2, 3])
valid_maps = []
current_map = map_identity
for k in xrange(4):
valid_maps.append(current_map)
current_map = current_map[map_rot90]
for k in xrange(4):
valid_maps.append(current_map[map_flip])
current_map = current_map[map_rot90]
self.perm_matrix = np.array(valid_maps)
def get_output_shape_for(self, input_shape):
return (input_shape[0], 8*input_shape[1])
def get_output_for(self, input, *args, **kwargs):
s = input.shape
input_unfolded = input.reshape((8, s[0] // 8, s[1]))
permuted_inputs = []
for p in self.perm_matrix:
input_permuted = input_unfolded[p].reshape(s)
permuted_inputs.append(input_permuted)
return nn.utils.concatenate(permuted_inputs, axis=1) # concatenate long the channel axis
class CyclicConvRollLayer(CyclicRollLayer):
"""
This layer turns (n_views * batch_size, num_channels, r, c) into
(n_views * batch_size, n_views * num_channels, r, c) by rolling
and concatenating feature maps.
It also applies the correct inverse transforms to the r and c
dimensions to align the feature maps.
"""
def __init__(self, input_layer):
super(CyclicConvRollLayer, self).__init__(input_layer)
self.inv_tf_funcs = [array_tf_0, array_tf_270, array_tf_180, array_tf_90]
def get_output_shape_for(self, input_shape):
return (input_shape[0], 4*input_shape[1]) + input_shape[2:]
def get_output_for(self, input, *args, **kwargs):
s = input.shape
input_unfolded = input.reshape((4, s[0] // 4, s[1], s[2], s[3]))
permuted_inputs = []
for p, inv_tf in zip(self.perm_matrix, self.inv_tf_funcs):
input_permuted = inv_tf(input_unfolded[p].reshape(s))
permuted_inputs.append(input_permuted)
return nn.utils.concatenate(permuted_inputs, axis=1) # concatenate long the channel axis
class DihedralConvRollLayer(DihedralRollLayer):
"""
This layer turns (n_views * batch_size, num_channels, r, c) into
(n_views * batch_size, n_views * num_channels, r, c) by rolling
and concatenating feature maps.
It also applies the correct inverse transforms to the r and c
dimensions to align the feature maps.
"""
def __init__(self, input_layer):
super(DihedralConvRollLayer, self).__init__(input_layer)
self.inv_tf_funcs = [array_tf_0, array_tf_270, array_tf_180, array_tf_90,
array_tf_0f, array_tf_90f, array_tf_180f, array_tf_270f]
raise RuntimeError("The implementation of this class is not correct.")
def get_output_shape_for(self, input_shape):
return (input_shape[0], 8*input_shape[1]) + input_shape[2:]
def get_output_for(self, input, *args, **kwargs):
s = input.shape
input_unfolded = input.reshape((8, s[0] // 8, s[1], s[2], s[3]))
permuted_inputs = []
for p, inv_tf in zip(self.perm_matrix, self.inv_tf_funcs):
input_permuted = inv_tf(input_unfolded[p].reshape(s))
permuted_inputs.append(input_permuted)
return nn.utils.concatenate(permuted_inputs, axis=1) # concatenate along the channel axis
class CyclicConvRollLayer_c01b(CyclicConvRollLayer):
"""
This layer turns (n_views * batch_size, num_channels, r, c) into
(n_views * batch_size, n_views * num_channels, r, c) by rolling
and concatenating feature maps.
It also applies the correct inverse transforms to the r and c
dimensions to align the feature maps.
"""
def __init__(self, input_layer):
super(CyclicConvRollLayer, self).__init__(input_layer)
self.inv_tf_funcs = [array_tf_0_c01b, array_tf_270_c01b, array_tf_180_c01b, array_tf_90_c01b]
def get_output_shape_for(self, input_shape):
return (4 * input_shape[0],) + input_shape[1:]
def get_output_for(self, input, *args, **kwargs):
s = input.shape
input_unfolded = input.reshape((s[0], s[1], s[2], 4, s[3] // 4))
permuted_inputs = []
for p, inv_tf in zip(self.perm_matrix, self.inv_tf_funcs):
input_permuted = inv_tf(input_unfolded[:, :, :, p, :].reshape(s))
permuted_inputs.append(input_permuted)
return nn.utils.concatenate(permuted_inputs, axis=0) # concatenate long the channel axis
class CyclicPoolLayer(nn.layers.Layer):
"""
Utility layer that unfolds the viewpoints dimension and pools over it.
Note that this only makes sense for dense representations, not for
feature maps (because no inverse transforms are applied to align them).
"""
def __init__(self, input_layer, pool_function=T.mean):
super(CyclicPoolLayer, self).__init__(input_layer)
self.pool_function = pool_function
def get_output_shape_for(self, input_shape):
return (input_shape[0] // 4, input_shape[1])
def get_output_for(self, input, *args, **kwargs):
unfolded_input = input.reshape((4, input.shape[0] // 4, input.shape[1]))
return self.pool_function(unfolded_input, axis=0)
class DihedralPoolLayer(nn.layers.Layer):
"""
Utility layer that unfolds the viewpoints dimension and pools over it.
Note that this only makes sense for dense representations, not for
feature maps (because no inverse transforms are applied to align them).
"""
def __init__(self, input_layer, pool_function=T.mean):
super(DihedralPoolLayer, self).__init__(input_layer)
self.pool_function = pool_function
def get_output_shape_for(self, input_shape):
return (input_shape[0] // 8, input_shape[1])
def get_output_for(self, input, *args, **kwargs):
unfolded_input = input.reshape((8, input.shape[0] // 8, input.shape[1]))
return self.pool_function(unfolded_input, axis=0)
class NINCyclicPoolLayer(nn.layers.Layer):
"""
Like CyclicPoolLayer, but broadcasting along all axes beyond the first two.
"""
def __init__(self, input_layer, pool_function=T.mean):
super(NINCyclicPoolLayer, self).__init__(input_layer)
self.pool_function = pool_function
def get_output_shape_for(self, input_shape):
return (input_shape[0] // 4,) + input_shape[1:]
def get_output_for(self, input, *args, **kwargs):
unfolded_input = input.reshape((4, self.input_shape[0] // 4) + self.input_shape[1:])
return self.pool_function(unfolded_input, axis=0)
class FlipSliceLayer(nn.layers.Layer):
"""
This layer stacks the input images along with their flips along the batch
dimension.
If the input has shape (batch_size, num_channels, r, c),
then the output will have shape (2 * batch_size, num_channels, r, c).
Note that the stacking happens on axis 0, so a reshape to
(2, batch_size, num_channels, r, c) will separate the slice axis.
"""
def __init__(self, input_layer):
super(FlipSliceLayer, self).__init__(input_layer)
def get_output_shape_for(self, input_shape):
return (2 * input_shape[0],) + input_shape[1:]
def get_output_for(self, input, *args, **kwargs):
return nn.utils.concatenate([
array_tf_0(input),
array_tf_0f(input),
], axis=0)