-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_layers.py
507 lines (452 loc) · 22.4 KB
/
custom_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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# -*- coding: utf-8 -*-
'''
This is a modification of the SeparableConv3D code in Keras,
to perform just the Depthwise Convolution (1st step) of the
Depthwise Separable Convolution layer.
'''
from __future__ import absolute_import
from keras import backend as K
from keras import initializers
from keras import regularizers
from keras import constraints
from keras import layers
import keras.utils as conv_utils
from _keras_legacy_interfaces import conv3d_args_preprocessor, generate_legacy_interface
from keras.layers import Conv3D, InputSpec
from _keras_backend_tensorflow_backend import _preprocess_padding, _preprocess_conv3d_input
import tensorflow as tf
def depthwise_conv3d_args_preprocessor(args, kwargs):
converted = []
if 'init' in kwargs:
init = kwargs.pop('init')
kwargs['depthwise_initializer'] = init
converted.append(('init', 'depthwise_initializer'))
args, kwargs, _converted = conv3d_args_preprocessor(args, kwargs)
return args, kwargs, converted + _converted
legacy_depthwise_conv3d_support = generate_legacy_interface(
allowed_positional_args=['filters', 'kernel_size'],
conversions=[('nb_filter', 'filters'),
('subsample', 'strides'),
('border_mode', 'padding'),
('dim_ordering', 'data_format'),
('b_regularizer', 'bias_regularizer'),
('b_constraint', 'bias_constraint'),
('bias', 'use_bias')],
value_conversions={'dim_ordering': {'tf': 'channels_last',
'th': 'channels_first',
'default': None}},
preprocessor=depthwise_conv3d_args_preprocessor)
class DepthwiseConv3D(Conv3D):
"""Depthwise separable 3D convolution.
Depthwise Separable convolutions consist in performing
just the first step in a depthwise spatial convolution
(which acts on each input channel separately).
It does not perform the pointwise convolution (second step).
The `depth_multiplier` argument controls how many
output channels are generated per input channel in the depthwise step.
# Arguments
kernel_size: An integer or tuple/list of 3 integers, specifying the
depth, width and height of the 3D convolution window.
Can be a single integer to specify the same value for
all spatial dimensions.
strides: An integer or tuple/list of 3 integers,
specifying the strides of the convolution along the depth, width and height.
Can be a single integer to specify the same value for
all spatial dimensions.
padding: one of `"valid"` or `"same"` (case-insensitive).
depth_multiplier: The number of depthwise convolution output channels
for each input channel.
The total number of depthwise convolution output
channels will be equal to `filterss_in * depth_multiplier`.
data_format: A string,
one of `channels_last` (default) or `channels_first`.
The ordering of the dimensions in the inputs.
`channels_last` corresponds to inputs with shape
`(batch, height, width, channels)` while `channels_first`
corresponds to inputs with shape
`(batch, channels, height, width)`.
It defaults to the `image_data_format` value found in your
Keras config file at `~/.keras/keras.json`.
If you never set it, then it will be "channels_last".
activation: Activation function to use
(see [activations](../activations.md)).
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
depthwise_initializer: Initializer for the depthwise kernel matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
depthwise_regularizer: Regularizer function applied to
the depthwise kernel matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
dialation_rate: List of ints.
Defines the dilation factor for each dimension in the
input. Defaults to (1,1,1)
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
depthwise_constraint: Constraint function applied to
the depthwise kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
# Input shape
5D tensor with shape:
`(batch, depth, channels, rows, cols)` if data_format='channels_first'
or 5D tensor with shape:
`(batch, depth, rows, cols, channels)` if data_format='channels_last'.
# Output shape
5D tensor with shape:
`(batch, filters * depth, new_depth, new_rows, new_cols)` if data_format='channels_first'
or 4D tensor with shape:
`(batch, new_depth, new_rows, new_cols, filters * depth)` if data_format='channels_last'.
`rows` and `cols` values might have changed due to padding.
"""
#@legacy_depthwise_conv3d_support
def __init__(self,
kernel_size,
strides=(1, 1, 1),
padding='valid',
depth_multiplier=1,
group_size=None,
data_format=None,
activation=None,
use_bias=True,
depthwise_initializer='glorot_uniform',
bias_initializer='zeros',
dilation_rate = (1, 1, 1),
depthwise_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
depthwise_constraint=None,
bias_constraint=None,
**kwargs):
super(DepthwiseConv3D, self).__init__(
filters=None,
kernel_size=kernel_size,
strides=strides,
padding=padding,
data_format=data_format,
activation=activation,
use_bias=use_bias,
bias_regularizer=bias_regularizer,
dilation_rate=dilation_rate,
activity_regularizer=activity_regularizer,
bias_constraint=bias_constraint,
**kwargs)
self.depth_multiplier = depth_multiplier
self.group_size = group_size
self.depthwise_initializer = initializers.get(depthwise_initializer)
self.depthwise_regularizer = regularizers.get(depthwise_regularizer)
self.depthwise_constraint = constraints.get(depthwise_constraint)
self.bias_initializer = initializers.get(bias_initializer)
self.dilation_rate = dilation_rate
self._padding = _preprocess_padding(self.padding)
self._strides = (1,) + self.strides + (1,)
self._data_format = 'NDHWC' if self.data_format=='channels_last' else 'NCDHW'
def build(self, input_shape):
if len(input_shape) < 5:
raise ValueError('Inputs to `DepthwiseConv3D` should have rank 5. '
'Received input shape:', str(input_shape))
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs to '
'`DepthwiseConv3D` '
'should be defined. Found `None`.')
self.input_dim = int(input_shape[channel_axis])
depthwise_kernel_shape = (self.kernel_size[0],
self.kernel_size[1],
self.kernel_size[2],
1,
self.input_dim * self.depth_multiplier)
self.depthwise_kernel = self.add_weight(
shape=depthwise_kernel_shape,
initializer=self.depthwise_initializer,
name='depthwise_kernel',
regularizer=self.depthwise_regularizer,
constraint=self.depthwise_constraint)
if self.use_bias:
self.bias = self.add_weight(shape=self.input_dim * self.depth_multiplier,
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
else:
self.bias = None
# Set input spec.
self.input_spec = InputSpec(ndim=5, axes={channel_axis: self.input_dim})
self.built = True
def call(self, inputs, training=None):
inputs = _preprocess_conv3d_input(inputs, self.data_format)
if self.data_format == 'channels_first':
dilation = tuple(list(self.dilation_rate) + [1,] + [1,])
elif self.data_format == 'channels_last':
dilation = tuple([1,] + list(self.dilation_rate) + [1,])
outputs = tf.nn.conv3d(inputs[0], self.depthwise_kernel, strides=self._strides,
padding= self._padding, dilations = dilation,
data_format=self._data_format)
if self.bias is not None:
outputs = K.bias_add(
outputs,
self.bias,
data_format=self.data_format)
if self.activation is not None:
return self.activation(outputs)
return outputs
def compute_output_shape(self, input_shape):
if self.data_format == 'channels_first':
depth = input_shape[2]
rows = input_shape[3]
cols = input_shape[4]
out_filters = self.groups * self.depth_multiplier
elif self.data_format == 'channels_last':
depth = input_shape[1]
rows = input_shape[2]
cols = input_shape[3]
out_filters = self.groups * self.depth_multiplier
depth = conv_utils.conv_output_length(depth, self.kernel_size[0],
self.padding,
self.strides[0])
rows = conv_utils.conv_output_length(rows, self.kernel_size[1],
self.padding,
self.strides[1])
cols = conv_utils.conv_output_length(cols, self.kernel_size[2],
self.padding,
self.strides[2])
if self.data_format == 'channels_first':
return (input_shape[0], out_filters, depth, rows, cols)
elif self.data_format == 'channels_last':
return (input_shape[0], depth, rows, cols, out_filters)
def get_config(self):
config = super(DepthwiseConv3D, self).get_config()
config.pop('filters')
config.pop('kernel_initializer')
config.pop('kernel_regularizer')
config.pop('kernel_constraint')
config['depth_multiplier'] = self.depth_multiplier
config['depthwise_initializer'] = initializers.serialize(self.depthwise_initializer)
config['depthwise_regularizer'] = regularizers.serialize(self.depthwise_regularizer)
config['depthwise_constraint'] = constraints.serialize(self.depthwise_constraint)
return config
class SeparableConv3D(DepthwiseConv3D):
"""Depthwise separable 3D convolution.
Depthwise Separable convolutions consist in performing
just the first and second step in a depthwise and pointwise convolution
The `depth_multiplier` argument controls how many
output channels are generated per input channel in the depthwise step.
# Arguments
filters: Number of filters
kernel_size: An integer or tuple/list of 3 integers, specifying the
depth, width and height of the 3D convolution window.
Can be a single integer to specify the same value for
all spatial dimensions.
strides: An integer or tuple/list of 3 integers,
specifying the strides of the convolution along the depth, width and height.
Can be a single integer to specify the same value for
all spatial dimensions.
padding: one of `"valid"` or `"same"` (case-insensitive).
depth_multiplier: The number of depthwise convolution output channels
for each input channel.
The total number of depthwise convolution output
channels will be equal to `filterss_in * depth_multiplier`.
data_format: A string,
one of `channels_last` (default) or `channels_first`.
The ordering of the dimensions in the inputs.
`channels_last` corresponds to inputs with shape
`(batch, height, width, channels)` while `channels_first`
corresponds to inputs with shape
`(batch, channels, height, width)`.
It defaults to the `image_data_format` value found in your
Keras config file at `~/.keras/keras.json`.
If you never set it, then it will be "channels_last".
activation: Activation function to use
(see [activations](../activations.md)).
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
depthwise_initializer: Initializer for the depthwise kernel matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
depthwise_regularizer: Regularizer function applied to
the depthwise kernel matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
dialation_rate: List of ints.
Defines the dilation factor for each dimension in the
input. Defaults to (1,1,1)
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
depthwise_constraint: Constraint function applied to
the depthwise kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
# Input shape
5D tensor with shape:
`(batch, depth, channels, rows, cols)` if data_format='channels_first'
or 5D tensor with shape:
`(batch, depth, rows, cols, channels)` if data_format='channels_last'.
# Output shape
5D tensor with shape:
`(batch, filters * depth, new_depth, new_rows, new_cols)` if data_format='channels_first'
or 4D tensor with shape:
`(batch, new_depth, new_rows, new_cols, filters * depth)` if data_format='channels_last'.
`rows` and `cols` values might have changed due to padding.
"""
def __init__(self,
filters,
kernel_size,
strides=(1, 1, 1),
padding='valid',
depth_multiplier=1,
group_size=None,
data_format=None,
activation=None,
use_bias=True,
depthwise_initializer='glorot_uniform',
bias_initializer='zeros',
dilation_rate = (1, 1, 1),
depthwise_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
depthwise_constraint=None,
bias_constraint=None,
pointwise_initializer='glorot_uniform',
pointwise_regularizer=None,
pointwise_constraint=None,
**kwargs):
super(SeparableConv3D, self).__init__(
kernel_size=kernel_size,
strides=strides,
padding=padding,
data_format=data_format,
activation=None,
use_bias=False,
bias_initializer=None,
bias_regularizer=None,
bias_constraint=None,
dilation_rate=dilation_rate,
activity_regularizer=activity_regularizer,
depth_multiplier=depth_multiplier,
group_size=group_size,
depthwise_constraint=depthwise_constraint,
depthwise_regularizer=depthwise_regularizer,
depthwise_initializer=depthwise_initializer,
**kwargs)
self.filters = filters
self.activation = activation
self.pointwise_initializer = initializers.get(pointwise_initializer)
self.pointwise_regularizer = regularizers.get(pointwise_regularizer)
self.pointwise_constraint = constraints.get(pointwise_constraint)
self.use_pointwise_bias = use_bias
self.pointwise_bias_initializer = initializers.get(bias_initializer)
self.pointwise_bias_regularizer = regularizers.get(bias_regularizer)
self.pointwise_bias_constraint = constraints.get(bias_constraint)
def build(self, input_shape):
if len(input_shape) < 5:
raise ValueError('Inputs to `SeparableConv3D` should have rank 5. '
'Received input shape:', str(input_shape))
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs to '
'`SeparableConv3D` '
'should be defined. Found `None`.')
self.input_dim = int(input_shape[channel_axis])
depthwise_kernel_shape = (self.kernel_size[0],
self.kernel_size[1],
self.kernel_size[2],
self.input_dim,
self.input_dim * self.depth_multiplier)
self.depthwise_kernel = self.add_weight(
shape=depthwise_kernel_shape,
initializer=self.depthwise_initializer,
name='depthwise_kernel',
regularizer=self.depthwise_regularizer,
constraint=self.depthwise_constraint)
pointwise_kernel_shape = (1,
1,
1,
self.input_dim * self.depth_multiplier,
self.filters)
self.pointwise_kernel = self.add_weight(
shape=pointwise_kernel_shape,
initializer=self.pointwise_initializer,
name='pointwise_kernel',
regularizer=self.pointwise_regularizer,
constraint=self.pointwise_constraint)
self.bias = None
if self.use_pointwise_bias:
self.pointwise_bias = self.add_weight(
shape=self.filters,
initializer=self.pointwise_bias_initializer,
name='bias',
regularizer=self.pointwise_bias_regularizer,
constraint=self.pointwise_bias_constraint)
else:
self.pointwise_bias = None
# Set input spec.
self.input_spec = InputSpec(ndim=5, axes={channel_axis: self.input_dim})
self.built = True
def call(self, inputs, training=None):
depthwise_output = super().call(inputs, training=training)
if self.data_format == 'channels_first':
dilation = tuple(list(self.dilation_rate) + [1,] + [1,])
elif self.data_format == 'channels_last':
dilation = tuple([1,] + list(self.dilation_rate) + [1,])
### Notice: Originally, 'depthwise_output' was 'input[0]'
outputs = tf.nn.conv3d(depthwise_output, self.pointwise_kernel, strides=self._strides,
padding= self._padding, dilations = dilation,
data_format=self._data_format)
if self.pointwise_bias is not None:
outputs = K.bias_add(
outputs,
self.pointwise_bias,
data_format=self.data_format)
if self.activation is not None:
return self.activation(outputs)
return outputs
def compute_output_shape(self, input_shape):
if self.data_format == 'channels_first':
depth = input_shape[2]
rows = input_shape[3]
cols = input_shape[4]
out_filters = self.filters
elif self.data_format == 'channels_last':
depth = input_shape[1]
rows = input_shape[2]
cols = input_shape[3]
out_filters = self.filters
depth = conv_utils.conv_output_length(depth, self.kernel_size[0],
self.padding,
self.strides[0])
rows = conv_utils.conv_output_length(rows, self.kernel_size[1],
self.padding,
self.strides[1])
cols = conv_utils.conv_output_length(cols, self.kernel_size[2],
self.padding,
self.strides[2])
if self.data_format == 'channels_first':
return (input_shape[0], out_filters, depth, rows, cols)
elif self.data_format == 'channels_last':
return (input_shape[0], depth, rows, cols, out_filters)
def get_config(self):
config = super().get_config()
config['filters'] = self.filters
config['pointwise_initializer'] = initializers.serialize(self.pointwise_initializer)
config['pointwise_regularizer'] = regularizers.serialize(self.pointwise_regularizer)
config['pointwise_constraint'] = constraints.serialize(self.pointwise_constraint)
return config
DepthwiseConvolution3D = DepthwiseConv3D
SeparableConvolution3D = SeparableConv3D