{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n", "Requirement already satisfied: librosa in c:\\users\\user\\anaconda3\\lib\\site-packages (0.10.0.post2)\n", "Requirement already satisfied: soundfile in c:\\users\\user\\anaconda3\\lib\\site-packages (0.12.1)\n", "Requirement already satisfied: numpy in c:\\users\\user\\anaconda3\\lib\\site-packages (1.20.3)\n", "Requirement already satisfied: sklearn in c:\\users\\user\\anaconda3\\lib\\site-packages (0.0)\n", "Requirement already satisfied: pyaudio in c:\\users\\user\\anaconda3\\lib\\site-packages (0.2.13)\n", "Requirement already satisfied: audioread>=2.1.9 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (3.0.0)\n", "Requirement already satisfied: scipy>=1.2.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (1.7.1)\n", "Requirement already satisfied: scikit-learn>=0.20.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (1.2.0)\n", "Requirement already satisfied: joblib>=0.14 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (1.2.0)\n", "Requirement already satisfied: decorator>=4.3.0 in c:\\users\\user\\appdata\\roaming\\python\\python39\\site-packages (from librosa) (5.1.1)\n", "Requirement already satisfied: numba>=0.51.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (0.54.1)\n", "Requirement already satisfied: pooch<1.7,>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (1.6.0)\n", "Requirement already satisfied: soxr>=0.3.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (0.3.5)\n", "Requirement already satisfied: typing-extensions>=4.1.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (4.4.0)\n", "Requirement already satisfied: lazy-loader>=0.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (0.2)\n", "Requirement already satisfied: msgpack>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from librosa) (1.0.2)\n", "Requirement already satisfied: cffi>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from soundfile) (1.14.6)\n", "Requirement already satisfied: pycparser in c:\\users\\user\\anaconda3\\lib\\site-packages (from cffi>=1.0->soundfile) (2.20)\n", "Requirement already satisfied: llvmlite<0.38,>=0.37.0rc1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from numba>=0.51.0->librosa) (0.37.0)\n", "Requirement already satisfied: setuptools in c:\\users\\user\\anaconda3\\lib\\site-packages (from numba>=0.51.0->librosa) (65.6.3)\n", "Requirement already satisfied: appdirs>=1.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from pooch<1.7,>=1.0->librosa) (1.4.4)\n", "Requirement already satisfied: packaging>=20.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from pooch<1.7,>=1.0->librosa) (21.0)\n", "Requirement already satisfied: requests>=2.19.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from pooch<1.7,>=1.0->librosa) (2.30.0)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from scikit-learn>=0.20.0->librosa) (2.2.0)\n", "Requirement already satisfied: pyparsing>=2.0.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from packaging>=20.0->pooch<1.7,>=1.0->librosa) (2.4.7)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.19.0->pooch<1.7,>=1.0->librosa) (2.0.4)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.19.0->pooch<1.7,>=1.0->librosa) (3.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.19.0->pooch<1.7,>=1.0->librosa) (1.26.14)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.19.0->pooch<1.7,>=1.0->librosa) (2023.5.7)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Ignoring invalid distribution -andas (c:\\users\\user\\anaconda3\\lib\\site-packages)\n", "WARNING: Ignoring invalid distribution -andas (c:\\users\\user\\anaconda3\\lib\\site-packages)\n" ] } ], "source": [ "!pip install librosa soundfile numpy sklearn pyaudio" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import librosa\n", "import soundfile\n", "import os, glob, pickle\n", "import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.neural_network import MLPClassifier\n", "from sklearn.metrics import accuracy_score" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#DataFlair - Extract features (mfcc, chroma, mel) from a sound file\n", "def extract_feature(file_name, mfcc, chroma, mel):\n", " with soundfile.SoundFile(file_name) as sound_file:\n", " X = sound_file.read(dtype=\"float32\")\n", " sample_rate=sound_file.samplerate\n", " if chroma:\n", " stft=np.abs(librosa.stft(X))\n", " result=np.array([])\n", " if mfcc:\n", " mfccs=np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)\n", " result=np.hstack((result, mfccs))\n", " if chroma:\n", " chroma=np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)\n", " result=np.hstack((result, chroma))\n", " if mel:\n", " mel=np.mean(librosa.feature.melspectrogram(y=X, sr=sample_rate).T,axis=0)\n", " result=np.hstack((result, mel))\n", " return result" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "#DataFlair - Emotions in the RAVDESS dataset\n", "emotions={\n", " '01':'neutral',\n", " '02':'calm',\n", " '03':'happy',\n", " '04':'sad',\n", " '05':'angry',\n", " '06':'fearful',\n", " '07':'disgust',\n", " '08':'surprised'\n", "}\n", "#DataFlair - Emotions to observe\n", "observed_emotions=['calm', 'happy', 'disgust']" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "#DataFlair - Load the data and extract features for each sound file\n", "\n", "from pathlib import Path\n", "path = str(Path.cwd())\n", "def load_data(test_size=0.2):\n", " x,y=[],[]\n", " for file in glob.glob(f\"{path}\\data\\Actor_*\\*.wav\"):\n", " file_name=os.path.basename(file)\n", " emotion=emotions[file_name.split(\"-\")[2]]\n", " if emotion not in observed_emotions:\n", " continue\n", " feature=extract_feature(file, mfcc=True, chroma=True, mel=True)\n", " x.append(feature)\n", " y.append(emotion)\n", " return train_test_split(np.array(x), y, test_size=test_size, random_state=9)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#DataFlair - Split the dataset\n", "x_train,x_test,y_train,y_test=load_data(test_size=0.25)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(432, 144)\n" ] } ], "source": [ "#DataFlair - Get the shape of the training and testing datasets\n", "print((x_train.shape[0], x_test.shape[0]))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Features extracted: 180\n" ] } ], "source": [ "#DataFlair - Get the number of features extracted\n", "print(f'Features extracted: {x_train.shape[1]}')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "#DataFlair - Initialize the Multi Layer Perceptron Classifier\n", "model=MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08, hidden_layer_sizes=(300,), learning_rate='adaptive', max_iter=500)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\User\\anaconda3\\lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:679: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (500) reached and the optimization hasn't converged yet.\n", " warnings.warn(\n" ] }, { "data": { "text/html": [ "
MLPClassifier(alpha=0.01, batch_size=256, hidden_layer_sizes=(300,),\n", " learning_rate='adaptive', max_iter=500)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
MLPClassifier(alpha=0.01, batch_size=256, hidden_layer_sizes=(300,),\n", " learning_rate='adaptive', max_iter=500)