-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
52 lines (36 loc) · 1.82 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
import tensorflow as tf
from keras import layers
from keras.models import Model
from soundutils.model_utils import activation_layer
def train_model(input_dim, output_dim, activation="leaky_relu", dropout=0.2):
inputs = layers.Input(shape=input_dim, name="input", dtype=tf.float32)
# expand dims to add channel dimension
model_input = layers.Lambda(lambda y: tf.expand_dims(y, axis=-1))(inputs)
# Convolution layer 1
x = layers.Conv2D(filters=32, kernel_size=[11, 41], strides=[2, 2], padding="same", use_bias=False)(model_input)
x = layers.BatchNormalization()(x)
x = activation_layer(x, activation="leaky_relu")
# Convolution layer 2
x = layers.Conv2D(filters=32, kernel_size=[11, 21], strides=[1, 2], padding="same", use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = activation_layer(x, activation="leaky_relu")
# Reshape the resulted volumne to feed the RNNs layers
x = layers.Reshape((-1, x.shape[-2] * x.shape[-1]))(x)
# RNN layers
x = layers.Bidirectional(layer=layers.LSTM(128, return_sequences=True))(x)
x = layers.Dropout(dropout)(x)
x = layers.Bidirectional(layer=layers.LSTM(128, return_sequences=True))(x)
x = layers.Dropout(dropout)(x)
x = layers.Bidirectional(layer=layers.LSTM(128, return_sequences=True))(x)
x = layers.Dropout(dropout)(x)
x = layers.Bidirectional(layer=layers.LSTM(128, return_sequences=True))(x)
x = layers.Dropout(dropout)(x)
x = layers.Bidirectional(layer=layers.LSTM(128, return_sequences=True))(x)
# Dense layer
x = layers.Dense(256)(x)
x = activation_layer(x, activation="leaky_relu")
x = layers.Dropout(dropout)(x)
# Classification layer
output = layers.Dense(output_dim + 1, activation="softmax", dtype=tf.float32)(x)
model = Model(inputs=inputs, outputs=output)
return model