{"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":"2023-01-17T17:25:24.938181Z","iopub.execute_input":"2023-01-17T17:25:24.938511Z","iopub.status.idle":"2023-01-17T17:25:24.971117Z","shell.execute_reply.started":"2023-01-17T17:25:24.938419Z","shell.execute_reply":"2023-01-17T17:25:24.970259Z"},"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":"2023-01-17T17:25:24.974564Z","iopub.execute_input":"2023-01-17T17:25:24.974827Z","iopub.status.idle":"2023-01-17T17:25:26.919863Z","shell.execute_reply.started":"2023-01-17T17:25:24.974804Z","shell.execute_reply":"2023-01-17T17:25:26.918608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyunpack \n!pip install patool","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:25:26.930755Z","iopub.execute_input":"2023-01-17T17:25:26.931515Z","iopub.status.idle":"2023-01-17T17:25:49.011052Z","shell.execute_reply.started":"2023-01-17T17:25:26.931462Z","shell.execute_reply":"2023-01-17T17:25:49.009879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"./data\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:25:49.014576Z","iopub.execute_input":"2023-01-17T17:25:49.014970Z","iopub.status.idle":"2023-01-17T17:25:49.024475Z","shell.execute_reply.started":"2023-01-17T17:25:49.014920Z","shell.execute_reply":"2023-01-17T17:25:49.023000Z"},"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":"2023-01-17T17:25:49.026113Z","iopub.execute_input":"2023-01-17T17:25:49.027197Z","iopub.status.idle":"2023-01-17T17:27:39.044192Z","shell.execute_reply.started":"2023-01-17T17:25:49.027159Z","shell.execute_reply":"2023-01-17T17:27:39.042847Z"},"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":"2023-01-17T17:27:39.046287Z","iopub.execute_input":"2023-01-17T17:27:39.046722Z","iopub.status.idle":"2023-01-17T17:27:39.057226Z","shell.execute_reply.started":"2023-01-17T17:27:39.046678Z","shell.execute_reply":"2023-01-17T17:27:39.056286Z"},"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":"2023-01-17T17:27:39.058756Z","iopub.execute_input":"2023-01-17T17:27:39.059329Z","iopub.status.idle":"2023-01-17T17:27:39.091291Z","shell.execute_reply.started":"2023-01-17T17:27:39.059292Z","shell.execute_reply":"2023-01-17T17:27:39.090230Z"},"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":"2023-01-17T17:27:39.093205Z","iopub.execute_input":"2023-01-17T17:27:39.093597Z","iopub.status.idle":"2023-01-17T17:27:39.099041Z","shell.execute_reply.started":"2023-01-17T17:27:39.093557Z","shell.execute_reply":"2023-01-17T17:27:39.098024Z"},"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":"2023-01-17T17:27:39.100665Z","iopub.execute_input":"2023-01-17T17:27:39.101442Z","iopub.status.idle":"2023-01-17T17:27:39.109437Z","shell.execute_reply.started":"2023-01-17T17:27:39.101404Z","shell.execute_reply":"2023-01-17T17:27:39.108498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_silence()","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:27:39.113961Z","iopub.execute_input":"2023-01-17T17:27:39.114886Z","iopub.status.idle":"2023-01-17T17:27:41.292280Z","shell.execute_reply.started":"2023-01-17T17:27:39.114860Z","shell.execute_reply":"2023-01-17T17:27:41.291007Z"},"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":"2023-01-17T17:27:41.294319Z","iopub.execute_input":"2023-01-17T17:27:41.294733Z","iopub.status.idle":"2023-01-17T17:27:41.302273Z","shell.execute_reply.started":"2023-01-17T17:27:41.294692Z","shell.execute_reply":"2023-01-17T17:27:41.301145Z"},"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":"2023-01-18T17:32:12.075548Z","iopub.execute_input":"2023-01-18T17:32:12.076369Z","iopub.status.idle":"2023-01-18T17:32:12.154004Z","shell.execute_reply.started":"2023-01-18T17:32:12.076297Z","shell.execute_reply":"2023-01-18T17:32:12.152741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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":"2023-01-17T17:27:41.316637Z","iopub.execute_input":"2023-01-17T17:27:41.317061Z","iopub.status.idle":"2023-01-17T17:27:41.327298Z","shell.execute_reply.started":"2023-01-17T17:27:41.317027Z","shell.execute_reply":"2023-01-17T17:27:41.326225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#validation_list.extend(testing_list)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:27:41.328658Z","iopub.execute_input":"2023-01-17T17:27:41.329002Z","iopub.status.idle":"2023-01-17T17:27:41.333690Z","shell.execute_reply.started":"2023-01-17T17:27:41.328960Z","shell.execute_reply":"2023-01-17T17:27:41.332510Z"},"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":"2023-01-17T17:27:41.335271Z","iopub.execute_input":"2023-01-17T17:27:41.335928Z","iopub.status.idle":"2023-01-17T17:27:41.343208Z","shell.execute_reply.started":"2023-01-17T17:27:41.335891Z","shell.execute_reply":"2023-01-17T17:27:41.342250Z"},"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":"2023-01-17T17:27:41.344919Z","iopub.execute_input":"2023-01-17T17:27:41.345289Z","iopub.status.idle":"2023-01-17T17:27:47.830409Z","shell.execute_reply.started":"2023-01-17T17:27:41.345256Z","shell.execute_reply":"2023-01-17T17:27:47.829260Z"},"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":"2023-01-17T17:27:47.831665Z","iopub.execute_input":"2023-01-17T17:27:47.832040Z","iopub.status.idle":"2023-01-17T17:27:47.836425Z","shell.execute_reply.started":"2023-01-17T17:27:47.831999Z","shell.execute_reply":"2023-01-17T17:27:47.835552Z"},"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":"2023-01-17T17:27:47.837852Z","iopub.execute_input":"2023-01-17T17:27:47.838379Z","iopub.status.idle":"2023-01-17T17:27:47.849555Z","shell.execute_reply.started":"2023-01-17T17:27:47.838344Z","shell.execute_reply":"2023-01-17T17:27:47.848510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(class_counts)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:27:47.850861Z","iopub.execute_input":"2023-01-17T17:27:47.851280Z","iopub.status.idle":"2023-01-17T17:27:47.858636Z","shell.execute_reply.started":"2023-01-17T17:27:47.851246Z","shell.execute_reply":"2023-01-17T17:27:47.857537Z"},"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":"2023-01-17T17:27:47.860114Z","iopub.execute_input":"2023-01-17T17:27:47.861118Z","iopub.status.idle":"2023-01-17T17:27:48.093449Z","shell.execute_reply.started":"2023-01-17T17:27:47.861083Z","shell.execute_reply":"2023-01-17T17:27:48.092463Z"},"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":"2023-01-17T17:27:48.094883Z","iopub.execute_input":"2023-01-17T17:27:48.095325Z","iopub.status.idle":"2023-01-17T17:27:48.103342Z","shell.execute_reply.started":"2023-01-17T17:27:48.095289Z","shell.execute_reply":"2023-01-17T17:27:48.102343Z"},"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":"2023-01-17T17:27:48.105097Z","iopub.execute_input":"2023-01-17T17:27:48.105862Z","iopub.status.idle":"2023-01-17T17:27:48.383789Z","shell.execute_reply.started":"2023-01-17T17:27:48.105824Z","shell.execute_reply":"2023-01-17T17:27:48.382817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_spec('yes/bfdb9801_nohash_0.wav').shape","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:27:48.385477Z","iopub.execute_input":"2023-01-17T17:27:48.386131Z","iopub.status.idle":"2023-01-17T17:27:48.401172Z","shell.execute_reply.started":"2023-01-17T17:27:48.386094Z","shell.execute_reply":"2023-01-17T17:27:48.400028Z"},"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":"2023-01-17T17:27:48.402799Z","iopub.execute_input":"2023-01-17T17:27:48.403432Z","iopub.status.idle":"2023-01-17T17:27:48.411575Z","shell.execute_reply.started":"2023-01-17T17:27:48.403393Z","shell.execute_reply":"2023-01-17T17:27:48.410206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = create_sets() # takes a while","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:27:48.413658Z","iopub.execute_input":"2023-01-17T17:27:48.414355Z","iopub.status.idle":"2023-01-17T17:33:55.980851Z","shell.execute_reply.started":"2023-01-17T17:27:48.414321Z","shell.execute_reply":"2023-01-17T17:33:55.979478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:33:55.988364Z","iopub.execute_input":"2023-01-17T17:33:55.991577Z","iopub.status.idle":"2023-01-17T17:33:56.006363Z","shell.execute_reply.started":"2023-01-17T17:33:55.991520Z","shell.execute_reply":"2023-01-17T17:33:56.004909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:33:56.010957Z","iopub.execute_input":"2023-01-17T17:33:56.015067Z","iopub.status.idle":"2023-01-17T17:33:56.026056Z","shell.execute_reply.started":"2023-01-17T17:33:56.015016Z","shell.execute_reply":"2023-01-17T17:33:56.024573Z"},"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":"2023-01-17T17:33:56.046852Z","iopub.execute_input":"2023-01-17T17:33:56.049850Z","iopub.status.idle":"2023-01-17T17:33:56.290289Z","shell.execute_reply.started":"2023-01-17T17:33:56.049800Z","shell.execute_reply":"2023-01-17T17:33:56.289379Z"},"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":"2023-01-17T17:33:56.292012Z","iopub.execute_input":"2023-01-17T17:33:56.292673Z","iopub.status.idle":"2023-01-17T17:34:12.975384Z","shell.execute_reply.started":"2023-01-17T17:33:56.292636Z","shell.execute_reply":"2023-01-17T17:34:12.974301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(X_train.flatten(), bins=50)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:34:12.976915Z","iopub.execute_input":"2023-01-17T17:34:12.978790Z","iopub.status.idle":"2023-01-17T17:34:25.664448Z","shell.execute_reply.started":"2023-01-17T17:34:12.978742Z","shell.execute_reply":"2023-01-17T17:34:25.663511Z"},"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":"2023-01-17T17:34:25.665941Z","iopub.execute_input":"2023-01-17T17:34:25.666300Z","iopub.status.idle":"2023-01-17T17:34:46.382361Z","shell.execute_reply.started":"2023-01-17T17:34:25.666265Z","shell.execute_reply":"2023-01-17T17:34:46.377598Z"},"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":"2023-01-17T17:34:46.386799Z","iopub.execute_input":"2023-01-17T17:34:46.387148Z","iopub.status.idle":"2023-01-17T17:35:36.572598Z","shell.execute_reply.started":"2023-01-17T17:34:46.387104Z","shell.execute_reply":"2023-01-17T17:35:36.571242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(X_val.flatten(), bins=50)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:35:36.579089Z","iopub.execute_input":"2023-01-17T17:35:36.582327Z","iopub.status.idle":"2023-01-17T17:35:38.440425Z","shell.execute_reply.started":"2023-01-17T17:35:36.582273Z","shell.execute_reply":"2023-01-17T17:35:38.439552Z"},"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":"2023-01-17T17:35:38.444652Z","iopub.execute_input":"2023-01-17T17:35:38.446790Z","iopub.status.idle":"2023-01-17T17:35:39.114212Z","shell.execute_reply.started":"2023-01-17T17:35:38.446751Z","shell.execute_reply":"2023-01-17T17:35:39.113172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reset -f","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:35:39.115584Z","iopub.execute_input":"2023-01-17T17:35:39.116539Z","iopub.status.idle":"2023-01-17T17:35:39.697992Z","shell.execute_reply.started":"2023-01-17T17:35:39.116481Z","shell.execute_reply":"2023-01-17T17:35:39.697008Z"},"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":"2023-01-17T17:35:39.699504Z","iopub.execute_input":"2023-01-17T17:35:39.699846Z","iopub.status.idle":"2023-01-17T17:35:39.705303Z","shell.execute_reply.started":"2023-01-17T17:35:39.699813Z","shell.execute_reply":"2023-01-17T17:35:39.704310Z"},"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":"2023-01-17T17:35:39.706760Z","iopub.execute_input":"2023-01-17T17:35:39.707323Z","iopub.status.idle":"2023-01-17T17:36:00.879510Z","shell.execute_reply.started":"2023-01-17T17:35:39.707289Z","shell.execute_reply":"2023-01-17T17:36:00.878464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:36:00.881049Z","iopub.execute_input":"2023-01-17T17:36:00.881474Z","iopub.status.idle":"2023-01-17T17:36:00.889300Z","shell.execute_reply.started":"2023-01-17T17:36:00.881416Z","shell.execute_reply":"2023-01-17T17:36:00.888132Z"},"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":"2023-01-17T17:36:00.890912Z","iopub.execute_input":"2023-01-17T17:36:00.891537Z","iopub.status.idle":"2023-01-17T17:36:00.896806Z","shell.execute_reply.started":"2023-01-17T17:36:00.891473Z","shell.execute_reply":"2023-01-17T17:36:00.895868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = os.listdir(train_dir)\nclasses","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:36:00.898265Z","iopub.execute_input":"2023-01-17T17:36:00.898858Z","iopub.status.idle":"2023-01-17T17:36:00.908846Z","shell.execute_reply.started":"2023-01-17T17:36:00.898822Z","shell.execute_reply":"2023-01-17T17:36:00.907706Z"},"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":"2023-01-17T17:36:00.910609Z","iopub.execute_input":"2023-01-17T17:36:00.910952Z","iopub.status.idle":"2023-01-17T17:36:00.928967Z","shell.execute_reply.started":"2023-01-17T17:36:00.910918Z","shell.execute_reply":"2023-01-17T17:36:00.927941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NB_CLASSES = len(classes)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:36:00.930371Z","iopub.execute_input":"2023-01-17T17:36:00.930801Z","iopub.status.idle":"2023-01-17T17:36:00.936117Z","shell.execute_reply.started":"2023-01-17T17:36:00.930767Z","shell.execute_reply":"2023-01-17T17:36:00.935144Z"},"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":"2023-01-17T17:36:00.937524Z","iopub.execute_input":"2023-01-17T17:36:00.938526Z","iopub.status.idle":"2023-01-17T17:36:00.947421Z","shell.execute_reply.started":"2023-01-17T17:36:00.938468Z","shell.execute_reply":"2023-01-17T17:36:00.946349Z"},"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":"2023-01-17T17:36:00.948989Z","iopub.execute_input":"2023-01-17T17:36:00.949450Z","iopub.status.idle":"2023-01-17T17:36:05.838196Z","shell.execute_reply.started":"2023-01-17T17:36:00.949413Z","shell.execute_reply":"2023-01-17T17:36:05.837137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:36:05.839783Z","iopub.execute_input":"2023-01-17T17:36:05.840625Z","iopub.status.idle":"2023-01-17T17:36:16.182045Z","shell.execute_reply.started":"2023-01-17T17:36:05.840577Z","shell.execute_reply":"2023-01-17T17:36:16.180838Z"},"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":"2023-01-17T17:36:16.185360Z","iopub.execute_input":"2023-01-17T17:36:16.185798Z","iopub.status.idle":"2023-01-17T17:36:16.202975Z","shell.execute_reply.started":"2023-01-17T17:36:16.185752Z","shell.execute_reply":"2023-01-17T17:36:16.202105Z"},"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":"2023-01-17T17:36:16.205226Z","iopub.execute_input":"2023-01-17T17:36:16.206095Z","iopub.status.idle":"2023-01-17T17:36:16.213345Z","shell.execute_reply.started":"2023-01-17T17:36:16.206058Z","shell.execute_reply":"2023-01-17T17:36:16.212466Z"},"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":"2023-01-17T17:36:16.214903Z","iopub.execute_input":"2023-01-17T17:36:16.215348Z","iopub.status.idle":"2023-01-17T17:36:16.237926Z","shell.execute_reply.started":"2023-01-17T17:36:16.215315Z","shell.execute_reply":"2023-01-17T17:36:16.236220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_SHAPE = X_train.shape[1:]\nBATCH_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2023-01-17T17:36:16.240063Z","iopub.execute_input":"2023-01-17T17:36:16.240590Z","iopub.status.idle":"2023-01-17T17:36:16.247762Z","shell.execute_reply.started":"2023-01-17T17:36:16.240554Z","shell.execute_reply":"2023-01-17T17:36:16.246821Z"},"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":"2023-01-17T17:36:16.250197Z","iopub.execute_input":"2023-01-17T17:36:16.250465Z","iopub.status.idle":"2023-01-17T17:36:20.101226Z","shell.execute_reply.started":"2023-01-17T17:36:16.250423Z","shell.execute_reply":"2023-01-17T17:36:20.100249Z"},"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":"2023-01-17T17:36:20.102911Z","iopub.execute_input":"2023-01-17T17:36:20.103606Z","iopub.status.idle":"2023-01-17T17:36:22.318379Z","shell.execute_reply.started":"2023-01-17T17:36:20.103565Z","shell.execute_reply":"2023-01-17T17:36:22.317236Z"},"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":"2023-01-17T17:36:22.319913Z","iopub.execute_input":"2023-01-17T17:36:22.320984Z","iopub.status.idle":"2023-01-17T18:09:49.310061Z","shell.execute_reply.started":"2023-01-17T17:36:22.320855Z","shell.execute_reply":"2023-01-17T18:09:49.308911Z"},"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":"2023-01-17T18:09:49.314958Z","iopub.execute_input":"2023-01-17T18:09:49.315257Z","iopub.status.idle":"2023-01-17T18:09:50.390410Z","shell.execute_reply.started":"2023-01-17T18:09:49.315229Z","shell.execute_reply":"2023-01-17T18:09:50.389425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception.model.load_weights(\"./best_weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:09:50.391798Z","iopub.execute_input":"2023-01-17T18:09:50.392254Z","iopub.status.idle":"2023-01-17T18:09:50.500682Z","shell.execute_reply.started":"2023-01-17T18:09:50.392217Z","shell.execute_reply":"2023-01-17T18:09:50.499760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception.model.evaluate(X_val, y_val)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:09:50.502000Z","iopub.execute_input":"2023-01-17T18:09:50.502361Z","iopub.status.idle":"2023-01-17T18:09:54.540775Z","shell.execute_reply.started":"2023-01-17T18:09:50.502321Z","shell.execute_reply":"2023-01-17T18:09:54.539900Z"},"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":"2023-01-17T18:09:54.544780Z","iopub.execute_input":"2023-01-17T18:09:54.546954Z","iopub.status.idle":"2023-01-17T18:09:57.453860Z","shell.execute_reply.started":"2023-01-17T18:09:54.546915Z","shell.execute_reply":"2023-01-17T18:09:57.452946Z"},"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":"2023-01-17T18:09:57.456933Z","iopub.execute_input":"2023-01-17T18:09:57.457289Z","iopub.status.idle":"2023-01-17T18:09:57.471830Z","shell.execute_reply.started":"2023-01-17T18:09:57.457260Z","shell.execute_reply":"2023-01-17T18:09:57.470476Z"},"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":"2023-01-17T18:09:57.473305Z","iopub.execute_input":"2023-01-17T18:09:57.473931Z","iopub.status.idle":"2023-01-17T18:09:58.071199Z","shell.execute_reply.started":"2023-01-17T18:09:57.473896Z","shell.execute_reply":"2023-01-17T18:09:58.070281Z"},"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":"2023-01-17T18:09:58.072834Z","iopub.execute_input":"2023-01-17T18:09:58.073606Z","iopub.status.idle":"2023-01-17T18:09:58.223152Z","shell.execute_reply.started":"2023-01-17T18:09:58.073568Z","shell.execute_reply":"2023-01-17T18:09:58.222211Z"},"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":"2023-01-17T18:09:58.224356Z","iopub.execute_input":"2023-01-17T18:09:58.226033Z","iopub.status.idle":"2023-01-17T18:10:01.986935Z","shell.execute_reply.started":"2023-01-17T18:09:58.225994Z","shell.execute_reply":"2023-01-17T18:10:01.985831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reset -f","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:10:01.988571Z","iopub.execute_input":"2023-01-17T18:10:01.988922Z","iopub.status.idle":"2023-01-17T18:10:02.709384Z","shell.execute_reply.started":"2023-01-17T18:10:01.988887Z","shell.execute_reply":"2023-01-17T18:10:02.708392Z"},"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":"2023-01-17T18:10:02.711570Z","iopub.execute_input":"2023-01-17T18:10:02.711987Z","iopub.status.idle":"2023-01-17T18:10:02.719448Z","shell.execute_reply.started":"2023-01-17T18:10:02.711948Z","shell.execute_reply":"2023-01-17T18:10:02.718526Z"},"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":"2023-01-17T18:10:02.720979Z","iopub.execute_input":"2023-01-17T18:10:02.721406Z","iopub.status.idle":"2023-01-17T18:10:23.958470Z","shell.execute_reply.started":"2023-01-17T18:10:02.721370Z","shell.execute_reply":"2023-01-17T18:10:23.956685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:10:23.959948Z","iopub.execute_input":"2023-01-17T18:10:23.961129Z","iopub.status.idle":"2023-01-17T18:10:23.969021Z","shell.execute_reply.started":"2023-01-17T18:10:23.961091Z","shell.execute_reply":"2023-01-17T18:10:23.967902Z"},"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":"2023-01-17T18:10:23.970673Z","iopub.execute_input":"2023-01-17T18:10:23.971275Z","iopub.status.idle":"2023-01-17T18:10:23.981664Z","shell.execute_reply.started":"2023-01-17T18:10:23.971240Z","shell.execute_reply":"2023-01-17T18:10:23.980518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = os.listdir(train_dir)\nclasses","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:10:23.984438Z","iopub.execute_input":"2023-01-17T18:10:23.985277Z","iopub.status.idle":"2023-01-17T18:10:23.994088Z","shell.execute_reply.started":"2023-01-17T18:10:23.985242Z","shell.execute_reply":"2023-01-17T18:10:23.992839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NB_CLASSES = len(classes)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:10:23.996014Z","iopub.execute_input":"2023-01-17T18:10:23.996548Z","iopub.status.idle":"2023-01-17T18:10:24.002884Z","shell.execute_reply.started":"2023-01-17T18:10:23.996512Z","shell.execute_reply":"2023-01-17T18:10:24.001763Z"},"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":"2023-01-17T18:10:24.006087Z","iopub.execute_input":"2023-01-17T18:10:24.006346Z","iopub.status.idle":"2023-01-17T18:10:24.016068Z","shell.execute_reply.started":"2023-01-17T18:10:24.006322Z","shell.execute_reply":"2023-01-17T18:10:24.015029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:10:24.017774Z","iopub.execute_input":"2023-01-17T18:10:24.018321Z","iopub.status.idle":"2023-01-17T18:10:35.261004Z","shell.execute_reply.started":"2023-01-17T18:10:24.018285Z","shell.execute_reply":"2023-01-17T18:10:35.259797Z"},"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":"2023-01-17T18:10:35.263051Z","iopub.execute_input":"2023-01-17T18:10:35.263353Z","iopub.status.idle":"2023-01-17T18:10:35.269963Z","shell.execute_reply.started":"2023-01-17T18:10:35.263325Z","shell.execute_reply":"2023-01-17T18:10:35.268947Z"},"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":"2023-01-17T18:10:35.271554Z","iopub.execute_input":"2023-01-17T18:10:35.271905Z","iopub.status.idle":"2023-01-17T18:10:35.280131Z","shell.execute_reply.started":"2023-01-17T18:10:35.271871Z","shell.execute_reply":"2023-01-17T18:10:35.279006Z"},"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":"2023-01-17T18:10:35.282163Z","iopub.execute_input":"2023-01-17T18:10:35.282705Z","iopub.status.idle":"2023-01-17T18:10:35.289456Z","shell.execute_reply.started":"2023-01-17T18:10:35.282670Z","shell.execute_reply":"2023-01-17T18:10:35.288442Z"},"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":"2023-01-17T18:10:35.290793Z","iopub.execute_input":"2023-01-17T18:10:35.291780Z","iopub.status.idle":"2023-01-17T18:10:35.298869Z","shell.execute_reply.started":"2023-01-17T18:10:35.291746Z","shell.execute_reply":"2023-01-17T18:10:35.297951Z"},"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":"2023-01-17T18:10:35.300159Z","iopub.execute_input":"2023-01-17T18:10:35.300990Z","iopub.status.idle":"2023-01-17T18:10:35.308644Z","shell.execute_reply.started":"2023-01-17T18:10:35.300954Z","shell.execute_reply":"2023-01-17T18:10:35.307628Z"},"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":"2023-01-17T18:10:35.310837Z","iopub.execute_input":"2023-01-17T18:10:35.311543Z","iopub.status.idle":"2023-01-17T18:10:35.335052Z","shell.execute_reply.started":"2023-01-17T18:10:35.311508Z","shell.execute_reply":"2023-01-17T18:10:35.334035Z"},"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":"2023-01-17T18:10:35.336765Z","iopub.execute_input":"2023-01-17T18:10:35.337159Z","iopub.status.idle":"2023-01-17T18:10:35.544442Z","shell.execute_reply.started":"2023-01-17T18:10:35.337124Z","shell.execute_reply":"2023-01-17T18:10:35.543536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_SHAPE = X_train.shape[1:]\nBATCH_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2023-01-17T18:10:35.545809Z","iopub.execute_input":"2023-01-17T18:10:35.546351Z","iopub.status.idle":"2023-01-17T18:10:35.552185Z","shell.execute_reply.started":"2023-01-17T18:10:35.546315Z","shell.execute_reply":"2023-01-17T18:10:35.550386Z"},"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":"2023-01-17T18:10:35.553661Z","iopub.execute_input":"2023-01-17T18:10:35.554678Z","iopub.status.idle":"2023-01-17T18:10:36.706371Z","shell.execute_reply.started":"2023-01-17T18:10:35.554640Z","shell.execute_reply":"2023-01-17T18:10:36.705326Z"},"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":"2023-01-17T18:10:36.708100Z","iopub.execute_input":"2023-01-17T18:10:36.708464Z","iopub.status.idle":"2023-01-17T19:11:11.137437Z","shell.execute_reply.started":"2023-01-17T18:10:36.708426Z","shell.execute_reply":"2023-01-17T19:11:11.136299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"date = '4-10-2022'\nmodel_name = 'CTC'","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:11:11.143795Z","iopub.execute_input":"2023-01-17T19:11:11.144093Z","iopub.status.idle":"2023-01-17T19:11:11.149294Z","shell.execute_reply.started":"2023-01-17T19:11:11.144065Z","shell.execute_reply":"2023-01-17T19:11:11.148186Z"},"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":"2023-01-17T19:11:11.150865Z","iopub.execute_input":"2023-01-17T19:11:11.151842Z","iopub.status.idle":"2023-01-17T19:11:13.412923Z","shell.execute_reply.started":"2023-01-17T19:11:11.151805Z","shell.execute_reply":"2023-01-17T19:11:13.411962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_hat = sr_ctc.str_out(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:11:13.427396Z","iopub.execute_input":"2023-01-17T19:11:13.429860Z","iopub.status.idle":"2023-01-17T19:11:16.551273Z","shell.execute_reply.started":"2023-01-17T19:11:13.429822Z","shell.execute_reply":"2023-01-17T19:11:16.550321Z"},"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":"2023-01-17T19:11:16.552845Z","iopub.execute_input":"2023-01-17T19:11:16.553209Z","iopub.status.idle":"2023-01-17T19:11:16.559447Z","shell.execute_reply.started":"2023-01-17T19:11:16.553173Z","shell.execute_reply":"2023-01-17T19:11:16.558291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:11:16.560967Z","iopub.execute_input":"2023-01-17T19:11:16.562238Z","iopub.status.idle":"2023-01-17T19:11:16.571057Z","shell.execute_reply.started":"2023-01-17T19:11:16.562200Z","shell.execute_reply":"2023-01-17T19:11:16.569814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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":"2023-01-17T19:11:16.572474Z","iopub.execute_input":"2023-01-17T19:11:16.573003Z","iopub.status.idle":"2023-01-17T19:11:16.581864Z","shell.execute_reply.started":"2023-01-17T19:11:16.572868Z","shell.execute_reply":"2023-01-17T19:11:16.580444Z"},"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":"2023-01-17T19:11:16.583575Z","iopub.execute_input":"2023-01-17T19:11:16.584380Z","iopub.status.idle":"2023-01-17T19:11:17.326136Z","shell.execute_reply.started":"2023-01-17T19:11:16.584344Z","shell.execute_reply":"2023-01-17T19:11:17.325160Z"},"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":"2023-01-17T19:11:17.327722Z","iopub.execute_input":"2023-01-17T19:11:17.328114Z","iopub.status.idle":"2023-01-17T19:11:17.338457Z","shell.execute_reply.started":"2023-01-17T19:11:17.328072Z","shell.execute_reply":"2023-01-17T19:11:17.337390Z"},"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":"2023-01-17T19:11:17.340243Z","iopub.execute_input":"2023-01-17T19:11:17.340604Z","iopub.status.idle":"2023-01-17T19:11:21.714286Z","shell.execute_reply.started":"2023-01-17T19:11:17.340569Z","shell.execute_reply":"2023-01-17T19:11:21.713415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}