-
Notifications
You must be signed in to change notification settings - Fork 233
/
deep_emotion_recognition.py
401 lines (348 loc) · 17.3 KB
/
deep_emotion_recognition.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
import os
# disable keras loggings
import sys
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
import tensorflow as tf
from tensorflow.keras.layers import LSTM, GRU, Dense, Activation, LeakyReLU, Dropout
from tensorflow.keras.layers import Conv1D, MaxPool1D, GlobalAveragePooling1D
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard
from tensorflow.keras.utils import to_categorical
from sklearn.metrics import accuracy_score, mean_absolute_error, confusion_matrix
from data_extractor import load_data
from create_csv import write_custom_csv, write_emodb_csv, write_tess_ravdess_csv
from emotion_recognition import EmotionRecognizer
from utils import get_first_letters, AVAILABLE_EMOTIONS, extract_feature, get_dropout_str
import numpy as np
import pandas as pd
import random
class DeepEmotionRecognizer(EmotionRecognizer):
"""
The Deep Learning version of the Emotion Recognizer.
This class uses RNN (LSTM, GRU, etc.) and Dense layers.
#TODO add CNNs
"""
def __init__(self, **kwargs):
"""
params:
emotions (list): list of emotions to be used. Note that these emotions must be available in
RAVDESS_TESS & EMODB Datasets, available nine emotions are the following:
'neutral', 'calm', 'happy', 'sad', 'angry', 'fear', 'disgust', 'ps' ( pleasant surprised ), 'boredom'.
Default is ["sad", "neutral", "happy"].
tess_ravdess (bool): whether to use TESS & RAVDESS Speech datasets, default is True.
emodb (bool): whether to use EMO-DB Speech dataset, default is True.
custom_db (bool): whether to use custom Speech dataset that is located in `data/train-custom`
and `data/test-custom`, default is True.
tess_ravdess_name (str): the name of the output CSV file for TESS&RAVDESS dataset, default is "tess_ravdess.csv".
emodb_name (str): the name of the output CSV file for EMO-DB dataset, default is "emodb.csv".
custom_db_name (str): the name of the output CSV file for the custom dataset, default is "custom.csv".
features (list): list of speech features to use, default is ["mfcc", "chroma", "mel"]
(i.e MFCC, Chroma and MEL spectrogram ).
classification (bool): whether to use classification or regression, default is True.
balance (bool): whether to balance the dataset ( both training and testing ), default is True.
verbose (bool/int): whether to print messages on certain tasks.
==========================================================
Model params
n_rnn_layers (int): number of RNN layers, default is 2.
cell (keras.layers.RNN instance): RNN cell used to train the model, default is LSTM.
rnn_units (int): number of units of `cell`, default is 128.
n_dense_layers (int): number of Dense layers, default is 2.
dense_units (int): number of units of the Dense layers, default is 128.
dropout (list/float): dropout rate,
- if list, it indicates the dropout rate of each layer.
- if float, it indicates the dropout rate for all layers.
Default is 0.3.
==========================================================
Training params
batch_size (int): number of samples per gradient update, default is 64.
epochs (int): number of epochs, default is 1000.
optimizer (str/keras.optimizers.Optimizer instance): optimizer used to train, default is "adam".
loss (str/callback from keras.losses): loss function that is used to minimize during training,
default is "categorical_crossentropy" for classification and "mean_squared_error" for
regression.
"""
# init EmotionRecognizer
super().__init__(**kwargs)
self.n_rnn_layers = kwargs.get("n_rnn_layers", 2)
self.n_dense_layers = kwargs.get("n_dense_layers", 2)
self.rnn_units = kwargs.get("rnn_units", 128)
self.dense_units = kwargs.get("dense_units", 128)
self.cell = kwargs.get("cell", LSTM)
# list of dropouts of each layer
# must be len(dropouts) = n_rnn_layers + n_dense_layers
self.dropout = kwargs.get("dropout", 0.3)
self.dropout = self.dropout if isinstance(self.dropout, list) else [self.dropout] * ( self.n_rnn_layers + self.n_dense_layers )
# number of classes ( emotions )
self.output_dim = len(self.emotions)
# optimization attributes
self.optimizer = kwargs.get("optimizer", "adam")
self.loss = kwargs.get("loss", "categorical_crossentropy")
# training attributes
self.batch_size = kwargs.get("batch_size", 64)
self.epochs = kwargs.get("epochs", 500)
# the name of the model
self.model_name = ""
self._update_model_name()
# init the model
self.model = None
# compute the input length
self._compute_input_length()
# boolean attributes
self.model_created = False
def _update_model_name(self):
"""
Generates a unique model name based on parameters passed and put it on `self.model_name`.
This is used when saving the model.
"""
# get first letters of emotions, for instance:
# ["sad", "neutral", "happy"] => 'HNS' (sorted alphabetically)
emotions_str = get_first_letters(self.emotions)
# 'c' for classification & 'r' for regression
problem_type = 'c' if self.classification else 'r'
dropout_str = get_dropout_str(self.dropout, n_layers=self.n_dense_layers + self.n_rnn_layers)
self.model_name = f"{emotions_str}-{problem_type}-{self.cell.__name__}-layers-{self.n_rnn_layers}-{self.n_dense_layers}-units-{self.rnn_units}-{self.dense_units}-dropout-{dropout_str}.h5"
def _get_model_filename(self):
"""Returns the relative path of this model name"""
return f"results/{self.model_name}"
def _model_exists(self):
"""
Checks if model already exists in disk, returns the filename,
and returns `None` otherwise.
"""
filename = self._get_model_filename()
return filename if os.path.isfile(filename) else None
def _compute_input_length(self):
"""
Calculates the input shape to be able to construct the model.
"""
if not self.data_loaded:
self.load_data()
self.input_length = self.X_train[0].shape[1]
def _verify_emotions(self):
super()._verify_emotions()
self.int2emotions = {i: e for i, e in enumerate(self.emotions)}
self.emotions2int = {v: k for k, v in self.int2emotions.items()}
def create_model(self):
"""
Constructs the neural network based on parameters passed.
"""
if self.model_created:
# model already created, why call twice
return
if not self.data_loaded:
# if data isn't loaded yet, load it
self.load_data()
model = Sequential()
# rnn layers
for i in range(self.n_rnn_layers):
if i == 0:
# first layer
model.add(self.cell(self.rnn_units, return_sequences=True, input_shape=(None, self.input_length)))
model.add(Dropout(self.dropout[i]))
else:
# middle layers
model.add(self.cell(self.rnn_units, return_sequences=True))
model.add(Dropout(self.dropout[i]))
if self.n_rnn_layers == 0:
i = 0
# dense layers
for j in range(self.n_dense_layers):
# if n_rnn_layers = 0, only dense
if self.n_rnn_layers == 0 and j == 0:
model.add(Dense(self.dense_units, input_shape=(None, self.input_length)))
model.add(Dropout(self.dropout[i+j]))
else:
model.add(Dense(self.dense_units))
model.add(Dropout(self.dropout[i+j]))
if self.classification:
model.add(Dense(self.output_dim, activation="softmax"))
model.compile(loss=self.loss, metrics=["accuracy"], optimizer=self.optimizer)
else:
model.add(Dense(1, activation="linear"))
model.compile(loss="mean_squared_error", metrics=["mean_absolute_error"], optimizer=self.optimizer)
self.model = model
self.model_created = True
if self.verbose > 0:
print("[+] Model created")
def load_data(self):
"""
Loads and extracts features from the audio files for the db's specified.
And then reshapes the data.
"""
super().load_data()
# reshape X's to 3 dims
X_train_shape = self.X_train.shape
X_test_shape = self.X_test.shape
self.X_train = self.X_train.reshape((1, X_train_shape[0], X_train_shape[1]))
self.X_test = self.X_test.reshape((1, X_test_shape[0], X_test_shape[1]))
if self.classification:
# one-hot encode when its classification
self.y_train = to_categorical([ self.emotions2int[str(e)] for e in self.y_train ])
self.y_test = to_categorical([ self.emotions2int[str(e)] for e in self.y_test ])
# reshape labels
y_train_shape = self.y_train.shape
y_test_shape = self.y_test.shape
if self.classification:
self.y_train = self.y_train.reshape((1, y_train_shape[0], y_train_shape[1]))
self.y_test = self.y_test.reshape((1, y_test_shape[0], y_test_shape[1]))
else:
self.y_train = self.y_train.reshape((1, y_train_shape[0], 1))
self.y_test = self.y_test.reshape((1, y_test_shape[0], 1))
def train(self, override=False):
"""
Trains the neural network.
Params:
override (bool): whether to override the previous identical model, can be used
when you changed the dataset, default is False
"""
# if model isn't created yet, create it
if not self.model_created:
self.create_model()
# if the model already exists and trained, just load the weights and return
# but if override is True, then just skip loading weights
if not override:
model_name = self._model_exists()
if model_name:
self.model.load_weights(model_name)
self.model_trained = True
if self.verbose > 0:
print("[*] Model weights loaded")
return
if not os.path.isdir("results"):
os.mkdir("results")
if not os.path.isdir("logs"):
os.mkdir("logs")
model_filename = self._get_model_filename()
self.checkpointer = ModelCheckpoint(model_filename, save_best_only=True, verbose=1)
self.tensorboard = TensorBoard(log_dir=os.path.join("logs", self.model_name))
self.history = self.model.fit(self.X_train, self.y_train,
batch_size=self.batch_size,
epochs=self.epochs,
validation_data=(self.X_test, self.y_test),
callbacks=[self.checkpointer, self.tensorboard],
verbose=self.verbose)
self.model_trained = True
if self.verbose > 0:
print("[+] Model trained")
def predict(self, audio_path):
feature = extract_feature(audio_path, **self.audio_config).reshape((1, 1, self.input_length))
if self.classification:
prediction = self.model.predict(feature)
prediction = np.argmax(np.squeeze(prediction))
return self.int2emotions[prediction]
else:
return np.squeeze(self.model.predict(feature))
def predict_proba(self, audio_path):
if self.classification:
feature = extract_feature(audio_path, **self.audio_config).reshape((1, 1, self.input_length))
proba = self.model.predict(feature)[0][0]
result = {}
for prob, emotion in zip(proba, self.emotions):
result[emotion] = prob
return result
else:
raise NotImplementedError("Probability prediction doesn't make sense for regression")
def test_score(self):
y_test = self.y_test[0]
if self.classification:
y_pred = self.model.predict(self.X_test)[0]
y_pred = [np.argmax(y, out=None, axis=None) for y in y_pred]
y_test = [np.argmax(y, out=None, axis=None) for y in y_test]
return accuracy_score(y_true=y_test, y_pred=y_pred)
else:
y_pred = self.model.predict(self.X_test)[0]
return mean_absolute_error(y_true=y_test, y_pred=y_pred)
def train_score(self):
y_train = self.y_train[0]
if self.classification:
y_pred = self.model.predict(self.X_train)[0]
y_pred = [np.argmax(y, out=None, axis=None) for y in y_pred]
y_train = [np.argmax(y, out=None, axis=None) for y in y_train]
return accuracy_score(y_true=y_train, y_pred=y_pred)
else:
y_pred = self.model.predict(self.X_train)[0]
return mean_absolute_error(y_true=y_train, y_pred=y_pred)
def confusion_matrix(self, percentage=True, labeled=True):
"""Compute confusion matrix to evaluate the test accuracy of the classification"""
if not self.classification:
raise NotImplementedError("Confusion matrix works only when it is a classification problem")
y_pred = self.model.predict(self.X_test)[0]
y_pred = np.array([ np.argmax(y, axis=None, out=None) for y in y_pred])
# invert from keras.utils.to_categorical
y_test = np.array([ np.argmax(y, axis=None, out=None) for y in self.y_test[0] ])
matrix = confusion_matrix(y_test, y_pred, labels=[self.emotions2int[e] for e in self.emotions]).astype(np.float32)
if percentage:
for i in range(len(matrix)):
matrix[i] = matrix[i] / np.sum(matrix[i])
# make it percentage
matrix *= 100
if labeled:
matrix = pd.DataFrame(matrix, index=[ f"true_{e}" for e in self.emotions ],
columns=[ f"predicted_{e}" for e in self.emotions ])
return matrix
def get_n_samples(self, emotion, partition):
"""Returns number data samples of the `emotion` class in a particular `partition`
('test' or 'train')
"""
if partition == "test":
if self.classification:
y_test = np.array([ np.argmax(y, axis=None, out=None)+1 for y in np.squeeze(self.y_test) ])
else:
y_test = np.squeeze(self.y_test)
return len([y for y in y_test if y == emotion])
elif partition == "train":
if self.classification:
y_train = np.array([ np.argmax(y, axis=None, out=None)+1 for y in np.squeeze(self.y_train) ])
else:
y_train = np.squeeze(self.y_train)
return len([y for y in y_train if y == emotion])
def get_samples_by_class(self):
"""
Returns a dataframe that contains the number of training
and testing samples for all emotions
"""
train_samples = []
test_samples = []
total = []
for emotion in self.emotions:
n_train = self.get_n_samples(self.emotions2int[emotion]+1, "train")
n_test = self.get_n_samples(self.emotions2int[emotion]+1, "test")
train_samples.append(n_train)
test_samples.append(n_test)
total.append(n_train + n_test)
# get total
total.append(sum(train_samples) + sum(test_samples))
train_samples.append(sum(train_samples))
test_samples.append(sum(test_samples))
return pd.DataFrame(data={"train": train_samples, "test": test_samples, "total": total}, index=self.emotions + ["total"])
def get_random_emotion(self, emotion, partition="train"):
"""
Returns random `emotion` data sample index on `partition`
"""
if partition == "train":
y_train = self.y_train[0]
index = random.choice(list(range(len(y_train))))
element = self.int2emotions[np.argmax(y_train[index])]
while element != emotion:
index = random.choice(list(range(len(y_train))))
element = self.int2emotions[np.argmax(y_train[index])]
elif partition == "test":
y_test = self.y_test[0]
index = random.choice(list(range(len(y_test))))
element = self.int2emotions[np.argmax(y_test[index])]
while element != emotion:
index = random.choice(list(range(len(y_test))))
element = self.int2emotions[np.argmax(y_test[index])]
else:
raise TypeError("Unknown partition, only 'train' or 'test' is accepted")
return index
def determine_best_model(self):
# TODO
# raise TypeError("This method isn't supported yet for deep nn")
pass
if __name__ == "__main__":
rec = DeepEmotionRecognizer(emotions=['angry', 'sad', 'neutral', 'ps', 'happy'],
epochs=300, verbose=0)
rec.train(override=False)
print("Test accuracy score:", rec.test_score() * 100, "%")