-
Notifications
You must be signed in to change notification settings - Fork 40
/
train_model.py
843 lines (765 loc) · 35.1 KB
/
train_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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# This is the python script for training the final model architecture
# To train the model, run: python train_model.py
# Common, File Based, and Math imports
import pandas as pd
import numpy as np
import collections
import os
from os.path import isdir, join
from pathlib import Path
from subprocess import check_output
import sys
import math
import pickle
from glob import glob
import random
from random import sample
import json
from mpl_toolkits.axes_grid1 import make_axes_locatable
from numpy.lib.stride_tricks import as_strided
from tqdm import tqdm
# Audio processing imports
from scipy import signal
from scipy.fftpack import dct
import soundfile
import json
from python_speech_features import mfcc
import scipy.io.wavfile as wav
from scipy.fftpack import fft
# Neural Network imports
import keras
from keras.utils.generic_utils import get_custom_objects
from keras import backend as K
from keras import regularizers, callbacks
from keras.constraints import max_norm
from keras.models import Model, Sequential, load_model
from keras.layers import Input, Lambda, Dense, Dropout, Flatten, Embedding, merge, Activation, GRUCell, LSTMCell,SimpleRNNCell
from keras.layers import Convolution2D, MaxPooling2D, Convolution1D, Conv1D, SimpleRNN, GRU, LSTM, CuDNNLSTM, CuDNNGRU, Conv2D
from keras.layers.advanced_activations import LeakyReLU, PReLU, ThresholdedReLU, ELU
from keras.layers import LeakyReLU, PReLU, ThresholdedReLU, ELU
from keras.layers import BatchNormalization, TimeDistributed, Bidirectional
from keras.layers import activations, Wrapper
from keras.regularizers import l2
from keras.optimizers import Adam, SGD, RMSprop, Adagrad, Adadelta, Adamax, Nadam
from keras.callbacks import ModelCheckpoint
from keras.utils import np_utils
from keras import constraints, initializers, regularizers
from keras.engine.topology import Layer
import keras.losses
from keras.backend.tensorflow_backend import set_session
from keras.engine import InputSpec
import tensorflow as tf
from tensorflow.python.framework import graph_io
from tensorflow.python.tools import freeze_graph
from tensorflow.core.protobuf import saver_pb2
from tensorflow.python.training import saver as saver_lib
# Model metric imports
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Visualization imports
import IPython.display as ipd
from IPython.display import Markdown, display, Audio
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.offline as py
import plotly.graph_objs as go
import plotly.tools as tls
color = sns.color_palette()
sns.set_style('darkgrid')
# Setting Random Seeds
np.random.seed(95)
RNG_SEED = 95
# Suppressing some of Tensorflow's warnings
tf.logging.set_verbosity(tf.logging.ERROR)
# Function for shuffling data
def shuffle_dataset(audio_paths, durations, texts):
p = np.random.permutation(len(audio_paths))
audio_paths = [audio_paths[i] for i in p]
durations = [durations[i] for i in p]
texts = [texts[i] for i in p]
return audio_paths, durations, texts
# Function for sorting data by duration
def sort_dataset(audio_paths, durations, texts):
p = np.argsort(durations).tolist()
audio_paths = [audio_paths[i] for i in p]
durations = [durations[i] for i in p]
texts = [texts[i] for i in p]
return audio_paths, durations, texts
# Mapping each character that could be spoken at each time step
char_map_str = """
' 0
<SPACE> 1
a 2
b 3
c 4
d 5
e 6
f 7
g 8
h 9
i 10
j 11
k 12
l 13
m 14
n 15
o 16
p 17
q 18
r 19
s 20
t 21
u 22
v 23
w 24
x 25
y 26
z 27
"""
# This leaves "blank" character mapped to number 28
char_map = {}
index_map = {}
for line in char_map_str.strip().split('\n'):
ch, index = line.split()
char_map[ch] = int(index)
index_map[int(index)+1] = ch
index_map[2] = ' '
# Function for converting text to an integer sequence
def text_to_int_seq(text):
int_sequence = []
for c in text:
if c == ' ':
ch = char_map['<SPACE>']
else:
ch = char_map[c]
int_sequence.append(ch)
return int_sequence
# Function for converting an integer sequence to text
def int_seq_to_text(int_sequence):
text = []
for c in int_sequence:
ch = index_map[c]
text.append(ch)
return text
# Function for calculating feature dimensions.
def calc_feat_dim(window, max_freq):
return int(0.001 * window * max_freq) + 1
class AudioGenerator():
def __init__(self, step=10, window=20, max_freq=8000, mfcc_dim=13,
minibatch_size=20, desc_file=None, spectrogram=True, max_duration=10.0,
sort_by_duration=False):
# Initializing variables
self.feat_dim = calc_feat_dim(window, max_freq)
self.mfcc_dim = mfcc_dim
self.feats_mean = np.zeros((self.feat_dim,))
self.feats_std = np.ones((self.feat_dim,))
self.rng = random.Random(RNG_SEED)
if desc_file is not None:
self.load_metadata_from_desc_file(desc_file)
self.step = step
self.window = window
self.max_freq = max_freq
self.cur_train_index = 0
self.cur_valid_index = 0
self.cur_test_index = 0
self.max_duration=max_duration
self.minibatch_size = minibatch_size
self.spectrogram = spectrogram
self.sort_by_duration = sort_by_duration
def get_batch(self, partition):
# Obtain a batch of audio files
if partition == 'train':
audio_paths = self.train_audio_paths
cur_index = self.cur_train_index
texts = self.train_texts
elif partition == 'valid':
audio_paths = self.valid_audio_paths
cur_index = self.cur_valid_index
texts = self.valid_texts
elif partition == 'test':
audio_paths = self.test_audio_paths
cur_index = self.test_valid_index
texts = self.test_texts
else:
raise Exception("Invalid partition. "
"Must be train/val")
features = [self.normalize(self.featurize(a)) for a in
audio_paths[cur_index:cur_index+self.minibatch_size]]
# Calculate size
max_length = max([features[i].shape[0]
for i in range(0, self.minibatch_size)])
max_string_length = max([len(texts[cur_index+i])
for i in range(0, self.minibatch_size)])
# Initialize arrays
X_data = np.zeros([self.minibatch_size, max_length,
self.feat_dim*self.spectrogram + self.mfcc_dim*(not self.spectrogram)])
labels = np.ones([self.minibatch_size, max_string_length]) * 28
input_length = np.zeros([self.minibatch_size, 1])
label_length = np.zeros([self.minibatch_size, 1])
for i in range(0, self.minibatch_size):
# Calculate input_length
feat = features[i]
input_length[i] = feat.shape[0]
X_data[i, :feat.shape[0], :] = feat
# Calculate label_length
label = np.array(text_to_int_seq(texts[cur_index+i]))
labels[i, :len(label)] = label
label_length[i] = len(label)
# Output arrays
outputs = {'ctc': np.zeros([self.minibatch_size])}
inputs = {'the_input': X_data,
'the_labels': labels,
'input_length': input_length,
'label_length': label_length
}
return (inputs, outputs)
def shuffle_dataset_by_partition(self, partition):
# Shuffle the data
if partition == 'train':
self.train_audio_paths, self.train_durations, self.train_texts = shuffle_dataset(
self.train_audio_paths, self.train_durations, self.train_texts)
elif partition == 'valid':
self.valid_audio_paths, self.valid_durations, self.valid_texts = shuffle_dataset(
self.valid_audio_paths, self.valid_durations, self.valid_texts)
else:
raise Exception("Invalid partition. "
"Must be train/val")
def sort_dataset_by_duration(self, partition):
# Sort the data
if partition == 'train':
self.train_audio_paths, self.train_durations, self.train_texts = sort_dataset(
self.train_audio_paths, self.train_durations, self.train_texts)
elif partition == 'valid':
self.valid_audio_paths, self.valid_durations, self.valid_texts = sort_dataset(
self.valid_audio_paths, self.valid_durations, self.valid_texts)
else:
raise Exception("Invalid partition. "
"Must be train/val")
def next_train(self):
# Get a batch of training data
while True:
ret = self.get_batch('train')
self.cur_train_index += self.minibatch_size
if self.cur_train_index >= len(self.train_texts) - self.minibatch_size:
self.cur_train_index = 0
self.shuffle_dataset_by_partition('train')
yield ret
def next_valid(self):
# Get a batch of validation data
while True:
ret = self.get_batch('valid')
self.cur_valid_index += self.minibatch_size
if self.cur_valid_index >= len(self.valid_texts) - self.minibatch_size:
self.cur_valid_index = 0
self.shuffle_dataset_by_partition('valid')
yield ret
def next_test(self):
# Get a batch of testing data
while True:
ret = self.get_batch('test')
self.cur_test_index += self.minibatch_size
if self.cur_test_index >= len(self.test_texts) - self.minibatch_size:
self.cur_test_index = 0
yield ret
# Load datasets
def load_train_data(self, desc_file='train_corpus.json'):
self.load_metadata_from_desc_file(desc_file, 'train')
self.fit_train()
if self.sort_by_duration:
self.sort_dataset_by_duration('train')
def load_validation_data(self, desc_file='valid_corpus.json'):
self.load_metadata_from_desc_file(desc_file, 'validation')
if self.sort_by_duration:
self.sort_dataset_by_duration('valid')
def load_test_data(self, desc_file='test_corpus.json'):
self.load_metadata_from_desc_file(desc_file, 'test')
def load_metadata_from_desc_file(self, desc_file, partition):
# Get metadata from json corpus
audio_paths, durations, texts = [], [], []
with open(desc_file) as json_line_file:
for line_num, json_line in enumerate(json_line_file):
try:
spec = json.loads(json_line)
if float(spec['duration']) > self.max_duration:
continue
audio_paths.append(spec['key'])
durations.append(float(spec['duration']))
texts.append(spec['text'])
except Exception as e:
print('Error reading line #{}: {}'
.format(line_num, json_line))
if partition == 'train':
self.train_audio_paths = audio_paths
self.train_durations = durations
self.train_texts = texts
elif partition == 'validation':
self.valid_audio_paths = audio_paths
self.valid_durations = durations
self.valid_texts = texts
elif partition == 'test':
self.test_audio_paths = audio_paths
self.test_durations = durations
self.test_texts = texts
else:
raise Exception("Invalid partition. "
"Must be train/val/test")
def fit_train(self, k_samples=100):
# Estimate descriptive stats for training set based on sample of 100
k_samples = min(k_samples, len(self.train_audio_paths))
samples = self.rng.sample(self.train_audio_paths, k_samples)
feats = [self.featurize(s) for s in samples]
feats = np.vstack(feats)
self.feats_mean = np.mean(feats, axis=0)
self.feats_std = np.std(feats, axis=0)
def featurize(self, audio_clip):
# Create features from data, either spectrogram or mfcc
if self.spectrogram:
return spectrogram_from_file(
audio_clip, step=self.step, window=self.window,
max_freq=self.max_freq)
else:
(rate, sig) = wav.read(audio_clip)
return mfcc(sig, rate, numcep=self.mfcc_dim)
def normalize(self, feature, eps=1e-14):
# Scale the data
return (feature - self.feats_mean) / (self.feats_std + eps)
def spectrogram(samples, fft_length=256, sample_rate=2, hop_length=128):
# Create a spectrogram from audio signals
assert not np.iscomplexobj(samples), "You shall not pass in complex numbers"
window = np.hanning(fft_length)[:, None]
window_norm = np.sum(window**2)
scale = window_norm * sample_rate
trunc = (len(samples) - fft_length) % hop_length
x = samples[:len(samples) - trunc]
# Reshape to include the overlap
nshape = (fft_length, (len(x) - fft_length) // hop_length + 1)
nstrides = (x.strides[0], x.strides[0] * hop_length)
x = as_strided(x, shape=nshape, strides=nstrides)
# Window stride sanity check
assert np.all(x[:, 1] == samples[hop_length:(hop_length + fft_length)])
# Broadcast window, and then compute fft over columns and square mod
x = np.fft.rfft(x * window, axis=0)
x = np.absolute(x)**2
# Scale 2.0 for everything except dc and fft_length/2
x[1:-1, :] *= (2.0 / scale)
x[(0, -1), :] /= scale
freqs = float(sample_rate) / fft_length * np.arange(x.shape[0])
return x, freqs
def spectrogram_from_file(filename, step=10, window=20, max_freq=None, eps=1e-14):
# Calculate log(linear spectrogram) from FFT energy
with soundfile.SoundFile(filename) as sound_file:
audio = sound_file.read(dtype='float32')
sample_rate = sound_file.samplerate
if audio.ndim >= 2:
audio = np.mean(audio, 1)
if max_freq is None:
max_freq = sample_rate / 2
if max_freq > sample_rate / 2:
raise ValueError("max_freq can not be > than 0.5 of "
" sample rate")
if step > window:
raise ValueError("step size can not be > than window size")
hop_length = int(0.001 * step * sample_rate)
fft_length = int(0.001 * window * sample_rate)
pxx, freqs = spectrogram(
audio, fft_length=fft_length, sample_rate=sample_rate,
hop_length=hop_length)
ind = np.where(freqs <= max_freq)[0][-1] + 1
return np.transpose(np.log(pxx[:ind, :] + eps))
def vis_train_features(index):
# Function for visualizing a single audio file based on index chosen
# Get spectrogram
audio_gen = AudioGenerator(spectrogram=True)
audio_gen.load_train_data()
vis_audio_path = audio_gen.train_audio_paths[index]
vis_spectrogram_feature = audio_gen.normalize(audio_gen.featurize(vis_audio_path))
# Get mfcc
audio_gen = AudioGenerator(spectrogram=False)
audio_gen.load_train_data()
vis_mfcc_feature = audio_gen.normalize(audio_gen.featurize(vis_audio_path))
# Obtain text label
vis_text = audio_gen.train_texts[index]
# Obtain raw audio
sample_rate, samples = wav.read(vis_audio_path)
# Print total number of training examples
print('There are %d total training examples.' % len(audio_gen.train_audio_paths))
# Return labels for plotting
return vis_text, vis_mfcc_feature, vis_spectrogram_feature, vis_audio_path, sample_rate, samples
def vis_audio_features(index, partition):
# Function for visualizing a single audio file based on index chosen
if partition == 'validation':
audio_gen = AudioGenerator(spectrogram=True)
audio_gen.load_validation_data()
vis_audio_path = audio_gen.valid_audio_paths[index]
vis_spectrogram_feature = audio_gen.normalize(audio_gen.featurize(vis_audio_path))
vis_text = audio_gen.valid_texts[index]
sample_rate, samples = wav.read(vis_audio_path)
return vis_text, vis_spectrogram_feature, vis_audio_path, sample_rate, samples
elif partition == 'test':
audio_gen = AudioGenerator(spectrogram=True)
audio_gen.load_test_data()
vis_audio_path = audio_gen.test_audio_paths[index]
vis_spectrogram_feature = audio_gen.normalize(audio_gen.featurize(vis_audio_path))
vis_text = audio_gen.test_texts[index]
sample_rate, samples = wav.read(vis_audio_path)
return vis_text, vis_spectrogram_feature, vis_audio_path, sample_rate, samples
elif partition == 'train':
audio_gen = AudioGenerator(spectrogram=True)
audio_gen.load_train_data()
vis_audio_path = audio_gen.train_audio_paths[index]
vis_spectrogram_feature = audio_gen.normalize(audio_gen.featurize(vis_audio_path))
vis_text = audio_gen.train_texts[index]
sample_rate, samples = wav.read(vis_audio_path)
return vis_text, vis_spectrogram_feature, vis_audio_path, sample_rate, samples
else:
raise Exception('Invalid partition! Must be "train", "test", or "validation"')
# Custom CTC loss function (discussed below)
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def add_ctc_loss(input_to_softmax):
the_labels = Input(name='the_labels', shape=(None,), dtype='float32')
input_lengths = Input(name='input_length', shape=(1,), dtype='int64')
label_lengths = Input(name='label_length', shape=(1,), dtype='int64')
output_lengths = Lambda(input_to_softmax.output_length)(input_lengths)
# CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')(
[input_to_softmax.output, the_labels, output_lengths, label_lengths])
model = Model(
inputs=[input_to_softmax.input, the_labels, input_lengths, label_lengths],
outputs=loss_out)
return model
# Function for modifying CNN layers for sequence problems
def cnn_output_length(input_length, filter_size, border_mode, stride,
dilation=1):
# Compute the length of cnn output seq after 1D convolution across time
if input_length is None:
return None
assert border_mode in {'same', 'valid', 'causal'}
dilated_filter_size = filter_size + (filter_size - 1) * (dilation - 1)
if border_mode == 'same':
output_length = input_length
elif border_mode == 'valid':
output_length = input_length - dilated_filter_size + 1
elif border_mode == 'causal':
output_length = input_length
return (output_length + stride - 1) // stride
def train_model(input_to_softmax,
pickle_path,
save_model_path,
train_json='train_corpus.json',
valid_json='valid_corpus.json',
minibatch_size=16, # You will want to change this depending on the GPU you are training on
spectrogram=True,
mfcc_dim=13,
optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False, clipnorm=1, clipvalue=.5),
epochs=30, # You will want to change this depending on the model you are training and data you are using
verbose=1,
sort_by_duration=False,
max_duration=10.0):
# Obtain batches of data
audio_gen = AudioGenerator(minibatch_size=minibatch_size,
spectrogram=spectrogram, mfcc_dim=mfcc_dim, max_duration=max_duration,
sort_by_duration=sort_by_duration)
# Load the datasets
audio_gen.load_train_data(train_json)
audio_gen.load_validation_data(valid_json)
# Calculate steps per epoch
num_train_examples=len(audio_gen.train_audio_paths)
steps_per_epoch = num_train_examples//minibatch_size
# Calculate validation steps
num_valid_samples = len(audio_gen.valid_audio_paths)
validation_steps = num_valid_samples//minibatch_size
# Add custom CTC loss function to the nn
model = add_ctc_loss(input_to_softmax)
# Dummy lambda function for loss since CTC loss is implemented above
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=optimizer)
# Make initial results/ directory for saving model pickles
if not os.path.exists('results'):
os.makedirs('results')
# Add callbacks
checkpointer = ModelCheckpoint(filepath='results/'+save_model_path, verbose=0)
terminator = callbacks.TerminateOnNaN()
time_machiner = callbacks.History()
logger = callbacks.CSVLogger('training.log')
tensor_boarder = callbacks.TensorBoard(log_dir='./logs', batch_size=16,
write_graph=True, write_grads=True, write_images=True,)
# Fit/train model
hist = model.fit_generator(generator=audio_gen.next_train(), steps_per_epoch=steps_per_epoch,
epochs=epochs, validation_data=audio_gen.next_valid(), validation_steps=validation_steps,
callbacks=[checkpointer, terminator, logger, time_machiner, tensor_boarder], verbose=verbose)
# Save model loss
with open('results/'+pickle_path, 'wb') as f:
pickle.dump(hist.history, f)
# Creating a TensorFlow session
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 1.0
set_session(tf.Session(config=config))
# Class for attention mechanism
class Attention(keras.layers.Layer):
ATTENTION_TYPE_ADD = 'additive'
ATTENTION_TYPE_MUL = 'multiplicative'
def __init__(self,
units=512,
attention_width=None,
attention_type=ATTENTION_TYPE_MUL,
return_attention=False,
history_only=False,
kernel_initializer='glorot_normal',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
use_additive_bias=True,
use_attention_bias=True,
attention_activation=None,
attention_regularizer_weight=0.0,
**kwargs):
"""Layer initialization.
For additive attention, see: https://arxiv.org/pdf/1806.01264.pdf
:param units: The dimension of the vectors that used to calculate the attention weights.
:param attention_width: The width of local attention.
:param attention_type: 'additive' or 'multiplicative'.
:param return_attention: Whether to return the attention weights for visualization.
:param history_only: Only use historical pieces of data.
:param kernel_initializer: The initializer for weight matrices.
:param bias_initializer: The initializer for biases.
:param kernel_regularizer: The regularization for weight matrices.
:param bias_regularizer: The regularization for biases.
:param kernel_constraint: The constraint for weight matrices.
:param bias_constraint: The constraint for biases.
:param use_additive_bias: Whether to use bias while calculating the relevance of inputs features
in additive mode.
:param use_attention_bias: Whether to use bias while calculating the weights of attention.
:param attention_activation: The activation used for calculating the weights of attention.
:param attention_regularizer_weight: The weights of attention regularizer.
:param kwargs: Parameters for parent class.
"""
super(Attention, self).__init__(**kwargs)
self.supports_masking = True
self.units = units
self.attention_width = attention_width
self.attention_type = attention_type
self.return_attention = return_attention
self.history_only = history_only
if history_only and attention_width is None:
self.attention_width = int(1e9)
self.use_additive_bias = use_additive_bias
self.use_attention_bias = use_attention_bias
self.kernel_initializer = keras.initializers.get(kernel_initializer)
self.bias_initializer = keras.initializers.get(bias_initializer)
self.kernel_regularizer = keras.regularizers.get(kernel_regularizer)
self.bias_regularizer = keras.regularizers.get(bias_regularizer)
self.kernel_constraint = keras.constraints.get(kernel_constraint)
self.bias_constraint = keras.constraints.get(bias_constraint)
self.attention_activation = keras.activations.get(attention_activation)
self.attention_regularizer_weight = attention_regularizer_weight
self._backend = keras.backend.backend()
if attention_type == Attention.ATTENTION_TYPE_ADD:
self.Wx, self.Wt, self.bh = None, None, None
self.Wa, self.ba = None, None
elif attention_type == Attention.ATTENTION_TYPE_MUL:
self.Wa, self.ba = None, None
else:
raise NotImplementedError('No implementation for attention type : ' + attention_type)
def get_config(self):
config = {
'units': self.units,
'attention_width': self.attention_width,
'attention_type': self.attention_type,
'return_attention': self.return_attention,
'history_only': self.history_only,
'use_additive_bias': self.use_additive_bias,
'use_attention_bias': self.use_attention_bias,
'kernel_initializer': keras.regularizers.serialize(self.kernel_initializer),
'bias_initializer': keras.regularizers.serialize(self.bias_initializer),
'kernel_regularizer': keras.regularizers.serialize(self.kernel_regularizer),
'bias_regularizer': keras.regularizers.serialize(self.bias_regularizer),
'kernel_constraint': keras.constraints.serialize(self.kernel_constraint),
'bias_constraint': keras.constraints.serialize(self.bias_constraint),
'attention_activation': keras.activations.serialize(self.attention_activation),
'attention_regularizer_weight': self.attention_regularizer_weight,
}
base_config = super(Attention, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def build(self, input_shape):
if self.attention_type == Attention.ATTENTION_TYPE_ADD:
self._build_additive_attention(input_shape)
elif self.attention_type == Attention.ATTENTION_TYPE_MUL:
self._build_multiplicative_attention(input_shape)
super(Attention, self).build(input_shape)
def _build_additive_attention(self, input_shape):
feature_dim = int(input_shape[2])
self.Wt = self.add_weight(shape=(feature_dim, self.units),
name='{}_Add_Wt'.format(self.name),
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
self.Wx = self.add_weight(shape=(feature_dim, self.units),
name='{}_Add_Wx'.format(self.name),
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_additive_bias:
self.bh = self.add_weight(shape=(self.units,),
name='{}_Add_bh'.format(self.name),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
self.Wa = self.add_weight(shape=(self.units, 1),
name='{}_Add_Wa'.format(self.name),
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_attention_bias:
self.ba = self.add_weight(shape=(1,),
name='{}_Add_ba'.format(self.name),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
def _build_multiplicative_attention(self, input_shape):
feature_dim = int(input_shape[2])
self.Wa = self.add_weight(shape=(feature_dim, feature_dim),
name='{}_Mul_Wa'.format(self.name),
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_attention_bias:
self.ba = self.add_weight(shape=(1,),
name='{}_Mul_ba'.format(self.name),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
def call(self, inputs, mask=None, **kwargs):
input_len = K.shape(inputs)[1]
if self.attention_type == Attention.ATTENTION_TYPE_ADD:
e = self._call_additive_emission(inputs)
elif self.attention_type == Attention.ATTENTION_TYPE_MUL:
e = self._call_multiplicative_emission(inputs)
if self.attention_activation is not None:
e = self.attention_activation(e)
e = K.exp(e - K.max(e, axis=-1, keepdims=True))
if self.attention_width is not None:
if self.history_only:
lower = K.arange(0, input_len) - (self.attention_width - 1)
else:
lower = K.arange(0, input_len) - self.attention_width // 2
lower = K.expand_dims(lower, axis=-1)
upper = lower + self.attention_width
indices = K.expand_dims(K.arange(0, input_len), axis=0)
e = e * K.cast(lower <= indices, K.floatx()) * K.cast(indices < upper, K.floatx())
if mask is not None:
mask = K.cast(mask, K.floatx())
mask = K.expand_dims(mask)
e = K.permute_dimensions(K.permute_dimensions(e * mask, (0, 2, 1)) * mask, (0, 2, 1))
# a_{t} = \text{softmax}(e_t)
s = K.sum(e, axis=-1, keepdims=True)
a = e / (s + K.epsilon())
# l_t = \sum_{t'} a_{t, t'} x_{t'}
v = K.batch_dot(a, inputs)
if self.attention_regularizer_weight > 0.0:
self.add_loss(self._attention_regularizer(a))
if self.return_attention:
return [v, a]
return v
def _call_additive_emission(self, inputs):
input_shape = K.shape(inputs)
batch_size, input_len = input_shape[0], input_shape[1]
# h_{t, t'} = \tanh(x_t^T W_t + x_{t'}^T W_x + b_h)
q = K.expand_dims(K.dot(inputs, self.Wt), 2)
k = K.expand_dims(K.dot(inputs, self.Wx), 1)
if self.use_additive_bias:
h = K.tanh(q + k + self.bh)
else:
h = K.tanh(q + k)
# e_{t, t'} = W_a h_{t, t'} + b_a
if self.use_attention_bias:
e = K.reshape(K.dot(h, self.Wa) + self.ba, (batch_size, input_len, input_len))
else:
e = K.reshape(K.dot(h, self.Wa), (batch_size, input_len, input_len))
return e
def _call_multiplicative_emission(self, inputs):
# e_{t, t'} = x_t^T W_a x_{t'} + b_a
e = K.batch_dot(K.dot(inputs, self.Wa), K.permute_dimensions(inputs, (0, 2, 1)))
if self.use_attention_bias:
e += self.ba[0]
return e
def compute_output_shape(self, input_shape):
output_shape = input_shape
if self.return_attention:
attention_shape = (input_shape[0], output_shape[1], input_shape[1])
return [output_shape, attention_shape]
return output_shape
def compute_mask(self, inputs, mask=None):
if self.return_attention:
return [mask, None]
return mask
def _attention_regularizer(self, attention):
batch_size = K.cast(K.shape(attention)[0], K.floatx())
input_len = K.shape(attention)[-1]
indices = K.expand_dims(K.arange(0, input_len), axis=0)
diagonal = K.expand_dims(K.arange(0, input_len), axis=-1)
eye = K.cast(K.equal(indices, diagonal), K.floatx())
return self.attention_regularizer_weight * K.sum(K.square(K.batch_dot(
attention,
K.permute_dimensions(attention, (0, 2, 1))) - eye)) / batch_size
def keras_model(input_dim, filters, activation, kernel_size, conv_stride,
conv_border_mode, recur_layers, dilation_rate, units, conv_layers, output_dim=29):
# Input
input_data = Input(name='the_input', shape=(None, input_dim))
# Inital Convolutional layer
conv_1d = Conv1D(filters, kernel_size,
strides=conv_stride,
padding=conv_border_mode,
activation=activation,
name='conv1d')(input_data)
# Batch normalization
bn_cnn = BatchNormalization()(conv_1d)
# Loop for additional layers
for i in range(conv_layers - 1):
conv_1d = Conv1D(filters, kernel_size,
padding=conv_border_mode,
activation=activation,
dilation_rate=2**i,
name="conv_1d_"+str(i))(bn_cnn)
bn_cnn = BatchNormalization()(conv_1d)
# Initial Bidirectional recurrent layer
brnn = Bidirectional(GRU(units, activation=activation,
return_sequences=True, implementation=2, recurrent_dropout=0.02, name='brnn'))(bn_cnn)
# Batch normalization
bn_rnn = BatchNormalization()(brnn)
# Loop for additional layers
for i in range(recur_layers - 1):
name = 'brnn_' + str(i + 1)
brnn = Bidirectional(GRU(units, activation=activation,
return_sequences=True, implementation=2, name=name))(bn_rnn)
bn_rnn = BatchNormalization()(brnn)
# Attention layer
attentive = Attention()(bn_rnn)
# TimeDistributed Dense layers
time_distributed_dense = TimeDistributed(Dense(1024))(attentive)
time_dense = TimeDistributed(Dense(output_dim))(time_distributed_dense)
# Softmax activation layer
y_pred = Activation('softmax', name='softmax')(time_dense)
# Specifying the model
model = Model(inputs=input_data, outputs=y_pred)
model.output_length = lambda x: cnn_output_length(
x, kernel_size, conv_border_mode, conv_stride)
print(model.summary())
return model
hey_jetson = keras_model(input_dim=161, # 161 for Spectrogram/13 for MFCC
filters=256,
activation='relu',
kernel_size=5,
conv_stride=2,
recur_layers=7,
conv_border_mode='causal',
conv_layers=3,
dilation_rate=2,
units=256)
train_model(input_to_softmax=hey_jetson,
pickle_path='model_11.pickle',
save_model_path='model_11.h5',
spectrogram=True) # True for Spectrogram/False for MFCC