/
models.py
227 lines (191 loc) · 8.49 KB
/
models.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
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense,Flatten, GRU, BatchNormalization, Conv1D, Dropout, Bidirectional,MaxPooling1D, Input
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.optimizers import RMSprop, SGD
from tensorflow.keras import layers as L
from tensorflow.keras import backend as K
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Lambda, BatchNormalization, Conv1D, GRU, TimeDistributed, Activation, Dense, Flatten
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.losses import categorical_crossentropy
import tensorflow as tf
def dense_Model(x, labels):
"""Initializes and returns a custom Keras model
which is ready to be trained."""
if len(x.shape) >= 3:
h_feat,w_feat,ch_size = x.shape
input_layer = keras.layers.Input(shape=(h_feat,w_feat,ch_size))
else:
h_feat,w_feat = x.shape
input_layer = keras.layers.Input(shape=(h_feat,w_feat))
model = keras.models.Sequential([
input_layer,
keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=1e-3, center=True, scale=True),
keras.layers.Flatten(),
keras.layers.Dense(64),
keras.layers.Dense(64),
keras.layers.Dense(32),
keras.layers.Dense(len(labels), activation="softmax")
])
model.compile(
optimizer=SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5),
loss="categorical_crossentropy",
metrics=["accuracy"],
)
return model
# define cnn model
def cnn_Model(h_feat, w_feat, labels):
model = Sequential()
model.add(Conv2D(6, (2, 2), padding='valid', activation='relu', input_shape=(h_feat, w_feat, 1)))
#model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(len(labels), activation='softmax'))
# compile model
opt = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
return model
def attrnn_Model(x_in, labels, ablation = False):
# simple LSTM
rnn_func = L.LSTM
use_Unet = True
if len(x_in.shape) >= 3:
h_feat,w_feat,ch_size = x_in.shape
inputs = keras.layers.Input(shape=(h_feat, w_feat, ch_size))
else:
h_feat, w_feat = x_in.shape
inputs = keras.layers.Input(shape=(h_feat, w_feat))
inputs = L.Input(shape=(h_feat, w_feat, ch_size))
if ablation == True:
x = L.Conv2D(4, (1, 1), strides=(2, 2), activation='relu', padding='same', name='abla_conv')(inputs)
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=1e-3, center=True, scale=True)(x)
else:
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=1e-3, center=True, scale=True)(inputs)
# note that Melspectrogram puts the sequence in shape (batch_size, melDim, timeSteps, 1)
# we would rather have it the other way around for LSTMs
x = L.Permute((2, 1, 3))(x)
if use_Unet == True:
x = L.Conv2D(16, (5, 1), activation='relu', padding='same')(x)
up = L.BatchNormalization()(x)
x = L.Conv2D(32, (5, 1), activation='relu', padding='same')(up)
x = L.BatchNormalization()(x)
x = L.Conv2D(16, (5, 1), activation='relu', padding='same')(x)
down = L.BatchNormalization()(x)
merge = L.Concatenate(axis=3)([up,down])
x = L.Conv2D(1, (5, 1), activation='relu', padding='same')(merge)
x = L.BatchNormalization()(x)
else:
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.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.Dense(32)(x)
output = L.Dense(len(labels), activation='softmax', name='output')(x)
model = Model(inputs=inputs, outputs=output)
model.compile(
optimizer=SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5),
loss="categorical_crossentropy",
metrics=["accuracy"],
)
return model
class CTCLayer(L.Layer):
def __init__(self, name=None):
super().__init__(name=name)
self.loss_fn = keras.backend.ctc_batch_cost
def call(self, y_true, y_pred):
# Compute the training-time loss value and add it
# to the layer using `self.add_loss()`.
batch_len = tf.cast(tf.shape(y_true)[0], dtype="int64")
input_length = tf.cast(tf.shape(y_pred)[1], dtype="int64")
label_length = tf.cast(tf.shape(y_true)[1], dtype="int64")
input_length = input_length * tf.ones(shape=(batch_len, 1), dtype="int64")
label_length = label_length * tf.ones(shape=(batch_len, 1), dtype="int64")
loss = self.loss_fn(y_true, y_pred, input_length, label_length)
self.add_loss(loss)
# At test time, just return the computed predictions
return y_pred
def build_asr_model(h_feat, w_feat, ch_size = 1, volc_size = 26):
use_cnn = False
use_Unet = True
# Inputs to the model
input_img = L.Input(
shape=(h_feat, w_feat, ch_size), name="speech", dtype="float32"
)
labels = L.Input(name="label", shape=(None,), dtype="float32")
x = L.Permute((2, 1, 3))(input_img)
if use_cnn != True:
if use_Unet == True:
x = L.Conv2D(16, (5, 1), activation='relu', padding='same')(x)
up = L.BatchNormalization()(x)
x = L.Conv2D(32, (5, 1), activation='relu', padding='same')(up)
x = L.BatchNormalization()(x)
x = L.Conv2D(16, (5, 1), activation='relu', padding='same')(x)
down = L.BatchNormalization()(x)
merge = L.Concatenate(axis=3)([up,down])
x = L.Conv2D(1, (5, 1), activation='relu', padding='same')(merge)
x = L.BatchNormalization()(x)
else:
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.Lambda(lambda q: K.squeeze(q, -1), name='squeeze_last_dim')(x)
else:
# First conv block
x = L.Conv2D(
32,
(3, 3),
activation="relu",
kernel_initializer="he_normal",
padding="same",
name="Conv1",
)(x)
x = L.MaxPooling2D((2, 2), name="pool1")(x)
# Second conv block
x = L.Conv2D(
64,
(3, 3),
activation="relu",
kernel_initializer="he_normal",
padding="same",
name="Conv2",
)(x)
x = L.MaxPooling2D((2, 2), name="pool2")(x)
# We have used two max pool with pool size and strides 2.
# Hence, downsampled feature maps are 4x smaller. The number of
# filters in the last layer is 64. Reshape accordingly before
# passing the output to the RNN part of the model
new_shape = ((h_feat // 4), (w_feat // 4) * 64)
x = L.Reshape(target_shape=new_shape, name="reshape")(x)
# RNNs
x = L.Bidirectional(L.LSTM(64, return_sequences=True))(x)
x = L.Bidirectional(L.LSTM(64, return_sequences=True))(x)
x = L.Dense(64, activation="relu", name="dense1")(x)
x = L.Dense(32, activation="relu", name="dense11")(x)
# Output layer
x = L.Dense(volc_size, activation="softmax", name="dense2")(x)
# Add CTC layer for calculating CTC loss at each step
output = CTCLayer(name="ctc_loss")(labels, x)
# Define the model
model = keras.models.Model(
inputs=[input_img, labels], outputs=output, name="asr_model_v1"
)
# Optimizer
opt = keras.optimizers.Adam()
# Compile the model and return
model.compile(optimizer=opt)
return model