Commit f6a46d22 authored by Lisa Casino's avatar Lisa Casino
Browse files

add fichier loading

parent b3bc45b1
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "tough-exposure",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np \n",
"import os\n",
"import pandas as pd \n",
"from scipy.io import wavfile\n",
"\n",
"import librosa\n",
"from tqdm import tqdm\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"import matplotlib.pyplot as plt\n",
"from tensorflow.keras import regularizers, activations\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, MaxPooling2D, GlobalAveragePooling2D\n",
"from keras.utils import np_utils, to_categorical\n",
"\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"from datetime import datetime \n",
"\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "polished-tongue",
"metadata": {},
"outputs": [],
"source": [
"us8k_df = pd.read_pickle(\"us8k_df.pkl\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "elementary-congress",
"metadata": {},
"outputs": [],
"source": [
"def init_model():\n",
" model1 = Sequential()\n",
" \n",
" #layer-1\n",
" model1.add(Conv2D(filters=24, kernel_size=5, input_shape=(128, 128, 1),\n",
" kernel_regularizer=regularizers.l2(1e-3)))\n",
" model1.add(MaxPooling2D(pool_size=(3,3), strides=3))\n",
" model1.add(Activation(activations.relu))\n",
" \n",
" #layer-2\n",
" model1.add(Conv2D(filters=36, kernel_size=4, padding='valid', kernel_regularizer=regularizers.l2(1e-3)))\n",
" model1.add(MaxPooling2D(pool_size=(2,2), strides=2))\n",
" model1.add(Activation(activations.relu))\n",
" \n",
" #layer-3\n",
" model1.add(Conv2D(filters=48, kernel_size=3, padding='valid'))\n",
" model1.add(Activation(activations.relu))\n",
" \n",
" model1.add(GlobalAveragePooling2D())\n",
" \n",
" #layer-4 (1st dense layer)\n",
" model1.add(Dense(60, activation='relu'))\n",
" model1.add(Dropout(0.5))\n",
" \n",
" #layer-5 (2nd dense layer)\n",
" model1.add(Dense(10, activation='softmax'))\n",
"\n",
" \n",
" # compile\n",
" model1.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')\n",
" \n",
" return model1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "afraid-louisville",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d (Conv2D) (None, 124, 124, 24) 624 \n",
"_________________________________________________________________\n",
"max_pooling2d (MaxPooling2D) (None, 41, 41, 24) 0 \n",
"_________________________________________________________________\n",
"activation (Activation) (None, 41, 41, 24) 0 \n",
"_________________________________________________________________\n",
"conv2d_1 (Conv2D) (None, 38, 38, 36) 13860 \n",
"_________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2 (None, 19, 19, 36) 0 \n",
"_________________________________________________________________\n",
"activation_1 (Activation) (None, 19, 19, 36) 0 \n",
"_________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, 17, 17, 48) 15600 \n",
"_________________________________________________________________\n",
"activation_2 (Activation) (None, 17, 17, 48) 0 \n",
"_________________________________________________________________\n",
"global_average_pooling2d (Gl (None, 48) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 60) 2940 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 60) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 10) 610 \n",
"=================================================================\n",
"Total params: 33,634\n",
"Trainable params: 33,634\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model = init_model()\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "altered-vermont",
"metadata": {},
"outputs": [],
"source": [
"def train_test_split(fold_k, data, X_dim=(128, 128, 1)):\n",
" X_train = np.stack(data[data.fold != fold_k].melspectrogram.to_numpy())\n",
" X_test = np.stack(data[data.fold == fold_k].melspectrogram.to_numpy())\n",
"\n",
" y_train = data[data.fold != fold_k].label.to_numpy()\n",
" y_test = data[data.fold == fold_k].label.to_numpy()\n",
"\n",
" XX_train = X_train.reshape(X_train.shape[0], *X_dim)\n",
" XX_test = X_test.reshape(X_test.shape[0], *X_dim)\n",
" \n",
" yy_train = to_categorical(y_train)\n",
" yy_test = to_categorical(y_test)\n",
" \n",
" return XX_train, XX_test, yy_train, yy_test"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "phantom-strap",
"metadata": {},
"outputs": [],
"source": [
"def process_fold(fold_k, data, epochs=100, num_batch_size=32):\n",
" # split the data\n",
" X_train, X_test, y_train, y_test = train_test_split(fold_k, data)\n",
"\n",
" # init data augmention\n",
" #train_datagen, val_datagen = init_data_aug()\n",
" \n",
" # fit augmentation\n",
" #train_datagen.fit(X_train)\n",
" #val_datagen.fit(X_train)\n",
"\n",
" # init model\n",
" model = init_model()\n",
"\n",
" # pre-training accuracy\n",
" score = model.evaluate(X_test, y_test, batch_size=num_batch_size, verbose=0)\n",
" print(\"Pre-training accuracy: %.4f%%\\n\" % (100 * score[1]))\n",
" \n",
" # train the model\n",
" start = datetime.now()\n",
" history = model.fit(X_train,y_train, epochs=epochs,validation_data=(X_test,y_test),batch_size=num_batch_size)\n",
" end = datetime.now()\n",
" print(\"Training completed in time: \", end - start, '\\n')\n",
" \n",
" return history"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "raised-puppy",
"metadata": {},
"outputs": [],
"source": [
"def show_results(tot_history):\n",
" \"\"\"Show accuracy and loss graphs for train and test sets.\"\"\"\n",
"\n",
" for i, history in enumerate(tot_history):\n",
" print('\\n({})'.format(i+1))\n",
"\n",
" plt.figure(figsize=(15,5))\n",
"\n",
" plt.subplot(121)\n",
" plt.plot(history.history['accuracy'])\n",
" plt.plot(history.history['val_accuracy'])\n",
" plt.grid(linestyle='--')\n",
" plt.ylabel('Accuracy')\n",
" plt.xlabel('Epoch')\n",
" plt.legend(['train', 'validation'], loc='upper left')\n",
"\n",
" plt.subplot(122)\n",
" plt.plot(history.history['loss'])\n",
" plt.plot(history.history['val_loss'])\n",
" plt.grid(linestyle='--')\n",
" plt.ylabel('Loss')\n",
" plt.xlabel('Epoch')\n",
" plt.legend(['train', 'validation'], loc='upper left')\n",
" \n",
" plt.show()\n",
"\n",
" print('\\tMax validation accuracy: %.4f %%' % (np.max(history.history['val_accuracy']) * 100))\n",
" print('\\tMin validation loss: %.5f' % np.min(history.history['val_loss']))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "legislative-prior",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--------------------------------------------------------------------------------\n",
"\n",
"(1)\n",
"\n",
"Pre-training accuracy: 13.6312%\n",
"\n",
"Epoch 1/10\n",
"246/246 [==============================] - 46s 185ms/step - loss: 2.1745 - accuracy: 0.1996 - val_loss: 1.7348 - val_accuracy: 0.3253\n",
"Epoch 2/10\n",
"246/246 [==============================] - 44s 178ms/step - loss: 1.8258 - accuracy: 0.3251 - val_loss: 1.5448 - val_accuracy: 0.4948\n",
"Epoch 3/10\n",
"246/246 [==============================] - 44s 180ms/step - loss: 1.6410 - accuracy: 0.3967 - val_loss: 1.3963 - val_accuracy: 0.5155\n",
"Epoch 4/10\n",
"246/246 [==============================] - 44s 180ms/step - loss: 1.4471 - accuracy: 0.4709 - val_loss: 1.1977 - val_accuracy: 0.4960\n",
"Epoch 5/10\n",
"246/246 [==============================] - 45s 182ms/step - loss: 1.3507 - accuracy: 0.5222 - val_loss: 1.1505 - val_accuracy: 0.6632\n",
"Epoch 6/10\n",
"246/246 [==============================] - 48s 195ms/step - loss: 1.2840 - accuracy: 0.5477 - val_loss: 1.1121 - val_accuracy: 0.5750\n",
"Epoch 7/10\n",
"246/246 [==============================] - 50s 203ms/step - loss: 1.2254 - accuracy: 0.5791 - val_loss: 1.1266 - val_accuracy: 0.6037\n",
"Epoch 8/10\n",
"246/246 [==============================] - 48s 195ms/step - loss: 1.1647 - accuracy: 0.5888 - val_loss: 1.0520 - val_accuracy: 0.6942\n",
"Epoch 9/10\n",
"246/246 [==============================] - 45s 185ms/step - loss: 1.1141 - accuracy: 0.6174 - val_loss: 1.0825 - val_accuracy: 0.7171\n",
"Epoch 10/10\n",
"246/246 [==============================] - 45s 184ms/step - loss: 1.0420 - accuracy: 0.6379 - val_loss: 1.1577 - val_accuracy: 0.6506\n",
"Training completed in time: 0:07:40.769112 \n",
"\n"
]
}
],
"source": [
"FOLD_K = 1\n",
"\n",
"history1 = []\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "retained-greenhouse",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--------------------------------------------------------------------------------\n",
"\n",
"(2)\n",
"\n"
]
},
{
"ename": "NameError",
"evalue": "name 'process_fold' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-c639b5d8c6f7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\n({})\\n\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mFOLD_K\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mhistory\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprocess_fold\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mFOLD_K\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mus8k_df\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mhistory1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhistory\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'process_fold' is not defined"
]
}
],
"source": [
"FOLD_K = 2\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "exotic-institution",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 3\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "behavioral-worker",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 4\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "binding-relation",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 5\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "front-apparel",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 6\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "elder-saint",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 7\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "major-subject",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 8\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "threaded-wallace",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 9\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "nonprofit-neighborhood",
"metadata": {},
"outputs": [],
"source": [
"FOLD_K = 10\n",
"\n",
"print('-'*80)\n",
"print(\"\\n({})\\n\".format(FOLD_K))\n",
" \n",
"history = process_fold(FOLD_K, us8k_df, epochs=10)\n",
"history1.append(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "electric-colorado",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
......@@ -3,7 +3,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "powerful-episode",
"id": "naked-arabic",
"metadata": {},
"outputs": [],
"source": [
......@@ -32,7 +32,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "green-moisture",
"id": "stopped-livestock",
"metadata": {},
"outputs": [],
"source": [
......@@ -43,7 +43,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "charged-thong",
"id": "located-formation",
"metadata": {},
"outputs": [],
"source": [
......@@ -53,7 +53,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "individual-obligation",
"id": "entire-safety",
"metadata": {},
"outputs": [
{
......@@ -256,7 +256,7 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "accurate-charleston",
"id": "seeing-opportunity",
"metadata": {},
"outputs": [
{
......@@ -278,7 +278,7 @@
},
{
"cell_type": "markdown",
"id": "intimate-deficit",
"id": "competitive-semiconductor",
"metadata": {},
"source": [
"Nous avons donc 10 classes dans le dataset qui sont : air_conditioner', 'car_horn', 'children_playing', 'dog_bark','drilling', 'engine_idling', 'gun_shot', 'jackhammer', 'siren','street_music'\n"
......@@ -287,7 +287,7 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "pleased-mother",
"id": "congressional-lancaster",
"metadata": {},
"outputs": [
{
......@@ -317,7 +317,7 @@
},
{
"cell_type": "markdown",
"id": "adolescent-optimum",
"id": "separated-drilling",
"metadata": {},
"source": [
"On remarque donc que les classes sont équilibrées sauf pour car_horn et gun_shot qui beaucoup moins d'échantillons. Nous pourrons donc regarder par la suite si ces 2 classes sont moins bien classifiées. "
......@@ -326,7 +326,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "therapeutic-classroom",
"id": "handy-bankruptcy",
"metadata": {},
"outputs": [
{
......@@ -461,7 +461,7 @@
{
"cell_type": "code",
"execution_count": 8,
"id": "personalized-convenience",
"id": "ceramic-depth",
"metadata": {},
"outputs": [],
"source": [
......@@ -500,7 +500,7 @@
{
"cell_type": "code",
"execution_count": 9,
"id": "significant-proposition",
"id": "infrared-block",
"metadata": {},
"outputs": [
{
......@@ -540,7 +540,7 @@
{
"cell_type": "code",
"execution_count": 10,
"id": "renewable-movement",
"id": "victorian-filter",
"metadata": {},
"outputs": [],
"source": [
......@@ -554,7 +554,7 @@
{
"cell_type": "code",
"execution_count": 11,
"id": "hungry-fields",
"id": "pediatric-interest",
"metadata": {},
"outputs": [],
"source": [
......@@ -564,7 +564,7 @@
{
"cell_type": "code",
"execution_count": 12,
"id": "incomplete-jacob",
"id": "unknown-basement",
"metadata": {},
"outputs": [
{
......@@ -597,7 +597,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "extensive-technician",
"id": "convenient-royal",
"metadata": {},
"outputs": [],
"source": [
......@@ -607,7 +607,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "detailed-parish",
"id": "trying-somalia",
"metadata": {},
"outputs": [],
"source": [
......@@ -648,7 +648,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "female-reason",
"id": "african-forum",
"metadata": {},
"outputs": [
{
......@@ -698,7 +698,7 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "worth-ultimate",
"id": "dietary-wagon",
"metadata": {},
"outputs": [],
"source": [
......@@ -721,7 +721,7 @@
{
"cell_type": "code",
"execution_count": 14,
"id": "otherwise-patrol",
"id": "reverse-shoulder",
"metadata": {},
"outputs": [],
"source": [
......@@ -755,7 +755,7 @@
{
"cell_type": "code",
"execution_count": 15,
"id": "settled-payroll",
"id": "sticky-secondary",
"metadata": {},
"outputs": [],
"source": [
......@@ -792,7 +792,7 @@
{
"cell_type": "code",
"execution_count": 16,
"id": "changing-african",
"id": "suited-cologne",
"metadata": {},