/
tf_layers.py
280 lines (219 loc) · 10.8 KB
/
tf_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
import tensorflow as tf
from tensorflow.contrib.keras.python.keras.layers import *
def Conv3DWithBN(x, filters, ksize, strides, name, padding='same', dilation_rate=1, center=True, scale=True, decay=0.99):
x = Conv3D(filters=filters, kernel_size=ksize, strides=strides, padding=padding, dilation_rate=dilation_rate,
use_bias=False, kernel_initializer='he_normal', name=name+'_conv')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn')(x)
x = Activation('relu', name=name+'_relu')(x)
return x
def Conv2DWithBN(x, filters, ksize, strides, name, padding='same', dilation_rate=1, center=True, scale=True, decay=0.99):
x = Conv2D(filters=filters, kernel_size=ksize, strides=strides, padding=padding, dilation_rate=dilation_rate,
use_bias=False, kernel_initializer='he_normal', name=name+'_conv')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn')(x)
x = Activation('relu', name=name+'_relu')(x)
return x
def Conv1DWithBN(x, filters, ksize, strides, name, padding='same', dilation_rate=1, center=True, scale=True, decay=0.99):
x = Conv1D(filters=filters, kernel_size=ksize, strides=strides, padding=padding, dilation_rate=dilation_rate,
use_bias=False, kernel_initializer='he_normal', name=name+'_conv')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn')(x)
x = Activation('relu', name=name+'_relu')(x)
return x
def DenseWithBN(x, units, name, kernel_regularizer=None, center=True, scale=True, decay=0.99):
x = Dense(units=units, use_bias=False, kernel_regularizer=kernel_regularizer, name=name+'_weight')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bias')(x)
x = Activation('relu', name=name+'_relu')(x)
return x
def ResNetUnit2D(x, filters, ksize, name, end=False, center=True, scale=True, decay=0.99):
identity = x
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_1')(x)
x = Activation('relu', name=name+'_relu_1')(x)
x = Conv2D(filters, kernel_size=ksize, strides=1, padding='same', kernel_initializer='he_normal', name=name+'_conv_1')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_2')(x)
x = Activation('relu', name=name+'_relu_2')(x)
x = Conv2D(filters, kernel_size=ksize, strides=1, padding='same', kernel_initializer='he_normal', name=name+'_conv2')(x)
x = add([x, identity])
if end:
x = BatchNormalization(center=center, scale=scale, momentum=decay)(x)
x = Activation('relu')(x)
return x
def ResNetUnitIncreasingDims2D(x, filters, ksize, strides, name, begin=False, center=True, scale=True, decay=0.99):
identity = x
if not begin:
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_1')(x)
x = Activation('relu', name=name+'_relu_1')(x)
x = Conv2D(filters, kernel_size=ksize, strides=strides[0], padding='same', kernel_initializer='he_normal', use_bias=False, name=name+'_conv_1')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_2')(x)
x = Activation('relu', name=name+'_relu_2')(x)
x = Conv2D(filters, kernel_size=ksize, strides=strides[1], padding='same', kernel_initializer='he_normal', use_bias=False, name=name+'_conv_2')(x)
identity = Conv2D(filters, kernel_size=1, strides=strides[0], padding='same', kernel_initializer='he_normal', name=name+'_conv_identity')(identity)
x = add([x, identity])
return x
def ResNetUnit1D(x, filters, ksize, name, end=False, center=True, scale=True, decay=0.99):
identity = x
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_1')(x)
x = Activation('relu', name=name+'_relu_1')(x)
x = Conv1D(filters, kernel_size=ksize, strides=1, padding='same', kernel_initializer='he_normal', use_bias=False, name=name+'_conv_1')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_2')(x)
x = Activation('relu', name=name+'_relu_2')(x)
x = Conv1D(filters, kernel_size=ksize, strides=1, padding='same', kernel_initializer='he_normal', use_bias=False, name=name+'_conv_2')(x)
x = add([x, identity])
if end:
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_3')(x)
x = Activation('relu', name=name+'_relu_3')(x)
return x
def ResNetUnitIncreasingDims1D(x, filters, ksize, strides, name, begin=False, center=True, scale=True, decay=0.99):
'''
ResNet unit without BottleNeck. 2 layers
:param x:
:param filters:
:param ksize:
:param strides: list with 2 elements, stride for each layer
:param begin:
:return:
'''
identity = x
if not begin:
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_1')(x)
x = Activation('relu', name=name+'_relu_1')(x)
x = Conv1D(filters, kernel_size=ksize, strides=strides[0], padding='same', kernel_initializer='he_normal', use_bias=False, name=name+'_conv_1')(x)
x = BatchNormalization(center=center, scale=scale, momentum=decay, name=name+'_bn_2')(x)
x = Activation('relu', name=name+'_relu_2')(x)
x = Conv1D(filters, kernel_size=ksize, strides=strides[1], padding='same', kernel_initializer='he_normal', use_bias=False, name=name+'_conv_2')(x)
identity = Conv1D(filters, kernel_size=1, strides=strides[0], padding='same', kernel_initializer='he_normal', name=name+'_conv_identity')(identity)
x = add([x, identity])
return x
def ContextualAvgPooling(x, ksizes, strides):
'''
Concatenate input with pooling result
:param x:
:param ksizes:
:param strides:
:return:
'''
out = None
for ks in ksizes:
x_pooled = AvgPool1D(pool_size=ks, strides=strides, padding='same')
if out is None:
out = x_pooled
else:
out = concatenate([out, x_pooled], axis=-1)
x = concatenate([x, out], axis=-1)
return x
def ContextualAtrousConv1D(x, filters, ksize, strides, dilation_rates, name):
"""
Retrieve contextual information with Atrous Convolution
:param x:
:param filters:
:param ksize:
:param strides:
:param dilation_rates:
:return:
"""
concat = x
for dr in dilation_rates:
x_atrous = Conv1DWithBN(x, filters=filters, ksize=ksize, strides=strides, dilation_rate=dr, name=name+'a_conv_s'+str(dr))
concat = concatenate([concat, x_atrous], axis=-1)
concat = Conv1DWithBN(concat, filters=256, ksize=ksize, strides=strides, name=name+'atrou_post_conv1')
return concat
def ContextualAtrousConv3D(x, filters, ksize, strides, dilation_rates, name):
"""
Retrieve contextual information with Atrous Convolution
:param x:
:param filters:
:param ksize:
:param strides:
:param dilation_rates:
:return:
"""
concat = None
for dr in dilation_rates:
x_atrous = Conv3DWithBN(x, filters=filters, ksize=ksize, strides=strides, dilation_rate=dr, name=name+'a_conv_s'+str(dr))
if concat is None:
concat = x_atrous
else:
concat = concatenate([concat, x_atrous], axis=-1)
concat = Conv3DWithBN(concat, filters=filters, ksize=ksize, strides=strides, name=name+'atrou_post_conv1')
return concat
def SharedAtrousConv1D(x, SharedConvs, PostConv):
concat = x
for SharedConv in SharedConvs:
x_atrous = SharedConv(x)
x_atrous = BatchNormalization()(x_atrous)
x_atrous = Activation('relu')(x_atrous)
concat = concatenate([concat, x_atrous], axis=-1)
concat = PostConv(concat)
concat = BatchNormalization()(concat)
concat = Activation('relu')(concat)
return concat
def densenet_block3d(x, k, rep):
dense_input = x
for i in range(rep):
x_dense = Conv3D(filters=k, kernel_size=3, strides=1, padding='same', activation='relu')(dense_input)
dense_input = concatenate([dense_input, x_dense])
return dense_input
def DenseNetTransit(x, rate=1, name=None):
if rate != 1:
out_features = x.get_shape().as_list()[-1] * rate
x = BatchNormalization(center=True, scale=True, name=name + '_bn')(x)
x = Activation('relu', name=name + '_relu')(x)
x = Conv3D(filters=out_features, kernel_size=1, strides=1, padding='same', kernel_initializer='he_normal',
use_bias=False, name=name + '_conv')(x)
x = AveragePooling3D(pool_size=2, strides=2, padding='same')(x)
return x
def DenseNetUnit3D(x, growth_rate, ksize, rep, bn_decay=0.99, name=None):
for i in range(rep):
concat = x
x = BatchNormalization(center=True, scale=True, momentum=bn_decay, name=name+'_bn_rep_'+str(i))(x)
x = Activation('relu')(x)
x = Conv3D(filters=growth_rate, kernel_size=ksize, padding='same',
kernel_initializer='glorot_normal', use_bias=False, name=name+'_conv_rep_'+str(i))(x)
x = concatenate([concat, x])
return x
class BilinearUpsampling3D(Layer):
"""
Wrapping 1D BilinearUpsamling as a Keras layer
Input: 3D Tensor (batch, dim, channels)
"""
def __init__(self, size, **kwargs):
self.size = size
super(BilinearUpsampling3D, self).__init__(**kwargs)
def build(self, input_shape):
super(BilinearUpsampling3D,self).build(input_shape)
def call(self, x, mask=None):
x = tf.expand_dims(x, axis=2)
x = tf.image.resize_bilinear(x, [self.size, 1])
x = tf.squeeze(x, axis=2)
return x
def get_output_shape_for(self, input_shape):
return (input_shape[0], self.size, input_shape[2])
# def SharedAtrousConv1D(x, SharedConvs, PostConv):
# concat = None
# for SharedConv in SharedConvs:
# x_atrous = SharedConv(x)
# x_atrous = BatchNormalization()(x_atrous)
# x_atrous = Activation('relu')(x_atrous)
# if concat is None:
# concat = x_atrous
# else:
# concat = concatenate([concat, x_atrous], axis=-1)
# concat = PostConv(concat)
# concat = BatchNormalization()(concat)
# concat = Activation('relu')(concat)
# return concat
class BilinearUpsampling1D(Layer):
"""
Wrapping 1D BilinearUpsamling as a Keras layer
Input: 3D Tensor (batch, dim, channels)
"""
def __init__(self, size, **kwargs):
self.size = size
super(BilinearUpsampling1D, self).__init__(**kwargs)
def build(self, input_shape):
super(BilinearUpsampling1D,self).build(input_shape)
def call(self, x, mask=None):
x = tf.expand_dims(x, axis=2)
x = tf.image.resize_bilinear(x, [self.size, 1])
x = tf.squeeze(x, axis=2)
return x
def get_output_shape_for(self, input_shape):
return (input_shape[0], self.size, input_shape[2])