-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_utils.py
355 lines (228 loc) · 10.6 KB
/
models_utils.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
# definition of tensorflow models
import tensorflow as tf
import types
from keras.models import Model
from keras import layers as L
from keras import backend as K
# from kapre.utils import Normalization2D
def cnn_trad_fpool3(input_shape):
model = tf.keras.Sequential(name='cnn_trad_fpool3')
model.add(L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1)))
model.add(L.Conv2D(64, (20, 8), strides=(1, 1), padding='same', activation='relu'))
model.add(L.MaxPool2D((1, 3)))
model.add(L.Conv2D(64, (10, 4), strides=(1, 1), padding='same', activation='relu'))
model.add(L.MaxPool2D((1, 1)))
model.add(L.Flatten())
model.add(L.Dense(32, activation='relu'))
model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(35, activation='softmax'))
return model
def cnn_one_fpool3(input_shape):
model = tf.keras.Sequential(name='cnn_one_fpool3')
model.add(L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1)))
model.add(L.Conv2D(54, (32, 8), strides=(1, 1), padding='same', activation='relu'))
model.add(L.MaxPool2D((1, 3)))
model.add(L.Flatten())
model.add(L.Dense(32, activation='relu'))
model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(35, activation='softmax'))
return model
def cnn_one_fstride4(input_shape):
model = tf.keras.Sequential(name='cnn_one_fstride4')
model.add(L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1)))
model.add(L.Conv2D(186, (32, 8), strides=(1, 4), padding='same', activation='relu'))
model.add(L.Flatten())
model.add(L.Dense(32, activation='relu'))
model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(35, activation='softmax'))
return model
def cnn_one_fstride8(input_shape):
model = tf.keras.Sequential(name='cnn_one_fstride8')
model.add(L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1)))
model.add(L.Conv2D(336, (32, 8), strides=(1, 8), padding='same', activation='relu'))
model.add(L.Flatten())
model.add(L.Dense(32, activation='relu'))
model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(35, activation='softmax'))
return model
def custom_cnn_simple(
input_shape,
bo_params_redefined = [48, 32, 8, 3, 2, 0.3, 128, 64, 5, 5, 3, 1, 0.3, 0.2, 0.01]
):
'''
4 cnn blocks, 2x pooling, 2x pooling in time
'''
nf_sp_1, nf_sp_2, nk_sp_l, nk_sp_r, mp_sp, dp_sp, nf_tp_1, nf_tp_2, nk_tp_l, nk_tp_r, mp_tp_1, mp_tp_2, dp_tp, dp_fc, lr = bo_params_redefined
model = tf.keras.models.Sequential(name='custom_cnn')
model.add(L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1)))
model.add(L.BatchNormalization())
filters_pool = [nf_sp_1, nf_sp_2]
for num_filters in filters_pool:
model.add(L.Conv2D(num_filters, kernel_size=(nk_sp_l, nk_sp_r), padding='same'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.MaxPooling2D(pool_size=(mp_sp, mp_sp)))
model.add(L.Dropout(dp_sp))
filters_pool_in_time = [nf_tp_1, nf_tp_2]
for p, num_filters in zip([mp_tp_1, mp_tp_2], filters_pool_in_time):
model.add(L.Conv2D(num_filters, kernel_size=(nk_tp_l, nk_tp_r), padding='same'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.MaxPooling2D(pool_size=(p, 1)))
model.add(L.Dropout(dp_tp))
model.add(L.Flatten())
model.add(L.Dropout(dp_fc))
model.add(L.Dense(256))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.Dense(35, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(learning_rate=lr),
metrics=["sparse_categorical_accuracy"])
return model
def custom_cnn(input_shape):
model = tf.keras.models.Sequential(name='custom_cnn')
model.add(L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1)))
model.add(L.BatchNormalization())
filters_pool = [48, 32]
for num_filters in filters_pool:
model.add(L.Conv2D(num_filters, kernel_size=(7, 3), padding='same'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.Conv2D(num_filters, kernel_size=(7, 3), padding='same'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.Conv2D(num_filters, kernel_size=(7, 3), padding='same'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.MaxPooling2D(pool_size=(2, 2)))
model.add(L.Dropout(0.3))
filters_pool_in_time = [128, 64]
for p, num_filters in zip([3, 3], filters_pool_in_time):
model.add(L.Conv2D(num_filters, kernel_size=(5, 7), padding='same'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.MaxPooling2D(pool_size=(p, 1)))
model.add(L.Dropout(0.3))
model.add(L.Flatten())
model.add(L.Dense(512, name='features512'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.Dropout(0.4))
model.add(L.Dense(256, name='features256'))
model.add(L.BatchNormalization())
model.add(L.Activation('relu'))
model.add(L.Dense(35, activation='softmax'))
return model
def residual_block(x_input, n_filt, ks=5, downsample=False):
x = L.Conv2D(filters=n_filt, kernel_size=(ks,ks), strides=(1 if not downsample else 2), padding='same', activation='relu')(x_input)
x = L.BatchNormalization()(x)
x = L.Conv2D(filters=n_filt, kernel_size=(ks,ks), strides=1, padding='same', activation='relu')(x)
x = L.BatchNormalization()(x)
if downsample:
x_input = L.Conv2D(kernel_size=1,
strides=2,
filters=n_filt,
padding="same")(x)
out = L.Add()([x_input, x])
out = L.ReLU()(out)
out = L.BatchNormalization()(out)
return out
def resnet(input_shape=(99, 40), output_shape=35, n_filters=45, num_blocks=3, ks=5, downsample=False, triplet_loss=False):
# reshape
input_layer = L.Input(shape=input_shape)
reshape_layer = L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1))(input_layer)
# expand input for residual block
x = L.BatchNormalization()(reshape_layer)
x = L.Conv2D(filters=n_filters, kernel_size=(ks,ks), strides=(1,1), padding='same')(x)
x = L.ReLU()(x)
x = L.BatchNormalization()(x)
x = L.MaxPooling2D(pool_size=(2,1))(x)
# residual blocks
for _ in range(num_blocks):
x = residual_block(x, n_filt=n_filters, ks=ks, downsample=downsample)
# one cnn block
x = L.Conv2D(filters=n_filters, kernel_size=(ks,ks), strides=1, padding='same', dilation_rate=8)(x)
x = L.ReLU()(x)
x = L.BatchNormalization()(x)
if not triplet_loss:
# final part of the network
x = L.GlobalAveragePooling2D()(x)
x = L.Flatten()(x)
x = L.Dropout(0.4)(x)
x = L.Dense(output_shape, activation='softmax')(x)
else:
# final part of the network
x = L.MaxPooling2D(pool_size=4)(x)
x = L.Dropout(0.3)(x)
x = L.Flatten()(x)
x = L.Dense(output_shape, activation=None)(x) # No activation on final dense layer
x = L.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))(x) # L2 normalize embeddings
model = Model(inputs=input_layer, outputs=x, name='Resnet')
return model
def RNNSpeechModel(input_shape=(99, 40), output_shape=35):
# simple LSTM
input_layer = L.Input(shape=input_shape)
x = L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1))(input_layer)
# x = Normalization2D(int_axis=0)(x)
# x = L.Permute((2, 1, 3))(x)
x = L.Conv2D(10, (5, 1), activation='relu', padding='same')(x)
x = L.BatchNormalization()(x)
x = L.Conv2D(1, (5, 1), activation='relu', padding='same')(x)
x = L.BatchNormalization()(x)
x = L.Dropout(0.3)(x)
# x = Reshape((125, 80)) (x)
# keras.backend.squeeze(x, axis)
x = L.Lambda(lambda q: K.squeeze(q, -1), name='squeeze_last_dim')(x)
x = L.Bidirectional(L.CuDNNLSTM(64, return_sequences=True))(x) # [b_s, seq_len, vec_dim]
x = L.Bidirectional(L.CuDNNLSTM(64))(x)
x = L.Dense(64, activation='relu')(x)
x = L.Dropout(0.3)(x)
output = L.Dense(output_shape, activation='softmax')(x)
model = Model(inputs=input_layer, outputs=output, name='RNNSpeechModel')
return model
def AttRNNSpeechModel(input_shape=(99, 40), output_shape=35, rnn_func=L.LSTM):
# simple LSTM
input_layer = L.Input(shape=input_shape)
x = L.Reshape(input_shape=input_shape, target_shape=(input_shape[0], input_shape[1], 1))(input_layer)
# x = Normalization2D(int_axis=0, name='mel_stft_norm')(x)
# x = L.Permute((2, 1, 3))(x)
x = L.Conv2D(10, (5, 1), activation='relu', padding='same')(x)
x = L.BatchNormalization()(x)
x = L.Conv2D(1, (5, 1), activation='relu', padding='same')(x)
x = L.BatchNormalization()(x)
x = L.Dropout(0.3)(x)
# x = Reshape((125, 80)) (x)
# keras.backend.squeeze(x, axis)
x = L.Lambda(lambda q: K.squeeze(q, -1), name='squeeze_last_dim')(x)
x = L.Bidirectional(rnn_func(64, return_sequences=True))(x) # [b_s, seq_len, vec_dim]
x = L.Bidirectional(rnn_func(64, return_sequences=True))(x) # [b_s, seq_len, vec_dim]
xFirst = L.Lambda(lambda q: q[:, -1])(x) # [b_s, vec_dim]
query = L.Dense(128)(xFirst)
# dot product attention
attScores = L.Dot(axes=[1, 2])([query, x])
attScores = L.Softmax(name='attSoftmax')(attScores) # [b_s, seq_len]
# rescale sequence
attVector = L.Dot(axes=[1, 1])([attScores, x]) # [b_s, vec_dim]
x = L.Dense(64, activation='relu')(attVector)
x = L.Dropout(0.3)(x)
output = L.Dense(output_shape, activation='softmax', name='output')(x)
model = Model(inputs=input_layer, outputs=output, name='AttRNNSpeechModel')
return model
################################################
# NOTE: define all the models above this line! #
################################################
models = [f for f in globals().values() if type(f) == types.FunctionType]
models_names = [str(f).split()[1] for f in models]
def available_models():
print('Available models:')
for name in models_names:
print(name)
def select_model(model_name, input_shape):
model_index = models_names.index(model_name)
model = models[model_index](input_shape)
print('Selected model:', model_name)
return model