-
Notifications
You must be signed in to change notification settings - Fork 79
/
model.py
656 lines (473 loc) · 27.1 KB
/
model.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import keras.backend as K
from keras.models import Model
from keras.layers import Input
from keras.layers import TimeDistributed
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Bidirectional
from keras.layers import Lambda
from keras.layers import Dropout
from keras.regularizers import l2
from keras.initializers import random_normal
from keras.utils.conv_utils import conv_output_length
from keras.layers import GaussianNoise
'''
This file builds the models
'''
import numpy as np
from keras import backend as K
from keras.models import Model, Sequential
from keras.layers.recurrent import SimpleRNN
from keras.layers import Dense, Activation, Bidirectional, Reshape,Flatten, Lambda, Input,\
Masking, Convolution1D, BatchNormalization, GRU, Conv1D, RepeatVector, Conv2D
from keras.optimizers import SGD, adam
from keras.layers import ZeroPadding1D, Convolution1D, ZeroPadding2D, Convolution2D, MaxPooling2D, GlobalMaxPooling2D
from keras.layers import TimeDistributed, Dropout
from keras.layers.merge import add # , # concatenate BAD FOR COREML
from keras.utils.conv_utils import conv_output_length
from keras.activations import relu
import tensorflow as tf
def selu(x):
# from Keras 2.0.6 - does not exist in 2.0.4
"""Scaled Exponential Linear Unit. (Klambauer et al., 2017)
# Arguments
x: A tensor or variable to compute the activation function for.
# References
- [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515)
"""
alpha = 1.6732632423543772848170429916717
scale = 1.0507009873554804934193349852946
return scale * K.elu(x, alpha)
def clipped_relu(x):
return relu(x, max_value=20)
# Define CTC loss
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
# hack for load_model
import tensorflow as tf
''' from TF: Input requirements
1. sequence_length(b) <= time for all b
2. max(labels.indices(labels.indices[:, 1] == b, 2)) <= sequence_length(b) for all b.
'''
# print("CTC lambda inputs / shape")
# print("y_pred:",y_pred.shape) # (?, 778, 30)
# print("labels:",labels.shape) # (?, 80)
# print("input_length:",input_length.shape) # (?, 1)
# print("label_length:",label_length.shape) # (?, 1)
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def ctc(y_true, y_pred):
return y_pred
######################################
######################################
def ds1_dropout(input_dim=26, fc_size=2048, rnn_size=512, dropout=[0.1, 0.1, 0.1], output_dim=29):
""" DeepSpeech 1 Implementation with Dropout
Architecture:
Input MFCC TIMEx26
3 Fully Connected using Clipped Relu activation function
3 Dropout layers between each FC
1 BiDirectional LSTM
1 Dropout applied to BLSTM
1 Dropout applied to FC dense
1 Fully connected Softmax
Details:
- Uses MFCC's rather paper's 80 linear spaced log filterbanks
- Uses LSTM's rather than SimpleRNN
- No translation of raw audio by 5ms
- No stride the RNN
Reference:
https://arxiv.org/abs/1412.5567
"""
from keras.utils.generic_utils import get_custom_objects
get_custom_objects().update({"clipped_relu": clipped_relu})
K.set_learning_phase(1)
# Creates a tensor there are usually 26 MFCC
input_data = Input(name='the_input', shape=(None, input_dim)) # >>(?, max_batch_seq, 26)
# First 3 FC layers
init = random_normal(stddev=0.046875)
x = TimeDistributed(Dense(fc_size, name='fc1', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(input_data) # >>(?, 778, 2048)
x = TimeDistributed(Dropout(dropout[0]))(x)
x = TimeDistributed(Dense(fc_size, name='fc2', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(x) # >>(?, 778, 2048)
x = TimeDistributed(Dropout(dropout[0]))(x)
x = TimeDistributed(Dense(fc_size, name='fc3', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(x) # >>(?, 778, 2048)
x = TimeDistributed(Dropout(dropout[0]))(x)
# Layer 4 BiDirectional RNN
x = Bidirectional(LSTM(rnn_size, return_sequences=True, activation=clipped_relu, dropout=dropout[1],
kernel_initializer='he_normal', name='birnn'), merge_mode='sum')(x)
# Layer 5+6 Time Dist Dense Layer & Softmax
# x = TimeDistributed(Dense(fc_size, activation=clipped_relu, kernel_initializer=init, bias_initializer=init))(x)
x = TimeDistributed(Dropout(dropout[2]))(x)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", kernel_initializer=init, bias_initializer=init, activation="softmax"), name="out")(x)
# Change shape
labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
labels,
input_length,
label_length])
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
return model
def ds1(input_dim=26, fc_size=1024, rnn_size=1024, output_dim=29):
""" DeepSpeech 1 Implementation without dropout
Architecture:
Input MFCC TIMEx26
3 Fully Connected using Clipped Relu activation function
1 BiDirectional LSTM
1 Fully connected Softmax
Details:
- Removed Dropout on this implementation
- Uses MFCC's rather paper's 80 linear spaced log filterbanks
- Uses LSTM's rather than SimpleRNN
- No translation of raw audio by 5ms
- No stride the RNN
References:
https://arxiv.org/abs/1412.5567
"""
# hack to get clipped_relu to work on bidir layer
from keras.utils.generic_utils import get_custom_objects
get_custom_objects().update({"clipped_relu": clipped_relu})
input_data = Input(name='the_input', shape=(None, input_dim)) # >>(?, 778, 26)
init = random_normal(stddev=0.046875)
# First 3 FC layers
x = TimeDistributed(Dense(fc_size, name='fc1', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(input_data) # >>(?, 778, 2048)
x = TimeDistributed(Dense(fc_size, name='fc2', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(x) # >>(?, 778, 2048)
x = TimeDistributed(Dense(fc_size, name='fc3', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(x) # >>(?, 778, 2048)
# # Layer 4 BiDirectional RNN - note coreml only supports LSTM BIDIR
x = Bidirectional(LSTM(rnn_size, return_sequences=True, activation=clipped_relu,
kernel_initializer='glorot_uniform', name='birnn'), merge_mode='sum')(x) #
# Layer 5+6 Time Dist Layer & Softmax
# x = TimeDistributed(Dense(fc_size, activation=clipped_relu))(x)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", kernel_initializer=init, bias_initializer=init, activation="softmax"), name="out")(x)
#y_pred = Dense(output_dim, name="y_pred", kernel_initializer=init, bias_initializer=init, activation="softmax")(x)
# Input of labels and other CTC requirements
labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
labels,
input_length,
label_length])
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=[loss_out])
return model
def ds2_gru_model(input_dim=161, fc_size=1024, rnn_size=512, output_dim=29, initialization='glorot_uniform',
conv_layers=1, gru_layers=1, use_conv=True):
""" DeepSpeech 2 implementation
Architecture:
Input Spectrogram TIMEx161
1 Batch Normalisation layer on input
1-3 Convolutional Layers
1 Batch Normalisation layer
1-7 BiDirectional GRU Layers
1 Batch Normalisation layer
1 Fully connected Dense
1 Softmax output
Details:
- Uses Spectrogram as input rather than MFCC
- Did not use BN on the first input
- Network does not dynamically adapt to maximum audio size in the first convolutional layer. Max conv
length padded at 2048 chars, otherwise use_conv=False
Reference:
https://arxiv.org/abs/1512.02595
"""
K.set_learning_phase(1)
input_data = Input(shape=(None, input_dim), name='the_input')
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=1e-3, center=True, scale=True)(input_data)
if use_conv:
conv = ZeroPadding1D(padding=(0, 2048))(x)
for l in range(conv_layers):
x = Conv1D(filters=fc_size, name='conv_{}'.format(l+1), kernel_size=11, padding='valid', activation='relu', strides=2)(conv)
else:
for l in range(conv_layers):
x = TimeDistributed(Dense(fc_size, name='fc_{}'.format(l + 1), activation='relu'))(x) # >>(?, time, fc_size)
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=1e-3, center=True, scale=True)(x)
for l in range(gru_layers):
x = Bidirectional(GRU(rnn_size, name='fc_{}'.format(l + 1), return_sequences=True, activation='relu', kernel_initializer=initialization),
merge_mode='sum')(x)
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=1e-3, center=True, scale=True)(x)
# Last Layer 5+6 Time Dist Dense Layer & Softmax
x = TimeDistributed(Dense(fc_size, activation=clipped_relu))(x)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax"))(x)
# labels = K.placeholder(name='the_labels', ndim=1, dtype='int32')
labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
labels,
input_length,
label_length])
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
return model
def ownModel(input_dim=26, fc_size=512, rnn_size=512, dropout=[0.1, 0.1, 0.1], output_dim=29):
""" Own model BN+SELU-FC+GRU+BN+DR
Architecture:
Batch Normalisation layer on the input data
1 Fully connected layer of fc_size with SELU
2 Fully connected layer of fc_size with Clipped Relu
3 Dropout layers applied between the FC layers
Batch Normalisation layer on the final FC output
1 BiDirectional GRU layer with Clipped Relu
1 Fully connected layer of fc_size with SELU
1 Dropout layer
1 Softmax out
"""
from keras.utils.generic_utils import get_custom_objects
get_custom_objects().update({"clipped_relu": clipped_relu})
get_custom_objects().update({"selu": selu})
K.set_learning_phase(1)
# Creates a tensor there are usually 26 MFCC
input_data = Input(name='the_input', shape=(None, input_dim)) # >>(?, max_batch_seq, 26)
x = BatchNormalization(axis=-1, momentum=0.99,epsilon=1e-3,center=True,scale=True)(input_data)
# First 3 FC layers
init = random_normal(stddev=0.046875)
x = TimeDistributed(Dense(fc_size, name='fc1', kernel_initializer=init, bias_initializer=init, activation=selu))(x) # >>(?, 778, 2048)
x = TimeDistributed(Dropout(dropout[0]))(x)
x = TimeDistributed(Dense(fc_size, name='fc2', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(x) # >>(?, 778, 2048)
x = TimeDistributed(Dropout(dropout[0]))(x)
x = TimeDistributed(Dense(fc_size, name='fc3', kernel_initializer=init, bias_initializer=init, activation=clipped_relu))(x) # >>(?, 778, 2048)
x = TimeDistributed(Dropout(dropout[0]))(x)
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=1e-3, center=True, scale=True)(x)
# Layer 4 BiDirectional RNN
x = Bidirectional(GRU(rnn_size, return_sequences=True, activation=clipped_relu, dropout=dropout[1],
kernel_initializer='he_normal', name='birnn'), merge_mode='sum')(x)
# Layer 5+6 Time Dist Dense Layer & Softmax
x = TimeDistributed(Dense(fc_size, activation=selu, kernel_initializer=init, bias_initializer=init))(x)
x = TimeDistributed(Dropout(dropout[2]))(x)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", kernel_initializer=init, bias_initializer=init, activation="softmax"), name="out")(x)
# Change shape
labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
labels,
input_length,
label_length])
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
return model
def graves(input_dim=26, rnn_size=512, output_dim=29, std=0.6):
""" Implementation of Graves 2006 model
Architecture:
Gaussian Noise on input
BiDirectional LSTM
Reference:
ftp://ftp.idsia.ch/pub/juergen/icml2006.pdf
"""
K.set_learning_phase(1)
input_data = Input(name='the_input', shape=(None, input_dim))
# x = BatchNormalization(axis=-1)(input_data)
x = GaussianNoise(std)(input_data)
x = Bidirectional(LSTM(rnn_size,
return_sequences=True,
implementation=0))(x)
y_pred = TimeDistributed(Dense(output_dim, activation='softmax'))(x)
# Input of labels and other CTC requirements
labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
labels,
input_length,
label_length])
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=[loss_out])
return model
def cnn_city(input_dim=161, fc_size=1024, rnn_size=512, output_dim=29, initialization='glorot_uniform',
conv_layers=4):
""" Pure CNN implementation
Architecture:
1 Convolutional Layers
1 Fully connected Dense
1 Softmax output
Details:s
- Network does not dynamically adapt to maximum audio size in the first convolutional layer. Max conv
length padded at 2048 chars, otherwise use_conv=False
Reference:
"""
#filters = outputsize
#kernal_size = heigth and width of conv window
#strides = stepsize on conv window
kernel_size = 11 #
conv_depth_1 = 64 #
conv_depth_2 = 256 #
input_data = Input(shape=(None, input_dim), name='the_input') #batch x time x spectro size
conv = ZeroPadding1D(padding=(0, 2048))(input_data) #pad on time dimension
x = Conv1D(filters=128, name='conv_1', kernel_size=kernel_size, padding='valid', activation='relu', strides=2)(conv)
# x = Conv1D(filters=1024, name='conv_2', kernel_size=kernel_size, padding='valid', activation='relu', strides=2)(x)
# Last Layer 5+6 Time Dist Dense Layer & Softmax
x = TimeDistributed(Dense(fc_size, activation='relu'))(x)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax"))(x)
# labels = K.placeholder(name='the_labels', ndim=1, dtype='int32')
labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
labels,
input_length,
label_length])
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
return model
def const(input_dim=26, fc_size=1024, rnn_size=1024, output_dim=29):
""" Implementation of constrained model for CoreML
Architecture:
N number of Fully connected layer of variable FC units
*optional* GRU RNN of rnn_size
Details:
The RNN has been removed in order to allow the network to run in coreml
"""
#loop FC
input_data = Input(name='the_input', shape=(None, input_dim)) # >>(?, time, input_dim)
x = input_data
init = random_normal(stddev=0.046875)
layercount = 3
for l in range(layercount):
x = TimeDistributed(Dense(fc_size, name='fc_{}'.format(l+1), kernel_initializer=init,
bias_initializer=init, activation='relu'))(x) # >>(?, time, fc_size)
# x = GRU(rnn_size, return_sequences=True, activation='relu', name='rnn1')(x) # >> (?, time, rnn_size)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax"))(x) # >> (?,time,output_dim)
# Input of labels and other CTC requirements
labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,
labels,
input_length,
label_length])
model = Model(inputs=[input_data, labels, input_length, label_length], outputs=[loss_out])
return model
###########################
# TRANSFER MODEL WEIGHTS
def build_const_no_ctc_and_xfer_weights(loaded_model, input_dim=26, fc_size=1024, rnn_size=512,
output_dim=29):
'''
CONST model but convert into CoreML
'''
K.set_learning_phase(0)
for ind, i in enumerate(loaded_model.layers):
print(ind, i)
input_data = Input(name='the_input', shape=(None, input_dim)) # >>(?, 778, 26)
x = input_data
layercount = 3
for l in range(layercount):
x = TimeDistributed(Dense(fc_size, name='fc_{}'.format(l+1), weights=loaded_model.layers[l+1].get_weights(),
activation='relu'))(x) # >>(?, time, fc_size)
# x = GRU(rnn_size, return_sequences=True, activation='relu', name='rnn1',
# weights=loaded_model.layers[4].get_weights())(x) # >> (?, time, rnn_size)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax",
weights=loaded_model.layers[5].get_weights()))(x) # >> (?,time,output_dim)
# First 3 FC layers
# x = TimeDistributed(Dense(fc_size, name='fc1', activation='relu',
# weights=loaded_model.layers[1].get_weights()))(input_data)
#
# x = TimeDistributed(Dense(fc_size, name='fc2', activation='relu',
# weights=loaded_model.layers[2].get_weights()))(x) # >>(?, 778, 2048)
#
# x = TimeDistributed(Dense(fc_size, name='fc3', activation='relu',
# weights=loaded_model.layers[3].get_weights()))(x) # >>(?, 778, 2048)
# conv = ZeroPadding1D(padding=(1, 1000))(input_data)
# conv = Conv1D(filters=2, kernel_size=10, padding='valid', activation='relu',
# weights=loaded_model.layers[2].get_weights(), strides=2)(conv)
# Layer 4 RNN
# rnn_1 = GRU(rnn_size, return_sequences=True, activation='relu', name='rnn1',
# weights=loaded_model.layers[3].get_weights())(conv)
# # rnn_2 = GRU(rnn_size, return_sequences=True, activation='relu', name='rnn2',
# # weights=loaded_model.layers[5].get_weights())(rnn_1)
#
# x = Dense(fc_size, activation='relu',
# weights=loaded_model.layers[5].get_weights())(rnn_1)
# x = Bidirectional(LSTM(rnn_size, return_sequences=True, activation='relu'),
# weights=loaded_model.layers[3].get_weights(),
# merge_mode='sum')(conv)
# conv = ZeroPadding1D(padding=(1, 500))(rnn_1)
# conv = Convolution1D(1, 2, padding='valid',
# weights=loaded_model.layers[6].get_weights())(conv)
# y_pred = GRU(output_dim, return_sequences=True, activation='softmax', kernel_initializer='glorot_uniform', name='y_pred',
# weights=loaded_model.layers[4].get_weights())(x)
# x = TimeDistributed(Dense(fc_size, activation='relu',
# weights=loaded_model.layers[5].get_weights()))(rnn_1)
# y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax",
# weights=loaded_model.layers[6].get_weights()), name="out")(x)
# y_pred = Dense(output_dim, name="y_pred", activation="softmax",
# weights=loaded_model.layers[6].get_weights())(x)
# Layer 5+6 Time Dist Layer & Softmax
# y_pred = Dense(output_dim, name="y_pred", activation="softmax", weights=loaded_model.layers[5].get_weights())(rnn_1)
# y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation='softmax',
# weights=loaded_model.layers[4].get_weights()))(x)
model = Model(inputs=input_data, outputs=y_pred)
return model
def build_ds0_no_ctc_and_xfer_weights(loaded_model, input_dim=26, fc_size=1024, rnn_size=512,
dropout=[0, 0, 0],
output_dim=29):
'''
DS1 model but convert into CoreML
'''
from keras.utils.generic_utils import get_custom_objects
get_custom_objects().update({"clipped_relu": clipped_relu})
K.set_learning_phase(0)
for ind, i in enumerate(loaded_model.layers):
print(ind, i)
input_data = Input(name='the_input', shape=(None, input_dim)) # >>(?, 778, 26)
# First 3 FC layers
x = TimeDistributed(Dense(fc_size, name='fc1', activation='relu',
weights=loaded_model.layers[1].get_weights()))(input_data)
# x = TimeDistributed(Dropout(dropout[0]))(x) #2
x = TimeDistributed(Dense(fc_size, name='fc2', activation='relu',
weights=loaded_model.layers[3].get_weights()))(x) # >>(?, 778, 2048)
# x = TimeDistributed(Dropout(dropout[0]))(x) #4
x = TimeDistributed(Dense(fc_size, name='fc3', activation='relu',
weights=loaded_model.layers[5].get_weights()))(x) # >>(?, 778, 2048)
# x = TimeDistributed(Dropout(dropout[0]))(x) #6
# x = Dense(fc_size, name='fc1', activation='relu',
# weights=loaded_model.layers[1].get_weights())(input_data) # >>(?, 778, 2048)
# x = Dense(fc_size, name='fc2', activation='relu',
# weights=loaded_model.layers[2].get_weights())(x) # >>(?, 778, 2048)
# x = Dense(fc_size, name='fc3', activation='relu',
# weights=loaded_model.layers[3].get_weights())(x) # >>(?, 778, 2048)
# Layer 4 BiDirectional RNN - note coreml only supports LSTM BIDIR
x = Bidirectional(LSTM(rnn_size, return_sequences=True, activation='relu',
kernel_initializer='he_normal'),
weights=loaded_model.layers[7].get_weights(),
merge_mode='sum')(x)
x = TimeDistributed(Dense(fc_size, activation='relu',
weights=loaded_model.layers[8].get_weights()))(x)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax", weights=loaded_model.layers[10].get_weights()), name="out")(x)
# Layer 5+6 Time Dist Layer & Softmax
#y_pred = Dense(num_classes, name="y_pred", activation="softmax", weights=loaded_model.layers[4].get_weights())(x)
model = Model(inputs=input_data, outputs=y_pred)
return model
def build_ds5_no_ctc_and_xfer_weights(loaded_model, input_dim=161, fc_size=1024, rnn_size=512, output_dim=29, initialization='glorot_uniform',
conv_layers=4):
""" Pure CNN implementation"""
K.set_learning_phase(0)
for ind, i in enumerate(loaded_model.layers):
print(ind, i)
kernel_size = 11 #
conv_depth_1 = 64 #
conv_depth_2 = 256 #
input_data = Input(shape=(None, input_dim), name='the_input') #batch x time x spectro size
conv = ZeroPadding1D(padding=(0, 2048))(input_data) #pad on time dimension
x = Conv1D(filters=128, name='conv_1', kernel_size=kernel_size, padding='valid', activation='relu', strides=2,
weights = loaded_model.layers[2].get_weights())(conv)
# x = Conv1D(filters=1024, name='conv_2', kernel_size=kernel_size, padding='valid', activation='relu', strides=2,
# weights=loaded_model.layers[3].get_weights())(x)
# Last Layer 5+6 Time Dist Dense Layer & Softmax
x = TimeDistributed(Dense(fc_size, activation='relu',
weights=loaded_model.layers[3].get_weights()))(x)
y_pred = TimeDistributed(Dense(output_dim, name="y_pred", activation="softmax"))(x)
model = Model(inputs=input_data, outputs=y_pred)
return model