{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-04T17:02:50.558443Z","iopub.execute_input":"2022-10-04T17:02:50.559180Z","iopub.status.idle":"2022-10-04T17:02:50.567840Z","shell.execute_reply.started":"2022-10-04T17:02:50.559143Z","shell.execute_reply":"2022-10-04T17:02:50.566836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:02:50.715256Z","iopub.execute_input":"2022-10-04T17:02:50.715821Z","iopub.status.idle":"2022-10-04T17:02:52.834620Z","shell.execute_reply.started":"2022-10-04T17:02:50.715790Z","shell.execute_reply":"2022-10-04T17:02:52.833483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyunpack \n!pip install patool","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:02:52.837226Z","iopub.execute_input":"2022-10-04T17:02:52.837939Z","iopub.status.idle":"2022-10-04T17:03:14.503875Z","shell.execute_reply.started":"2022-10-04T17:02:52.837899Z","shell.execute_reply":"2022-10-04T17:03:14.502694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"./data\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:03:14.506255Z","iopub.execute_input":"2022-10-04T17:03:14.507868Z","iopub.status.idle":"2022-10-04T17:03:14.513525Z","shell.execute_reply.started":"2022-10-04T17:03:14.507823Z","shell.execute_reply":"2022-10-04T17:03:14.512364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pyunpack import Archive\n\nArchive(\"../input/tensorflow-speech-recognition-challenge/train.7z\").extractall(\"./data\")\n#Archive(\"../input/tensorflow-speech-recognition-challenge/test.7z\").extractall(\"./\")","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:03:14.515083Z","iopub.execute_input":"2022-10-04T17:03:14.515419Z","iopub.status.idle":"2022-10-04T17:04:56.598849Z","shell.execute_reply.started":"2022-10-04T17:03:14.515386Z","shell.execute_reply":"2022-10-04T17:04:56.597413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"./data/train/audio/\"\n\nclasses = os.listdir(train_dir)\nclasses.remove(\"_background_noise_\")\nclasses","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:06:51.794593Z","iopub.execute_input":"2022-10-04T17:06:51.795045Z","iopub.status.idle":"2022-10-04T17:06:51.804539Z","shell.execute_reply.started":"2022-10-04T17:06:51.795006Z","shell.execute_reply":"2022-10-04T17:06:51.803588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\nmv ./data/train/audio/_background_noise_ ./data/train\nls ./data/train/audio","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:06:57.245501Z","iopub.execute_input":"2022-10-04T17:06:57.246124Z","iopub.status.idle":"2022-10-04T17:06:57.283446Z","shell.execute_reply.started":"2022-10-04T17:06:57.246090Z","shell.execute_reply":"2022-10-04T17:06:57.282292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_arr(arr):\n    \"\"\"\n    split an array into chunks of length 16000\n    Returns:\n        list of arrays\n    \"\"\"\n    return np.split(arr, np.arange(16000, len(arr), 16000))\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:06:59.197826Z","iopub.execute_input":"2022-10-04T17:06:59.198245Z","iopub.status.idle":"2022-10-04T17:06:59.205729Z","shell.execute_reply.started":"2022-10-04T17:06:59.198207Z","shell.execute_reply":"2022-10-04T17:06:59.204605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import soundfile as sf\n\ndef create_silence():\n    \"\"\"\n    reads wav files in background noises folder, \n    splits them and saves to silence folder in train_dir\n    \"\"\"\n    for file in os.listdir(\"./data/train/_background_noise_/\"):\n        if \".wav\" in file:\n            sig, sr = librosa.load(\"./data/train/_background_noise_/\"+file, sr = 16000) \n            sig_arr = split_arr(sig)\n            if not os.path.exists(train_dir+\"silence/\"):\n                os.makedirs(train_dir+\"silence/\")\n            for ind, arr in enumerate(sig_arr):\n                file_name = \"frag%d\" %ind + \"_%s\" %file # example: frag0_running_tap.wav\n                sf.write(train_dir+\"silence/\"+file_name, arr, 16000)\n  ","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:07:01.241285Z","iopub.execute_input":"2022-10-04T17:07:01.242001Z","iopub.status.idle":"2022-10-04T17:07:01.249036Z","shell.execute_reply.started":"2022-10-04T17:07:01.241964Z","shell.execute_reply":"2022-10-04T17:07:01.247886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_silence()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:07:06.126951Z","iopub.execute_input":"2022-10-04T17:07:06.127338Z","iopub.status.idle":"2022-10-04T17:07:08.510330Z","shell.execute_reply.started":"2022-10-04T17:07:06.127305Z","shell.execute_reply":"2022-10-04T17:07:08.509036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders = os.listdir(train_dir)\n# put folders in same order as in the classes list, used when making sets\nall_classes = [x for x in classes]\nfor ind, cl in enumerate(folders):\n    if cl not in classes:\n        all_classes.append(cl)\nprint(all_classes)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:07:44.988415Z","iopub.execute_input":"2022-10-04T17:07:44.988845Z","iopub.status.idle":"2022-10-04T17:07:44.996785Z","shell.execute_reply.started":"2022-10-04T17:07:44.988806Z","shell.execute_reply":"2022-10-04T17:07:44.995773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"./data/train/validation_list.txt\") as val_list:\n    validation_list = [row[0] for row in csv.reader(val_list)]\nassert len(validation_list) == 6798, \"Validation files not loaded\"\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:08:17.195379Z","iopub.execute_input":"2022-10-04T17:08:17.195778Z","iopub.status.idle":"2022-10-04T17:08:17.206430Z","shell.execute_reply.started":"2022-10-04T17:08:17.195745Z","shell.execute_reply":"2022-10-04T17:08:17.205529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"./data/train/testing_list.txt\") as val_list:\n    validation_list = [row[0] for row in csv.reader(val_list)]\nassert len(validation_list) == 6835, \"testing files not loaded\"\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:08:18.443399Z","iopub.execute_input":"2022-10-04T17:08:18.443967Z","iopub.status.idle":"2022-10-04T17:08:18.459089Z","shell.execute_reply.started":"2022-10-04T17:08:18.443926Z","shell.execute_reply":"2022-10-04T17:08:18.456896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#validation_list.extend(testing_list)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:08:20.062380Z","iopub.execute_input":"2022-10-04T17:08:20.062749Z","iopub.status.idle":"2022-10-04T17:08:20.067408Z","shell.execute_reply.started":"2022-10-04T17:08:20.062716Z","shell.execute_reply":"2022-10-04T17:08:20.066324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add silence files to validation_list\nfor i, file in enumerate(os.listdir(train_dir+\"silence/\")):\n    if i%10 == 0:\n        validation_list.append(\"silence/\"+file)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:08:20.708417Z","iopub.execute_input":"2022-10-04T17:08:20.709124Z","iopub.status.idle":"2022-10-04T17:08:20.717245Z","shell.execute_reply.started":"2022-10-04T17:08:20.709084Z","shell.execute_reply":"2022-10-04T17:08:20.716235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_list  = []\nall_files_list = []\nclass_counts = {}\n\nfor folder in folders:\n    files = os.listdir(train_dir+folder)\n    for i, f in enumerate(files):\n        all_files_list.append(folder+\"/\"+f)\n        path = folder+'/'+f\n        if path not in validation_list:\n            training_list .append(folder+'/'+f)\n        class_counts[folder] = i\n\n#remove filenames from validation_list that don't exist anymore (due to eda)\nvalidation_list = list(set(validation_list).intersection(all_files_list))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:08:23.000420Z","iopub.execute_input":"2022-10-04T17:08:23.001121Z","iopub.status.idle":"2022-10-04T17:08:28.806093Z","shell.execute_reply.started":"2022-10-04T17:08:23.001083Z","shell.execute_reply":"2022-10-04T17:08:28.805003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert len(validation_list) + len(training_list) == len(all_files_list), \"Not All files splitted\"","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:08:28.807863Z","iopub.execute_input":"2022-10-04T17:08:28.808232Z","iopub.status.idle":"2022-10-04T17:08:28.813365Z","shell.execute_reply.started":"2022-10-04T17:08:28.808195Z","shell.execute_reply":"2022-10-04T17:08:28.812238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check random file name\nprint(training_list[345], \"Size training set: \", len(training_list), 'size validation set: ', len(validation_list))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:08:28.814910Z","iopub.execute_input":"2022-10-04T17:08:28.815530Z","iopub.status.idle":"2022-10-04T17:08:28.826611Z","shell.execute_reply.started":"2022-10-04T17:08:28.815494Z","shell.execute_reply":"2022-10-04T17:08:28.825622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(class_counts)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:09:33.973067Z","iopub.execute_input":"2022-10-04T17:09:33.973468Z","iopub.status.idle":"2022-10-04T17:09:33.979259Z","shell.execute_reply.started":"2022-10-04T17:09:33.973434Z","shell.execute_reply":"2022-10-04T17:09:33.978170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, r = librosa.load(train_dir+\"yes/bfdb9801_nohash_0.wav\", sr=16000)\n\nprint(\"Min: \", np.min(x), \n      \"\\nMax: \", np.max(x),\n      \"\\nMean: \", np.mean(x),\n      \"\\nMedian: \", np.median(x),\n      \"\\nVariance: \", np.var(x),\n      \"\\nLength: \", len(x),)\nplt.plot(x)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:11:10.329869Z","iopub.execute_input":"2022-10-04T17:11:10.330258Z","iopub.status.idle":"2022-10-04T17:11:10.568110Z","shell.execute_reply.started":"2022-10-04T17:11:10.330226Z","shell.execute_reply":"2022-10-04T17:11:10.567241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Turn all wav files into spectrograms\n","metadata":{}},{"cell_type":"code","source":"def make_spec(file, file_dir=train_dir, flip=False, ps=False, st = 4):\n    \"\"\"\n    create a melspectrogram from the amplitude of the sound\n    \n    Args:\n        file (str): filename\n        file_dir (str): directory path\n        flip (bool): reverse time axis\n        ps (bool): pitch shift\n        st (int): half-note steps for pitch shift\n    Returns:\n        np.array with shape (122,85) (time, freq)\n    \"\"\"\n    \n    sig, sr = librosa.load(file_dir+file, sr=16000)\n    \n    if len(sig) < 16000: #pad shorter than 1 sec audio with ramp to zero\n        sig = np.pad(sig, (0,16000-len(sig)), \"linear_ramp\")\n        \n    if ps:\n        sig = librosa.effects.pitch_shift(sig, rate, st)\n        \n    D = librosa.amplitude_to_db(librosa.stft(sig[:16000], \n                                             n_fft=512, \n                                             hop_length=128,\n                                             center=False),\n                               ref=np.max)\n    S = librosa.feature.melspectrogram(S=D, n_mels=85).T\n    \n    if flip:\n        S = np.flipud(S)\n    \n    return S.astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:11:15.328866Z","iopub.execute_input":"2022-10-04T17:11:15.329631Z","iopub.status.idle":"2022-10-04T17:11:15.337868Z","shell.execute_reply.started":"2022-10-04T17:11:15.329567Z","shell.execute_reply":"2022-10-04T17:11:15.336628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"librosa.display.specshow(make_spec(\"yes/bfdb9801_nohash_0.wav\"),\n                         x_axis=\"mel\",\n                         fmax=8000,\n                         y_axis=\"time\",\n                         sr=16000,\n                         hop_length=128)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:11:17.781155Z","iopub.execute_input":"2022-10-04T17:11:17.781523Z","iopub.status.idle":"2022-10-04T17:11:18.066718Z","shell.execute_reply.started":"2022-10-04T17:11:17.781491Z","shell.execute_reply":"2022-10-04T17:11:18.065692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_spec('yes/bfdb9801_nohash_0.wav').shape","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:11:22.903049Z","iopub.execute_input":"2022-10-04T17:11:22.903439Z","iopub.status.idle":"2022-10-04T17:11:22.917305Z","shell.execute_reply.started":"2022-10-04T17:11:22.903405Z","shell.execute_reply":"2022-10-04T17:11:22.916086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_sets(file_list=training_list):\n    X_array = np.zeros([len(file_list), 122, 85])\n    y_array = np.zeros([len(file_list)])\n    for ind, file in enumerate(file_list):\n        if ind%2000 == 0:\n            print(ind, file)\n        try:\n            X_array[ind] = make_spec(file)\n        except ValueError:\n            print(ind, file, ValueError)\n        y_array[ind] = all_classes.index(file.rsplit('/')[0])\n        \n    return X_array, y_array","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:11:49.951783Z","iopub.execute_input":"2022-10-04T17:11:49.952184Z","iopub.status.idle":"2022-10-04T17:11:49.961036Z","shell.execute_reply.started":"2022-10-04T17:11:49.952143Z","shell.execute_reply":"2022-10-04T17:11:49.958066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = create_sets() # takes a while","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:11:50.575771Z","iopub.execute_input":"2022-10-04T17:11:50.576172Z","iopub.status.idle":"2022-10-04T17:17:48.902063Z","shell.execute_reply.started":"2022-10-04T17:11:50.576138Z","shell.execute_reply":"2022-10-04T17:17:48.900631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:17:48.903927Z","iopub.execute_input":"2022-10-04T17:17:48.904262Z","iopub.status.idle":"2022-10-04T17:17:48.914493Z","shell.execute_reply.started":"2022-10-04T17:17:48.904227Z","shell.execute_reply":"2022-10-04T17:17:48.913218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:17:48.916265Z","iopub.execute_input":"2022-10-04T17:17:48.916784Z","iopub.status.idle":"2022-10-04T17:17:48.927498Z","shell.execute_reply.started":"2022-10-04T17:17:48.916728Z","shell.execute_reply":"2022-10-04T17:17:48.926197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"librosa.display.specshow(X_train[6500],\n                         x_axis=\"mel\",\n                         fmax=8000,\n                         y_axis=\"time\",\n                         sr=16000,\n                         hop_length=128)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:17:48.931591Z","iopub.execute_input":"2022-10-04T17:17:48.932077Z","iopub.status.idle":"2022-10-04T17:17:49.200062Z","shell.execute_reply.started":"2022-10-04T17:17:48.932036Z","shell.execute_reply":"2022-10-04T17:17:49.198843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('min: ',np.min(X_train), \n      '\\nmax: ', np.max(X_train), \n      '\\nmean: ', np.mean(X_train),\n      '\\nmedian: ', np.median(X_train),\n      '\\nvariance: ', np.var(X_train))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:17:49.205810Z","iopub.execute_input":"2022-10-04T17:17:49.208861Z","iopub.status.idle":"2022-10-04T17:18:05.764062Z","shell.execute_reply.started":"2022-10-04T17:17:49.208806Z","shell.execute_reply":"2022-10-04T17:18:05.762860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(X_train.flatten(), bins=50)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:18:05.765512Z","iopub.execute_input":"2022-10-04T17:18:05.766168Z","iopub.status.idle":"2022-10-04T17:18:18.716507Z","shell.execute_reply.started":"2022-10-04T17:18:05.766127Z","shell.execute_reply":"2022-10-04T17:18:18.715649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"./data/X_train.npy\", np.expand_dims(X_train, -1)+1.3)\nnp.save(\"./data/y_train.npy\", y_train.astype(np.int))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:18:18.720698Z","iopub.execute_input":"2022-10-04T17:18:18.722859Z","iopub.status.idle":"2022-10-04T17:18:43.550620Z","shell.execute_reply.started":"2022-10-04T17:18:18.722822Z","shell.execute_reply":"2022-10-04T17:18:43.549560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val, y_val = create_sets(file_list=validation_list)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:18:43.552200Z","iopub.execute_input":"2022-10-04T17:18:43.552853Z","iopub.status.idle":"2022-10-04T17:19:37.206849Z","shell.execute_reply.started":"2022-10-04T17:18:43.552813Z","shell.execute_reply":"2022-10-04T17:19:37.205523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(X_val.flatten(), bins=50)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:19:37.208614Z","iopub.execute_input":"2022-10-04T17:19:37.209268Z","iopub.status.idle":"2022-10-04T17:19:38.970440Z","shell.execute_reply.started":"2022-10-04T17:19:37.209231Z","shell.execute_reply":"2022-10-04T17:19:38.969550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('data/X_val.npy', np.expand_dims(X_val, -1)+1.3)\nnp.save('data/y_val.npy', y_val.astype(np.int))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:19:38.973190Z","iopub.execute_input":"2022-10-04T17:19:38.973670Z","iopub.status.idle":"2022-10-04T17:19:39.542818Z","shell.execute_reply.started":"2022-10-04T17:19:38.973627Z","shell.execute_reply":"2022-10-04T17:19:39.541790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reset -f","metadata":{"execution":{"iopub.status.busy":"2022-10-04T18:19:07.740354Z","iopub.execute_input":"2022-10-04T18:19:07.740749Z","iopub.status.idle":"2022-10-04T18:19:08.500470Z","shell.execute_reply.started":"2022-10-04T18:19:07.740715Z","shell.execute_reply":"2022-10-04T18:19:08.499488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:25:42.595027Z","iopub.execute_input":"2022-10-04T17:25:42.595403Z","iopub.status.idle":"2022-10-04T17:25:42.600213Z","shell.execute_reply.started":"2022-10-04T17:25:42.595373Z","shell.execute_reply":"2022-10-04T17:25:42.599085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"./data/train/audio/\"\n\nX_train = np.load(\"./data/X_train.npy\")\ny_train = np.load(\"./data/y_train.npy\")\n\nX_val = np.load(\"./data/X_val.npy\")\ny_val = np.load(\"./data/y_val.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:25:43.099630Z","iopub.execute_input":"2022-10-04T17:25:43.100244Z","iopub.status.idle":"2022-10-04T17:25:56.220640Z","shell.execute_reply.started":"2022-10-04T17:25:43.100212Z","shell.execute_reply":"2022-10-04T17:25:56.219600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:25:56.222462Z","iopub.execute_input":"2022-10-04T17:25:56.222875Z","iopub.status.idle":"2022-10-04T17:25:56.230000Z","shell.execute_reply.started":"2022-10-04T17:25:56.222839Z","shell.execute_reply":"2022-10-04T17:25:56.228942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape((-1, X_train.shape[1], X_train.shape[2]))\nX_val = X_val.reshape((-1, X_val.shape[1], X_val.shape[2]))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:25:56.231858Z","iopub.execute_input":"2022-10-04T17:25:56.232561Z","iopub.status.idle":"2022-10-04T17:25:56.241017Z","shell.execute_reply.started":"2022-10-04T17:25:56.232525Z","shell.execute_reply":"2022-10-04T17:25:56.239866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = os.listdir(train_dir)\nclasses","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:03.559217Z","iopub.execute_input":"2022-10-04T17:27:03.559624Z","iopub.status.idle":"2022-10-04T17:27:03.567772Z","shell.execute_reply.started":"2022-10-04T17:27:03.559571Z","shell.execute_reply":"2022-10-04T17:27:03.566753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\n\ndef get_class_weights(y):\n    counter = Counter(y)\n    majority = max(counter.values())\n    return {cls: float(majority/count) for cls, count in counter.items()}\n\nclass_weights = get_class_weights(y_train)\nclass_weights","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:04.753743Z","iopub.execute_input":"2022-10-04T17:27:04.754330Z","iopub.status.idle":"2022-10-04T17:27:04.772282Z","shell.execute_reply.started":"2022-10-04T17:27:04.754297Z","shell.execute_reply":"2022-10-04T17:27:04.771043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NB_CLASSES = len(classes)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:06.497495Z","iopub.execute_input":"2022-10-04T17:27:06.497873Z","iopub.status.idle":"2022-10-04T17:27:06.502990Z","shell.execute_reply.started":"2022-10-04T17:27:06.497842Z","shell.execute_reply":"2022-10-04T17:27:06.501961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_list_dict(lst):\n    res_dct = {i: val for i, val in enumerate(lst)}\n    return res_dct\n         \nclasses_index = convert_list_dict(classes)\nclasses_index","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:08.582208Z","iopub.execute_input":"2022-10-04T17:27:08.582591Z","iopub.status.idle":"2022-10-04T17:27:08.590675Z","shell.execute_reply.started":"2022-10-04T17:27:08.582546Z","shell.execute_reply":"2022-10-04T17:27:08.589694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\ny_train = to_categorical(y_train, num_classes=NB_CLASSES)\ny_val = to_categorical(y_val, num_classes=NB_CLASSES)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:11.245395Z","iopub.execute_input":"2022-10-04T17:27:11.245763Z","iopub.status.idle":"2022-10-04T17:27:11.258451Z","shell.execute_reply.started":"2022-10-04T17:27:11.245731Z","shell.execute_reply":"2022-10-04T17:27:11.257492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:12.608652Z","iopub.execute_input":"2022-10-04T17:27:12.609834Z","iopub.status.idle":"2022-10-04T17:27:23.162587Z","shell.execute_reply.started":"2022-10-04T17:27:12.609787Z","shell.execute_reply":"2022-10-04T17:27:23.161380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Conv1D, MaxPool1D, Concatenate, BatchNormalization, Activation, Input, Add, \\\n                         GlobalAveragePooling1D, Dense\nfrom keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom livelossplot import PlotLossesKeras\nfrom tensorflow.keras.metrics import Recall, Precision\nimport keras\nimport time","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:23.165506Z","iopub.execute_input":"2022-10-04T17:27:23.166328Z","iopub.status.idle":"2022-10-04T17:27:23.183875Z","shell.execute_reply.started":"2022-10-04T17:27:23.166284Z","shell.execute_reply":"2022-10-04T17:27:23.182935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras.backend as K\n\ndef f1_score(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())\n    return f1_val","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:23.185487Z","iopub.execute_input":"2022-10-04T17:27:23.185917Z","iopub.status.idle":"2022-10-04T17:27:23.194598Z","shell.execute_reply.started":"2022-10-04T17:27:23.185862Z","shell.execute_reply":"2022-10-04T17:27:23.193534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Classifier_INCEPTION:\n    def __init__(self, weights_directory, input_shape, nb_classes, verbose=False, build=True, batch_size=64,\n                 nb_filters=32, use_residual=True, use_bottleneck=True, depth=10, kernel_size=41, nb_epochs=100):\n        self.weights_directory = weights_directory\n        self.nb_filters = nb_filters\n        self.use_residual = use_residual\n        self.use_bottleneck = use_bottleneck\n        self.depth = depth\n        self.kernel_size = kernel_size - 1\n        self.callbacks = None\n        self.batch_size = batch_size\n        self.bottleneck_size = 32\n        self.nb_epochs = nb_epochs\n\n        if build == True:\n            self.model = self.build_model(input_shape, nb_classes)\n            if (verbose == True):\n                self.model.summary()\n            self.verbose = verbose\n\n    def _inception_module(self, input_tensor, stride=1, activation='linear'):\n\n        if self.use_bottleneck and int(input_tensor.shape[-1]) > 1:\n            input_inception = Conv1D(filters=self.bottleneck_size, kernel_size=1,\n                                     padding='same', activation=activation, use_bias=False)(input_tensor)\n        else:\n            input_inception = input_tensor\n\n        kernel_size_s = [self.kernel_size // (2 ** i) for i in range(3)]\n\n        conv_list = []\n\n        for i in range(len(kernel_size_s)):\n            conv_list.append(Conv1D(filters=self.nb_filters, kernel_size=kernel_size_s[i],\n                                    strides=stride, padding='same', activation=activation, use_bias=False)(\n                input_inception))\n\n        max_pool_1 = MaxPool1D(pool_size=3, strides=stride, padding='same')(input_tensor)\n\n        conv_6 = Conv1D(filters=self.nb_filters, kernel_size=1,\n                        padding='same', activation=activation, use_bias=False)(max_pool_1)\n\n        conv_list.append(conv_6)\n\n        x = Concatenate(axis=2)(conv_list)\n        x = BatchNormalization()(x)\n        x = Activation(activation='relu')(x)\n        return x\n\n    def _shortcut_layer(self, input_tensor, out_tensor):\n        shortcut_y = Conv1D(filters=int(out_tensor.shape[-1]), kernel_size=1,\n                            padding='same', use_bias=False)(input_tensor)\n        shortcut_y = BatchNormalization()(shortcut_y)\n\n        x = Add()([shortcut_y, out_tensor])\n        x = Activation('relu')(x)\n        return x\n\n    def build_model(self, input_shape, nb_classes):\n        input_layer = Input(input_shape)\n\n        x = input_layer\n        input_res = input_layer\n\n        for d in range(self.depth):\n\n            x = self._inception_module(x)\n\n            if self.use_residual and d % 3 == 2:\n                x = self._shortcut_layer(input_res, x)\n                input_res = x\n\n        gap_layer = GlobalAveragePooling1D()(x)\n\n        output_layer = Dense(nb_classes, activation='softmax')(gap_layer)\n\n        model = Model(inputs=input_layer, outputs=output_layer)\n\n        model.compile(loss='categorical_crossentropy', \n                      optimizer=Adam(),\n                      metrics=['accuracy', Precision(), Recall(), f1_score])\n\n        reduce_lr = ReduceLROnPlateau(monitor='val_accuracy', \n                                      factor=0.5, \n                                      patience=int(self.nb_epochs/20),\n                                      min_lr=0.0001)\n        \n        file_path = os.path.join(self.weights_directory,\"best_weights.h5\")\n        model_checkpoint = ModelCheckpoint(filepath=file_path, \n                                           monitor='val_accuracy',\n                                           mode=\"max\",\n                                           save_best_only=True)\n        \n        early_stopping = EarlyStopping(monitor=\"val_accuracy\", \n                                       mode=\"max\", \n                                       verbose=1, \n                                       patience=int(self.nb_epochs/10))\n        plotlosses = PlotLossesKeras()\n        self.callbacks = [reduce_lr, model_checkpoint, early_stopping, plotlosses]\n        return model\n\n    def fit(self, x_train, y_train, x_val, y_val, class_weights=None):       \n        if self.batch_size is None:\n            mini_batch_size = int(min(x_train.shape[0] / 10, 16))\n        else:\n            mini_batch_size = self.batch_size\n\n        start_time = time.time()\n        hist = self.model.fit(x_train, y_train, \n                              batch_size=mini_batch_size, \n                              epochs=self.nb_epochs,\n                              verbose=self.verbose, \n                              validation_data=(x_val, y_val), \n                              callbacks=self.callbacks)\n        \n        duration = time.time() - start_time\n        keras.backend.clear_session()\n        print(\"Model take {} S to train \".format(duration))\n        return hist","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:23.197871Z","iopub.execute_input":"2022-10-04T17:27:23.198556Z","iopub.status.idle":"2022-10-04T17:27:23.221518Z","shell.execute_reply.started":"2022-10-04T17:27:23.198520Z","shell.execute_reply":"2022-10-04T17:27:23.220506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_SHAPE = X_train.shape[1:]\nBATCH_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:23.222921Z","iopub.execute_input":"2022-10-04T17:27:23.223733Z","iopub.status.idle":"2022-10-04T17:27:23.235620Z","shell.execute_reply.started":"2022-10-04T17:27:23.223696Z","shell.execute_reply":"2022-10-04T17:27:23.234627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WEIGHTS_DIR = \"./\"\ninception = Classifier_INCEPTION(WEIGHTS_DIR, INPUT_SHAPE, NB_CLASSES, 1, batch_size=BATCH_SIZE, build=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:23.238876Z","iopub.execute_input":"2022-10-04T17:27:23.239221Z","iopub.status.idle":"2022-10-04T17:27:26.996652Z","shell.execute_reply.started":"2022-10-04T17:27:23.239194Z","shell.execute_reply":"2022-10-04T17:27:26.995642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\n\n#adjust these strings for organizeing the saved files\ndate = '4-10-2022'\nmodel_name = 'InceptionTime'\n\n# to save a png of the model you need pydot and graphviz installed\nplot_model(inception.model, \n           to_file = './{}_{}.png'.format(model_name,date), \n           show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:26.998153Z","iopub.execute_input":"2022-10-04T17:27:26.998484Z","iopub.status.idle":"2022-10-04T17:27:29.317366Z","shell.execute_reply.started":"2022-10-04T17:27:26.998456Z","shell.execute_reply":"2022-10-04T17:27:29.312666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = inception.fit(X_train, y_train, X_val, y_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:27:29.318769Z","iopub.execute_input":"2022-10-04T17:27:29.320058Z","iopub.status.idle":"2022-10-04T17:45:24.517349Z","shell.execute_reply.started":"2022-10-04T17:27:29.320011Z","shell.execute_reply":"2022-10-04T17:45:24.516265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\n#%% visualize training\nprint(history.history.keys())\n# summarize history for accuracy\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.savefig('{}_{}_accuracy.png'.format(model_name, date),bbox_inches='tight')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.savefig('{}_{}_loss.png'.format(model_name, date), bbox_inches='tight')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:47:40.035903Z","iopub.execute_input":"2022-10-04T17:47:40.036355Z","iopub.status.idle":"2022-10-04T17:47:40.681815Z","shell.execute_reply.started":"2022-10-04T17:47:40.036318Z","shell.execute_reply":"2022-10-04T17:47:40.680876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception.model.load_weights(\"./best_weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:47:41.502529Z","iopub.execute_input":"2022-10-04T17:47:41.503149Z","iopub.status.idle":"2022-10-04T17:47:41.645133Z","shell.execute_reply.started":"2022-10-04T17:47:41.503113Z","shell.execute_reply":"2022-10-04T17:47:41.644059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception.model.evaluate(X_val, y_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:47:43.205269Z","iopub.execute_input":"2022-10-04T17:47:43.205651Z","iopub.status.idle":"2022-10-04T17:47:46.436525Z","shell.execute_reply.started":"2022-10-04T17:47:43.205617Z","shell.execute_reply":"2022-10-04T17:47:46.435624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat = inception.model.predict(X_val, batch_size = BATCH_SIZE, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:47:48.190863Z","iopub.execute_input":"2022-10-04T17:47:48.191231Z","iopub.status.idle":"2022-10-04T17:47:50.153812Z","shell.execute_reply.started":"2022-10-04T17:47:48.191200Z","shell.execute_reply":"2022-10-04T17:47:50.152884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\nfrom itertools import cycle\n\ndef ROC_plot(y_true_ohe, y_hat_ohe, label_encoder, n_classes):    \n    lw = 2\n    fpr = dict()\n    tpr = dict()\n    roc_auc = dict()\n    for i in range(n_classes):\n        fpr[i], tpr[i], _ = roc_curve(y_true_ohe[:, i], y_hat_ohe[:, i])\n        roc_auc[i] = auc(fpr[i], tpr[i])\n                                  \n    all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))\n\n    mean_tpr = np.zeros_like(all_fpr)\n    for i in range(n_classes):\n        mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])\n\n    mean_tpr /= n_classes\n    fpr[\"macro\"] = all_fpr\n    tpr[\"macro\"] = mean_tpr\n    roc_auc[\"macro\"] = auc(fpr[\"macro\"], tpr[\"macro\"])\n\n    fpr[\"micro\"], tpr[\"micro\"], _ = roc_curve(y_true_ohe.ravel(), y_hat_ohe.ravel())\n    roc_auc[\"micro\"] = auc(fpr[\"micro\"], tpr[\"micro\"])\n    \n    plt.figure(figsize=(20,20))\n    plt.plot(\n        fpr[\"micro\"],\n        tpr[\"micro\"],\n        label=\"micro-average ROC curve (area = {0:0.2f})\".format(roc_auc[\"micro\"]),\n        color=\"deeppink\",\n        linestyle=\":\",\n        linewidth=4,\n    )\n\n    plt.plot(\n        fpr[\"macro\"],\n        tpr[\"macro\"],\n        label=\"macro-average ROC curve (area = {0:0.2f})\".format(roc_auc[\"macro\"]),\n        color=\"navy\",\n        linestyle=\":\",\n        linewidth=4,\n    )\n\n    colors = cycle([\"aqua\", \"darkorange\", \"cornflowerblue\"])\n    for i, color in zip(range(n_classes), colors):\n        plt.plot(\n            fpr[i],\n            tpr[i],\n            color=color,\n            lw=lw,\n            label=\"ROC curve of class {0} (area = {1:0.2f})\".format(list(label_encoder.keys())[i], roc_auc[i]))\n\n    plt.plot([0, 1], [0, 1], \"k--\", lw=lw)\n    plt.xlim([0.0, 1.0])\n    plt.ylim([0.0, 1.05])\n    plt.xlabel(\"False Positive Rate\")\n    plt.ylabel(\"True Positive Rate\")\n    plt.title(\"multiclass characteristic\")\n    plt.legend(loc=\"lower right\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:47:55.269397Z","iopub.execute_input":"2022-10-04T17:47:55.269792Z","iopub.status.idle":"2022-10-04T17:47:55.284845Z","shell.execute_reply.started":"2022-10-04T17:47:55.269759Z","shell.execute_reply":"2022-10-04T17:47:55.283623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROC_plot(y_val, y_hat, classes_index, NB_CLASSES)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:47:55.286761Z","iopub.execute_input":"2022-10-04T17:47:55.287138Z","iopub.status.idle":"2022-10-04T17:47:55.936411Z","shell.execute_reply.started":"2022-10-04T17:47:55.287102Z","shell.execute_reply":"2022-10-04T17:47:55.935512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_recall_fscore_support,confusion_matrix, classification_report, precision_score, recall_score\nfrom sklearn.metrics import f1_score as f1_score_rep\nimport seaborn as sn\nimport pandas as pd\n\n\ndef print_score(y_pred, y_real, label_encoder):\n    print(\"Accuracy: \", accuracy_score(y_real, y_pred))\n    print(\"Precision:: \", precision_score(y_real, y_pred, average=\"micro\"))\n    print(\"Recall:: \", recall_score(y_real, y_pred, average=\"micro\"))\n    print(\"F1_Score:: \", f1_score_rep(y_real, y_pred, average=\"micro\"))\n\n    print()\n    print(\"Macro precision_recall_fscore_support (macro) average\")\n    print(precision_recall_fscore_support(y_real, y_pred, average=\"macro\"))\n\n    print()\n    print(\"Macro precision_recall_fscore_support (micro) average\")\n    print(precision_recall_fscore_support(y_real, y_pred, average=\"micro\"))\n\n    print()\n    print(\"Macro precision_recall_fscore_support (weighted) average\")\n    print(precision_recall_fscore_support(y_real, y_pred, average=\"weighted\"))\n    \n    print()\n    print(\"Confusion Matrix\")\n    cm = confusion_matrix(y_real, y_pred)\n    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n    df_cm = pd.DataFrame(cm, index = [i for i in label_encoder],\n                  columns = [i for i in label_encoder])\n    plt.figure(figsize = (20,20))\n    sn.heatmap(df_cm, annot=True)\n\n    print()\n    print(\"Classification Report\")\n    print(classification_report(y_real, y_pred, target_names=label_encoder))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:49:04.856485Z","iopub.execute_input":"2022-10-04T17:49:04.857493Z","iopub.status.idle":"2022-10-04T17:49:04.868585Z","shell.execute_reply.started":"2022-10-04T17:49:04.857453Z","shell.execute_reply":"2022-10-04T17:49:04.867515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat = np.argmax(y_hat, axis=1)\ny_true = np.argmax(y_val, axis=1)\n\nprint_score(y_hat, y_true, classes)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T17:49:05.681676Z","iopub.execute_input":"2022-10-04T17:49:05.682059Z","iopub.status.idle":"2022-10-04T17:49:09.993454Z","shell.execute_reply.started":"2022-10-04T17:49:05.682027Z","shell.execute_reply":"2022-10-04T17:49:09.992503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reset -f","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:06:38.285157Z","iopub.execute_input":"2022-10-04T21:06:38.286157Z","iopub.status.idle":"2022-10-04T21:06:38.565224Z","shell.execute_reply.started":"2022-10-04T21:06:38.286114Z","shell.execute_reply":"2022-10-04T21:06:38.563760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:08:55.564345Z","iopub.execute_input":"2022-10-04T21:08:55.564822Z","iopub.status.idle":"2022-10-04T21:08:55.591667Z","shell.execute_reply.started":"2022-10-04T21:08:55.564724Z","shell.execute_reply":"2022-10-04T21:08:55.590606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"./data/train/audio/\"\n\nX_train = np.load(\"./data/X_train.npy\")\ny_train = np.load(\"./data/y_train.npy\")\n\nX_val = np.load(\"./data/X_val.npy\")\ny_val = np.load(\"./data/y_val.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:08:56.267017Z","iopub.execute_input":"2022-10-04T21:08:56.268255Z","iopub.status.idle":"2022-10-04T21:09:24.417393Z","shell.execute_reply.started":"2022-10-04T21:08:56.268202Z","shell.execute_reply":"2022-10-04T21:09:24.416172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:24.419534Z","iopub.execute_input":"2022-10-04T21:09:24.419986Z","iopub.status.idle":"2022-10-04T21:09:24.432213Z","shell.execute_reply.started":"2022-10-04T21:09:24.419953Z","shell.execute_reply":"2022-10-04T21:09:24.431257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape((-1, X_train.shape[1], X_train.shape[2]))\nX_val = X_val.reshape((-1, X_val.shape[1], X_val.shape[2]))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:24.433819Z","iopub.execute_input":"2022-10-04T21:09:24.434479Z","iopub.status.idle":"2022-10-04T21:09:24.443830Z","shell.execute_reply.started":"2022-10-04T21:09:24.434441Z","shell.execute_reply":"2022-10-04T21:09:24.442571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = os.listdir(train_dir)\nclasses","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:24.446914Z","iopub.execute_input":"2022-10-04T21:09:24.447311Z","iopub.status.idle":"2022-10-04T21:09:24.459764Z","shell.execute_reply.started":"2022-10-04T21:09:24.447274Z","shell.execute_reply":"2022-10-04T21:09:24.458689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NB_CLASSES = len(classes)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:24.461548Z","iopub.execute_input":"2022-10-04T21:09:24.462040Z","iopub.status.idle":"2022-10-04T21:09:24.467197Z","shell.execute_reply.started":"2022-10-04T21:09:24.461999Z","shell.execute_reply":"2022-10-04T21:09:24.466077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_list_dict(lst):\n    res_dct = {i: val for i, val in enumerate(lst)}\n    return res_dct\n         \nclasses_index = convert_list_dict(classes)\nclasses_index","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:24.469027Z","iopub.execute_input":"2022-10-04T21:09:24.470303Z","iopub.status.idle":"2022-10-04T21:09:24.480316Z","shell.execute_reply.started":"2022-10-04T21:09:24.470152Z","shell.execute_reply":"2022-10-04T21:09:24.479222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:24.482608Z","iopub.execute_input":"2022-10-04T21:09:24.482939Z","iopub.status.idle":"2022-10-04T21:09:35.874251Z","shell.execute_reply.started":"2022-10-04T21:09:24.482914Z","shell.execute_reply":"2022-10-04T21:09:35.873023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Conv1D, BatchNormalization, Activation, Input, Dense, Bidirectional, LSTM, Dropout, TimeDistributed, Lambda\nfrom keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom livelossplot import PlotLossesKeras\nfrom tensorflow.keras.metrics import Recall, Precision\nimport keras\nimport keras.backend as K\nimport time","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:35.877670Z","iopub.execute_input":"2022-10-04T21:09:35.878090Z","iopub.status.idle":"2022-10-04T21:09:41.285542Z","shell.execute_reply.started":"2022-10-04T21:09:35.878048Z","shell.execute_reply":"2022-10-04T21:09:41.284458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nchar_map_str = \"\"\"\n<SPACE> 0\na 1\nb 2\nc 3\nd 4\ne 5\nf 6\ng 7\nh 8\ni 9\nj 10\nk 11\nl 12\nm 13\nn 14\no 15\np 16\nq 17\nr 18\ns 19\nt 20\nu 21\nv 22\nw 23\nx 24\ny 25\nz 26\n' 27\n\"\"\"\n\nchar_map = {}\nindex_map = {}\n\nfor line in char_map_str.strip().split('\\n'):\n    ch, index = line.split()\n    char_map[ch] = int(index)\n    index_map[int(index)] = ch\n\nindex_map[0] = ' '","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:41.287075Z","iopub.execute_input":"2022-10-04T21:09:41.287808Z","iopub.status.idle":"2022-10-04T21:09:41.296162Z","shell.execute_reply.started":"2022-10-04T21:09:41.287766Z","shell.execute_reply":"2022-10-04T21:09:41.295123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def text_to_int(text):\n    \"\"\"\n    takes the character map and returns a series of \n    integers for the inserted text\n    the 'silence' class returns only 27's\n    \"\"\"\n    int_seq = []\n    if text == 'silence':\n        for r in range(8):\n            int_seq.append(27)\n    else:\n        for c in text:\n            ch = char_map[c]\n            int_seq.append(ch)\n    return int_seq","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:41.300698Z","iopub.execute_input":"2022-10-04T21:09:41.301418Z","iopub.status.idle":"2022-10-04T21:09:41.310103Z","shell.execute_reply.started":"2022-10-04T21:09:41.301377Z","shell.execute_reply":"2022-10-04T21:09:41.309161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_intseq(trans, max_len = 8):\n    \"\"\"\n    pads integer list with 27's up to max length\n    \"\"\"\n    t = text_to_int(trans)\n    while (len(t) < max_len):\n        t.append(27)\n    return t","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:41.311934Z","iopub.execute_input":"2022-10-04T21:09:41.312328Z","iopub.status.idle":"2022-10-04T21:09:41.324073Z","shell.execute_reply.started":"2022-10-04T21:09:41.312293Z","shell.execute_reply":"2022-10-04T21:09:41.322746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_ctc_params(y, classes_list, len_char_map = 28):\n    \"\"\"\n    Usage:\n        creates parameters required for K.ctc_batch_cost function \n    Args:\n        Y (ndarray): target set with all classes\n        classes_list (list): list with class names\n        len_char_map (int): length of the character map\n    Returns:\n        3 ndarrays\n    \"\"\"\n    labels = np.array([get_intseq(classes_list[y[l]]) for l, _ in enumerate(y)])\n    input_length = np.array([len_char_map for _ in y])\n    label_length = np.array([8 for _ in y])\n    return labels, input_length, label_length","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:41.326780Z","iopub.execute_input":"2022-10-04T21:09:41.327088Z","iopub.status.idle":"2022-10-04T21:09:41.337009Z","shell.execute_reply.started":"2022-10-04T21:09:41.327062Z","shell.execute_reply":"2022-10-04T21:09:41.335957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CTC():\n    \"\"\"\n    Usage:\n        sr_ctc = CTC(enter input_size and output_size)\n        sr_ctc.build()\n        sr_ctc.m.compile()\n        sr_ctc.tm.compile()\n    \"\"\" \n    def __init__(self, input_shape, nb_classes, weights_directory='./', nb_epochs=100, batch_size=64):\n        self.input_shape = input_shape\n        self.nb_classes = nb_classes\n        self.weights_directory = weights_directory\n        self.nb_epochs = nb_epochs\n        self.batch_size = batch_size\n        self.m = None\n        self.tm = None    \n        \n        self.build()\n        \n    def ctc_layer_func(self, args):\n        y_pred, labels, input_length, label_length = args\n        return K.ctc_batch_cost(labels, y_pred, input_length, label_length)\n    \n    # dummy loss\n    def ctc_loss(self, y_true, y_pred):\n        return y_pred\n        \n    def build(self, conv_filters=196, conv_size=13, conv_strides=4, activation=\"relu\", rnn_layers=2, lstm_units=128, drop_out=0.8):\n        \"\"\"\n        build CTC training model (self.m) and \n        prediction model without the ctc loss function (self.tm)\n        \n        Usage: \n            enter conv parameters for Cov1D layer\n            specify number of rnn layers, LSTM units and dropout\n        Args:\n            \n        Returns:\n            self.m: keras.engine.training.Model\n            self.tm: keras.engine.training.Model\n        \"\"\"        \n        \n        inputs = Input(shape=self.input_shape, name='input')\n        x = Conv1D(conv_filters, \n                   conv_size, \n                   strides = conv_strides, \n                   name = 'conv1d')(inputs)\n        x = BatchNormalization()(x)\n        x = Activation(activation)(x)\n        for _ in range(rnn_layers):          \n            x = Bidirectional(LSTM(lstm_units, \n                                   return_sequences = True))(x)\n            x = Dropout(drop_out)(x)\n            x = BatchNormalization()(x)\n        outputs = TimeDistributed(Dense(self.nb_classes, activation=\"softmax\"))(x)\n        \n        # ctc inputs\n        labels = Input(name=\"the_labels\", shape=[None,], dtype=\"int32\")\n        input_length = Input(name=\"input_length\", shape=[1], dtype=\"int32\")\n        label_length = Input(name=\"label_length\", shape=[1], dtype=\"int32\")\n        \n        ctc_layer = Lambda(self.ctc_layer_func, output_shape=(1,), name=\"ctc\")([outputs, labels, input_length, label_length])\n        self.tm = Model(inputs=inputs, outputs=outputs)\n        self.m = Model(inputs=[inputs,labels,input_length,label_length],\n                       outputs=ctc_layer)            \n        \n        self.m.compile(loss=self.ctc_loss, \n                       optimizer=Adam(),\n                       metrics=['accuracy'])\n        \n        self.tm.compile(loss=self.ctc_loss, \n                        optimizer=Adam())\n              \n        reduce_lr = ReduceLROnPlateau(monitor='val_accuracy', \n                                      factor=0.5, \n                                      patience=int(self.nb_epochs/20),\n                                      min_lr=0.0001)\n        \n        file_path = os.path.join(self.weights_directory,\"ctc_best_weights.h5\")\n        model_checkpoint = ModelCheckpoint(filepath=file_path, \n                                           monitor='val_accuracy',\n                                           mode=\"max\",\n                                           save_best_only=True)\n        \n        early_stopping = EarlyStopping(monitor=\"val_accuracy\", \n                                       mode=\"max\", \n                                       verbose=1, \n                                       patience=int(self.nb_epochs/10))\n        plotlosses = PlotLossesKeras()\n        self.callbacks = [reduce_lr, model_checkpoint, early_stopping, plotlosses]\n     \n        print(self.m.summary())\n        print(self.tm.summary())\n        \n        return self.m, self.tm\n    \n    \n    def fit(self, X_train, train_labels, train_input_length, train_label_length, y_train,\n                  X_val, val_labels, val_input_length, val_label_length, y_val):       \n        if self.batch_size is None:\n            mini_batch_size = int(min(x_train.shape[0] / 10, 16))\n        else:\n            mini_batch_size = self.batch_size\n\n        start_time = time.time()\n        hist = sr_ctc.m.fit([np.squeeze(X_train), \n                            train_labels, \n                            train_input_length, \n                            train_label_length], \n                       np.zeros([len(y_train)]), \n                       batch_size = self.batch_size, \n                       epochs = self.nb_epochs, \n                       validation_data = ([np.squeeze(X_val), \n                                           val_labels, \n                                           val_input_length, \n                                           val_label_length],\n                                          np.zeros([len(y_val)])), \n                       callbacks = self.callbacks, \n                       verbose = 1, \n                       shuffle = True)\n        \n        duration = time.time() - start_time\n        keras.backend.clear_session()\n        print(\"Model take {} S to train \".format(duration))\n        return hist\n    \n    def str_out(self, dataset):\n        k_ctc_out = K.ctc_decode(self.tm.predict(np.squeeze(dataset), \n                                                verbose = 1), \n                             np.array([28 for _ in dataset]))\n        decoded_out = K.eval(k_ctc_out[0][0])\n        str_decoded_out = []\n        for i, _ in enumerate(decoded_out):     \n            str_decoded_out.append(\"\".join([index_map[c] for c in decoded_out[i] if not c == -1]))\n\n        return str_decoded_out","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:41.338657Z","iopub.execute_input":"2022-10-04T21:09:41.339190Z","iopub.status.idle":"2022-10-04T21:09:41.364983Z","shell.execute_reply.started":"2022-10-04T21:09:41.339148Z","shell.execute_reply":"2022-10-04T21:09:41.363990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels, train_input_length, train_label_length = get_ctc_params(y=y_train, classes_list=classes)\nval_labels, val_input_length, val_label_length = get_ctc_params(y=y_val, classes_list=classes)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:09:41.367687Z","iopub.execute_input":"2022-10-04T21:09:41.368383Z","iopub.status.idle":"2022-10-04T21:09:41.745522Z","shell.execute_reply.started":"2022-10-04T21:09:41.368354Z","shell.execute_reply":"2022-10-04T21:09:41.744374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_SHAPE = X_train.shape[1:]\nBATCH_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:10:24.952853Z","iopub.execute_input":"2022-10-04T21:10:24.953268Z","iopub.status.idle":"2022-10-04T21:10:24.959388Z","shell.execute_reply.started":"2022-10-04T21:10:24.953233Z","shell.execute_reply":"2022-10-04T21:10:24.958104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WEIGHTS_DIR = \"./\"\nsr_ctc = CTC(INPUT_SHAPE, NB_CLASSES, WEIGHTS_DIR, nb_epochs=100, batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:10:27.631857Z","iopub.execute_input":"2022-10-04T21:10:27.632334Z","iopub.status.idle":"2022-10-04T21:10:31.970733Z","shell.execute_reply.started":"2022-10-04T21:10:27.632292Z","shell.execute_reply":"2022-10-04T21:10:31.969650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = sr_ctc.fit(X_train, train_labels, train_input_length, train_label_length, y_train,\n                     X_val, val_labels, val_input_length, val_label_length, y_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T21:10:37.341096Z","iopub.execute_input":"2022-10-04T21:10:37.341513Z","iopub.status.idle":"2022-10-04T22:15:20.726853Z","shell.execute_reply.started":"2022-10-04T21:10:37.341480Z","shell.execute_reply":"2022-10-04T22:15:20.725743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"date = '4-10-2022'\nmodel_name = 'CTC'","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:16:31.820334Z","iopub.execute_input":"2022-10-04T22:16:31.820784Z","iopub.status.idle":"2022-10-04T22:16:31.826794Z","shell.execute_reply.started":"2022-10-04T22:16:31.820742Z","shell.execute_reply":"2022-10-04T22:16:31.825619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\n#%% visualize training\nprint(history.history.keys())\n# summarize history for accuracy\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.savefig('{}_{}_accuracy.png'.format(model_name, date),bbox_inches='tight')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.savefig('{}_{}_loss.png'.format(model_name, date), bbox_inches='tight')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:16:34.367982Z","iopub.execute_input":"2022-10-04T22:16:34.368383Z","iopub.status.idle":"2022-10-04T22:17:12.739557Z","shell.execute_reply.started":"2022-10-04T22:16:34.368349Z","shell.execute_reply":"2022-10-04T22:17:12.738529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat = sr_ctc.str_out(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:17:12.752067Z","iopub.execute_input":"2022-10-04T22:17:12.753101Z","iopub.status.idle":"2022-10-04T22:17:16.534829Z","shell.execute_reply.started":"2022-10-04T22:17:12.753059Z","shell.execute_reply":"2022-10-04T22:17:16.533785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('PREDICTED: \\t REAL:')\nfor i in range(10):\n    print(y_hat[i], '\\t\\t',classes[y_val[i]])","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:17:19.496019Z","iopub.execute_input":"2022-10-04T22:17:19.496405Z","iopub.status.idle":"2022-10-04T22:17:19.503626Z","shell.execute_reply.started":"2022-10-04T22:17:19.496372Z","shell.execute_reply":"2022-10-04T22:17:19.502597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:19:16.003166Z","iopub.execute_input":"2022-10-04T22:19:16.003783Z","iopub.status.idle":"2022-10-04T22:19:16.012174Z","shell.execute_reply.started":"2022-10-04T22:19:16.003738Z","shell.execute_reply":"2022-10-04T22:19:16.011170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:22:03.742132Z","iopub.execute_input":"2022-10-04T22:22:03.742542Z","iopub.status.idle":"2022-10-04T22:22:03.749981Z","shell.execute_reply.started":"2022-10-04T22:22:03.742508Z","shell.execute_reply":"2022-10-04T22:22:03.748918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_index_rev = dict([(val, k) for k, val in classes_index.items()])\nclasses_index_rev","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:25:27.373539Z","iopub.execute_input":"2022-10-04T22:25:27.374337Z","iopub.status.idle":"2022-10-04T22:25:27.385793Z","shell.execute_reply.started":"2022-10-04T22:25:27.374297Z","shell.execute_reply":"2022-10-04T22:25:27.384619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import difflib\n\ndef get_close_word(y_hat, classes_index_rev):\n    keys = list(classes_index_rev.keys())\n    result = []\n    for y in y_hat:\n        y = y.replace(\"'\", '')\n        y = difflib.get_close_matches(y, keys)\n        if not y:\n            result.append(classes_index_rev[\"silence\"])\n            continue\n        \n        y = y[0]\n        result.append(classes_index_rev[y])        \n            \n    return result\n\ny_hat_clean = get_close_word(y_hat, classes_index_rev)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:37:42.324602Z","iopub.execute_input":"2022-10-04T22:37:42.325330Z","iopub.status.idle":"2022-10-04T22:37:43.160403Z","shell.execute_reply.started":"2022-10-04T22:37:42.325288Z","shell.execute_reply":"2022-10-04T22:37:43.159283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_recall_fscore_support,confusion_matrix, classification_report, precision_score, recall_score\nfrom sklearn.metrics import f1_score as f1_score_rep\nimport seaborn as sn\nimport pandas as pd\n\n\ndef print_score(y_pred, y_real, label_encoder):\n    print(\"Accuracy: \", accuracy_score(y_real, y_pred))\n    print(\"Precision:: \", precision_score(y_real, y_pred, average=\"micro\"))\n    print(\"Recall:: \", recall_score(y_real, y_pred, average=\"micro\"))\n    print(\"F1_Score:: \", f1_score_rep(y_real, y_pred, average=\"micro\"))\n\n    print()\n    print(\"Macro precision_recall_fscore_support (macro) average\")\n    print(precision_recall_fscore_support(y_real, y_pred, average=\"macro\"))\n\n    print()\n    print(\"Macro precision_recall_fscore_support (micro) average\")\n    print(precision_recall_fscore_support(y_real, y_pred, average=\"micro\"))\n\n    print()\n    print(\"Macro precision_recall_fscore_support (weighted) average\")\n    print(precision_recall_fscore_support(y_real, y_pred, average=\"weighted\"))\n    \n    print()\n    print(\"Confusion Matrix\")\n    cm = confusion_matrix(y_real, y_pred)\n    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n    df_cm = pd.DataFrame(cm, index = [i for i in label_encoder],\n                  columns = [i for i in label_encoder])\n    plt.figure(figsize = (20,20))\n    sn.heatmap(df_cm, annot=True)\n\n    print()\n    print(\"Classification Report\")\n    print(classification_report(y_real, y_pred, target_names=label_encoder))","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:42:19.052823Z","iopub.execute_input":"2022-10-04T22:42:19.053199Z","iopub.status.idle":"2022-10-04T22:42:19.481641Z","shell.execute_reply.started":"2022-10-04T22:42:19.053169Z","shell.execute_reply":"2022-10-04T22:42:19.480634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_score(y_hat_clean, y_val,classes_index_rev)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T22:42:39.242130Z","iopub.execute_input":"2022-10-04T22:42:39.242516Z","iopub.status.idle":"2022-10-04T22:42:43.450495Z","shell.execute_reply.started":"2022-10-04T22:42:39.242483Z","shell.execute_reply":"2022-10-04T22:42:43.449586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}