Building and training Speech Emotion Recognizer that predicts human emotions using Python, Sci-kit learn and Keras
convert_wavs.py
Install these libraries by the following command:
pip3 install -r requirements.txt
This repository used 4 datasets (including this repo's custom dataset) which are downloaded and formatted already in
datafolder: - RAVDESS : The Ryson Audio-Visual Database of Emotional Speech and Song that contains 24 actors (12 male, 12 female), vocalizing two lexically-matched statements in a neutral North American accent. - TESS : Toronto Emotional Speech Set that was modeled on the Northwestern University Auditory Test No. 6 (NU-6; Tillman & Carhart, 1966). A set of 200 target words were spoken in the carrier phrase "Say the word ____' by two actresses (aged 26 and 64 years). - EMO-DB : As a part of the DFG funded research project SE462/3-1 in 1997 and 1999 we recorded a database of emotional utterances spoken by actors. The recordings took place in the anechoic chamber of the Technical University Berlin, department of Technical Acoustics. Director of the project was Prof. Dr. W. Sendlmeier, Technical University of Berlin, Institute of Speech and Communication, department of communication science. Members of the project were mainly Felix Burkhardt, Miriam Kienast, Astrid Paeschke and Benjamin Weiss. - Custom : Some unbalanced noisy dataset that is located in
data/train-customfor training and
data/test-customfor testing in which you can add/remove recording samples easily by converting the raw audio to 16000 sample rate, mono channel (this is provided in `createwavs.py
script in
convert_audio(audio_path)` method which requires ffmpeg to be installed and in PATH) and adding the emotion to the end of audio file name separated with '' (e.g "20190616125714_happy.wav" will be parsed automatically as happy)
There are 9 emotions available: "neutral", "calm", "happy" "sad", "angry", "fear", "disgust", "ps" (pleasant surprise) and "boredom".
Feature extraction is the main part of the speech emotion recognition system. It is basically accomplished by changing the speech waveform to a form of parametric representation at a relatively lesser data rate.
In this repository, we have used the most used features that are available in librosa library including: - MFCC - Chromagram - MEL Spectrogram Frequency (mel) - Contrast - Tonnetz (tonal centroid features)
Grid search results are already provided in
gridfolder, but if you want to tune various grid search parameters in
parameters.py, you can run the script
grid_search.pyby:
python grid_search.pyThis may take several hours to complete execution, once it is finished, best estimators are stored and pickled in
gridfolder.
The way to build and train a model for classifying 3 emotions is as shown below: ```python from emotion_recognition import EmotionRecognizer from sklearn.svm import SVC
my_model = SVC()
rec = EmotionRecognizer(model=my_model, emotions=['sad', 'neutral', 'happy'], balance=True, verbose=0)
rec.train()
print("Test score:", rec.test_score())
print("Train score:", rec.train_score())
**Output:**Test score: 0.8148148148148148 Train score: 1.0 ```
In order to determine the best model, you can by:
# loads the best estimators from `grid` folder that was searched by GridSearchCV in `grid_search.py`, # and set the model to the best in terms of test score, and then train it rec.determine_best_model(train=True) # get the determined sklearn model name print(rec.model.__class__.__name__, "is the best") # get the test accuracy score for the best estimator print("Test score:", rec.test_score())
Output:
MLPClassifier is the best Test Score: 0.8958333333333334
Just pass an audio path to the
rec.predict()method as shown below: ```python
print("Prediction:", rec.predict("data/emodb/wav/15a04Nc.wav"))
print("Prediction:", rec.predict("data/tessravdess/validation/Actor25/25010101mob_sad.wav"))
**Output:**Prediction: neutral Prediction: sad ```
from deep_emotion_recognition import DeepEmotionRecognizer # initialize instance # inherited from emotion_recognition.EmotionRecognizer # default parameters (LSTM: 128x2, Dense:128x2) deeprec = DeepEmotionRecognizer(emotions=['angry', 'sad', 'neutral', 'ps', 'happy'], n_rnn_layers=2, n_dense_layers=2, rnn_units=128, dense_units=128) # train the model deeprec.train() # get the accuracy print(deeprec.test_score()) # predict angry audio sample prediction = deeprec.predict('data/validation/Actor_10/03-02-05-02-02-02-10_angry.wav') print(f"Prediction: {prediction}")
Output:
0.7948717948717948 Prediction: angryPredicting probabilities is also possible (for classification ofc):
python print(deeprec.predict_proba("data/emodb/wav/16a01Wb.wav"))Output:
{'angry': 0.8502438, 'sad': 1.15252915e-05, 'neutral': 8.986728e-05, 'ps': 0.14671412, 'happy': 0.0029406736}
print(deeprec.confusion_matrix(percentage=True, labeled=True))
Output:
predicted_angry predicted_sad predicted_neutral predicted_ps predicted_happy true_angry 92.307693 0.000000 1.282051 2.564103 3.846154 true_sad 12.820514 67.948715 3.846154 6.410257 8.974360 true_neutral 3.846154 8.974360 82.051285 2.564103 2.564103 true_ps 2.564103 0.000000 1.282051 83.333328 12.820514 true_happy 20.512821 2.564103 2.564103 2.564103 71.794876
This repository can be used to build machine learning classifiers as well as regressors for the case of 3 emotions {'sad': 0, 'neutral': 1, 'happy': 2} and the case of 5 emotions {'angry': 1, 'sad': 2, 'neutral': 3, 'ps': 4, 'happy': 5}
You can test your own voice by executing the following command:
python test.pyWait until "Please talk" prompt is appeared, then you can start talking, and the model will automatically detects your emotion when you stop (talking).
You can change emotions to predict, as well as models, type
--helpfor more information.
python test.py --helpOutput: ``` usage: test.py [-h] [-e EMOTIONS] [-m MODEL]
Testing emotion recognition system using your voice, please consider changing the model and/or parameters as you wish.
optional arguments: -h, --help show this help message and exit -e EMOTIONS, --emotions EMOTIONS Emotions to recognize separated by a comma ',', available emotions are "neutral", "calm", "happy" "sad", "angry", "fear", "disgust", "ps" (pleasant surprise) and "boredom", default is "sad,neutral,happy" -m MODEL, --model MODEL The model to use, 8 models available are: "SVC","AdaBo ostClassifier","RandomForestClassifier","GradientBoost ingClassifier","DecisionTreeClassifier","KNeighborsCla ssifier","MLPClassifier","BaggingClassifier", default is "BaggingClassifier"
## Plotting Histograms This will only work if grid search is performed. ```python from emotion_recognition import plot_histograms # plot histograms on different classifiers plot_histograms(classifiers=True)
Output:
A Histogram shows different algorithms metric results on different data sizes as well as time consumed to train/predict.