-
Notifications
You must be signed in to change notification settings - Fork 10
/
dnn.py
162 lines (137 loc) · 5.54 KB
/
dnn.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
"""
This file contains classes which implement deep neural networks namely CNN and LSTM
"""
import sys
import numpy as np
from keras import Sequential
from keras.layers import LSTM as KERAS_LSTM, Dense, Dropout, Conv2D, Flatten, \
BatchNormalization, Activation, MaxPooling2D
from . import Model
class DNN(Model):
"""
This class is parent class for all Deep neural network models. Any class
inheriting this class should implement `make_default_model` method which
creates a model with a set of hyper parameters.
"""
def __init__(self, input_shape, num_classes, **params):
"""
Constructor to initialize the deep neural network model. Takes the input
shape and number of classes and other parameters required for the
abstract class `Model` as parameters.
Args:
input_shape (tuple): shape of the input
num_classes (int): number of different classes ( labels ) in the data.
**params: Additional parameters required by the underlying abstract
class `Model`.
"""
super(DNN, self).__init__(**params)
self.input_shape = input_shape
self.model = Sequential()
self.make_default_model()
self.model.add(Dense(num_classes, activation='softmax'))
self.model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
print(self.model.summary(), file=sys.stderr)
self.save_path = self.save_path or self.name + '_best_model.h5'
def load_model(self, to_load):
"""
Load the model weights from the given path.
Args:
to_load (str): path to the saved model file in h5 format.
"""
try:
self.model.load_weights(to_load)
except:
sys.stderr.write("Invalid saved file provided")
sys.exit(-1)
def save_model(self):
"""
Save the model weights to `save_path` provided while creating the model.
"""
self.model.save_weights(self.save_path)
def train(self, x_train, y_train, x_val=None, y_val=None, n_epochs=50):
"""
Train the model on the given training data.
Args:
x_train (numpy.ndarray): samples of training data.
y_train (numpy.ndarray): labels for training data.
x_val (numpy.ndarray): Optional, samples in the validation data.
y_val (numpy.ndarray): Optional, labels of the validation data.
n_epochs (int): Number of epochs to be trained.
"""
best_acc = 0
if x_val is None or y_val is None:
x_val, y_val = x_train, y_train
for i in range(n_epochs):
# Shuffle the data for each epoch in unison inspired
# from https://stackoverflow.com/a/4602224
p = np.random.permutation(len(x_train))
x_train = x_train[p]
y_train = y_train[p]
self.model.fit(x_train, y_train, batch_size=32, epochs=1)
loss, acc = self.model.evaluate(x_val, y_val)
if acc > best_acc:
best_acc = acc
self.trained = True
def predict_one(self, sample):
if not self.trained:
sys.stderr.write(
"Model should be trained or loaded before doing predict\n")
sys.exit(-1)
return np.argmax(self.model.predict(np.array([sample])))
def make_default_model(self) -> None:
"""
Make the model with default hyper parameters
"""
# This has to be implemented by child classes. The reason is that the
# hyper parameters depends on the model.
raise NotImplementedError()
class CNN(DNN):
"""
This class handles CNN for speech emotion recognitions
"""
def __init__(self, **params):
params['name'] = 'CNN'
super(CNN, self).__init__(**params)
def make_default_model(self):
"""
Makes a CNN keras model with the default hyper parameters.
"""
self.model.add(Conv2D(8, (13, 13),
input_shape=(
self.input_shape[0], self.input_shape[1], 1)))
self.model.add(BatchNormalization(axis=-1))
self.model.add(Activation('relu'))
self.model.add(Conv2D(8, (13, 13)))
self.model.add(BatchNormalization(axis=-1))
self.model.add(Activation('relu'))
self.model.add(MaxPooling2D(pool_size=(2, 1)))
self.model.add(Conv2D(8, (13, 13)))
self.model.add(BatchNormalization(axis=-1))
self.model.add(Activation('relu'))
self.model.add(Conv2D(8, (2, 2)))
self.model.add(BatchNormalization(axis=-1))
self.model.add(Activation('relu'))
self.model.add(MaxPooling2D(pool_size=(2, 1)))
self.model.add(Flatten())
self.model.add(Dense(64))
self.model.add(BatchNormalization())
self.model.add(Activation('relu'))
self.model.add(Dropout(0.2))
class LSTM(DNN):
"""
This class handles CNN for speech emotion recognitions
"""
def __init__(self, **params):
params['name'] = 'LSTM'
super(LSTM, self).__init__(**params)
def make_default_model(self):
"""
Makes the LSTM model with keras with the default hyper parameters.
"""
self.model.add(
KERAS_LSTM(128,
input_shape=(self.input_shape[0], self.input_shape[1])))
self.model.add(Dropout(0.5))
self.model.add(Dense(32, activation='relu'))
self.model.add(Dense(16, activation='tanh'))