sztuczna-empatia-kaczuszka/lab8/zad3/zad3.ipynb

303 lines
17 KiB
Plaintext
Raw Normal View History

2023-06-04 23:08:59 +02:00
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/JulianZablonski37/uczenie_glebokie/blob/main/Untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"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": [
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-con
" learning_rate=&#x27;adaptive&#x27;, max_iter=500)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">MLPClassifier</label><div class=\"sk-toggleable__content\"><pre>MLPClassifier(alpha=0.01, batch_size=256, hidden_layer_sizes=(300,),\n",
" learning_rate=&#x27;adaptive&#x27;, max_iter=500)</pre></div></div></div></div></div>"
],
"text/plain": [
"MLPClassifier(alpha=0.01, batch_size=256, hidden_layer_sizes=(300,),\n",
" learning_rate='adaptive', max_iter=500)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#DataFlair - Train the model\n",
"model.fit(x_train,y_train)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"#DataFlair - Predict for the test set\n",
"y_pred=model.predict(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy: 84.03%\n"
]
}
],
"source": [
"#DataFlair - Calculate the accuracy of our model\n",
"accuracy=accuracy_score(y_true=y_test, y_pred=y_pred)\n",
"#DataFlair - Print the accuracy\n",
"print(\"Accuracy: {:.2f}%\".format(accuracy*100))"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"authorship_tag": "ABX9TyPkAYXCyvpIs/MEO8GiaU8/",
"include_colab_link": true,
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}