-
Notifications
You must be signed in to change notification settings - Fork 55
/
deep_learning_layers.py
executable file
·286 lines (236 loc) · 12.8 KB
/
deep_learning_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
"""Library providing some custom Lasagne layers.
"""
import lasagne
import theano
import theano.tensor as T
import numpy as np
from lasagne.layers.dnn import Conv2DDNNLayer, Conv3DDNNLayer, MaxPool2DDNNLayer, MaxPool3DDNNLayer
from lasagne.utils import as_tuple
from lasagne.layers import Conv1DLayer, MaxPool1DLayer, Layer
class ConvolutionOver2DAxisLayer(Conv2DDNNLayer):
def __init__(self, incoming, num_filters, filter_size, channel=1, axis=(2,3), **kwargs):
super(ConvolutionOver2DAxisLayer, self).__init__(incoming,
num_filters,
filter_size=filter_size,
check_shape=False,
**kwargs)
self.axis = axis
self.channel = channel
def get_output_shape_for(self, input_shape):
shape = list(input_shape)
# axis channel shrinks
for i in xrange(2):
shape[self.axis[i]] = lasagne.layers.conv.conv_output_length(shape[self.axis[i]], self.filter_size[i], self.stride[i], self.pad[i])
# filter channel changes
shape[self.channel] = self.num_filters
return tuple(shape)
def get_output_for(self, input, **kwargs):
conved = self.convolve(input, **kwargs)
if self.b is None:
activation = conved
elif self.untie_biases:
raise NotImplementedError("untie_biases has not been implemented")
else:
shuffle = ['x']*len(self.input_shape)
shuffle[self.channel] = 0
activation = conved + self.b.dimshuffle(tuple(shuffle))
return self.nonlinearity(activation)
def convolve(self, input, **kwargs):
dimshuffle = range(len(self.input_shape))
dimshuffle.remove(self.channel)
for axis in self.axis:
dimshuffle.remove(axis)
dimshuffle.append(self.channel)
for axis in self.axis:
dimshuffle.append(axis)
input = input.dimshuffle(*dimshuffle).reshape((-1,
self.input_shape[self.channel],
self.input_shape[self.axis[0]],
self.input_shape[self.axis[1]],
))
conved = super(ConvolutionOver2DAxisLayer, self).convolve(input, **kwargs)
output_shape = list(self.input_shape)
output_shape = [i for j, i in enumerate(output_shape) if j not in [self.channel, self.axis[0], self.axis[1]]]
conv_output_shape = self.get_output_shape_for(self.input_shape)
conved = conved.reshape(tuple(output_shape)+
(self.num_filters,
conv_output_shape[self.axis[0]],
conv_output_shape[self.axis[1]],
)
)
reverse_dimshuffle = [dimshuffle.index(i) for i in xrange(len(self.input_shape))]
conved = conved.dimshuffle(*reverse_dimshuffle)
return conved
class MaxPoolOver2DAxisLayer(MaxPool2DDNNLayer):
def __init__(self, incoming, pool_size, axis=(2,3), **kwargs):
super(MaxPoolOver2DAxisLayer, self).__init__(incoming, pool_size, check_shape=False, **kwargs)
self.axis = axis
def get_output_shape_for(self, input_shape):
shape = list(input_shape)
# axis channels shrink
for i in xrange(2):
shape[self.axis[i]] = lasagne.layers.pool.pool_output_length(shape[self.axis[i]],
pool_size=self.pool_size[i],
stride=self.stride[i],
pad=self.pad[i],
ignore_border=True
)
return tuple(shape)
def get_output_for(self, input, **kwargs):
dimshuffle = range(len(self.input_shape))
for axis in self.axis:
dimshuffle.remove(axis)
for axis in self.axis:
dimshuffle.append(axis)
input = input.dimshuffle(*dimshuffle).reshape((-1,
1,
self.input_shape[self.axis[0]],
self.input_shape[self.axis[1]],
))
pooled = super(MaxPoolOver2DAxisLayer, self).get_output_for(input, **kwargs)
output_shape = list(self.input_shape)
output_shape = [i for j, i in enumerate(output_shape) if j not in [self.axis[0], self.axis[1]]]
conv_output_shape = self.get_output_shape_for(self.input_shape)
pooled = pooled.reshape(tuple(output_shape)+
(conv_output_shape[self.axis[0]],
conv_output_shape[self.axis[1]],
)
)
reverse_dimshuffle = [dimshuffle.index(i) for i in xrange(len(self.input_shape))]
pooled = pooled.dimshuffle(*reverse_dimshuffle)
return pooled
class ConvolutionOver3DAxisLayer(Conv3DDNNLayer):
def __init__(self, incoming, num_filters, filter_size, channel=1, axis=(2,3,4), **kwargs):
super(ConvolutionOver3DAxisLayer, self).__init__(incoming,
num_filters,
filter_size=filter_size,
check_shape=False,
**kwargs)
self.axis = axis
self.channel = channel
def get_output_shape_for(self, input_shape):
shape = list(input_shape)
# axis channel shrinks
for i in xrange(3):
shape[self.axis[i]] = lasagne.layers.conv.conv_output_length(shape[self.axis[i]], self.filter_size[i], self.stride[i], self.pad[i])
# filter channel changes
shape[self.channel] = self.num_filters
return tuple(shape)
def get_output_for(self, input, **kwargs):
conved = self.convolve(input, **kwargs)
if self.b is None:
activation = conved
elif self.untie_biases:
raise NotImplementedError("untie_biases has not been implemented")
else:
shuffle = ['x']*len(self.input_shape)
shuffle[self.channel] = 0
activation = conved + self.b.dimshuffle(tuple(shuffle))
return self.nonlinearity(activation)
def convolve(self, input, **kwargs):
dimshuffle = range(len(self.input_shape))
dimshuffle.remove(self.channel)
for axis in self.axis:
dimshuffle.remove(axis)
dimshuffle.append(self.channel)
for axis in self.axis:
dimshuffle.append(axis)
input = input.dimshuffle(*dimshuffle).reshape((-1,
self.input_shape[self.channel],
self.input_shape[self.axis[0]],
self.input_shape[self.axis[1]],
self.input_shape[self.axis[2]],
))
conved = super(ConvolutionOver3DAxisLayer, self).convolve(input, **kwargs)
output_shape = list(self.input_shape)
output_shape = [i for j, i in enumerate(output_shape) if j not in [self.channel, self.axis[0], self.axis[1], self.axis[2]]]
conv_output_shape = self.get_output_shape_for(self.input_shape)
output_shape = tuple(output_shape)+(self.num_filters,
conv_output_shape[self.axis[0]],
conv_output_shape[self.axis[1]],
conv_output_shape[self.axis[2]],
)
conved = conved.reshape(output_shape)
reverse_dimshuffle = [dimshuffle.index(i) for i in xrange(len(self.input_shape))]
conved = conved.dimshuffle(*reverse_dimshuffle)
return conved
class MaxPoolOver3DAxisLayer(MaxPool3DDNNLayer):
def __init__(self, incoming, pool_size, axis=(2,3,4), **kwargs):
super(MaxPoolOver3DAxisLayer, self).__init__(incoming, pool_size, **kwargs)
self.axis = axis
def get_output_shape_for(self, input_shape):
shape = list(input_shape)
# axis channels shrink
for i in xrange(3):
shape[self.axis[i]] = lasagne.layers.pool.pool_output_length(shape[self.axis[i]],
pool_size=self.pool_size[i],
stride=self.stride[i],
pad=self.pad[i],
ignore_border=True
)
return tuple(shape)
def get_output_for(self, input, **kwargs):
dimshuffle = range(len(self.input_shape))
for axis in self.axis:
dimshuffle.remove(axis)
for axis in self.axis:
dimshuffle.append(axis)
input = input.dimshuffle(*dimshuffle).reshape((-1,
1,
self.input_shape[self.axis[0]],
self.input_shape[self.axis[1]],
self.input_shape[self.axis[2]],
))
pooled = super(MaxPoolOver3DAxisLayer, self).get_output_for(input, **kwargs)
output_shape = list(self.input_shape)
output_shape = [i for j, i in enumerate(output_shape) if j not in self.axis]
conv_output_shape = self.get_output_shape_for(self.input_shape)
output_shape = tuple(output_shape)+ (conv_output_shape[self.axis[0]],
conv_output_shape[self.axis[1]],
conv_output_shape[self.axis[2]],
)
pooled = pooled.reshape(output_shape)
reverse_dimshuffle = [dimshuffle.index(i) for i in xrange(len(self.input_shape))]
pooled = pooled.dimshuffle(*reverse_dimshuffle)
return pooled
class ConvolutionOverAxisLayer(ConvolutionOver2DAxisLayer):
def __init__(self, incoming, num_filters, filter_size, channel=1, axis=(2,), **kwargs):
assert channel != 0, "using batch as either axis or channel is not supported"
if axis[0] == len(incoming.output_shape)-1:
axis = (axis[0], len(incoming.output_shape)-2)
else:
axis = (axis[0], len(incoming.output_shape)-1)
filter_size = (filter_size[0], 1)
super(ConvolutionOverAxisLayer, self).__init__(incoming,
num_filters,
axis=axis,
channel=channel,
filter_size=filter_size,
**kwargs)
class MaxPoolOverAxisLayer(MaxPoolOver2DAxisLayer):
def __init__(self, incoming, pool_size, axis=(2,), **kwargs):
if axis[0] == len(incoming.output_shape)-1:
axis = (len(incoming.output_shape)-2, axis[0])
else:
axis = (len(incoming.output_shape)-1, axis[0])
pool_size = (1, pool_size[0])
super(MaxPoolOverAxisLayer, self).__init__(incoming,
pool_size=pool_size,
axis=axis,
**kwargs)
class FixedScaleLayer(Layer):
"""Layer which simply scales the input by a fixed factor.
"""
def __init__(self, incoming, scale=1, **kwargs):
super(FixedScaleLayer, self).__init__(incoming, **kwargs)
self.scale = scale
def get_output_for(self, input, **kwargs):
return input * self.scale
def FixedConstantLayer(constant):
"""Function creating an InputLayer which outputs a fixed scalar.
"""
theano_var = theano.shared(constant)
# Add 0 to the theano var, to prevent it from returning a cudandarray later
theano_var += theano.shared(np.array(0.0, dtype='float32'))
return lasagne.layers.InputLayer(
constant.shape, input_var=theano_var)