{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:30:07.155636Z","iopub.execute_input":"2022-02-27T12:30:07.155960Z","iopub.status.idle":"2022-02-27T12:30:07.161557Z","shell.execute_reply.started":"2022-02-27T12:30:07.155928Z","shell.execute_reply":"2022-02-27T12:30:07.160616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:30:07.163127Z","iopub.execute_input":"2022-02-27T12:30:07.163567Z","iopub.status.idle":"2022-02-27T12:30:07.178995Z","shell.execute_reply.started":"2022-02-27T12:30:07.163536Z","shell.execute_reply":"2022-02-27T12:30:07.178062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt-get install -y p7zip-full","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:30:07.180389Z","iopub.execute_input":"2022-02-27T12:30:07.181079Z","iopub.status.idle":"2022-02-27T12:30:09.523319Z","shell.execute_reply.started":"2022-02-27T12:30:07.181045Z","shell.execute_reply":"2022-02-27T12:30:09.522411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!7z x ../input/tensorflow-speech-recognition-challenge/train.7z","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:30:09.524478Z","iopub.execute_input":"2022-02-27T12:30:09.524711Z","iopub.status.idle":"2022-02-27T12:32:02.076722Z","shell.execute_reply.started":"2022-02-27T12:30:09.524685Z","shell.execute_reply":"2022-02-27T12:32:02.075163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_colwidth',200)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:02.081927Z","iopub.execute_input":"2022-02-27T12:32:02.082357Z","iopub.status.idle":"2022-02-27T12:32:02.089384Z","shell.execute_reply.started":"2022-02-27T12:32:02.082320Z","shell.execute_reply":"2022-02-27T12:32:02.088583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='train/audio'\nSAMPLE_RATE=16000\nN_FFT = 512\nHOP_LENGTH=128","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:02.091362Z","iopub.execute_input":"2022-02-27T12:32:02.091971Z","iopub.status.idle":"2022-02-27T12:32:02.102763Z","shell.execute_reply.started":"2022-02-27T12:32:02.091921Z","shell.execute_reply":"2022-02-27T12:32:02.102091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading train data","metadata":{}},{"cell_type":"code","source":"def load_train_data(path):\n    tmp_list=[]\n    for (dirpath, dirnames, filenames) in os.walk(path):\n        for file in filenames:\n            if file.endswith('.wav'):\n                tmp_path=os.path.join(dirpath, file)\n                class_label = tmp_path.split('/')[-2]\n                data,_ = librosa.load(tmp_path,sr=SAMPLE_RATE)\n                tmp_list.append([tmp_path,class_label,data])\n            else:\n                continue\n    return  pd.DataFrame(tmp_list,columns=['file_path','class_label','data'])","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:02.104850Z","iopub.execute_input":"2022-02-27T12:32:02.105533Z","iopub.status.idle":"2022-02-27T12:32:02.114743Z","shell.execute_reply.started":"2022-02-27T12:32:02.105356Z","shell.execute_reply":"2022-02-27T12:32:02.114142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = load_train_data(train_dir)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:02.117637Z","iopub.execute_input":"2022-02-27T12:32:02.118446Z","iopub.status.idle":"2022-02-27T12:32:19.366491Z","shell.execute_reply.started":"2022-02-27T12:32:02.118401Z","shell.execute_reply":"2022-02-27T12:32:19.365653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing train data","metadata":{}},{"cell_type":"markdown","source":"## separating noisy recordings from the rest\n**noisy records** - records in folder **\"_background_noise_\"**","metadata":{}},{"cell_type":"code","source":"noise_records_index = train_df.loc[train_df.class_label=='_background_noise_'].index\nnoise_df = train_df.iloc[noise_records_index].reset_index(drop=True)\ntrain_df = train_df.drop(noise_records_index).reset_index(drop=True)\ndel noise_records_index","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.367772Z","iopub.execute_input":"2022-02-27T12:32:19.368005Z","iopub.status.idle":"2022-02-27T12:32:19.403683Z","shell.execute_reply.started":"2022-02-27T12:32:19.367977Z","shell.execute_reply":"2022-02-27T12:32:19.402996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* increasing selection","metadata":{}},{"cell_type":"code","source":"#validation_labels =  'yes, no, up, down, left, right, on, off, stop, go'.split(', ')\nvalidation_labels =  'up, down, left, right'.split(', ')","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.404966Z","iopub.execute_input":"2022-02-27T12:32:19.405895Z","iopub.status.idle":"2022-02-27T12:32:19.410556Z","shell.execute_reply.started":"2022-02-27T12:32:19.405837Z","shell.execute_reply":"2022-02-27T12:32:19.409636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = [col for col in train_df.class_label.unique() if col not in validation_labels]\nprint(tmp)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.411849Z","iopub.execute_input":"2022-02-27T12:32:19.412649Z","iopub.status.idle":"2022-02-27T12:32:19.434604Z","shell.execute_reply.started":"2022-02-27T12:32:19.412608Z","shell.execute_reply":"2022-02-27T12:32:19.433903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df.loc[~train_df.class_label.isin(validation_labels),'class_label'].count()/len(tmp)\nnot_val_records_increas_selec = pd.DataFrame(columns=['file_path','class_label','data'])\nfor label in tmp:\n    selected_label_records = train_df.loc[train_df.class_label == label]\n    resempled = selected_label_records.sample(n=2350,replace=True,axis=0)\n    not_val_records_increas_selec = pd.concat([not_val_records_increas_selec,resempled], ignore_index=True)\n    del selected_label_records, resempled\nnot_val_records_increas_selec","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.436007Z","iopub.execute_input":"2022-02-27T12:32:19.436491Z","iopub.status.idle":"2022-02-27T12:32:19.685785Z","shell.execute_reply.started":"2022-02-27T12:32:19.436456Z","shell.execute_reply":"2022-02-27T12:32:19.684671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Changing class labels","metadata":{}},{"cell_type":"code","source":"# unknown records indexes\n#unknown_record_index = [indx for indx in train_df.index if train_df.loc[indx,'class_label'] not in validation_labels]\n# unknown_record_index = train_df.loc[train_df.class_label.isin(validation_labels)].index","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.687040Z","iopub.execute_input":"2022-02-27T12:32:19.687296Z","iopub.status.idle":"2022-02-27T12:32:19.690958Z","shell.execute_reply.started":"2022-02-27T12:32:19.687267Z","shell.execute_reply":"2022-02-27T12:32:19.690061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.drop(train_df.loc[~train_df.class_label.isin(validation_labels)].index).reset_index(drop=True)\ntrain_df.class_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.694529Z","iopub.execute_input":"2022-02-27T12:32:19.695241Z","iopub.status.idle":"2022-02-27T12:32:19.736029Z","shell.execute_reply.started":"2022-02-27T12:32:19.695202Z","shell.execute_reply":"2022-02-27T12:32:19.735128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.737438Z","iopub.execute_input":"2022-02-27T12:32:19.737744Z","iopub.status.idle":"2022-02-27T12:32:19.765164Z","shell.execute_reply.started":"2022-02-27T12:32:19.737702Z","shell.execute_reply":"2022-02-27T12:32:19.763991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Merge increased non validation records and validation records","metadata":{}},{"cell_type":"code","source":"#train_df = pd.concat([train_df,not_val_records_increas_selec], ignore_index=True)\n#display(train_df.head(3))\n#train_df.class_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.770529Z","iopub.execute_input":"2022-02-27T12:32:19.771263Z","iopub.status.idle":"2022-02-27T12:32:19.775648Z","shell.execute_reply.started":"2022-02-27T12:32:19.771208Z","shell.execute_reply":"2022-02-27T12:32:19.774673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display(train_df.class_label.value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.777537Z","iopub.execute_input":"2022-02-27T12:32:19.778192Z","iopub.status.idle":"2022-02-27T12:32:19.793884Z","shell.execute_reply.started":"2022-02-27T12:32:19.778139Z","shell.execute_reply":"2022-02-27T12:32:19.792825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df.loc[train_df.loc[~train_df.class_label.isin(validation_labels)].index,'class_label'] = 'unknown'\n#display(train_df.class_label.value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.797137Z","iopub.execute_input":"2022-02-27T12:32:19.798569Z","iopub.status.idle":"2022-02-27T12:32:19.808090Z","shell.execute_reply.started":"2022-02-27T12:32:19.798494Z","shell.execute_reply":"2022-02-27T12:32:19.806770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making silence records\ngenerating new records from records in  **\"_background_noise_\"** folder","metadata":{}},{"cell_type":"code","source":"def make_silence_records(noise_df):\n    silence_df = pd.DataFrame(columns=['file_path','class_label','data'])\n    for indx in noise_df.index:\n        record = noise_df.loc[indx,'data']\n        record_length = len(record)\n        duration = int(record_length/SAMPLE_RATE)\n        zeros = np.zeros(SAMPLE_RATE)\n        for i in range(duration*7):\n            random_sample = np.random.choice(record,SAMPLE_RATE)\n            silence_df = silence_df.append(\n                pd.Series([noise_df.loc[indx,'file_path'],'silence',  random_sample],\n                          index=['file_path','class_label','data']),\n                ignore_index=True,)  \n            silence_df = silence_df.append(\n                pd.Series(['own_made_silence','silence',\n                           zeros],\n                          index=['file_path','class_label','data']),\n                ignore_index=True,)\n#         silence_df = silence_df.sample(frac=1).reset_index(drop=True)\n    return silence_df","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.809716Z","iopub.execute_input":"2022-02-27T12:32:19.809961Z","iopub.status.idle":"2022-02-27T12:32:19.824331Z","shell.execute_reply.started":"2022-02-27T12:32:19.809934Z","shell.execute_reply":"2022-02-27T12:32:19.823363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"silence_df = make_silence_records(noise_df)\nsilence_df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:19.825865Z","iopub.execute_input":"2022-02-27T12:32:19.826848Z","iopub.status.idle":"2022-02-27T12:32:32.245529Z","shell.execute_reply.started":"2022-02-27T12:32:19.826793Z","shell.execute_reply":"2022-02-27T12:32:32.244667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making all records of the same length\n* Due to some records have a duration less than 1s. I should to pad them to the same length of 1s.","metadata":{}},{"cell_type":"code","source":"def pad_records_length(df):\n    smaller=0\n    bigger =0\n    df = df.copy()\n    SAMPLES_PER_TRACK= 16000\n    for indx in df.index:\n        record = df.loc[indx,'data']\n        if len(record)<SAMPLES_PER_TRACK:\n            smaller+=1\n            tmp = np.zeros(SAMPLES_PER_TRACK)\n            tmp[:record.shape[0]]=record\n            df.loc[indx,'data']= tmp\n            del tmp\n        elif len(record)>SAMPLES_PER_TRACK:\n            bigger+=1\n            df.loc[indx,'data']= record[:SAMPLES_PER_TRACK]   \n    print(f'Record {bigger} - bigger than 1s\\nRecords smaller then 1s = {smaller}')\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:32.246944Z","iopub.execute_input":"2022-02-27T12:32:32.247721Z","iopub.status.idle":"2022-02-27T12:32:32.256149Z","shell.execute_reply.started":"2022-02-27T12:32:32.247670Z","shell.execute_reply":"2022-02-27T12:32:32.255244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pad_records_length(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:32.257531Z","iopub.execute_input":"2022-02-27T12:32:32.257811Z","iopub.status.idle":"2022-02-27T12:32:32.604764Z","shell.execute_reply.started":"2022-02-27T12:32:32.257782Z","shell.execute_reply":"2022-02-27T12:32:32.603805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finding records that do not match their class","metadata":{}},{"cell_type":"code","source":"def is_bad_audio(df,column_name = 'data'):\n    df = df.copy()\n    df['is_bad'] = None\n    for indx in df.index:\n        data = df.loc[indx,column_name]\n        features=librosa.feature.spectral_centroid(y=data,sr=16000,n_fft=512,hop_length=128)[0]\n        m = np.mean(features)\n        t = np.std(features)\n        if  t < 80:\n            # silent\n            df.loc[indx,'is_bad']='silent'\n        elif (m > 2550 and t < 300):\n            # noisy\n            df.loc[indx,'is_bad']='noise'\n        elif (m > 3500 and t > 1200):\n            # distorted\n            df.loc[indx,'is_bad']='distorted'\n        else:\n            df.loc[indx,'is_bad']='good'\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:32.607414Z","iopub.execute_input":"2022-02-27T12:32:32.607781Z","iopub.status.idle":"2022-02-27T12:32:32.614878Z","shell.execute_reply.started":"2022-02-27T12:32:32.607744Z","shell.execute_reply":"2022-02-27T12:32:32.613606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = is_bad_audio(train_df)\ndisplay(train_df.head(1))\ndisplay(train_df.is_bad.value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:32.616714Z","iopub.execute_input":"2022-02-27T12:32:32.617262Z","iopub.status.idle":"2022-02-27T12:32:52.616161Z","shell.execute_reply.started":"2022-02-27T12:32:32.617215Z","shell.execute_reply":"2022-02-27T12:32:52.615462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Working with not matched records","metadata":{}},{"cell_type":"code","source":"#  changing noise records to silent class all distorted to unknown and dropping silent \ndistorted_indx = train_df.loc[train_df.is_bad=='distorted'].index\nnoise_indx = train_df.loc[train_df.is_bad=='noise'].index\nsilent_indx = train_df.loc[train_df.is_bad=='silent'].index","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:52.617194Z","iopub.execute_input":"2022-02-27T12:32:52.617510Z","iopub.status.idle":"2022-02-27T12:32:52.626858Z","shell.execute_reply.started":"2022-02-27T12:32:52.617484Z","shell.execute_reply":"2022-02-27T12:32:52.626185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[[*distorted_indx,*silent_indx],'class_label'] = 'unknown'\ntrain_df.loc[noise_indx,'class_label'] = 'silence'\n# train_df = train_df.drop(silent_indx).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:52.627973Z","iopub.execute_input":"2022-02-27T12:32:52.628374Z","iopub.status.idle":"2022-02-27T12:32:52.634836Z","shell.execute_reply.started":"2022-02-27T12:32:52.628344Z","shell.execute_reply":"2022-02-27T12:32:52.634160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.class_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:52.636200Z","iopub.execute_input":"2022-02-27T12:32:52.636620Z","iopub.status.idle":"2022-02-27T12:32:52.649514Z","shell.execute_reply.started":"2022-02-27T12:32:52.636587Z","shell.execute_reply":"2022-02-27T12:32:52.648594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# selected manually\nsilent = \"train/audio/stop/1fd85ee4_nohash_0.wav\"\n\nwrong_words = ['train/audio/right/46a153d8_nohash_4.wav',\n               'train/audio/down/c9b653a0_nohash_1.wav',\n               'train/audio/dog/94de6a6a_nohash_0.wav']\n\nbad_records = ['train/audio/on/99b05bcf_nohash_0.wav',\n               'train/audio/up/a13e0a74_nohash_0.wav',\n               'train/audio/no/e5dadd24_nohash_0.wav']","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:52.650688Z","iopub.execute_input":"2022-02-27T12:32:52.651018Z","iopub.status.idle":"2022-02-27T12:32:52.656918Z","shell.execute_reply.started":"2022-02-27T12:32:52.650990Z","shell.execute_reply":"2022-02-27T12:32:52.656030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[train_df.file_path.isin([*wrong_words,*bad_records]),'class_label'] = 'unknown'\ntrain_df.loc[train_df.file_path=='silent','class_label'] = 'silence'","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:52.658418Z","iopub.execute_input":"2022-02-27T12:32:52.658676Z","iopub.status.idle":"2022-02-27T12:32:52.673762Z","shell.execute_reply.started":"2022-02-27T12:32:52.658646Z","shell.execute_reply":"2022-02-27T12:32:52.673048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Merge silence dataframe and train dataframe","metadata":{}},{"cell_type":"code","source":"merged_df = pd.concat([train_df,silence_df], ignore_index=True)\nmerged_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:52.675009Z","iopub.execute_input":"2022-02-27T12:32:52.675402Z","iopub.status.idle":"2022-02-27T12:32:52.697668Z","shell.execute_reply.started":"2022-02-27T12:32:52.675371Z","shell.execute_reply":"2022-02-27T12:32:52.697000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_df","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:32:52.698982Z","iopub.execute_input":"2022-02-27T12:32:52.699381Z","iopub.status.idle":"2022-02-27T12:32:52.724162Z","shell.execute_reply.started":"2022-02-27T12:32:52.699348Z","shell.execute_reply":"2022-02-27T12:32:52.723298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create features","metadata":{}},{"cell_type":"markdown","source":"* Mel Spectrograms","metadata":{}},{"cell_type":"code","source":"def create_mel_spec_features(data,column_name = 'data'):\n    df = data.copy()\n    df['mel_spec'] = None\n    for indx in tqdm(df.index):\n        mel_spec = librosa.feature.melspectrogram(y=df.loc[indx,column_name],sr=16000,n_fft=512,hop_length=128,n_mels=90)\n        log_mel_spec = librosa.power_to_db(mel_spec)\n        df.loc[indx,'mel_spec'] = [log_mel_spec]\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:57:41.376215Z","iopub.execute_input":"2022-02-27T12:57:41.376497Z","iopub.status.idle":"2022-02-27T12:57:41.382715Z","shell.execute_reply.started":"2022-02-27T12:57:41.376468Z","shell.execute_reply":"2022-02-27T12:57:41.381439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_df_with_mel =create_mel_spec_features(merged_df)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:57:45.779500Z","iopub.execute_input":"2022-02-27T12:57:45.779822Z","iopub.status.idle":"2022-02-27T12:59:42.738054Z","shell.execute_reply.started":"2022-02-27T12:57:45.779756Z","shell.execute_reply":"2022-02-27T12:59:42.737158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train validation test split","metadata":{}},{"cell_type":"code","source":"\nfrom sklearn.model_selection import train_test_split\ntrain,test = train_test_split(merged_df_with_mel, test_size=0.3,random_state=21)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:59:51.394380Z","iopub.execute_input":"2022-02-27T12:59:51.395248Z","iopub.status.idle":"2022-02-27T12:59:51.406034Z","shell.execute_reply.started":"2022-02-27T12:59:51.395193Z","shell.execute_reply":"2022-02-27T12:59:51.405339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining feature and target variables","metadata":{}},{"cell_type":"markdown","source":"* Train","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.utils import to_categorical\nencoder = LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:59:54.409986Z","iopub.execute_input":"2022-02-27T12:59:54.410326Z","iopub.status.idle":"2022-02-27T12:59:54.416074Z","shell.execute_reply.started":"2022-02-27T12:59:54.410290Z","shell.execute_reply":"2022-02-27T12:59:54.415160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array([rec for rec in train['mel_spec']])","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:59:55.827626Z","iopub.execute_input":"2022-02-27T12:59:55.828224Z","iopub.status.idle":"2022-02-27T12:59:56.243670Z","shell.execute_reply.started":"2022-02-27T12:59:55.828166Z","shell.execute_reply":"2022-02-27T12:59:56.242733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train.class_label.values\ny_train = encoder.fit_transform(y_train)\n\nclasses_encoded = encoder.classes_\nnum_classes = len(classes_encoded)\nprint(num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T12:59:58.142966Z","iopub.execute_input":"2022-02-27T12:59:58.143269Z","iopub.status.idle":"2022-02-27T12:59:58.152478Z","shell.execute_reply.started":"2022-02-27T12:59:58.143234Z","shell.execute_reply":"2022-02-27T12:59:58.151408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train,num_classes = num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:00.555245Z","iopub.execute_input":"2022-02-27T13:00:00.556123Z","iopub.status.idle":"2022-02-27T13:00:00.561593Z","shell.execute_reply.started":"2022-02-27T13:00:00.556050Z","shell.execute_reply":"2022-02-27T13:00:00.560512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Test","metadata":{}},{"cell_type":"code","source":"X_test = np.array([rec for rec in test['mel_spec']])\ny_test = test.class_label.values\ny_test = encoder.transform(y_test)\ny_test = to_categorical(y_test,num_classes = num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:02.312857Z","iopub.execute_input":"2022-02-27T13:00:02.313539Z","iopub.status.idle":"2022-02-27T13:00:02.495978Z","shell.execute_reply.started":"2022-02-27T13:00:02.313499Z","shell.execute_reply":"2022-02-27T13:00:02.495363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Reshapping features data","metadata":{}},{"cell_type":"code","source":"# Reshape for mel spec features\nX_train = np.reshape(X_train,(X_train.shape[0],X_train.shape[1],X_train.shape[2],1))\nX_test = np.reshape(X_test,(X_test.shape[0],X_test.shape[1],X_test.shape[2],1))","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:04.890820Z","iopub.execute_input":"2022-02-27T13:00:04.891311Z","iopub.status.idle":"2022-02-27T13:00:04.896063Z","shell.execute_reply.started":"2022-02-27T13:00:04.891248Z","shell.execute_reply":"2022-02-27T13:00:04.895293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D,AveragePooling2D, MaxPooling2D,Flatten,Dropout,BatchNormalization\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:07.814466Z","iopub.execute_input":"2022-02-27T13:00:07.815575Z","iopub.status.idle":"2022-02-27T13:00:07.823877Z","shell.execute_reply.started":"2022-02-27T13:00:07.815508Z","shell.execute_reply":"2022-02-27T13:00:07.823163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(8, 2, padding='valid',activation='relu', input_shape=X_train.shape[1:]))\nfor i in range(2):\n    model.add(MaxPooling2D((2,2)))\n    model.add(BatchNormalization())\n    model.add(Conv2D(8, 2, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(16, 3, activation='relu'))\nmodel.add(AveragePooling2D((2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:09.960849Z","iopub.execute_input":"2022-02-27T13:00:09.961359Z","iopub.status.idle":"2022-02-27T13:00:10.207969Z","shell.execute_reply.started":"2022-02-27T13:00:09.961300Z","shell.execute_reply":"2022-02-27T13:00:10.206957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:14.556837Z","iopub.execute_input":"2022-02-27T13:00:14.557168Z","iopub.status.idle":"2022-02-27T13:00:14.576123Z","shell.execute_reply.started":"2022-02-27T13:00:14.557130Z","shell.execute_reply":"2022-02-27T13:00:14.575085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3)\nmc = ModelCheckpoint('best_model.h5', monitor='val_loss', mode='min', verbose=1, save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:17.644857Z","iopub.execute_input":"2022-02-27T13:00:17.645362Z","iopub.status.idle":"2022-02-27T13:00:17.651025Z","shell.execute_reply.started":"2022-02-27T13:00:17.645319Z","shell.execute_reply":"2022-02-27T13:00:17.650182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train, batch_size=64, epochs=5,callbacks=[es, mc]) ","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:00:20.110024Z","iopub.execute_input":"2022-02-27T13:00:20.111029Z","iopub.status.idle":"2022-02-27T13:01:04.031270Z","shell.execute_reply.started":"2022-02-27T13:00:20.110981Z","shell.execute_reply":"2022-02-27T13:01:04.030203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Loading best saved model","metadata":{}},{"cell_type":"code","source":"!ls ","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:01:05.050058Z","iopub.execute_input":"2022-02-27T13:01:05.050370Z","iopub.status.idle":"2022-02-27T13:01:05.972719Z","shell.execute_reply.started":"2022-02-27T13:01:05.050338Z","shell.execute_reply":"2022-02-27T13:01:05.971807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model on the test data using `evaluate`\nprint(\"Evaluate on test data\")\nresults = model.evaluate(X_test, y_test, batch_size=128)\nprint(\"test loss, test acc:\", results)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:01:05.975515Z","iopub.execute_input":"2022-02-27T13:01:05.975821Z","iopub.status.idle":"2022-02-27T13:01:07.431260Z","shell.execute_reply.started":"2022-02-27T13:01:05.975788Z","shell.execute_reply":"2022-02-27T13:01:07.430470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Making test prediction","metadata":{}},{"cell_type":"code","source":"prediction = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:01:07.432811Z","iopub.execute_input":"2022-02-27T13:01:07.433239Z","iopub.status.idle":"2022-02-27T13:01:08.927018Z","shell.execute_reply.started":"2022-02-27T13:01:07.433191Z","shell.execute_reply":"2022-02-27T13:01:08.926204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Confusion matrix","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:01:08.928877Z","iopub.execute_input":"2022-02-27T13:01:08.929627Z","iopub.status.idle":"2022-02-27T13:01:08.933549Z","shell.execute_reply.started":"2022-02-27T13:01:08.929581Z","shell.execute_reply":"2022-02-27T13:01:08.932637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = [classes_encoded[np.argmax(p)] for p in prediction]\ntrue_val = [classes_encoded[np.argmax(p)] for p in y_test]","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:01:08.935164Z","iopub.execute_input":"2022-02-27T13:01:08.935593Z","iopub.status.idle":"2022-02-27T13:01:08.972150Z","shell.execute_reply.started":"2022-02-27T13:01:08.935554Z","shell.execute_reply":"2022-02-27T13:01:08.971153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(true_val, pred, target_names=classes_encoded))","metadata":{"execution":{"iopub.status.busy":"2022-02-27T13:01:08.973902Z","iopub.execute_input":"2022-02-27T13:01:08.974642Z","iopub.status.idle":"2022-02-27T13:01:09.028907Z","shell.execute_reply.started":"2022-02-27T13:01:08.974591Z","shell.execute_reply":"2022-02-27T13:01:09.028006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}