{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1.  **Data Exploration**","metadata":{}},{"cell_type":"code","source":"import IPython.display as ipd\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:25:38.368857Z","iopub.execute_input":"2023-05-10T10:25:38.369307Z","iopub.status.idle":"2023-05-10T10:25:38.415092Z","shell.execute_reply.started":"2023-05-10T10:25:38.369267Z","shell.execute_reply":"2023-05-10T10:25:38.413706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold5/100032-3-0-0.wav'  #dog barking\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:25:41.913810Z","iopub.execute_input":"2023-05-10T10:25:41.914270Z","iopub.status.idle":"2023-05-10T10:25:55.126378Z","shell.execute_reply.started":"2023-05-10T10:25:41.914230Z","shell.execute_reply":"2023-05-10T10:25:55.124799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold5/100263-2-0-117.wav'   #children playing\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:25:59.170330Z","iopub.execute_input":"2023-05-10T10:25:59.170971Z","iopub.status.idle":"2023-05-10T10:25:59.806593Z","shell.execute_reply.started":"2023-05-10T10:25:59.170922Z","shell.execute_reply":"2023-05-10T10:25:59.805192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold10/100648-1-0-0.wav'  #car horn\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:11.648590Z","iopub.execute_input":"2023-05-10T10:26:11.649378Z","iopub.status.idle":"2023-05-10T10:26:12.355903Z","shell.execute_reply.started":"2023-05-10T10:26:11.649324Z","shell.execute_reply":"2023-05-10T10:26:12.354925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold5/100852-0-0-0.wav'  #air conditioner\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:15.453054Z","iopub.execute_input":"2023-05-10T10:26:15.454074Z","iopub.status.idle":"2023-05-10T10:26:16.161809Z","shell.execute_reply.started":"2023-05-10T10:26:15.454013Z","shell.execute_reply":"2023-05-10T10:26:16.160809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold7/101848-9-0-3.wav'  #street music\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:19.976374Z","iopub.execute_input":"2023-05-10T10:26:19.976795Z","iopub.status.idle":"2023-05-10T10:26:20.679752Z","shell.execute_reply.started":"2023-05-10T10:26:19.976758Z","shell.execute_reply":"2023-05-10T10:26:20.678444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold7/102853-8-0-0.wav'  #siren\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:24.343365Z","iopub.execute_input":"2023-05-10T10:26:24.343774Z","iopub.status.idle":"2023-05-10T10:26:25.013055Z","shell.execute_reply.started":"2023-05-10T10:26:24.343738Z","shell.execute_reply":"2023-05-10T10:26:25.011668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold10/102857-5-0-0.wav'     # engine idling\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:28.439389Z","iopub.execute_input":"2023-05-10T10:26:28.439863Z","iopub.status.idle":"2023-05-10T10:26:29.126054Z","shell.execute_reply.started":"2023-05-10T10:26:28.439819Z","shell.execute_reply":"2023-05-10T10:26:29.124850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold1/103074-7-0-0.wav'  #jack hammer\nplt.figure(figsize=(12,4))\ndata,sample_rate = librosa.load(filename)\n_ = librosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:32.703665Z","iopub.execute_input":"2023-05-10T10:26:32.704107Z","iopub.status.idle":"2023-05-10T10:26:33.647353Z","shell.execute_reply.started":"2023-05-10T10:26:32.704069Z","shell.execute_reply":"2023-05-10T10:26:33.645885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:35:09.416926Z","iopub.execute_input":"2023-05-04T14:35:09.417360Z","iopub.status.idle":"2023-05-04T14:35:09.423155Z","shell.execute_reply.started":"2023-05-04T14:35:09.417324Z","shell.execute_reply":"2023-05-04T14:35:09.421705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/urbansound8k/UrbanSound8K.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:35:11.813649Z","iopub.execute_input":"2023-05-04T14:35:11.814215Z","iopub.status.idle":"2023-05-04T14:35:11.863231Z","shell.execute_reply.started":"2023-05-04T14:35:11.814165Z","shell.execute_reply":"2023-05-04T14:35:11.861803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:35:14.370601Z","iopub.execute_input":"2023-05-04T14:35:14.371526Z","iopub.status.idle":"2023-05-04T14:35:14.414133Z","shell.execute_reply.started":"2023-05-04T14:35:14.371467Z","shell.execute_reply":"2023-05-04T14:35:14.413027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:35:16.909366Z","iopub.execute_input":"2023-05-04T14:35:16.910061Z","iopub.status.idle":"2023-05-04T14:35:16.919670Z","shell.execute_reply.started":"2023-05-04T14:35:16.910022Z","shell.execute_reply":"2023-05-04T14:35:16.918573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport librosa\nimport librosa.display\n\n#from helpers.wavfilehelper import WavFileHelper\nwavfilehelper = WavFileHelper()\n\naudiodata = []\nfor index, row in data.iterrows():\n    \n    file_name = os.path.join(os.path.abspath('/kaggle/input/urbansound8k/'),'fold'+str(row[\"fold\"])+'/',str(row[\"slice_file_name\"]))\n    data = wavfilehelper.read_file_properties(file_name)\n    audiodata.append(data)\n\n# Convert into a Panda dataframe\naudiodf = pd.DataFrame(audiodata, columns=['num_channels','sample_rate','bit_depth'])","metadata":{"execution":{"iopub.status.busy":"2023-05-04T14:35:19.354791Z","iopub.execute_input":"2023-05-04T14:35:19.355300Z","iopub.status.idle":"2023-05-04T14:38:10.324464Z","shell.execute_reply.started":"2023-05-04T14:35:19.355258Z","shell.execute_reply":"2023-05-04T14:38:10.322964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(audiodf.num_channels.value_counts(normalize=True))","metadata":{"execution":{"iopub.status.busy":"2023-05-03T17:38:04.493503Z","iopub.execute_input":"2023-05-03T17:38:04.494066Z","iopub.status.idle":"2023-05-03T17:38:04.502272Z","shell.execute_reply.started":"2023-05-03T17:38:04.494013Z","shell.execute_reply":"2023-05-03T17:38:04.501026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(audiodf.sample_rate.value_counts(normalize=True))","metadata":{"execution":{"iopub.status.busy":"2023-05-03T17:38:07.766463Z","iopub.execute_input":"2023-05-03T17:38:07.766852Z","iopub.status.idle":"2023-05-03T17:38:07.775503Z","shell.execute_reply.started":"2023-05-03T17:38:07.766818Z","shell.execute_reply":"2023-05-03T17:38:07.773970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(audiodf.bit_depth.value_counts(normalize=True))","metadata":{"execution":{"iopub.status.busy":"2023-05-03T17:38:12.559294Z","iopub.execute_input":"2023-05-03T17:38:12.559825Z","iopub.status.idle":"2023-05-03T17:38:12.567555Z","shell.execute_reply.started":"2023-05-03T17:38:12.559771Z","shell.execute_reply":"2023-05-03T17:38:12.566292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa \nfrom scipy.io import wavfile as wav\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:48.720570Z","iopub.execute_input":"2023-05-10T10:26:48.721049Z","iopub.status.idle":"2023-05-10T10:26:48.777546Z","shell.execute_reply.started":"2023-05-10T10:26:48.721008Z","shell.execute_reply":"2023-05-10T10:26:48.776116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/urbansound8k/fold1/103074-7-0-0.wav' \n\nlibrosa_audio, librosa_sample_rate = librosa.load(filename) \nscipy_sample_rate, scipy_audio = wav.read(filename) \n\nprint('Original sample rate:', scipy_sample_rate) \nprint('Librosa sample rate:', librosa_sample_rate) ","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:52.086102Z","iopub.execute_input":"2023-05-10T10:26:52.087150Z","iopub.status.idle":"2023-05-10T10:26:52.112494Z","shell.execute_reply.started":"2023-05-10T10:26:52.087101Z","shell.execute_reply":"2023-05-10T10:26:52.111068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Original audio file min~max range:', np.min(scipy_audio), 'to', np.max(scipy_audio))\nprint('Librosa audio file min~max range:', np.min(librosa_audio), 'to', np.max(librosa_audio))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:55.170463Z","iopub.execute_input":"2023-05-10T10:26:55.170914Z","iopub.status.idle":"2023-05-10T10:26:55.180029Z","shell.execute_reply.started":"2023-05-10T10:26:55.170858Z","shell.execute_reply":"2023-05-10T10:26:55.178652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(12, 4))\nplt.plot(scipy_audio)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:57.959969Z","iopub.execute_input":"2023-05-10T10:26:57.960670Z","iopub.status.idle":"2023-05-10T10:26:59.024411Z","shell.execute_reply.started":"2023-05-10T10:26:57.960626Z","shell.execute_reply":"2023-05-10T10:26:59.023101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.plot(librosa_audio)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:27:01.746988Z","iopub.execute_input":"2023-05-10T10:27:01.747455Z","iopub.status.idle":"2023-05-10T10:27:02.388477Z","shell.execute_reply.started":"2023-05-10T10:27:01.747416Z","shell.execute_reply":"2023-05-10T10:27:02.387039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfccs = librosa.feature.mfcc(y=librosa_audio, sr=librosa_sample_rate, n_mfcc=40)\nprint(mfccs.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:27:05.240270Z","iopub.execute_input":"2023-05-10T10:27:05.240718Z","iopub.status.idle":"2023-05-10T10:27:06.502844Z","shell.execute_reply.started":"2023-05-10T10:27:05.240676Z","shell.execute_reply":"2023-05-10T10:27:06.500947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa.display\nlibrosa.display.specshow(mfccs, sr=librosa_sample_rate, x_axis='time')","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:27:08.507720Z","iopub.execute_input":"2023-05-10T10:27:08.508173Z","iopub.status.idle":"2023-05-10T10:27:08.678058Z","shell.execute_reply.started":"2023-05-10T10:27:08.508131Z","shell.execute_reply":"2023-05-10T10:27:08.676381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfccs","metadata":{"execution":{"iopub.status.busy":"2023-05-03T17:38:42.420143Z","iopub.execute_input":"2023-05-03T17:38:42.420574Z","iopub.status.idle":"2023-05-03T17:38:42.429023Z","shell.execute_reply.started":"2023-05-03T17:38:42.420536Z","shell.execute_reply":"2023-05-03T17:38:42.427517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2 **Extracting Features from Audio**","metadata":{}},{"cell_type":"code","source":"class_labels = {0:\"air_conditioner\", 1:\"car_horn\", 2:\"children_playing\", 3:\"dog_bark\", 4:\"drilling\", \n                5:\"enginge_idling\", 6:\"gun_shot\", 7:\"jackhammer\", 8:\"siren\", 9:\"street_music\"}","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:13:42.117481Z","iopub.execute_input":"2023-05-10T11:13:42.118059Z","iopub.status.idle":"2023-05-10T11:13:42.124422Z","shell.execute_reply.started":"2023-05-10T11:13:42.118000Z","shell.execute_reply":"2023-05-10T11:13:42.122942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport time\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report,accuracy_score,average_precision_score,f1_score\n\nimport librosa\nimport IPython.display as ipd","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:13:44.855386Z","iopub.execute_input":"2023-05-10T11:13:44.856689Z","iopub.status.idle":"2023-05-10T11:13:44.864275Z","shell.execute_reply.started":"2023-05-10T11:13:44.856627Z","shell.execute_reply":"2023-05-10T11:13:44.862938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s, sr = librosa.load(\"../input/urbansound8k/fold1/101415-3-0-2.wav\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:13:47.969853Z","iopub.execute_input":"2023-05-10T11:13:47.970340Z","iopub.status.idle":"2023-05-10T11:13:47.996725Z","shell.execute_reply.started":"2023-05-10T11:13:47.970298Z","shell.execute_reply":"2023-05-10T11:13:47.995635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(\"../input/urbansound8k/fold1/101415-3-0-2.wav\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:13:50.559453Z","iopub.execute_input":"2023-05-10T11:13:50.559929Z","iopub.status.idle":"2023-05-10T11:13:50.579266Z","shell.execute_reply.started":"2023-05-10T11:13:50.559871Z","shell.execute_reply":"2023-05-10T11:13:50.577942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/urbansound8k/UrbanSound8K.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:13:53.687469Z","iopub.execute_input":"2023-05-10T11:13:53.688995Z","iopub.status.idle":"2023-05-10T11:13:53.739112Z","shell.execute_reply.started":"2023-05-10T11:13:53.688923Z","shell.execute_reply":"2023-05-10T11:13:53.737761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = \"../input/urbansound8k\"\n\nfolds = os.listdir(folder_path)\nprint(folds)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:13:56.457784Z","iopub.execute_input":"2023-05-10T11:13:56.458265Z","iopub.status.idle":"2023-05-10T11:13:56.467844Z","shell.execute_reply.started":"2023-05-10T11:13:56.458224Z","shell.execute_reply":"2023-05-10T11:13:56.466421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold_paths = [os.path.join(folder_path, i) for i in folds]\nfold_paths","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:13:59.111089Z","iopub.execute_input":"2023-05-10T11:13:59.112323Z","iopub.status.idle":"2023-05-10T11:13:59.121549Z","shell.execute_reply.started":"2023-05-10T11:13:59.112267Z","shell.execute_reply":"2023-05-10T11:13:59.119793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\n# Shuffling the folder paths inorder to aviod biased loeading of data\nrandom.shuffle(fold_paths)\nfold_paths","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:14:04.459186Z","iopub.execute_input":"2023-05-10T11:14:04.459643Z","iopub.status.idle":"2023-05-10T11:14:04.469909Z","shell.execute_reply.started":"2023-05-10T11:14:04.459601Z","shell.execute_reply":"2023-05-10T11:14:04.468431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = time.time()\ntrain_data_paths = []\n\nfor i in range(9):\n    \n    curr_folder_path = fold_paths[i]\n    \n    if (curr_folder_path == \"../input/urbansound8k/UrbanSound8K.csv\"):\n        continue\n    \n    curr_files = os.listdir(curr_folder_path)\n    \n    file_paths = [os.path.join(curr_folder_path, file) for file in curr_files]\n    \n    for j in range(len(file_paths)):\n        curr_file = curr_files[j]\n        class_id = df[df['slice_file_name'] == curr_file]\n        arr = np.array(class_id['classID'])\n\n        train_data_paths.append([file_paths[j], arr[0]])\n        \n        \ne = time.time()\nprint((e-s)/60, \" mins\")\nprint(len(train_data_paths))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:14:08.709045Z","iopub.execute_input":"2023-05-10T11:14:08.709473Z","iopub.status.idle":"2023-05-10T11:14:16.421174Z","shell.execute_reply.started":"2023-05-10T11:14:08.709436Z","shell.execute_reply":"2023-05-10T11:14:16.419668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = time.time()\n\ntest_data_paths = []\nfor i in range(9,11):\n    \n    curr_folder_path = fold_paths[i]\n    \n    if (curr_folder_path == \"../input/urbansound8k/UrbanSound8K.csv\"):\n        continue\n    \n    curr_files = os.listdir(curr_folder_path)\n    \n    file_paths = [os.path.join(curr_folder_path, file) for file in curr_files]\n    \n    for j in range(len(file_paths)):\n        curr_file = curr_files[j]\n        class_id = df[df['slice_file_name'] == curr_file]\n        arr = np.array(class_id['classID'])\n\n        test_data_paths.append([file_paths[j], arr[0]])\n        \n      \ne = time.time()\nprint((e-s)/60, \" mins\")\nprint(len(test_data_paths))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:14:19.375082Z","iopub.execute_input":"2023-05-10T11:14:19.375507Z","iopub.status.idle":"2023-05-10T11:14:21.619484Z","shell.execute_reply.started":"2023-05-10T11:14:19.375469Z","shell.execute_reply":"2023-05-10T11:14:21.617823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features1 = {\"ae_mean\", \"ae_var\", \"rms_mean\", \"rms_var\", \"zcr_mean\", \"zcr_var\", \"chroma_stft_mean\", \n            \"chroma_stft_var\", \"spec_centroid_mean\", \"spec_centroid_var\", \"spec_cont_mean\", \"spec_cont_var\",\n            \"spec_bw_mean\", \"spec_bw,var\",\"percep_mean\", \"percep_var\", \"tempo_mean\", \"tempo_var\", \n            \"roll_off_mean\", \"roll_off_var\", \"roll_off50_mean\",\"roll_off50_var\",\"roll_off25_mean\",\"roll_off25_var\",\n            \"log_mel_mean\", \"log_mel_var\", \"mfcc_mean\", \"mfcc_var\", \"spec_mean\", \"spec_var\"}","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:16:42.584567Z","iopub.execute_input":"2023-05-10T11:16:42.585119Z","iopub.status.idle":"2023-05-10T11:16:42.591966Z","shell.execute_reply.started":"2023-05-10T11:16:42.585073Z","shell.execute_reply":"2023-05-10T11:16:42.590613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(features1)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:16:44.887898Z","iopub.execute_input":"2023-05-10T11:16:44.888315Z","iopub.status.idle":"2023-05-10T11:16:44.895985Z","shell.execute_reply.started":"2023-05-10T11:16:44.888277Z","shell.execute_reply":"2023-05-10T11:16:44.894682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_paths = []\n\ns = time.time()\n\nfor i in range(11):\n    \n    curr_folder_path = fold_paths[i]\n    \n    if (curr_folder_path == \"../input/urbansound8k/UrbanSound8K.csv\"):\n        continue\n    \n    curr_files = os.listdir(curr_folder_path)\n    \n    file_paths = [os.path.join(curr_folder_path, file) for file in curr_files]\n    \n    for j in range(len(file_paths)):\n        curr_file = curr_files[j]\n        class_id = df[df['slice_file_name'] == curr_file]\n        arr = np.array(class_id['classID'])\n\n        data_paths.append([file_paths[j], arr[0]])","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:17:17.830060Z","iopub.execute_input":"2023-05-10T11:17:17.830539Z","iopub.status.idle":"2023-05-10T11:17:27.601775Z","shell.execute_reply.started":"2023-05-10T11:17:17.830493Z","shell.execute_reply":"2023-05-10T11:17:27.600716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(data_paths))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:17:50.681158Z","iopub.execute_input":"2023-05-10T11:17:50.681561Z","iopub.status.idle":"2023-05-10T11:17:50.687745Z","shell.execute_reply.started":"2023-05-10T11:17:50.681524Z","shell.execute_reply":"2023-05-10T11:17:50.686526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_list = {\"ae_mean\":[], \"ae_var\":[], \"rms_mean\":[], \"rms_var\":[], \"zcr_mean\":[], \"zcr_var\":[], \"chroma_stft_mean\":[], \n            \"chroma_stft_var\":[], \"spec_centroid_mean\":[], \"spec_centroid_var\":[], \"spec_cont_mean\":[], \"spec_cont_var\":[],\n            \"spec_bw_mean\":[], \"spec_bw_var\":[],\"percep_mean\":[], \"percep_var\":[], \"tempo_mean\":[], \"tempo_var\":[], \n            \"roll_off_mean\":[], \"roll_off_var\":[], \"roll_off50_mean\":[],\"roll_off50_var\":[],\"roll_off25_mean\":[],\"roll_off25_var\":[],\n            \"log_mel_mean\":[], \"log_mel_var\":[], \"mfcc_mean\":[], \"mfcc_var\":[],\"spec_mean\":[], \"spec_var\":[], \n            \"mag_spec_mean\" :[] ,\"mag_spec_var\":[], \"mel_mean\":[], \"mel_var\":[]}","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:17:53.336570Z","iopub.execute_input":"2023-05-10T11:17:53.337016Z","iopub.status.idle":"2023-05-10T11:17:53.345198Z","shell.execute_reply.started":"2023-05-10T11:17:53.336976Z","shell.execute_reply":"2023-05-10T11:17:53.343800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\nimport IPython.display as ipd\n\nFRAME_LENGTH = 2048\nHOP_LENGTH = 512\nFRAME_SIZE = 2048\nHOP_SIZE = 512\n\n\ndef amplitude_envelope(signal, frame_size = 2048, hop_length = 512):\n    amplitude_envelope = []\n        \n    for i in range(0, len(signal), hop_length):\n        current_frame_amplitude_envelope = max(signal[i:i+frame_size])\n        amplitude_envelope.append(current_frame_amplitude_envelope)\n        \n    return np.array(amplitude_envelope)\n\ndef Rms(song):\n    rms_song = librosa.feature.rms(y = song, frame_length=FRAME_LENGTH, hop_length=HOP_LENGTH)\n    return rms_song\ndef Zcr(song):\n    zcr_song = librosa.feature.zero_crossing_rate(y = song, frame_length=FRAME_LENGTH, hop_length=HOP_LENGTH)\n    return zcr_song\n\ndef Mag_spec(song):\n    signal_ft = np.fft.fft(song)\n    magnitude_spectrum = np.abs(signal_ft)\n    return magnitude_spectrum\n    \ndef spectrogram(song):\n    song_stft = librosa.stft(song, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)\n    y_song = np.abs(song_stft)**2\n    return y_song\n\ndef log_spec(song):    \n    spec_song = spectrogram(song)\n    y_song_log = librosa.power_to_db(spec_song)\n\ndef log_mel(song, samp_rate):\n    mel_spectrogram = librosa.feature.melspectrogram(y = song, n_fft= 2048, sr = samp_rate, hop_length = 512 ,n_mels=50)\n    log_mel_spectrogram = librosa.power_to_db(mel_spectrogram)\n    return log_mel_spectrogram\n\ndef Mfcc(song, samp_rate, nmfcc = 13):\n    mfccs = librosa.feature.mfcc(y = song, n_mfcc= nmfcc, sr = samp_rate)\n    return mfccs\n\ndef delta_mfcc(song, samp_rate, nmfcc = 13):\n    mfccs = Mfcc(song,samp_rate)\n    delta_mfcc = librosa.feature.delta(mfccs)\n    delta2_mfcc = librosa.feature.delta(mfccs, order=2)\n    delta3_mfcc = librosa.feature.delta(mfccs, order=3)\n    delta4_mfcc = librosa.feature.delta(mfccs, order=4)\n    delta5_mfcc = librosa.feature.delta(mfccs, order=5)\n    delta6_mfcc = librosa.feature.delta(mfccs, order=6)\n\n    return (delta_mfcc, delta2_mfcc, delta3_mfcc, delta4_mfcc, delta5_mfcc, delta6_mfcc)\ndef Chroma_stft(song, samp_rate):  \n    c_stft = librosa.feature.chroma_stft(y = song,sr = samp_rate)\n    return c_stft\n\ndef Spec_centriod(song, samp_rate):\n    return librosa.feature.spectral_centroid(y = song,sr = samp_rate)\n\n# spectral rolloff is the frequency below which a specified percentage of the total spectral energy, e.g. 85%, lies.\ndef spec_roll_off(song, samp_rate):\n    return librosa.feature.spectral_rolloff(y = song, sr =samp_rate)\n\ndef spec_roll_off50(song, samp_rate):\n    return librosa.feature.spectral_rolloff(y = song, sr= samp_rate, roll_percent=0.5)\n\ndef spec_roll_off25(song, samp_rate):\n    return librosa.feature.spectral_rolloff(y = song,sr = samp_rate, roll_percent=0.25)\n\ndef spec_contrast(song, samp_rate):\n    return librosa.feature.spectral_contrast(y = song,sr = samp_rate)\n\ndef tempogram(song, samp_rate):\n    return librosa.feature.tempogram(y = song, sr = samp_rate)\n\ndef spec_bandwidth(song, samp_rate):\n    return librosa.feature.spectral_bandwidth(y = song, sr = samp_rate)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:17:57.217168Z","iopub.execute_input":"2023-05-10T11:17:57.217609Z","iopub.status.idle":"2023-05-10T11:17:57.238647Z","shell.execute_reply.started":"2023-05-10T11:17:57.217570Z","shell.execute_reply":"2023-05-10T11:17:57.237498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = time.time()\n\nfor i in range(len(data_paths)):\n    \n    sample , sr = librosa.load(data_paths[i][0])\n    \n    \n    ae = amplitude_envelope(sample, frame_size = 2048, hop_length = 512)\n    ae_m, ae_v = ae.mean(), ae.var()\n    features_list[\"ae_mean\"].append(ae_m)\n    features_list[\"ae_var\"].append(ae_v)\n    \n    \n    rms = Rms(sample)\n    rms_m, rms_v = rms.mean(), rms.var()\n    features_list[\"rms_mean\"].append(rms_m)\n    features_list[\"rms_var\"].append(rms_v)\n    \n    zcr =  Zcr(sample)\n    zcr_m, zcr_v = zcr.mean(), zcr.var()\n    features_list[\"zcr_mean\"].append(zcr_m)\n    features_list['zcr_var'].append(zcr_v)\n    mag_spec = Mag_spec(sample)\n    features_list[\"mag_spec_mean\"].append(mag_spec.mean())\n    features_list[\"mag_spec_var\"].append(mag_spec.var())\n    \n    \n    spec = spectrogram(sample)\n    features_list[\"spec_mean\"].append(spec.mean())\n    features_list['spec_var'].append(spec.var())\n    \n    \n    mel_spec = log_mel(sample, sr)\n    features_list[\"mel_mean\"].append(mel_spec.mean())\n    features_list[\"mel_var\"].append(mel_spec.var())\n    \n    \n    mfcc = Mfcc(sample, sr)\n    features_list[\"mfcc_mean\"].append(mfcc.mean())\n    features_list[\"mfcc_var\"].append(mfcc.var())\n    chroma_stft = Chroma_stft(sample, sr)\n    features_list[\"chroma_stft_mean\"].append(chroma_stft.mean())\n    features_list['chroma_stft_var'].append(chroma_stft.var())\n    \n    spec_centriod = Spec_centriod(sample, sr)\n    features_list['spec_centroid_mean'].append(spec_centriod.mean())\n    features_list['spec_centroid_var'].append(spec_centriod.var())\n    \n    \n    spec_roll = spec_roll_off(sample, sr)\n    features_list[\"roll_off_mean\"].append(spec_roll.mean())\n    features_list['roll_off_var'].append(spec_roll.var())\n    \n    \n    spec_roll50 = spec_roll_off50(sample, sr)\n    features_list[\"roll_off50_mean\"].append(spec_roll50.mean())\n    features_list['roll_off50_var'].append(spec_roll50.var())\n    \n    spec_roll25 =  spec_roll_off25(sample, sr)\n    features_list[\"roll_off25_mean\"].append(spec_roll25.mean())\n    features_list['roll_off25_var'].append(spec_roll25.var())\n    \n    \n    spec_contr =  spec_contrast(sample, sr)\n    features_list[\"spec_cont_mean\"].append(spec_contr.mean())\n    features_list['spec_cont_var'].append(spec_contr.var())\n    tempo =  tempogram(sample, sr)\n    features_list[\"tempo_mean\"].append(tempo.mean())\n    features_list[\"tempo_var\"].append(tempo.var())\n    \n    \n    spec_bw =  spec_bandwidth(sample, sr)\n    features_list[\"spec_bw_mean\"].append(spec_bw.mean())\n    features_list['spec_bw_var'].append(spec_bw.var())\n    \n    log_me = log_mel(sample, sr)\n    features_list[\"log_mel_mean\"].append(log_me.mean())\n    features_list['log_mel_var'].append(log_me.var())\n    \ne = time.time()\nprint((e - s)/60  , \"mins\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T11:18:41.122078Z","iopub.execute_input":"2023-05-10T11:18:41.122704Z","iopub.status.idle":"2023-05-10T11:58:42.508541Z","shell.execute_reply.started":"2023-05-10T11:18:41.122644Z","shell.execute_reply":"2023-05-10T11:58:42.506451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(features_list))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:09:01.483076Z","iopub.execute_input":"2023-05-10T12:09:01.484077Z","iopub.status.idle":"2023-05-10T12:09:01.492509Z","shell.execute_reply.started":"2023-05-10T12:09:01.484022Z","shell.execute_reply":"2023-05-10T12:09:01.490727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keys = features_list.keys()\n\nfor key in keys:\n    print(len(features_list[key]), key)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:09:05.923862Z","iopub.execute_input":"2023-05-10T12:09:05.924335Z","iopub.status.idle":"2023-05-10T12:09:05.940052Z","shell.execute_reply.started":"2023-05-10T12:09:05.924293Z","shell.execute_reply":"2023-05-10T12:09:05.938930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del features_list[\"percep_mean\"]\ndel features_list[\"percep_var\"]","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:09:49.824559Z","iopub.execute_input":"2023-05-10T12:09:49.825078Z","iopub.status.idle":"2023-05-10T12:09:49.831466Z","shell.execute_reply.started":"2023-05-10T12:09:49.825027Z","shell.execute_reply":"2023-05-10T12:09:49.830181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_df = pd.DataFrame(features_list)\nfeature_df","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:09:52.676126Z","iopub.execute_input":"2023-05-10T12:09:52.676575Z","iopub.status.idle":"2023-05-10T12:09:52.812844Z","shell.execute_reply.started":"2023-05-10T12:09:52.676533Z","shell.execute_reply":"2023-05-10T12:09:52.811540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = feature_df.copy()\ndf1","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:10:04.658632Z","iopub.execute_input":"2023-05-10T12:10:04.659859Z","iopub.status.idle":"2023-05-10T12:10:04.697188Z","shell.execute_reply.started":"2023-05-10T12:10:04.659807Z","shell.execute_reply":"2023-05-10T12:10:04.695941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = [data_paths[i][0].split(\"/\")[4] for i in range(len(data_paths))]\nprint(len(file_names))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:10:08.261427Z","iopub.execute_input":"2023-05-10T12:10:08.261863Z","iopub.status.idle":"2023-05-10T12:10:08.276065Z","shell.execute_reply.started":"2023-05-10T12:10:08.261807Z","shell.execute_reply":"2023-05-10T12:10:08.274564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_index = [data_paths[i][1] for i in range(len(data_paths))]\nprint(len(class_index))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:10:11.415431Z","iopub.execute_input":"2023-05-10T12:10:11.416207Z","iopub.status.idle":"2023-05-10T12:10:11.425114Z","shell.execute_reply.started":"2023-05-10T12:10:11.416162Z","shell.execute_reply":"2023-05-10T12:10:11.423891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1[\"file_name\"] = file_names\ndf1","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:12:58.396493Z","iopub.execute_input":"2023-05-10T12:12:58.397069Z","iopub.status.idle":"2023-05-10T12:12:58.443089Z","shell.execute_reply.started":"2023-05-10T12:12:58.397017Z","shell.execute_reply":"2023-05-10T12:12:58.441555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1[\"class_id\"] = class_index\ndf1","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:10:13.814197Z","iopub.execute_input":"2023-05-10T12:10:13.815205Z","iopub.status.idle":"2023-05-10T12:10:13.857524Z","shell.execute_reply.started":"2023-05-10T12:10:13.815151Z","shell.execute_reply":"2023-05-10T12:10:13.856275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.to_csv('features_urban_sounds.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:13:58.604131Z","iopub.execute_input":"2023-05-10T12:13:58.604590Z","iopub.status.idle":"2023-05-10T12:13:58.984693Z","shell.execute_reply.started":"2023-05-10T12:13:58.604546Z","shell.execute_reply":"2023-05-10T12:13:58.983289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df1[\"class_id\"]\ny = np.array(y)\ny","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:14:01.344729Z","iopub.execute_input":"2023-05-10T12:14:01.345175Z","iopub.status.idle":"2023-05-10T12:14:01.352833Z","shell.execute_reply.started":"2023-05-10T12:14:01.345134Z","shell.execute_reply":"2023-05-10T12:14:01.351710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df1[\"file_name\"]\ndel df1[\"class_id\"]\n\nX = np.array(df1)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:14:50.366428Z","iopub.execute_input":"2023-05-10T12:14:50.367281Z","iopub.status.idle":"2023-05-10T12:14:50.377663Z","shell.execute_reply.started":"2023-05-10T12:14:50.367229Z","shell.execute_reply":"2023-05-10T12:14:50.376403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:14:56.213954Z","iopub.execute_input":"2023-05-10T12:14:56.214715Z","iopub.status.idle":"2023-05-10T12:14:56.221171Z","shell.execute_reply.started":"2023-05-10T12:14:56.214669Z","shell.execute_reply":"2023-05-10T12:14:56.219989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler, StandardScaler\n\nmmscale = MinMaxScaler()\n\nX_sc = mmscale.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:15:00.431260Z","iopub.execute_input":"2023-05-10T12:15:00.432434Z","iopub.status.idle":"2023-05-10T12:15:00.440289Z","shell.execute_reply.started":"2023-05-10T12:15:00.432381Z","shell.execute_reply":"2023-05-10T12:15:00.439069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(X_sc,y,random_state = 6, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:15:05.098755Z","iopub.execute_input":"2023-05-10T12:15:05.099667Z","iopub.status.idle":"2023-05-10T12:15:05.109939Z","shell.execute_reply.started":"2023-05-10T12:15:05.099618Z","shell.execute_reply":"2023-05-10T12:15:05.108123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape, x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:15:08.668576Z","iopub.execute_input":"2023-05-10T12:15:08.669901Z","iopub.status.idle":"2023-05-10T12:15:08.677255Z","shell.execute_reply.started":"2023-05-10T12:15:08.669817Z","shell.execute_reply":"2023-05-10T12:15:08.676247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3 **Data Visualization**","metadata":{}},{"cell_type":"code","source":"from sklearn import decomposition, ensemble, datasets, linear_model\n\npca = decomposition.PCA(n_components = 2)\nx_train_vis = pca.fit_transform(x_train)\n\nx_train_vis.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:15:19.588284Z","iopub.execute_input":"2023-05-10T12:15:19.588771Z","iopub.status.idle":"2023-05-10T12:15:19.638657Z","shell.execute_reply.started":"2023-05-10T12:15:19.588726Z","shell.execute_reply":"2023-05-10T12:15:19.637295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(12 ,6))\n\npc1 = x_train_vis[:,0]\npc2 = x_train_vis[:,1]\n\nplt.title('PCA on Urban sounds', fontsize = 20)\nplt.xlabel(\"Principal Component 1\", fontsize = 15)\nplt.ylabel(\"Principal Component 2\", fontsize = 15)\n\nprint(pc1.shape, pc2.shape)\nplt.scatter(pc1, pc2)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:15:22.754473Z","iopub.execute_input":"2023-05-10T12:15:22.754923Z","iopub.status.idle":"2023-05-10T12:15:23.040069Z","shell.execute_reply.started":"2023-05-10T12:15:22.754869Z","shell.execute_reply":"2023-05-10T12:15:23.039015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.manifold import TSNE\n\ntsne = TSNE(n_components=2, verbose=1, random_state=123)\nz = tsne.fit_transform(x_train) ","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:15:28.555281Z","iopub.execute_input":"2023-05-10T12:15:28.555701Z","iopub.status.idle":"2023-05-10T12:16:08.795751Z","shell.execute_reply.started":"2023-05-10T12:15:28.555664Z","shell.execute_reply":"2023-05-10T12:16:08.794449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:16:20.983270Z","iopub.execute_input":"2023-05-10T12:16:20.984378Z","iopub.status.idle":"2023-05-10T12:16:20.992193Z","shell.execute_reply.started":"2023-05-10T12:16:20.984327Z","shell.execute_reply":"2023-05-10T12:16:20.990917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_temp = pd.DataFrame()\ndf_temp[\"y\"] = y_train\ndf_temp[\"comp-1\"] = z[:,0]\ndf_temp[\"comp-2\"] = z[:,1]\n\nplt.figure(figsize=(15,8))\nsns.scatterplot(x=\"comp-1\", y=\"comp-2\", hue=df_temp.y.tolist(),\n                palette=sns.color_palette(\"hls\", 10),\n                data=df_temp).set(title=\"Audio data T-SNE projection\") \nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:16:25.576396Z","iopub.execute_input":"2023-05-10T12:16:25.576893Z","iopub.status.idle":"2023-05-10T12:16:26.669167Z","shell.execute_reply.started":"2023-05-10T12:16:25.576828Z","shell.execute_reply":"2023-05-10T12:16:26.668067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = df1.copy()\n\n# FIlling the class ids and file names to the respective audio files\n\ndf2[\"file_name\"] = file_names\ndf2[\"class_id\"] = class_index\n\ndf2.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:16:31.924295Z","iopub.execute_input":"2023-05-10T12:16:31.925093Z","iopub.status.idle":"2023-05-10T12:16:31.961955Z","shell.execute_reply.started":"2023-05-10T12:16:31.925029Z","shell.execute_reply":"2023-05-10T12:16:31.960706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = {0:\"air_conditioner\", 1:\"car_horn\", 2:\"children_playing\", 3:\"dog_bark\", 4:\"drilling\", 5:\"enginge_idling\", 6:\"gun_shot\", 7:\"jackhammer\", 8:\"siren\", 9:\"street_music\"}","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:16:38.228655Z","iopub.execute_input":"2023-05-10T12:16:38.229386Z","iopub.status.idle":"2023-05-10T12:16:38.235940Z","shell.execute_reply.started":"2023-05-10T12:16:38.229330Z","shell.execute_reply":"2023-05-10T12:16:38.234872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels.values()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:16:47.471556Z","iopub.execute_input":"2023-05-10T12:16:47.472050Z","iopub.status.idle":"2023-05-10T12:16:47.479515Z","shell.execute_reply.started":"2023-05-10T12:16:47.472005Z","shell.execute_reply":"2023-05-10T12:16:47.478481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def f(n):\n    return class_labels[n]\n\n\n# Filling the class name column as well\n\ndf2[\"class_name\"] = df2[\"class_id\"].apply(f)\ndf2.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:16:55.077300Z","iopub.execute_input":"2023-05-10T12:16:55.078088Z","iopub.status.idle":"2023-05-10T12:16:55.113242Z","shell.execute_reply.started":"2023-05-10T12:16:55.078041Z","shell.execute_reply":"2023-05-10T12:16:55.111936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data_paths = []\n    \ndp = np.array(data_paths)\ndp.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:16:58.677968Z","iopub.execute_input":"2023-05-10T12:16:58.678823Z","iopub.status.idle":"2023-05-10T12:16:58.710068Z","shell.execute_reply.started":"2023-05-10T12:16:58.678766Z","shell.execute_reply":"2023-05-10T12:16:58.708677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    temp = dp[dp[:,1] == str(i)]\n    sample_data_paths.append(temp[0][0])\n    \nsample_data_paths","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:17:01.701008Z","iopub.execute_input":"2023-05-10T12:17:01.701470Z","iopub.status.idle":"2023-05-10T12:17:01.717370Z","shell.execute_reply.started":"2023-05-10T12:17:01.701426Z","shell.execute_reply":"2023-05-10T12:17:01.716363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa.display\n\nplt.figure(figsize=(20,6))\n\nFRAME_LENGTH = 1024\nHOP_LENGTH = 512\n\nfor i in range(10):\n    \n    plt.subplot(2,5,i+1)\n    \n    s , sr = librosa.load(sample_data_paths[i])\n    plt.title(class_labels[i])\n    librosa.display.waveshow(s, alpha = 0.75)   \n    \n#     plt.show()\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:17:04.481582Z","iopub.execute_input":"2023-05-10T12:17:04.482716Z","iopub.status.idle":"2023-05-10T12:17:19.747749Z","shell.execute_reply.started":"2023-05-10T12:17:04.482654Z","shell.execute_reply":"2023-05-10T12:17:19.746569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa.display\n\nplt.figure(figsize=(20,5))\n\nFRAME_LENGTH = 1024\nHOP_LENGTH = 512\n\nfor i in range(10):\n    \n    plt.subplot(2,5,i+1)\n    \n    s , sr = librosa.load(sample_data_paths[i])\n    plt.title(class_labels[i])\n    plt.specgram(s, cmap = \"plasma\")\n    \n    \nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:17:28.601117Z","iopub.execute_input":"2023-05-10T12:17:28.601939Z","iopub.status.idle":"2023-05-10T12:17:30.711508Z","shell.execute_reply.started":"2023-05-10T12:17:28.601856Z","shell.execute_reply":"2023-05-10T12:17:30.709997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(x_train,y_train,random_state = 6, test_size=0.15)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:17:35.973286Z","iopub.execute_input":"2023-05-10T12:17:35.974264Z","iopub.status.idle":"2023-05-10T12:17:35.983404Z","shell.execute_reply.started":"2023-05-10T12:17:35.974200Z","shell.execute_reply":"2023-05-10T12:17:35.981937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train)\ny_train = np.array(y_train)\n\nprint(x_train.shape, y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:17:38.902636Z","iopub.execute_input":"2023-05-10T12:17:38.903108Z","iopub.status.idle":"2023-05-10T12:17:38.909491Z","shell.execute_reply.started":"2023-05-10T12:17:38.903067Z","shell.execute_reply":"2023-05-10T12:17:38.908285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20,6))\nplt.title(\"ZCR Mean Boxplot\", fontsize= 20)\n\nsns.boxplot(y=\"zcr_mean\",\n            x=\"class_name\",\n            data=df2)\n\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:17:42.296658Z","iopub.execute_input":"2023-05-10T12:17:42.297142Z","iopub.status.idle":"2023-05-10T12:17:42.696687Z","shell.execute_reply.started":"2023-05-10T12:17:42.297090Z","shell.execute_reply":"2023-05-10T12:17:42.695291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20,10))\nplt.subplot(2,1,1)\nplt.title(\"MFCC mean box plot\", fontsize= 20)\nsns.boxplot(x = \"class_name\",\n            y = \"mfcc_mean\",\n#             hue = 'class_name',\n            data = df2,\n            dodge=0,)\n\nplt.show()\n\n\nfig = plt.figure(figsize=(20,10))\nplt.subplot(2,1,2)\nplt.title(\"MFCC varinace box plot\", fontsize= 20)\nsns.boxplot(x = \"class_name\",\n            y = \"mfcc_var\",\n            hue = 'class_name',\n            data = df2,\n            dodge=0,)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:17:50.216116Z","iopub.execute_input":"2023-05-10T12:17:50.216572Z","iopub.status.idle":"2023-05-10T12:17:51.294772Z","shell.execute_reply.started":"2023-05-10T12:17:50.216531Z","shell.execute_reply":"2023-05-10T12:17:51.293378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4 **Classification using ML Algorithm**","metadata":{}},{"cell_type":"code","source":"from sklearn import preprocessing\n\nfrom sklearn.linear_model import SGDClassifier, LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.svm import SVC\nfrom xgboost import XGBClassifier, XGBRFClassifier\n\nfrom sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:18:44.117168Z","iopub.execute_input":"2023-05-10T12:18:44.117648Z","iopub.status.idle":"2023-05-10T12:18:44.244126Z","shell.execute_reply.started":"2023-05-10T12:18:44.117603Z","shell.execute_reply":"2023-05-10T12:18:44.242835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_n = x_train\ny_train_n = y_train","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:18:57.658893Z","iopub.execute_input":"2023-05-10T12:18:57.660159Z","iopub.status.idle":"2023-05-10T12:18:57.664854Z","shell.execute_reply.started":"2023-05-10T12:18:57.660100Z","shell.execute_reply":"2023-05-10T12:18:57.663649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val.shape , y_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:19:01.588801Z","iopub.execute_input":"2023-05-10T12:19:01.589602Z","iopub.status.idle":"2023-05-10T12:19:01.596062Z","shell.execute_reply.started":"2023-05-10T12:19:01.589554Z","shell.execute_reply":"2023-05-10T12:19:01.595077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_n1 = list(x_train_n)  + list(x_val)\ny_train_n1 = list(y_train_n) + list(y_val)\n\nx_train_n1 = np.array(x_train_n1)\ny_train_n1 = np.array(y_train_n1)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:19:03.852160Z","iopub.execute_input":"2023-05-10T12:19:03.853731Z","iopub.status.idle":"2023-05-10T12:19:03.865994Z","shell.execute_reply.started":"2023-05-10T12:19:03.853676Z","shell.execute_reply":"2023-05-10T12:19:03.864781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_n1.shape , y_train_n1.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:19:21.705336Z","iopub.execute_input":"2023-05-10T12:19:21.706513Z","iopub.status.idle":"2023-05-10T12:19:21.714307Z","shell.execute_reply.started":"2023-05-10T12:19:21.706456Z","shell.execute_reply":"2023-05-10T12:19:21.713129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nfrom matplotlib import pyplot as plt\nfrom sklearn.model_selection import cross_val_score\n\ns = time.time()\nmax_score = 0\nmax_i = -1\nx = []\ny = []\nfor i in range(1, 50, 2):\n    clf = KNeighborsClassifier(n_neighbors=i)\n    score = cross_val_score(clf, x_train_n1, y_train_n1)\n    \n    if (score.mean() > max_score):\n        max_score = score.mean()\n        max_i = i\n        \n    x.append(i)\n    y.append(score.mean())\n    \nplt.plot(x, y, color = \"darkblue\")\nplt.show()\ne = time.time()\n\nprint((e-s)/60 , \"mins\")\nprint(max_i, max_score)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:20:52.418497Z","iopub.execute_input":"2023-05-10T12:20:52.418965Z","iopub.status.idle":"2023-05-10T12:21:23.001479Z","shell.execute_reply.started":"2023-05-10T12:20:52.418922Z","shell.execute_reply":"2023-05-10T12:21:22.999895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = KNeighborsClassifier(3)\nclf.fit(x_train_n1, y_train_n1)\nprint(\"Test socre using 3 nearest neighbours in KNN\")\nclf.score(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:21:27.423145Z","iopub.execute_input":"2023-05-10T12:21:27.424225Z","iopub.status.idle":"2023-05-10T12:21:27.768229Z","shell.execute_reply.started":"2023-05-10T12:21:27.424154Z","shell.execute_reply":"2023-05-10T12:21:27.766517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_knn = clf.predict(x_test)\n\nprint(classification_report(y_test, y_pred_knn))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:21:41.457372Z","iopub.execute_input":"2023-05-10T12:21:41.457843Z","iopub.status.idle":"2023-05-10T12:21:41.793696Z","shell.execute_reply.started":"2023-05-10T12:21:41.457801Z","shell.execute_reply":"2023-05-10T12:21:41.792144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\n\nconfusion_mat = confusion_matrix(y_test, y_pred_knn)\n\nplt.figure(figsize=(10,4))\nplt.xlabel(\"Predicted class\")\nplt.ylabel(\"True class\")\nsns.heatmap(confusion_mat, annot=True, cmap=\"Blues\", xticklabels = ['air_conditioner', 'car_horn', 'children_playing', 'dog_bark', 'drilling', 'enginge_idling', 'gun_shot', 'jackhammer', 'siren','street_music'],\n           yticklabels=['air_conditioner', 'car_horn', 'children_playing', 'dog_bark', 'drilling', 'enginge_idling', 'gun_shot', 'jackhammer', 'siren','street_music'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:21:45.509446Z","iopub.execute_input":"2023-05-10T12:21:45.509931Z","iopub.status.idle":"2023-05-10T12:21:46.268436Z","shell.execute_reply.started":"2023-05-10T12:21:45.509869Z","shell.execute_reply":"2023-05-10T12:21:46.267097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5 **CNN Model**","metadata":{}},{"cell_type":"code","source":"from keras.utils.vis_utils import plot_model","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:21:52.529809Z","iopub.execute_input":"2023-05-10T12:21:52.530644Z","iopub.status.idle":"2023-05-10T12:21:52.535861Z","shell.execute_reply.started":"2023-05-10T12:21:52.530592Z","shell.execute_reply":"2023-05-10T12:21:52.534618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\ny_train = to_categorical(y_train_n, num_classes = 10)\ny_test = to_categorical(y_test, num_classes =10)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:22:11.433105Z","iopub.execute_input":"2023-05-10T12:22:11.433557Z","iopub.status.idle":"2023-05-10T12:22:11.440845Z","shell.execute_reply.started":"2023-05-10T12:22:11.433515Z","shell.execute_reply":"2023-05-10T12:22:11.439152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val = to_categorical(y_val, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:25:31.166743Z","iopub.execute_input":"2023-05-10T12:25:31.168230Z","iopub.status.idle":"2023-05-10T12:25:31.175541Z","shell.execute_reply.started":"2023-05-10T12:25:31.168172Z","shell.execute_reply":"2023-05-10T12:25:31.173686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_n.shape, x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:26:00.942492Z","iopub.execute_input":"2023-05-10T12:26:00.942980Z","iopub.status.idle":"2023-05-10T12:26:00.951407Z","shell.execute_reply.started":"2023-05-10T12:26:00.942929Z","shell.execute_reply":"2023-05-10T12:26:00.949967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:26:05.509894Z","iopub.execute_input":"2023-05-10T12:26:05.510324Z","iopub.status.idle":"2023-05-10T12:26:05.518257Z","shell.execute_reply.started":"2023-05-10T12:26:05.510285Z","shell.execute_reply":"2023-05-10T12:26:05.516977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = x_train_n.shape[1]\nprint(input_shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:26:08.082477Z","iopub.execute_input":"2023-05-10T12:26:08.082971Z","iopub.status.idle":"2023-05-10T12:26:08.089816Z","shell.execute_reply.started":"2023-05-10T12:26:08.082923Z","shell.execute_reply":"2023-05-10T12:26:08.088377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport keras\nfrom keras import layers\nfrom keras.models import Sequential\n\nmodel = Sequential()\n\nmodel.add(keras.Input(shape=input_shape))\n\nmodel.add(layers.Dense(124, activation = \"tanh\"))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(64, activation = \"sigmoid\"))\n\nmodel.add(layers.Dense(units = 10, activation = \"softmax\"))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:26:11.692401Z","iopub.execute_input":"2023-05-10T12:26:11.692835Z","iopub.status.idle":"2023-05-10T12:26:11.766849Z","shell.execute_reply.started":"2023-05-10T12:26:11.692795Z","shell.execute_reply":"2023-05-10T12:26:11.765193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:26:16.475057Z","iopub.execute_input":"2023-05-10T12:26:16.475488Z","iopub.status.idle":"2023-05-10T12:26:16.490677Z","shell.execute_reply.started":"2023-05-10T12:26:16.475450Z","shell.execute_reply":"2023-05-10T12:26:16.489360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nbatch_size = 32\nepochs = 500\nes = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience = 3, min_delta=0.005)\n\ns = time.time()\nhistory = model.fit(x_train_n, y_train, batch_size=batch_size, epochs=epochs, validation_data= (x_val ,y_val))\n\ne = time.time()\nprint((e-s)/60, \"mins\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:26:19.846447Z","iopub.execute_input":"2023-05-10T12:26:19.846942Z","iopub.status.idle":"2023-05-10T12:30:23.508762Z","shell.execute_reply.started":"2023-05-10T12:26:19.846888Z","shell.execute_reply":"2023-05-10T12:30:23.507266Z"},"_kg_hide-output":true,"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig = plt.figure()\n\n# Loss curves\nplt.figure(figsize=[12,4])\nplt.plot(history.history['loss'],'red',linewidth=3.0)\nplt.plot(history.history['val_loss'],'orange',linewidth=3.0)\nplt.legend(['Training loss', 'Validation Loss'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Loss',fontsize=15)\nplt.title('Loss Curves',fontsize=15)\n\n\n# Accuracy Curves\nplt.figure(figsize=[12,4])\nplt.plot(history.history['accuracy'],'darkgreen',linewidth=3.0)\nplt.plot(history.history['val_accuracy'],'lightgreen',linewidth=3.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Accuracy',fontsize=15)\nplt.title('Accuracy Curves',fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:31:46.476379Z","iopub.execute_input":"2023-05-10T12:31:46.476922Z","iopub.status.idle":"2023-05-10T12:31:46.988185Z","shell.execute_reply.started":"2023-05-10T12:31:46.476864Z","shell.execute_reply":"2023-05-10T12:31:46.986726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:32:01.777347Z","iopub.execute_input":"2023-05-10T12:32:01.777817Z","iopub.status.idle":"2023-05-10T12:32:01.996302Z","shell.execute_reply.started":"2023-05-10T12:32:01.777773Z","shell.execute_reply":"2023-05-10T12:32:01.995208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = Sequential()        #Creating another model, having bit more complex architecture than before, but just a sequential one.\n\nmodel1.add(keras.Input(shape=input_shape))\nmodel1.add(layers.Dense(4096 ,activation=\"tanh\"))\nmodel1.add(layers.Dropout(0.35))\nmodel1.add(layers.Dense(2048, activation = \"tanh\"))\nmodel1.add(layers.Dropout(0.45))\nmodel1.add(layers.Dense(1024, activation = \"sigmoid\"))\nmodel1.add(layers.Dropout(0.5))\nmodel1.add(layers.Dense(512, activation = \"tanh\"))\nmodel1.add(layers.Dropout(0.5))\nmodel1.add(layers.Dense(64))\nmodel1.add(layers.Dense(units = 10, activation = \"softmax\"))\n\nmodel1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:32:53.472746Z","iopub.execute_input":"2023-05-10T12:32:53.473249Z","iopub.status.idle":"2023-05-10T12:32:53.748846Z","shell.execute_reply.started":"2023-05-10T12:32:53.473203Z","shell.execute_reply":"2023-05-10T12:32:53.742464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model1, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:33:26.436934Z","iopub.execute_input":"2023-05-10T12:33:26.437431Z","iopub.status.idle":"2023-05-10T12:33:26.835457Z","shell.execute_reply.started":"2023-05-10T12:33:26.437391Z","shell.execute_reply":"2023-05-10T12:33:26.833921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:33:41.448396Z","iopub.execute_input":"2023-05-10T12:33:41.448926Z","iopub.status.idle":"2023-05-10T12:33:41.471655Z","shell.execute_reply.started":"2023-05-10T12:33:41.448860Z","shell.execute_reply":"2023-05-10T12:33:41.470181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nbatch_size = 32\nepochs = 120\n\n# es = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience = 3, min_delta=0.01)\n\ns = time.time()\nhistory1 = model1.fit(x_train_n, y_train, batch_size=batch_size, epochs=epochs, validation_data= (x_val ,y_val))\n#                      ,callbacks = [es])\ne = time.time()\nprint((e-s)/60, \"mins\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T12:33:45.394781Z","iopub.execute_input":"2023-05-10T12:33:45.395288Z","iopub.status.idle":"2023-05-10T13:26:09.121399Z","shell.execute_reply.started":"2023-05-10T12:33:45.395242Z","shell.execute_reply":"2023-05-10T13:26:09.119960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Loss curves\nplt.figure(figsize=[12,4])\nplt.plot(history1.history['loss'],'red',linewidth=3.0)\nplt.plot(history1.history['val_loss'],'orange',linewidth=3.0)\nplt.legend(['Training loss', 'Validation Loss'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Loss',fontsize=15)\nplt.title('Loss Curves',fontsize=15)\n\n\n# Accuracy Curves\nplt.figure(figsize=[12,4])\nplt.plot(history1.history['accuracy'],'darkgreen',linewidth=3.0)\nplt.plot(history1.history['val_accuracy'],'lightgreen',linewidth=3.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Accuracy',fontsize=15)\nplt.title('Accuracy Curves',fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:33:43.067501Z","iopub.execute_input":"2023-05-10T13:33:43.068153Z","iopub.status.idle":"2023-05-10T13:33:43.597548Z","shell.execute_reply.started":"2023-05-10T13:33:43.068103Z","shell.execute_reply":"2023-05-10T13:33:43.595983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:34:14.299452Z","iopub.execute_input":"2023-05-10T13:34:14.300256Z","iopub.status.idle":"2023-05-10T13:34:15.369751Z","shell.execute_reply.started":"2023-05-10T13:34:14.300213Z","shell.execute_reply":"2023-05-10T13:34:15.368394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pred_int(y_pred):\n    y_pred_int = []\n    for i in range(len(y_pred)):\n        max_i = 0\n        max_p = 0\n        for j in range(len(y_pred[i])):\n            if y_pred[i][j] > max_p:\n                max_p = y_pred[i][j]\n                max_i = j\n                \n        y_pred_int.append(max_i)\n        \n    return y_pred_int","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:34:31.242867Z","iopub.execute_input":"2023-05-10T13:34:31.245762Z","iopub.status.idle":"2023-05-10T13:34:31.255370Z","shell.execute_reply.started":"2023-05-10T13:34:31.245660Z","shell.execute_reply":"2023-05-10T13:34:31.253952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_m1 = model1.predict(x_test)\n\n\ny_pred_m1 = pred_int(y_pred_m1)\ny_test1 = pred_int(y_test)\n\nconfusion_mat1 = confusion_matrix(y_test1, y_pred_m1)\n\nplt.figure(figsize=(10,4))\n\nplt.xlabel(\"Predicted class\")\nplt.ylabel(\"True class\")\n\nsns.heatmap(confusion_mat, annot=True, cmap=\"Greens\",  xticklabels = ['air_conditioner', 'car_horn', 'children_playing', 'dog_bark', 'drilling', 'enginge_idling', 'gun_shot', 'jackhammer', 'siren','street_music'],\n           yticklabels=['air_conditioner', 'car_horn', 'children_playing', 'dog_bark', 'drilling', 'enginge_idling', 'gun_shot', 'jackhammer', 'siren','street_music'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:34:42.971058Z","iopub.execute_input":"2023-05-10T13:34:42.971511Z","iopub.status.idle":"2023-05-10T13:34:44.934993Z","shell.execute_reply.started":"2023-05-10T13:34:42.971471Z","shell.execute_reply":"2023-05-10T13:34:44.933848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test1, y_pred_m1))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:35:00.245263Z","iopub.execute_input":"2023-05-10T13:35:00.245994Z","iopub.status.idle":"2023-05-10T13:35:00.260575Z","shell.execute_reply.started":"2023-05-10T13:35:00.245950Z","shell.execute_reply":"2023-05-10T13:35:00.259470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Model\nfrom tensorflow.keras.layers import Concatenate,Input,Dropout, Dense\n\n\ninput_to_nn = Input(shape = input_shape)\n\nhidden_1 = Dense(512, activation = \"tanh\")(input_to_nn)\nd1 = Dropout(0.45)(hidden_1)\nhidden_2 = Dense(128, activation = \"tanh\")(d1)\nd2 = Dropout(0.5)(hidden_2)\nhidden_3 = Dense(32, activation = \"tanh\")(d2)\n\nhidden_4 = Dense(256, activation = \"sigmoid\")(input_to_nn)\nd3 = Dropout(0.4)(hidden_4)\nhidden_5 = Dense(64, activation = \"sigmoid\")(d3)\nd4 = Dropout(0.45)(hidden_5)\nhidden_6 = Dense(16, activation = \"sigmoid\")(d4)\n\nconcat = Concatenate()([hidden_3, hidden_6])\n\ndense1 = Dense(512, activation = \"tanh\")(concat)\ndense2 = Dense(128, activation = \"relu\")(dense1)\noutput = Dense(10, activation = \"softmax\")(dense2)\nmodel2 = Model(inputs = [input_to_nn], outputs = [output])\n\nmodel2.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:35:21.569731Z","iopub.execute_input":"2023-05-10T13:35:21.570284Z","iopub.status.idle":"2023-05-10T13:35:21.816564Z","shell.execute_reply.started":"2023-05-10T13:35:21.570234Z","shell.execute_reply":"2023-05-10T13:35:21.815283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model2, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:35:35.716267Z","iopub.execute_input":"2023-05-10T13:35:35.717175Z","iopub.status.idle":"2023-05-10T13:35:36.807693Z","shell.execute_reply.started":"2023-05-10T13:35:35.717122Z","shell.execute_reply":"2023-05-10T13:35:36.805902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.compile(optimizer=\"adam\", loss= \"categorical_crossentropy\", metrics= [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:36:10.165427Z","iopub.execute_input":"2023-05-10T13:36:10.166621Z","iopub.status.idle":"2023-05-10T13:36:10.189833Z","shell.execute_reply.started":"2023-05-10T13:36:10.166574Z","shell.execute_reply":"2023-05-10T13:36:10.188417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist2 = model2.fit(x_train_n , y_train, epochs=500, batch_size = 32, validation_data= (x_val ,y_val))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:36:17.762435Z","iopub.execute_input":"2023-05-10T13:36:17.763365Z","iopub.status.idle":"2023-05-10T13:46:14.119066Z","shell.execute_reply.started":"2023-05-10T13:36:17.763296Z","shell.execute_reply":"2023-05-10T13:46:14.117255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Loss curves\nplt.figure(figsize=[12,4])\nplt.plot(hist2.history['loss'],'red',linewidth=3.0)\nplt.plot(hist2.history['val_loss'],'orange',linewidth=3.0)\nplt.legend(['Training loss', 'Validation Loss'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Loss',fontsize=15)\nplt.title('Loss Curves',fontsize=15)\n\n\n# Accuracy Curves\nplt.figure(figsize=[12,4])\nplt.plot(hist2.history['accuracy'],'darkgreen',linewidth=3.0)\nplt.plot(hist2.history['val_accuracy'],'lightgreen',linewidth=3.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Accuracy',fontsize=15)\nplt.title('Accuracy Curves',fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:53:03.622686Z","iopub.execute_input":"2023-05-10T13:53:03.623869Z","iopub.status.idle":"2023-05-10T13:53:04.136605Z","shell.execute_reply.started":"2023-05-10T13:53:03.623807Z","shell.execute_reply":"2023-05-10T13:53:04.135315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:53:18.418977Z","iopub.execute_input":"2023-05-10T13:53:18.419471Z","iopub.status.idle":"2023-05-10T13:53:18.830359Z","shell.execute_reply.started":"2023-05-10T13:53:18.419425Z","shell.execute_reply":"2023-05-10T13:53:18.829133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_m2 = model2.predict(x_test)\ny_pred_m2 = pred_int(y_pred_m2)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:53:30.248280Z","iopub.execute_input":"2023-05-10T13:53:30.248727Z","iopub.status.idle":"2023-05-10T13:53:30.612752Z","shell.execute_reply.started":"2023-05-10T13:53:30.248687Z","shell.execute_reply":"2023-05-10T13:53:30.611251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(pred_int(y_test), y_pred_m2))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:53:41.709226Z","iopub.execute_input":"2023-05-10T13:53:41.709727Z","iopub.status.idle":"2023-05-10T13:53:41.753493Z","shell.execute_reply.started":"2023-05-10T13:53:41.709680Z","shell.execute_reply":"2023-05-10T13:53:41.752191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import AlphaDropout, BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:53:52.763819Z","iopub.execute_input":"2023-05-10T13:53:52.764287Z","iopub.status.idle":"2023-05-10T13:53:52.769694Z","shell.execute_reply.started":"2023-05-10T13:53:52.764245Z","shell.execute_reply":"2023-05-10T13:53:52.768279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape1 = (32)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:53:56.716937Z","iopub.execute_input":"2023-05-10T13:53:56.717396Z","iopub.status.idle":"2023-05-10T13:53:56.722913Z","shell.execute_reply.started":"2023-05-10T13:53:56.717352Z","shell.execute_reply":"2023-05-10T13:53:56.721349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_to_nn = Input(shape = input_shape1)\n\nhidden_1 = Dense(2048,  activation='selu',kernel_initializer='lecun_normal' )(input_to_nn)\nbn1 = BatchNormalization()(hidden_1)\n\nhidden_2 = Dense(1024, activation='selu',kernel_initializer='lecun_normal')(bn1)\nbn2 = BatchNormalization()(hidden_2)\n\nhidden_3 = Dense(512,  activation='selu',kernel_initializer='lecun_normal')(bn2)\nbn3 = BatchNormalization()(hidden_3)\n\nhidden_4 = Dense(256,  activation='selu',kernel_initializer='lecun_normal')(bn3)\nbn4 = BatchNormalization()(hidden_4)\n\nhidden_5 = Dense(128,  activation='selu',kernel_initializer='lecun_normal')(bn4)\nbn5 = BatchNormalization()(hidden_5)\n\nd1 = AlphaDropout(0.5)(bn5)\n\n\nhidden_6 = Dense(1024, activation = \"elu\")(input_to_nn)\nbn6 = BatchNormalization()(hidden_6)\n\nhidden_7 = Dense(256, activation = \"elu\")(bn6)\nbn7 = BatchNormalization()(hidden_7)\n\nhidden_8 = Dense(64, activation = \"elu\")(bn7)\nbn8 = BatchNormalization()(hidden_8)\n\nhidden_9 = Dense(32, activation = \"elu\")(bn8)\nbn9 = BatchNormalization()(hidden_9)\n\nhidden_10 = Dense(16, activation = \"elu\")(bn9)\nbn10 = BatchNormalization()(hidden_10)\n\nd2 = AlphaDropout(0.5)(bn10)\n\nconcat = Concatenate()([d1, d2])\n\ndense1 = Dense(64, activation = \"selu\", kernel_initializer='lecun_normal')(concat)\nd3 = AlphaDropout(0.5)(dense1)\ndense2 = Dense(32, activation = \"elu\")(d3)\n\noutput = Dense(10, activation = \"softmax\")(dense2)\n\nmodel3 = Model(inputs = [input_to_nn], outputs = [output])\n\nmodel3.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:54:02.138527Z","iopub.execute_input":"2023-05-10T13:54:02.138971Z","iopub.status.idle":"2023-05-10T13:54:02.699452Z","shell.execute_reply.started":"2023-05-10T13:54:02.138931Z","shell.execute_reply":"2023-05-10T13:54:02.691959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model3, show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:54:25.841964Z","iopub.execute_input":"2023-05-10T13:54:25.842475Z","iopub.status.idle":"2023-05-10T13:54:26.204765Z","shell.execute_reply.started":"2023-05-10T13:54:25.842431Z","shell.execute_reply":"2023-05-10T13:54:26.203128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3.compile(optimizer=\"adam\", loss= \"categorical_crossentropy\", metrics= [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:54:37.472740Z","iopub.execute_input":"2023-05-10T13:54:37.473811Z","iopub.status.idle":"2023-05-10T13:54:37.494338Z","shell.execute_reply.started":"2023-05-10T13:54:37.473756Z","shell.execute_reply":"2023-05-10T13:54:37.492871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist3 = model3.fit(x_train_n , y_train, epochs=300, batch_size = 32, validation_data= (x_val ,y_val))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T13:54:47.136532Z","iopub.execute_input":"2023-05-10T13:54:47.137015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Loss curves\nplt.figure(figsize=[12,4])\nplt.plot(hist3.history['loss'],'red',linewidth=3.0)\nplt.plot(hist3.history['val_loss'],'orange',linewidth=3.0)\nplt.legend(['Training loss', 'Validation Loss'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Loss',fontsize=15)\nplt.title('Loss Curves',fontsize=15)\n\n\n# Accuracy Curves\nplt.figure(figsize=[12,4])\nplt.plot(hist3.history['accuracy'],'darkgreen',linewidth=3.0)\nplt.plot(hist3.history['val_accuracy'],'lightgreen',linewidth=3.0)\nplt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=15)\nplt.xlabel('Epochs ',fontsize=15)\nplt.ylabel('Accuracy',fontsize=15)\nplt.title('Accuracy Curves',fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:31:44.532236Z","iopub.execute_input":"2023-05-10T14:31:44.532806Z","iopub.status.idle":"2023-05-10T14:31:45.078255Z","shell.execute_reply.started":"2023-05-10T14:31:44.532760Z","shell.execute_reply":"2023-05-10T14:31:45.076762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:32:06.450586Z","iopub.execute_input":"2023-05-10T14:32:06.451083Z","iopub.status.idle":"2023-05-10T14:32:07.193429Z","shell.execute_reply.started":"2023-05-10T14:32:06.451034Z","shell.execute_reply":"2023-05-10T14:32:07.191979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pred_int(y_pred):\n    y_pred_int = []\n    for i in range(len(y_pred)):\n        max_i = 0\n        max_p = 0\n        for j in range(len(y_pred[i])):\n            if y_pred[i][j] > max_p:\n                max_p = y_pred[i][j]\n                max_i = j\n                \n        y_pred_int.append(max_i)\n        \n    return y_pred_int","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:32:20.023140Z","iopub.execute_input":"2023-05-10T14:32:20.023628Z","iopub.status.idle":"2023-05-10T14:32:20.031713Z","shell.execute_reply.started":"2023-05-10T14:32:20.023585Z","shell.execute_reply":"2023-05-10T14:32:20.029931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\ntest_samples = []\nsample_classes = []\n\nfor i in range(5):\n    random_number = random.randint(0, len(x_test)-1)\n    test_samples.append(x_test[random_number])\n    \n    y_curr = y_test[random_number]\n    for j in range(len(y_curr)):\n        if y_curr[j] == 1:\n            sample_classes.append(j)\n            break\n                        \nsample_classes","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:33:05.144680Z","iopub.execute_input":"2023-05-10T14:33:05.145168Z","iopub.status.idle":"2023-05-10T14:33:05.158578Z","shell.execute_reply.started":"2023-05-10T14:33:05.145128Z","shell.execute_reply":"2023-05-10T14:33:05.157060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_samples = np.array(test_samples)\nprint(test_samples.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:33:16.269480Z","iopub.execute_input":"2023-05-10T14:33:16.270669Z","iopub.status.idle":"2023-05-10T14:33:16.277145Z","shell.execute_reply.started":"2023-05-10T14:33:16.270616Z","shell.execute_reply":"2023-05-10T14:33:16.275541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_p = model3.predict(test_samples)\n    \ny_true = sample_classes\n\ny_pred_p  = pred_int(y_pred_p)\n\n    \nprint(\"Predicted labels      True lebels\")\nprint(y_pred_p, \"    \",y_true)\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:33:29.716072Z","iopub.execute_input":"2023-05-10T14:33:29.716514Z","iopub.status.idle":"2023-05-10T14:33:30.233609Z","shell.execute_reply.started":"2023-05-10T14:33:29.716477Z","shell.execute_reply":"2023-05-10T14:33:30.232213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6 **Performance Metrices**","metadata":{}},{"cell_type":"code","source":"y_pred_m3 = model3.predict(x_test)\n\ny_pred_m3 = pred_int(y_pred_m3)\n\ny_test3 = pred_int(y_test)\n\nconfusion_mat3 = confusion_matrix(y_test3, y_pred_m3)\n\nplt.figure(figsize=(10,4))\n\nplt.xlabel(\"Predicted class\")\nplt.ylabel(\"True class\")\n\nsns.heatmap(confusion_mat, annot=True, cmap=\"Greens\",  xticklabels = ['air_conditioner', 'car_horn', 'children_playing', 'dog_bark', 'drilling', 'enginge_idling', 'gun_shot', 'jackhammer', 'siren','street_music'],\n           yticklabels=['air_conditioner', 'car_horn', 'children_playing', 'dog_bark', 'drilling', 'enginge_idling', 'gun_shot', 'jackhammer', 'siren','street_music'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:33:43.544398Z","iopub.execute_input":"2023-05-10T14:33:43.544870Z","iopub.status.idle":"2023-05-10T14:33:45.042715Z","shell.execute_reply.started":"2023-05-10T14:33:43.544829Z","shell.execute_reply":"2023-05-10T14:33:45.041283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test3, y_pred_m3))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:34:05.868367Z","iopub.execute_input":"2023-05-10T14:34:05.868819Z","iopub.status.idle":"2023-05-10T14:34:05.887370Z","shell.execute_reply.started":"2023-05-10T14:34:05.868781Z","shell.execute_reply":"2023-05-10T14:34:05.886318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path = \"model3.h5\"\n\ntf.keras.models.save_model(model3, filepath=model_path, overwrite=True, include_optimizer=True, save_format=\"h5\")","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:36:09.742504Z","iopub.execute_input":"2023-05-10T14:36:09.743189Z","iopub.status.idle":"2023-05-10T14:36:10.051311Z","shell.execute_reply.started":"2023-05-10T14:36:09.743142Z","shell.execute_reply":"2023-05-10T14:36:10.049791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nModel3 = load_model(\"model3.h5\")\n\nModel3.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T14:36:54.211023Z","iopub.execute_input":"2023-05-10T14:36:54.211857Z","iopub.status.idle":"2023-05-10T14:36:55.961765Z","shell.execute_reply.started":"2023-05-10T14:36:54.211805Z","shell.execute_reply":"2023-05-10T14:36:55.960563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7 **LSTM Approach**","metadata":{}},{"cell_type":"code","source":"import random\nimport os\nimport glob\nimport time\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport librosa\nfrom IPython import display\n\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras import layers, Sequential\nfrom tensorflow.keras.utils import plot_model\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import classification_report, precision_recall_fscore_support\nfrom sklearn.metrics import accuracy_score, f1_score, matthews_corrcoef\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nfrom scikitplot.metrics import plot_roc","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:10:28.096635Z","iopub.execute_input":"2023-05-16T02:10:28.097521Z","iopub.status.idle":"2023-05-16T02:10:41.599728Z","shell.execute_reply.started":"2023-05-16T02:10:28.097472Z","shell.execute_reply":"2023-05-16T02:10:41.598133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    EPOCHS = 100\n    BATCH_SIZE = 32\n    SEED = 42\n    TF_SEED = 768","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:10:56.644360Z","iopub.execute_input":"2023-05-16T02:10:56.645278Z","iopub.status.idle":"2023-05-16T02:10:56.652048Z","shell.execute_reply.started":"2023-05-16T02:10:56.645229Z","shell.execute_reply":"2023-05-16T02:10:56.650550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATASET_PATH = \"/kaggle/input/urbansound8k/\"\nDATASET_CSV = \"/kaggle/input/urbansound8k/UrbanSound8K.csv\"","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:11:02.637910Z","iopub.execute_input":"2023-05-16T02:11:02.638331Z","iopub.status.idle":"2023-05-16T02:11:02.643887Z","shell.execute_reply.started":"2023-05-16T02:11:02.638295Z","shell.execute_reply":"2023-05-16T02:11:02.642774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df = pd.read_csv(DATASET_CSV)\ndataset_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:11:06.399590Z","iopub.execute_input":"2023-05-16T02:11:06.400065Z","iopub.status.idle":"2023-05-16T02:11:06.480284Z","shell.execute_reply.started":"2023-05-16T02:11:06.400019Z","shell.execute_reply":"2023-05-16T02:11:06.478939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_sample(df, idx):\n    print('==============================================')\n    print(f\"Filename:\\t{df['slice_file_name'][idx]}\")\n    print(f\"Fold:\\t\\t{df['fold'][idx]}\")\n    print(f\"Class:\\t\\t{df['class'][idx]}\\n\\n\")\n    sample_path = DATASET_PATH + f\"fold{df['fold'][idx]}/{df['slice_file_name'][idx]}\"\n    display.display(display.Audio(sample_path, rate=16000))\n    print('==============================================')\n    return\n\ndef generate_spectrogram(waveform, length=255, step=128, fft_length=255):\n    # Compute Short-Time Fourier Transform of waveform\n    spectrogram = tf.signal.stft(waveform, \n                                frame_length=length, \n                                frame_step=step, \n                                fft_length=fft_length)\n    \n    # Get magnitude of STFT\n    spectrogram = tf.abs(spectrogram)\n    \n    # Convert spectrogram into image with (height, width, channels) dimensions\n    spectrogram = spectrogram[..., tf.newaxis]\n    \n    return spectrogram","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:11:10.150009Z","iopub.execute_input":"2023-05-16T02:11:10.150440Z","iopub.status.idle":"2023-05-16T02:11:10.162222Z","shell.execute_reply.started":"2023-05-16T02:11:10.150404Z","shell.execute_reply":"2023-05-16T02:11:10.160703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_audio_data(waveform, figsize=(16, 8)):\n    fig, (ax1, ax2) = plt.subplots(2, figsize=figsize)\n    \n    # Plot waveform\n    ax1.plot(waveform)\n    ax1.set_title('Waveform')\n    ax1.set_xlim([0, len(waveform)])\n    \n    # Plot spectrogram\n    #-----------------\n    # Generate spectogram from waveform\n    spectrogram = generate_spectrogram(waveform) \n    \n    # View shapes\n    print(f'Waveform shape: {waveform.shape}')\n    print(f'Spectrogram shape: {spectrogram.shape}')\n    \n    # Convert spectrogram shape to 3D if 2D\n    if len(spectrogram.shape) > 2:\n        assert len(spectrogram.shape) == 3\n        spectrogram = np.squeeze(spectrogram, axis=-1)\n     # Take log of spectrogram (epsillon is added to avoid taking log of 0)\n    log_spec = np.log(spectrogram.T + np.finfo(float).eps)\n    height = log_spec.shape[0]\n    width = log_spec.shape[1]\n    \n    # Plot Spectrogram\n    X = np.linspace(0, np.size(spectrogram), num=width, dtype=int)\n    Y = range(height)\n    ax2.pcolormesh(X, Y, log_spec)\n    ax2.set_title('Spectrogram')\n    plt.show()\n    \n    return","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:11:14.004593Z","iopub.execute_input":"2023-05-16T02:11:14.005041Z","iopub.status.idle":"2023-05-16T02:11:14.017422Z","shell.execute_reply.started":"2023-05-16T02:11:14.005000Z","shell.execute_reply":"2023-05-16T02:11:14.016164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = random.sample(dataset_df.index.to_list(), 1)[0]\nview_sample(dataset_df, idx)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:11:18.462447Z","iopub.execute_input":"2023-05-16T02:11:18.463182Z","iopub.status.idle":"2023-05-16T02:11:18.557601Z","shell.execute_reply.started":"2023-05-16T02:11:18.463139Z","shell.execute_reply":"2023-05-16T02:11:18.556813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_path = DATASET_PATH + f\"fold{dataset_df['fold'][idx]}/{dataset_df['slice_file_name'][idx]}\"\nsample_path","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:11:23.901555Z","iopub.execute_input":"2023-05-16T02:11:23.902040Z","iopub.status.idle":"2023-05-16T02:11:23.911545Z","shell.execute_reply.started":"2023-05-16T02:11:23.901993Z","shell.execute_reply":"2023-05-16T02:11:23.910049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"waveform, sample_rate = librosa.load(sample_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:11:27.139681Z","iopub.execute_input":"2023-05-16T02:11:27.140162Z","iopub.status.idle":"2023-05-16T02:11:41.245079Z","shell.execute_reply.started":"2023-05-16T02:11:27.140119Z","shell.execute_reply":"2023-05-16T02:11:41.243640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_audio_data(tf.squeeze(waveform))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:51:47.752553Z","iopub.execute_input":"2023-05-11T13:51:47.754469Z","iopub.status.idle":"2023-05-11T13:51:48.874083Z","shell.execute_reply.started":"2023-05-11T13:51:47.754405Z","shell.execute_reply":"2023-05-11T13:51:48.872629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 8))\nplt.title('Audio Class Distribution', fontsize=22)\nclass_distribution = dataset_df['class'].value_counts().sort_values()\n\nsns.barplot(x=class_distribution.values,\n            y=list(class_distribution.keys()),\n            orient=\"h\");","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:51:53.994121Z","iopub.execute_input":"2023-05-11T13:51:53.994701Z","iopub.status.idle":"2023-05-11T13:51:54.373759Z","shell.execute_reply.started":"2023-05-11T13:51:53.994653Z","shell.execute_reply":"2023-05-11T13:51:54.372379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folds = [1, 2, 3, 4, 5, 6]\nval_folds = [7, 8]\ntest_folds = [9, 10]","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:04.582261Z","iopub.execute_input":"2023-05-11T13:52:04.582913Z","iopub.status.idle":"2023-05-11T13:52:04.590916Z","shell.execute_reply.started":"2023-05-11T13:52:04.582862Z","shell.execute_reply":"2023-05-11T13:52:04.589393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = dataset_df.loc[dataset_df['fold'].isin(train_folds)]\nval_df = dataset_df.loc[dataset_df['fold'].isin(val_folds)]\ntest_df = dataset_df.loc[dataset_df['fold'].isin(test_folds)]\n\ntrain_df.shape, val_df.shape, test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:07.843342Z","iopub.execute_input":"2023-05-11T13:52:07.843990Z","iopub.status.idle":"2023-05-11T13:52:07.866264Z","shell.execute_reply.started":"2023-05-11T13:52:07.843930Z","shell.execute_reply":"2023-05-11T13:52:07.864157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:11.151955Z","iopub.execute_input":"2023-05-11T13:52:11.152482Z","iopub.status.idle":"2023-05-11T13:52:11.171922Z","shell.execute_reply.started":"2023-05-11T13:52:11.152437Z","shell.execute_reply":"2023-05-11T13:52:11.170376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2, ax3) = plt.subplots(3, figsize=(16, 14))\n\n# Set the spacing between subplots\nfig.tight_layout(pad=6.0)\n\n# Plot Train Class Distribution\nax1.set_title('Train Audio Class Distribution', fontsize=22)\ntrain_distribution = train_df['class'].value_counts().sort_values()\nsns.barplot(x=train_distribution.values,\n            y=list(train_distribution.keys()),\n            orient=\"h\",\n            ax=ax1)\n\n# Plot Validation Class Distribution\nax2.set_title('Validation Audio Class Distribution', fontsize=22)\nval_distribution = val_df['class'].value_counts().sort_values()\n\nsns.barplot(x=val_distribution.values,\n            y=list(val_distribution.keys()),\n            orient=\"h\",\n            ax=ax2)\n# Plot Test Class Distribution\nax3.set_title('Test Audio Class Distribution', fontsize=22)\ntest_distribution = test_df['class'].value_counts().sort_values()\n\nsns.barplot(x=test_distribution.values,\n            y=list(test_distribution.keys()),\n            orient=\"h\",\n            ax=ax3);","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:13.960943Z","iopub.execute_input":"2023-05-11T13:52:13.962610Z","iopub.status.idle":"2023-05-11T13:52:14.979744Z","shell.execute_reply.started":"2023-05-11T13:52:13.962543Z","shell.execute_reply":"2023-05-11T13:52:14.978231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_len = len(train_df)\nval_len = len(val_df)\ntest_len = len(test_df)\ntotal_len = len(dataset_df)\n\nprint(f\"Train Set Size:\\t\\t{train_len}({round(100* train_len/total_len, 3)}%)\")\nprint(f\"Validation Set Size:\\t{val_len}({round(100* val_len/total_len, 3)}%)\")\nprint(f\"Test Set Size:\\t\\t{test_len}({round(100* test_len/total_len, 3)}%)\")\nprint('------------------------------------')\nprint(f\"Total Samples:\\t\\t{total_len}(100%)\")","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:20.652881Z","iopub.execute_input":"2023-05-11T13:52:20.653417Z","iopub.status.idle":"2023-05-11T13:52:20.663474Z","shell.execute_reply.started":"2023-05-11T13:52:20.653370Z","shell.execute_reply":"2023-05-11T13:52:20.662032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_paths = [DATASET_PATH + f\"fold{train_df['fold'].iloc[_]}/{train_df['slice_file_name'].iloc[_]}\" for _ in range(len(train_df))]\n\n# Generate validation audio paths\nval_paths = [DATASET_PATH + f\"fold{val_df['fold'].iloc[_]}/{val_df['slice_file_name'].iloc[_]}\" for _ in range(len(val_df))]\n\n# Generate test audio paths\ntest_paths = [DATASET_PATH + f\"fold{test_df['fold'].iloc[_]}/{test_df['slice_file_name'].iloc[_]}\" for _ in range(len(test_df))]","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:23.255941Z","iopub.execute_input":"2023-05-11T13:52:23.256518Z","iopub.status.idle":"2023-05-11T13:52:23.560894Z","shell.execute_reply.started":"2023-05-11T13:52:23.256418Z","shell.execute_reply":"2023-05-11T13:52:23.559400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_paths[:10]","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:27.045870Z","iopub.execute_input":"2023-05-11T13:52:27.046388Z","iopub.status.idle":"2023-05-11T13:52:27.054985Z","shell.execute_reply.started":"2023-05-11T13:52:27.046342Z","shell.execute_reply":"2023-05-11T13:52:27.053461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_data = dataset_df[['classID', 'class']].value_counts().keys().sort_values()\nlabel_data = dict(label_data)\nlabel_data","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:29.644949Z","iopub.execute_input":"2023-05-11T13:52:29.645620Z","iopub.status.idle":"2023-05-11T13:52:29.664793Z","shell.execute_reply.started":"2023-05-11T13:52:29.645562Z","shell.execute_reply":"2023-05-11T13:52:29.663375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels_encoded = train_df[\"classID\"].to_numpy()\nval_labels_encoded = val_df[\"classID\"].to_numpy()\ntest_labels_encoded = test_df[\"classID\"].to_numpy()\n\n# Inspect label encoded targets\ntrain_labels_encoded","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:32.329001Z","iopub.execute_input":"2023-05-11T13:52:32.329555Z","iopub.status.idle":"2023-05-11T13:52:32.340240Z","shell.execute_reply.started":"2023-05-11T13:52:32.329503Z","shell.execute_reply":"2023-05-11T13:52:32.339074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(label_data.keys())\nclass_names = list(label_data.values())\n\nprint('==============================================')\nprint(f'Number of classes: {num_classes}\\n')\nprint(f'Classes:\\n{class_names}')\nprint('==============================================')","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:34.952144Z","iopub.execute_input":"2023-05-11T13:52:34.952712Z","iopub.status.idle":"2023-05-11T13:52:34.961183Z","shell.execute_reply.started":"2023-05-11T13:52:34.952660Z","shell.execute_reply":"2023-05-11T13:52:34.959854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def one_hot_encode(encoded_classes:np.ndarray, num_classes:int):\n    return tf.one_hot(encoded_classes, depth=num_classes)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:37.743818Z","iopub.execute_input":"2023-05-11T13:52:37.745499Z","iopub.status.idle":"2023-05-11T13:52:37.753003Z","shell.execute_reply.started":"2023-05-11T13:52:37.745420Z","shell.execute_reply":"2023-05-11T13:52:37.751372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_one_hot = one_hot_encode(train_labels_encoded, num_classes)\n\n# One-hot encode validation labels\nval_one_hot = one_hot_encode(val_labels_encoded, num_classes)\n\n# One-hot encode test labels\ntest_one_hot = one_hot_encode(test_labels_encoded, num_classes)\n\n# View one-hot encodings of train labels\ntrain_one_hot","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:55.986514Z","iopub.execute_input":"2023-05-11T13:52:55.987071Z","iopub.status.idle":"2023-05-11T13:52:56.004254Z","shell.execute_reply.started":"2023-05-11T13:52:55.986998Z","shell.execute_reply":"2023-05-11T13:52:56.002833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_idx = 20\nprint('Sample Class:\\t\\t\\t', train_df['class'].iloc[sample_idx])\nprint('Sample Label Encoding:\\t\\t', train_labels_encoded[sample_idx])\nprint('Sample One-Hot Encoding:\\t', train_one_hot[sample_idx].numpy())","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:52:59.054571Z","iopub.execute_input":"2023-05-11T13:52:59.055054Z","iopub.status.idle":"2023-05-11T13:52:59.069844Z","shell.execute_reply.started":"2023-05-11T13:52:59.055014Z","shell.execute_reply":"2023-05-11T13:52:59.067871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _load(audio_path: str):\n    # Load Audio\n    waveform, sample_rate = librosa.load(audio_path, sr=16000, mono=True)\n    \n    # Pad or Truncate audio to standardize waveform lenght\n    waveform = librosa.util.fix_length(waveform, size=32000)\n    \n    return waveform\n\n\n# Here's a function to get any model/preprocessor from tensorflow hub\ndef get_tfhub_model(model_link, model_name, model_trainable=False, input_shape=None):\n    return hub.KerasLayer(model_link,\n                          trainable=model_trainable,\n                          name=model_name)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:53:02.471097Z","iopub.execute_input":"2023-05-11T13:53:02.472608Z","iopub.status.idle":"2023-05-11T13:53:02.482915Z","shell.execute_reply.started":"2023-05-11T13:53:02.472540Z","shell.execute_reply":"2023-05-11T13:53:02.481024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yamnet_url = 'https://tfhub.dev/google/yamnet/1'\nmodel_name = 'yamnet_1'\nset_trainable=False # set trainable to False for inference-only \n\nyamnet = get_tfhub_model(yamnet_url,\n                         model_name, \n                         model_trainable=set_trainable)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:53:09.358372Z","iopub.execute_input":"2023-05-11T13:53:09.358877Z","iopub.status.idle":"2023-05-11T13:53:16.234584Z","shell.execute_reply.started":"2023-05-11T13:53:09.358833Z","shell.execute_reply":"2023-05-11T13:53:16.233041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AudioGenerator:\n    def __init__(self, audio_paths, audio_labels):\n        self.audio_paths = audio_paths\n        self.audio_labels = audio_labels\n        \n    def get_paths(self):\n        return self.audio_paths\n    \n    def get_labels(self):\n        return self.audio_labels\n    \n    def __len__(self):\n        return len(self.audio_labels)\n    def __call__(self):\n        # Get paths and labels\n        paths = self.get_paths()\n        labels = self.get_labels()\n        \n        # Zip paths and labels together\n        data = list(zip(paths, labels))\n        \n        # Iterate over paths and labels to yield \n        # loaded embeddings with labels  \n        for path, label in data:\n            audio =_load(path)\n            _, embeddings_output, _ = yamnet(audio)\n            \n            yield embeddings_output, label","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:54:48.390266Z","iopub.execute_input":"2023-05-11T13:54:48.391229Z","iopub.status.idle":"2023-05-11T13:54:48.402724Z","shell.execute_reply.started":"2023-05-11T13:54:48.391171Z","shell.execute_reply":"2023-05-11T13:54:48.401421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_pipeline(audio_paths, audio_labels,\n                    batch_size=32, shuffle=False, cache=False, prefetch=False):\n    AUTOTUNE = tf.data.AUTOTUNE\n    \n    \n    # Define dataset output signature\n    gen_output_signature = (\n        tf.TensorSpec(shape=(4, 1024), dtype=tf.float32), \n        tf.TensorSpec(shape=(audio_labels.shape[-1]), dtype=tf.float32)\n    )\n    \n    # Create frames dataset with generator\n    ds = tf.data.Dataset.from_generator(\n        AudioGenerator(audio_paths, audio_labels),\n        output_signature=gen_output_signature\n    )\n        \n    # Apply shuffling based on condition\n    if shuffle:\n        ds = ds.shuffle(buffer_size=1000)\n    ds = ds.batch(batch_size)\n    \n    # Apply caching based on condition\n    # Note: Do this if the data is small enough to fit in memory!!!\n    if cache:\n        ds = ds.cache(buffer_size=AUTOTUNE)\n    \n    # Apply prefetching based on condition\n    # Note: This will result in memory trade-offs\n    if prefetch:\n        ds = ds.prefetch(buffer_size=AUTOTUNE)\n    \n    # Return the dataset\n    return ds","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:54:54.480891Z","iopub.execute_input":"2023-05-11T13:54:54.481436Z","iopub.status.idle":"2023-05-11T13:54:54.493966Z","shell.execute_reply.started":"2023-05-11T13:54:54.481384Z","shell.execute_reply":"2023-05-11T13:54:54.492891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = create_pipeline(train_paths, train_one_hot, batch_size=32, shuffle=False, prefetch=False)\nval_ds = create_pipeline(val_paths, val_one_hot, batch_size=32, shuffle=False, prefetch=False)\ntest_ds = create_pipeline(test_paths, test_one_hot, batch_size=32, shuffle=False, prefetch=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:55:00.432268Z","iopub.execute_input":"2023-05-11T13:55:00.432817Z","iopub.status.idle":"2023-05-11T13:55:00.549551Z","shell.execute_reply.started":"2023-05-11T13:55:00.432771Z","shell.execute_reply":"2023-05-11T13:55:00.547950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:56:06.119424Z","iopub.execute_input":"2023-05-11T13:56:06.119973Z","iopub.status.idle":"2023-05-11T13:56:06.128650Z","shell.execute_reply.started":"2023-05-11T13:56:06.119930Z","shell.execute_reply":"2023-05-11T13:56:06.127308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_ds_audio, sample_ds_labels = next(iter(train_ds))\nprint(f'Audio shape: {sample_ds_audio.shape}')\nprint(f'Labels shape: {sample_ds_labels.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:56:09.523833Z","iopub.execute_input":"2023-05-11T13:56:09.524403Z","iopub.status.idle":"2023-05-11T13:56:13.378574Z","shell.execute_reply.started":"2023-05-11T13:56:09.524352Z","shell.execute_reply":"2023-05-11T13:56:13.377398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(num_labels):\n    # Define kernel initializer\n    initializer = tf.keras.initializers.GlorotNormal()\n    \n    # Input Layer\n    audio = layers.Input(shape=(4, 1024), dtype=tf.float32, name='audio_input')\n    \n    # LSTM Layer\n    audio_lstm = layers.Bidirectional(layers.LSTM(128, kernel_initializer=initializer))(audio)\n    \n    # Apply Dropout\n    audio_dropout = layers.Dropout(0.25)(audio_lstm)\n    \n    # Compute classification probabilities\n    output_layer = layers.Dense(num_labels, activation='softmax', \n                                kernel_initializer=initializer, \n                                name='output_layer')(audio_dropout)\n    \n    return tf.keras.Model(inputs=[audio], \n                          outputs=[output_layer], name=\"audio_classifier_model\")","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:56:16.633234Z","iopub.execute_input":"2023-05-11T13:56:16.635178Z","iopub.status.idle":"2023-05-11T13:56:16.647411Z","shell.execute_reply.started":"2023-05-11T13:56:16.635095Z","shell.execute_reply":"2023-05-11T13:56:16.645506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_model = build_model(num_classes)\naudio_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:56:23.994571Z","iopub.execute_input":"2023-05-11T13:56:23.995068Z","iopub.status.idle":"2023-05-11T13:56:24.780870Z","shell.execute_reply.started":"2023-05-11T13:56:23.995026Z","shell.execute_reply":"2023-05-11T13:56:24.779373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(\n    audio_model, dpi=60,\n    show_shapes=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:56:28.912796Z","iopub.execute_input":"2023-05-11T13:56:28.913345Z","iopub.status.idle":"2023-05-11T13:56:29.087778Z","shell.execute_reply.started":"2023-05-11T13:56:28.913285Z","shell.execute_reply":"2023-05-11T13:56:29.086122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping_callback = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss', \n    patience=3, \n    restore_best_weights=True)\n\n# Define reduce learning rate callback\nreduce_lr_callback = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor='val_loss',\n    patience=2,\n    factor=0.1,\n    verbose=1)\n\n# Define callbacks and metrics lists\nCALLBACKS = [early_stopping_callback, reduce_lr_callback]\nMETRICS = ['accuracy']","metadata":{"execution":{"iopub.status.busy":"2023-05-11T13:56:33.894562Z","iopub.execute_input":"2023-05-11T13:56:33.895126Z","iopub.status.idle":"2023-05-11T13:56:33.905777Z","shell.execute_reply.started":"2023-05-11T13:56:33.895071Z","shell.execute_reply":"2023-05-11T13:56:33.904206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(CFG.SEED)\n\n# Compile the model\naudio_model.compile(\n    loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),\n    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n    metrics=METRICS\n)\n\n# Train the model \nprint(f'Training {audio_model.name}.')\nprint(f'Train on {len(train_df)} samples, validate on {len(val_df)} samples.')\nprint('----------------------------------')\n\naudio_model_history = audio_model.fit(\n    train_ds,\n    epochs = CFG.EPOCHS,\n    validation_data=val_ds,\n    callbacks=CALLBACKS)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:43:55.093709Z","iopub.execute_input":"2023-05-11T15:43:55.094245Z","iopub.status.idle":"2023-05-11T16:18:57.441053Z","shell.execute_reply.started":"2023-05-11T15:43:55.094204Z","shell.execute_reply":"2023-05-11T16:18:57.439363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_model_evaluation = audio_model.evaluate(test_ds)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:20:20.434384Z","iopub.execute_input":"2023-05-11T16:20:20.434962Z","iopub.status.idle":"2023-05-11T16:22:41.141399Z","shell.execute_reply.started":"2023-05-11T16:20:20.434916Z","shell.execute_reply":"2023-05-11T16:22:41.139624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_probabilities = audio_model.predict(test_ds, verbose=1)\ntest_predictions = tf.argmax(test_probabilities, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:28:23.429367Z","iopub.execute_input":"2023-05-10T15:28:23.429856Z","iopub.status.idle":"2023-05-10T15:29:17.116717Z","shell.execute_reply.started":"2023-05-10T15:28:23.429812Z","shell.execute_reply":"2023-05-10T15:29:17.115569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8 **Model Performance**","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"def plot_training_curves(history):\n    \n    loss = np.array(history.history['loss'])\n    val_loss = np.array(history.history['val_loss'])\n\n    accuracy = np.array(history.history['accuracy'])\n    val_accuracy = np.array(history.history['val_accuracy'])\n\n    epochs = range(1, len(history.history['loss']) + 1)\n\n    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))\n\n    # Plot loss\n    ax1.plot(epochs, loss, label='training_loss', marker='o')\n    ax1.plot(epochs, val_loss, label='val_loss', marker='o')\n    \n    ax1.fill_between(epochs, loss, val_loss, where=(loss > val_loss), color='C0', alpha=0.3, interpolate=True)\n    ax1.fill_between(epochs, loss, val_loss, where=(loss < val_loss), color='C1', alpha=0.3, interpolate=True)\n\n    ax1.set_title('Loss (Lower Means Better)', fontsize=16)\n    ax1.set_xlabel('Epochs', fontsize=12)\n    ax1.legend()\n    ax2.plot(epochs, accuracy, label='training_accuracy', marker='o')\n    ax2.plot(epochs, val_accuracy, label='val_accuracy', marker='o')\n    \n    ax2.fill_between(epochs, accuracy, val_accuracy, where=(accuracy > val_accuracy), color='C0', alpha=0.3, interpolate=True)\n    ax2.fill_between(epochs, accuracy, val_accuracy, where=(accuracy < val_accuracy), color='C1', alpha=0.3, interpolate=True)\n\n    ax2.set_title('Accuracy (Higher Means Better)', fontsize=16)\n    ax2.set_xlabel('Epochs', fontsize=12)\n    ax2.legend();","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:30:14.040453Z","iopub.execute_input":"2023-05-10T15:30:14.041067Z","iopub.status.idle":"2023-05-10T15:30:14.060510Z","shell.execute_reply.started":"2023-05-10T15:30:14.041006Z","shell.execute_reply":"2023-05-10T15:30:14.059167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_training_curves(audio_model_history)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:30:17.650637Z","iopub.execute_input":"2023-05-10T15:30:17.651127Z","iopub.status.idle":"2023-05-10T15:30:18.280482Z","shell.execute_reply.started":"2023-05-10T15:30:17.651082Z","shell.execute_reply":"2023-05-10T15:30:18.278912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(y_true, y_pred, classes='auto', figsize=(10, 10), text_size=12): \n    # Generate confusion matrix \n    cm = confusion_matrix(y_true, y_pred)\n    \n    # Set plot size\n    plt.figure(figsize=figsize)\n\n    # Create confusion matrix heatmap\n    disp = sns.heatmap(\n        cm, annot=True, cmap='Greens',\n        annot_kws={\"size\": text_size}, fmt='g',\n        linewidths=1, linecolor='black', clip_on=False,\n        xticklabels=classes, yticklabels=classes)\n    \n    # Set title and axis labels\n    disp.set_title('Confusion Matrix', fontsize=24)\n    disp.set_xlabel('Predicted Label', fontsize=20) \n    disp.set_ylabel('True Label', fontsize=20)\n    plt.yticks(rotation=0) \n\n    # Plot confusion matrix\n    plt.show()\n    \n    return","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:31:42.641516Z","iopub.execute_input":"2023-05-10T15:31:42.642315Z","iopub.status.idle":"2023-05-10T15:31:42.652379Z","shell.execute_reply.started":"2023-05-10T15:31:42.642243Z","shell.execute_reply":"2023-05-10T15:31:42.650947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(\n    test_labels_encoded, \n    test_predictions, \n    figsize=(18, 10), \n    classes=class_names)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:31:45.731451Z","iopub.execute_input":"2023-05-10T15:31:45.732758Z","iopub.status.idle":"2023-05-10T15:31:46.559818Z","shell.execute_reply.started":"2023-05-10T15:31:45.732703Z","shell.execute_reply":"2023-05-10T15:31:46.558349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc(test_labels_encoded, \n         test_probabilities, \n         figsize=(16, 12), \n         title_fontsize='large');","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:31:54.023053Z","iopub.execute_input":"2023-05-10T15:31:54.023849Z","iopub.status.idle":"2023-05-10T15:31:54.550402Z","shell.execute_reply.started":"2023-05-10T15:31:54.023774Z","shell.execute_reply":"2023-05-10T15:31:54.549022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(test_labels_encoded, \n                            test_predictions, \n                            target_names=class_names))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:32:02.964993Z","iopub.execute_input":"2023-05-10T15:32:02.965461Z","iopub.status.idle":"2023-05-10T15:32:02.981028Z","shell.execute_reply.started":"2023-05-10T15:32:02.965419Z","shell.execute_reply":"2023-05-10T15:32:02.979807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_performance_scores(y_true, y_pred, y_probabilities):\n    \n    model_accuracy = round(accuracy_score(y_true, y_pred), 5)\n    model_precision, model_recall, model_f1, _ = precision_recall_fscore_support(y_true, \n                                                                                 y_pred, \n                                                                                 average=\"weighted\")\n    model_matthews_corrcoef = round(matthews_corrcoef(y_true, y_pred), 5)\n    \n    print('=============================================')\n    print(f'\\nPerformance Metrics:\\n')\n    print('=============================================')\n    print(f'accuracy_score:\\t\\t{model_accuracy}\\n')\n    print('_____________________________________________')\n    print(f'precision_score:\\t{model_precision}\\n')\n    print('_____________________________________________')\n    print(f'recall_score:\\t\\t{model_recall}\\n')\n    print('_____________________________________________')\n    print(f'f1_score:\\t\\t{model_f1}\\n')\n    print('_____________________________________________')\n    print(f'matthews_corrcoef:\\t{model_matthews_corrcoef}\\n')\n    print('=============================================')\n    \n    performance_scores = {\n        'accuracy_score': model_accuracy,\n        'precision_score': model_precision,\n        'recall_score': model_recall,\n        'f1_score': model_f1,\n        'matthews_corrcoef': model_matthews_corrcoef\n    }\n    return performance_scores","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:32:12.283019Z","iopub.execute_input":"2023-05-10T15:32:12.283489Z","iopub.status.idle":"2023-05-10T15:32:12.293767Z","shell.execute_reply.started":"2023-05-10T15:32:12.283444Z","shell.execute_reply":"2023-05-10T15:32:12.292289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_model_performance = generate_performance_scores(\n    test_labels_encoded, \n    test_predictions, \n    test_probabilities)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:32:16.049873Z","iopub.execute_input":"2023-05-10T15:32:16.050415Z","iopub.status.idle":"2023-05-10T15:32:16.065341Z","shell.execute_reply.started":"2023-05-10T15:32:16.050366Z","shell.execute_reply":"2023-05-10T15:32:16.063348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_audio(model, audio_waveform):\n    # Get waveform embedding \n    _, embedding, _ = yamnet(audio_waveform)\n    \n    # Generate prediction\n    probabilities = model(np.array([embedding]))\n    prediction = class_names[np.argmax(probabilities)]\n    \n    # Return results\n    return prediction, np.squeeze(probabilities.numpy())","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:32:20.693711Z","iopub.execute_input":"2023-05-10T15:32:20.695094Z","iopub.status.idle":"2023-05-10T15:32:20.701453Z","shell.execute_reply.started":"2023-05-10T15:32:20.695041Z","shell.execute_reply":"2023-05-10T15:32:20.700419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = random.sample(test_df.index.to_list(), 10)\n\n# Generate sample path\nsample_paths = [DATASET_PATH + f\"fold{dataset_df['fold'][_]}/{dataset_df['slice_file_name'][_]}\" for _ in idx]\n\n# Load audio and generate prediction\nsample_waveforms = [_load(path) for path in sample_paths]\nresults = [predict_audio(audio_model, waveform) for waveform in sample_waveforms]","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:32:24.306580Z","iopub.execute_input":"2023-05-10T15:32:24.307998Z","iopub.status.idle":"2023-05-10T15:32:25.054573Z","shell.execute_reply.started":"2023-05-10T15:32:24.307936Z","shell.execute_reply":"2023-05-10T15:32:25.053546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 0\n\n# View Predictions\nfor _ in idx:\n    pred, prob = results[index]\n    \n    print(f\"Filename:\\t\\t{test_df['slice_file_name'][_]}\\n\")\n    print(f\"Class:\\t\\t\\t{test_df['class'][_]}\")\n    print(f\"Predicted Class:\\t{pred}\")\n    print(f\"Probability:\\t\\t{prob[np.argmax(prob)]:.4f}\\n\")\n    display.display(display.Audio(sample_paths[index], rate=16000))\n    print('==============================================')\n    \n    index +=1","metadata":{"execution":{"iopub.status.busy":"2023-05-10T15:32:27.384505Z","iopub.execute_input":"2023-05-10T15:32:27.385396Z","iopub.status.idle":"2023-05-10T15:32:27.738480Z","shell.execute_reply.started":"2023-05-10T15:32:27.385349Z","shell.execute_reply":"2023-05-10T15:32:27.737212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix, roc_curve, roc_auc_score\nfrom keras.utils import np_utils","metadata":{"execution":{"iopub.status.busy":"2023-06-01T09:56:06.839584Z","iopub.execute_input":"2023-06-01T09:56:06.840043Z","iopub.status.idle":"2023-06-01T09:56:17.469824Z","shell.execute_reply.started":"2023-06-01T09:56:06.839998Z","shell.execute_reply":"2023-06-01T09:56:17.468557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/urbansound8k'\nmetadata = pd.read_csv(path + '/UrbanSound8K.csv')\nmfcc_data = []\nfor index, row in metadata.iterrows():\n    file_path = os.path.join(os.path.abspath(path), 'fold' + str(row[\"fold\"]) + '/', str(row[\"slice_file_name\"]))\n    sound_data, sr = librosa.load(file_path)\n    mfccs = librosa.feature.mfcc(y=sound_data, sr=sr, n_mfcc=40)\n    mfccs_scaled = np.mean(mfccs.T, axis=0)\n    mfcc_data.append([mfccs_scaled, row['classID']])\nmfcc_data = pd.DataFrame(mfcc_data, columns=['mfcc', 'class'])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T09:56:20.712183Z","iopub.execute_input":"2023-06-01T09:56:20.713077Z","iopub.status.idle":"2023-06-01T10:04:44.721481Z","shell.execute_reply.started":"2023-06-01T09:56:20.713031Z","shell.execute_reply":"2023-06-01T10:04:44.718994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(mfcc_data.mfcc.tolist())\ny = np.array(mfcc_data['class'].tolist())\ny = np_utils.to_categorical(y)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:43:28.185157Z","iopub.execute_input":"2023-06-01T10:43:28.186465Z","iopub.status.idle":"2023-06-01T10:43:28.204906Z","shell.execute_reply.started":"2023-06-01T10:43:28.186415Z","shell.execute_reply":"2023-06-01T10:43:28.203225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_labels = y.shape[1]\nmodel = keras.Sequential()\nmodel.add(keras.layers.Reshape(input_shape=(40,), target_shape=(40, 1)))\nmodel.add(keras.layers.Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(40, 1)))\nmodel.add(keras.layers.MaxPooling1D(pool_size=2))\nmodel.add(keras.layers.Dropout(0.3))\nmodel.add(keras.layers.Conv1D(filters=128, kernel_size=3, activation='relu'))\nmodel.add(keras.layers.MaxPooling1D(pool_size=2))\nmodel.add(keras.layers.Dropout(0.4))\nmodel.add(keras.layers.Conv1D(filters=256, kernel_size=3, activation='relu'))\nmodel.add(keras.layers.MaxPooling1D(pool_size=2))\nmodel.add(keras.layers.Dropout(0.5))\nmodel.add(keras.layers.LSTM(128, return_sequences=True))\nmodel.add(keras.layers.Dropout(0.4))\nmodel.add(keras.layers.LSTM(64))\nmodel.add(keras.layers.Dense(128, activation='relu'))\nmodel.add(keras.layers.Dropout(0.3))\nmodel.add(keras.layers.Dense(num_labels, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:43:30.585290Z","iopub.execute_input":"2023-06-01T10:43:30.585824Z","iopub.status.idle":"2023-06-01T10:43:31.400934Z","shell.execute_reply.started":"2023-06-01T10:43:30.585770Z","shell.execute_reply":"2023-06-01T10:43:31.399600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')\nhistory = model.fit(X_train, y_train, batch_size=64, epochs=500, validation_data=(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:43:33.814359Z","iopub.execute_input":"2023-06-01T10:43:33.814798Z","iopub.status.idle":"2023-06-01T11:16:58.095864Z","shell.execute_reply.started":"2023-06-01T10:43:33.814760Z","shell.execute_reply":"2023-06-01T11:16:58.094586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(X_test, y_test)\nprint('Test accuracy:', test_acc)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T11:39:20.572058Z","iopub.execute_input":"2023-06-01T11:39:20.572550Z","iopub.status.idle":"2023-06-01T11:39:20.967328Z","shell.execute_reply.started":"2023-06-01T11:39:20.572510Z","shell.execute_reply":"2023-06-01T11:39:20.965719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc\n\n# Generate predictions for test data\ny_pred = model.predict(X_test)\ny_pred = np.argmax(y_pred, axis=1)\n\n# Generate classification report\ntarget_names = ['air_conditioner', 'car_horn', 'children_playing', 'dog_bark', 'drilling', 'engine_idling', 'gun_shot', 'jackhammer', 'siren', 'street_music']\nprint(classification_report(np.argmax(y_test, axis=1), y_pred, target_names=target_names))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T11:39:30.041453Z","iopub.execute_input":"2023-06-01T11:39:30.042559Z","iopub.status.idle":"2023-06-01T11:39:31.503445Z","shell.execute_reply.started":"2023-06-01T11:39:30.042510Z","shell.execute_reply":"2023-06-01T11:39:31.501958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-06-01T11:39:34.363324Z","iopub.execute_input":"2023-06-01T11:39:34.363912Z","iopub.status.idle":"2023-06-01T11:39:34.799280Z","shell.execute_reply.started":"2023-06-01T11:39:34.363849Z","shell.execute_reply":"2023-06-01T11:39:34.797464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_mat = confusion_matrix(np.argmax(y_test, axis=1), y_pred)\nfig, ax = plt.subplots(figsize=(10, 10))\nsns.heatmap(conf_mat, annot=True, fmt='d',\n            xticklabels=target_names, yticklabels=target_names)\nplt.ylabel('Actual')\nplt.xlabel('Predicted')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T11:39:37.151179Z","iopub.execute_input":"2023-06-01T11:39:37.152372Z","iopub.status.idle":"2023-06-01T11:39:38.084878Z","shell.execute_reply.started":"2023-06-01T11:39:37.152290Z","shell.execute_reply":"2023-06-01T11:39:38.082255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}