{"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":"## Import the libraries","metadata":{"_uuid":"66b52e09-1f91-4ad8-92fd-c5b7aa5e9ae1","_cell_guid":"a2666fbd-3222-4b5b-b796-c6d428e14596","execution":{"iopub.status.busy":"2022-02-11T15:15:42.09471Z","iopub.execute_input":"2022-02-11T15:15:42.095103Z","iopub.status.idle":"2022-02-11T15:15:42.099724Z","shell.execute_reply.started":"2022-02-11T15:15:42.095016Z","shell.execute_reply":"2022-02-11T15:15:42.098645Z"},"trusted":true}},{"cell_type":"code","source":"!pip install pyunpack\n!pip install patool\n!pip install py7zr\n!pip install sounddevice\n!pip install noisereduce\n!pip install librosa\n! pip install python_speech_features\n! pip install tensorflow==2.4\n! pip install malaya_speech\n! pip install webrtcvad","metadata":{"_uuid":"c9fc4f47-c37e-40b9-a884-9a4ed21d7f8b","_cell_guid":"9ba4a47a-285e-4f5c-ba73-107aea820f8f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:38:26.900954Z","iopub.execute_input":"2022-03-06T13:38:26.901287Z","iopub.status.idle":"2022-03-06T13:39:43.311006Z","shell.execute_reply.started":"2022-03-06T13:38:26.901254Z","shell.execute_reply":"2022-03-06T13:39:43.310020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom py7zr import unpack_7zarchive\nimport shutil\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport librosa\nimport IPython.display as ipd\nfrom scipy.io import wavfile\n\nimport noisereduce as nr\nimport tensorflow \nfrom malaya_speech import Pipeline\n\nimport malaya_speech\nimport os\n\nfrom python_speech_features import mfcc\n\nfrom sklearn.preprocessing import LabelEncoder\nimport seaborn as sn","metadata":{"_uuid":"91de8368-3fac-45fc-bead-e1c6808ef818","_cell_guid":"cf822639-0fbe-4612-bae6-fec0492b916e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:39:43.312897Z","iopub.execute_input":"2022-03-06T13:39:43.313239Z","iopub.status.idle":"2022-03-06T13:39:43.321458Z","shell.execute_reply.started":"2022-03-06T13:39:43.313200Z","shell.execute_reply":"2022-03-06T13:39:43.320265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)\nshutil.unpack_archive('/kaggle/input/tensorflow-speech-recognition-challenge/train.7z', '/kaggle/working/tensorflow-speech-recognition-challenge/train/')","metadata":{"_uuid":"0d531811-b845-4ebb-94cf-159a7b17706d","_cell_guid":"86c93da2-7c5c-4f7a-b457-0eccc6c346a3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:43:31.245805Z","iopub.execute_input":"2022-03-06T13:43:31.246125Z","iopub.status.idle":"2022-03-06T13:45:56.823571Z","shell.execute_reply.started":"2022-03-06T13:43:31.246095Z","shell.execute_reply":"2022-03-06T13:45:56.822648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from pyunpack import Archive\n# import shutil\n# if not os.path.exists('/kaggle/working/tensorflow-speech-recognition-challenge/train/'):\n#     os.makedirs('/kaggle/working/tensorflow-speech-recognition-challenge/train/')\n# Archive('/kaggle/input/tensorflow-speech-recognition-challenge/train.7z').extractall('/kaggle/working/tensorflow-speech-recognition-challenge/train/')\n\n#for dirname, _, filenames in os.walk('/kaggle/working/tensorflow-speech-recognition-challenge/train/train/audio'):\n #   for filename in filename[:5]:\n  #      print(os.path.join(dirname, filename))","metadata":{"_uuid":"7b9a2333-a026-4303-a7c5-e7a3cc216d7b","_cell_guid":"ee168d34-990c-40bc-b62c-c30b7d2c55bf","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:07.701979Z","iopub.execute_input":"2022-03-06T13:46:07.702289Z","iopub.status.idle":"2022-03-06T13:46:07.706343Z","shell.execute_reply.started":"2022-03-06T13:46:07.702261Z","shell.execute_reply":"2022-03-06T13:46:07.705239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <center> Implementing the Speech Recognition Model in Python\n**Dataset used for our Speech Recognition Project**\n\n__You can download the dataset from__ [here](https://www.kaggle.com/c/tensorflow-speech-recognition-challenge).\n    \nTensorFlow recently released the Speech Commands Datasets. It includes 65,000 one-second long utterances of 30 short words, by thousands of different people. We’ll build a speech recognition system that understands simple spoken commands. <br>","metadata":{"_uuid":"4302e734-a9a0-41d8-8941-81be13c6e4ca","_cell_guid":"c11ffa81-5cd6-4ce6-8b43-d02bb65931a1","trusted":true}},{"cell_type":"markdown","source":"**Data Exploration and Visualization**\n\nData Exploration and Visualization helps us to understand the data as well as pre-processing steps in a better way. \n\n**Visualization of Audio signal in time series domain**\n\nNow, we’ll visualize the audio signal in the time series domain:","metadata":{"_uuid":"beb46fcf-3298-4e03-8420-424a9425eae9","_cell_guid":"7080dca6-f712-460c-bc3a-f0f53990c557","trusted":true}},{"cell_type":"code","source":"train_audio_path = '/kaggle/working/tensorflow-speech-recognition-challenge/train/train/audio/'","metadata":{"_uuid":"79b1e48f-c963-4800-9f56-4686f15edb2a","_cell_guid":"b7d2a0df-8ab5-413d-9dea-95e7ad03d4f3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:10.063931Z","iopub.execute_input":"2022-03-06T13:46:10.064285Z","iopub.status.idle":"2022-03-06T13:46:10.068468Z","shell.execute_reply.started":"2022-03-06T13:46:10.064256Z","shell.execute_reply":"2022-03-06T13:46:10.067622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Accessing each file in data**","metadata":{"_uuid":"8a33ee2b-7e37-40d0-9ba0-f8f65f21f4da","_cell_guid":"0b698fb9-5c6c-4b4f-80df-c1912d4ec246","trusted":true}},{"cell_type":"code","source":"#!apt-get install -y p7zip-full\n#!7z x ../input/tensorflow-speech-recognition-challenge/train.7z","metadata":{"_uuid":"a160f0b6-cb66-4ff7-b04c-3c2a596377e1","_cell_guid":"a3f9558a-50d7-42da-b48d-a6f41c97a5e9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:11.795020Z","iopub.execute_input":"2022-03-06T13:46:11.795547Z","iopub.status.idle":"2022-03-06T13:46:11.799547Z","shell.execute_reply.started":"2022-03-06T13:46:11.795498Z","shell.execute_reply":"2022-03-06T13:46:11.798629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples, sample_rate = librosa.load(train_audio_path+'on/5a3712c9_nohash_1.wav', sr = 16000)\nfig = plt.figure(figsize=(14, 8))\nax1 = fig.add_subplot(211)\nax1.set_title('Raw wave of ' + '../input/train/audio/on/0a7c2a8d_nohash_0.wav')\nax1.set_xlabel('time')\nax1.set_ylabel('Amplitude')\nax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)","metadata":{"_uuid":"5dbb5089-572e-4ccd-916a-3464ce42dd5c","_cell_guid":"33a4c325-39b2-4178-bc43-ca5237bf7c65","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:12.393336Z","iopub.execute_input":"2022-03-06T13:46:12.393696Z","iopub.status.idle":"2022-03-06T13:46:12.574561Z","shell.execute_reply.started":"2022-03-06T13:46:12.393667Z","shell.execute_reply":"2022-03-06T13:46:12.573733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Sampling rate **\n\nLet us now look at the sampling rate of the audio signals","metadata":{"_uuid":"39e3f2d2-0b94-40fa-9522-d6031e74a059","_cell_guid":"b9770ec5-05b4-4428-a9e9-7a653830ee1f","trusted":true}},{"cell_type":"code","source":"ipd.Audio(samples, rate=sample_rate)","metadata":{"_uuid":"e1af3897-06de-4bca-8aa1-161b2c7838a8","_cell_guid":"d57d2ef4-90e2-4188-879b-5390dce3eeb8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:13.882092Z","iopub.execute_input":"2022-03-06T13:46:13.882439Z","iopub.status.idle":"2022-03-06T13:46:13.889807Z","shell.execute_reply.started":"2022-03-06T13:46:13.882409Z","shell.execute_reply":"2022-03-06T13:46:13.888781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sample_rate)\nsig1=samples\nfs=sample_rate\nsr=fs","metadata":{"_uuid":"6b8a30cb-250f-4763-b41e-047c56b0d2d1","_cell_guid":"a6245bd5-7319-4d90-9ef4-a3b3cb32cb02","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:14.675245Z","iopub.execute_input":"2022-03-06T13:46:14.675597Z","iopub.status.idle":"2022-03-06T13:46:14.680211Z","shell.execute_reply.started":"2022-03-06T13:46:14.675568Z","shell.execute_reply":"2022-03-06T13:46:14.679336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time = np.linspace(0, len(sig1 - 1) / fs, len(sig1 - 1))\nreduced_noise1 = nr.reduce_noise(y=sig1, sr=fs,stationary=True)\nplt.plot(time, reduced_noise1)  # plot in seconds\n#reduced_noise2 = nr.reduce_noise(y=sig2, sr=fs,stationary=True)\n#plt.plot(time, reduced_noise2)  # plot in seconds\n#plt.title(\"Voice Signal\")\nplt.xlabel(\"Time [seconds]\")\nplt.ylabel(\"Voice amplitude\")\nplt.show()","metadata":{"_uuid":"1ffd499a-e5c8-49ef-bd60-bb7bb87ba04a","_cell_guid":"b3f20389-1653-4c78-b418-8e697a525bef","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:16.003191Z","iopub.execute_input":"2022-03-06T13:46:16.003565Z","iopub.status.idle":"2022-03-06T13:46:16.695970Z","shell.execute_reply.started":"2022-03-06T13:46:16.003536Z","shell.execute_reply":"2022-03-06T13:46:16.695109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(reduced_noise1, rate=sample_rate)","metadata":{"_uuid":"e85b646f-da77-45a1-b0db-0f3f95bc645e","_cell_guid":"49e3daa9-08c5-479a-b280-27c74e7e17f3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:17.216872Z","iopub.execute_input":"2022-03-06T13:46:17.217316Z","iopub.status.idle":"2022-03-06T13:46:17.224877Z","shell.execute_reply.started":"2022-03-06T13:46:17.217275Z","shell.execute_reply":"2022-03-06T13:46:17.223968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Silence Removal\nvad = malaya_speech.vad.webrtc()\ny_=reduced_noise1\n#y_= malaya_speech.resample(y, sr, 16000)\ny_ = malaya_speech.astype.float_to_int(y_)\n#frames = malaya_speech.generator.frames(y_, 30, sr)\nframes_ = list(malaya_speech.generator.frames(y_, 30, 16000, append_ending_trail = False))\nframes_webrtc = [(frames_[no], vad(frame)) for no, frame in enumerate(frames_)]\ny_ = malaya_speech.combine.without_silent(frames_webrtc)\ny_\nlen(y_)","metadata":{"_uuid":"a425df57-cbb2-4691-8fe7-0f580e8f7db6","_cell_guid":"8af194f6-2e06-4c38-b4ae-aa0de76cbe81","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:18.503779Z","iopub.execute_input":"2022-03-06T13:46:18.504124Z","iopub.status.idle":"2022-03-06T13:46:18.525283Z","shell.execute_reply.started":"2022-03-06T13:46:18.504088Z","shell.execute_reply":"2022-03-06T13:46:18.524496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(y_, rate = sr )","metadata":{"_uuid":"f2ceeccb-2f43-493c-9c1d-680897c80300","_cell_guid":"67177975-7836-48da-9d81-155b967fcf26","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:19.268508Z","iopub.execute_input":"2022-03-06T13:46:19.268849Z","iopub.status.idle":"2022-03-06T13:46:19.277645Z","shell.execute_reply.started":"2022-03-06T13:46:19.268820Z","shell.execute_reply":"2022-03-06T13:46:19.275939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero = np.zeros((1*sr-y_.shape[0]))\nsignal = np.concatenate((y_,zero))\nsignal.shape\ntime = np.linspace(0, len(signal - 1) / fs, len(signal - 1))","metadata":{"_uuid":"0b4ad16b-f826-45f9-b69c-c78e4e2eb316","_cell_guid":"5574ecbd-5373-411d-ae3d-565cffdabb06","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:20.010668Z","iopub.execute_input":"2022-03-06T13:46:20.010986Z","iopub.status.idle":"2022-03-06T13:46:20.018938Z","shell.execute_reply.started":"2022-03-06T13:46:20.010955Z","shell.execute_reply":"2022-03-06T13:46:20.018140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(time,signal)","metadata":{"_uuid":"f5a8627a-5f97-47d9-835e-1387441b8f05","_cell_guid":"ee65e441-dd5f-46d2-936d-90306c08ac72","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:20.670171Z","iopub.execute_input":"2022-03-06T13:46:20.670517Z","iopub.status.idle":"2022-03-06T13:46:20.818431Z","shell.execute_reply.started":"2022-03-06T13:46:20.670486Z","shell.execute_reply":"2022-03-06T13:46:20.817527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=os.listdir(train_audio_path)","metadata":{"_uuid":"5f979841-5e99-421b-9256-f242e29eab0b","_cell_guid":"b629339c-a88f-4c00-9b59-e3a9483ab14c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:21.375607Z","iopub.execute_input":"2022-03-06T13:46:21.375935Z","iopub.status.idle":"2022-03-06T13:46:21.380099Z","shell.execute_reply.started":"2022-03-06T13:46:21.375906Z","shell.execute_reply":"2022-03-06T13:46:21.379209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#find count of each label and plot bar graph\nno_of_recordings=[]\nfor label in labels:\n    waves = [f for f in os.listdir(train_audio_path + '/'+ label) if f.endswith('.wav')]\n    no_of_recordings.append(len(waves))\n    \n#plot\nplt.figure(figsize=(30,5))\nindex = np.arange(len(labels))\nplt.bar(index, no_of_recordings)\nplt.xlabel('Commands', fontsize=12)\nplt.ylabel('No of recordings', fontsize=12)\nplt.xticks(index, labels, fontsize=15, rotation=60)\nplt.title('No. of recordings for each command')\nplt.show()","metadata":{"_uuid":"332c281b-d45a-4393-ad7b-6003b241cdaa","_cell_guid":"f8642b69-c396-4ee7-a45a-8141b3719be5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:22.370202Z","iopub.execute_input":"2022-03-06T13:46:22.370650Z","iopub.status.idle":"2022-03-06T13:46:22.699904Z","shell.execute_reply.started":"2022-03-06T13:46:22.370614Z","shell.execute_reply":"2022-03-06T13:46:22.699115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Words used","metadata":{"_uuid":"bd76d03c-a1db-4835-ab02-230035ce461f","_cell_guid":"68df39c1-5c5c-4161-9956-88325117f039","trusted":true}},{"cell_type":"code","source":"labb=['bird','cat','yes','nine','zero','marvin','three','five','wow','four','tree','dog','two','no','sheila','house','six','one','happy','bed','seven','eight']\nlen(labb)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T13:46:24.898049Z","iopub.execute_input":"2022-03-06T13:46:24.898389Z","iopub.status.idle":"2022-03-06T13:46:24.905266Z","shell.execute_reply.started":"2022-03-06T13:46:24.898357Z","shell.execute_reply":"2022-03-06T13:46:24.904426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=[\"on\",\"off\",\"up\",\"down\",\"go\",\"stop\",\"left\",\"right\"]","metadata":{"_uuid":"42739fb4-bedc-41e3-97ff-cae39af2e487","_cell_guid":"094e1ad8-2e0e-4bdf-a588-4c56c88ceb84","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:26.145282Z","iopub.execute_input":"2022-03-06T13:46:26.145647Z","iopub.status.idle":"2022-03-06T13:46:26.149364Z","shell.execute_reply.started":"2022-03-06T13:46:26.145615Z","shell.execute_reply":"2022-03-06T13:46:26.148466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noise_path_1 = '../input/hospital-ambient-noise/Hospital noise original/Hospital noise original/'\nnoise_path_2='../input/pinknoise/'","metadata":{"_uuid":"e81439f3-75e3-4030-a9f0-fc7178f1d459","_cell_guid":"6d359081-f605-442d-9ea1-3dd27e02e243","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:13:13.756862Z","iopub.execute_input":"2022-03-06T14:13:13.757207Z","iopub.status.idle":"2022-03-06T14:13:13.761049Z","shell.execute_reply.started":"2022-03-06T14:13:13.757176Z","shell.execute_reply":"2022-03-06T14:13:13.760212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_of_other=['can not be identified']","metadata":{"_uuid":"91768513-b20b-415d-b24f-184f58f459f5","_cell_guid":"b4f542bc-c3d3-4304-98d1-76da8ff18bef","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T13:46:28.257671Z","iopub.execute_input":"2022-03-06T13:46:28.257996Z","iopub.status.idle":"2022-03-06T13:46:28.262624Z","shell.execute_reply.started":"2022-03-06T13:46:28.257966Z","shell.execute_reply":"2022-03-06T13:46:28.260859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Preprocessing the audio waves**\n\nlet us read the audio waves and use the below-preprocessing steps :\n\n* Noise Reduction\n* Silence Removal\n* Extracting MFCCs\n\nLet us define these preprocessing steps in the below code snippet:","metadata":{"_uuid":"03c82300-b635-4330-be13-b7addff93c6f","_cell_guid":"4fa19225-2ac7-4570-a723-c5ea9b1dfc7b","trusted":true}},{"cell_type":"code","source":"sr=16000\nvad = malaya_speech.vad.webrtc()\nall_wave = []\nall_label = []\nfor label in labels:\n    print(label)\n    waves = [f for f in os.listdir(train_audio_path + '/'+ label) if f.endswith('.wav')]\n    i=0\n    for wav in waves:\n        samples, sample_rate = librosa.load(train_audio_path + '/' + label + '/' + wav, sr = 16000)\n        samples = nr.reduce_noise(y=samples, sr=sr,stationary=True)\n        y_= malaya_speech.resample(samples, sr, 16000)\n        y_ = malaya_speech.astype.float_to_int(y_)\n        frames = malaya_speech.generator.frames(samples, 30, sr)\n        frames_ = list(malaya_speech.generator.frames(y_, 30, 16000, append_ending_trail = False))\n        frames_webrtc = [(frames[no], vad(frame)) for no, frame in enumerate(frames_)]\n        y_ = malaya_speech.combine.without_silent(frames_webrtc)\n        zero = np.zeros(((1*sr+4000)-y_.shape[0]))\n        signal = np.concatenate((y_,zero))\n        all_wave.append(signal)\n        all_label.append(label)\n        i+=1\n        if i==2300:\n            break","metadata":{"_uuid":"09b55783-27cb-47ba-8bab-a4660db0a6d9","_cell_guid":"daf0099f-548f-4ff4-8bc5-297ef7e88415","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:13:58.856728Z","iopub.execute_input":"2022-03-06T14:13:58.857061Z","iopub.status.idle":"2022-03-06T14:22:51.782835Z","shell.execute_reply.started":"2022-03-06T14:13:58.857029Z","shell.execute_reply":"2022-03-06T14:22:51.781947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.array(all_wave).shape)\nprint(np.array(all_label).shape)\nfs=16000\nsr=fs\ntime = np.linspace(0, len(signal - 1) / fs, len(signal - 1))\nplt.plot(time,np.array(all_wave)[2000,:])\nprint(np.array(all_label)[2000])\nipd.Audio(np.array(all_wave)[2000,:], rate = sr )","metadata":{"_uuid":"e5e43d4e-4934-464e-8cf8-b45dd7cf6c6f","_cell_guid":"d8e151d0-1b4c-4fa0-bf54-8c5decefedc3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:23:13.352986Z","iopub.execute_input":"2022-03-06T14:23:13.353318Z","iopub.status.idle":"2022-03-06T14:23:22.258823Z","shell.execute_reply.started":"2022-03-06T14:23:13.353286Z","shell.execute_reply":"2022-03-06T14:23:22.258010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_mfcc=[]\nfor wave in all_wave:\n    i=0\n    mfcc_feat = mfcc(wave , fs, winlen=256/fs, winstep=256/(2*fs), numcep=13, nfilt=26, nfft=256,\n                 lowfreq=0, highfreq=fs/2, preemph=0.97, ceplifter=22, appendEnergy=True, winfunc=np.hamming)\n    mfcc_feat= np.transpose(mfcc_feat)\n    all_mfcc.append(mfcc_feat)","metadata":{"_uuid":"d9e30c79-bb55-4ac4-9e4b-16bcf0512703","_cell_guid":"884a53ed-02c1-4a8e-8432-1365d96e1679","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:23:30.946252Z","iopub.execute_input":"2022-03-06T14:23:30.946620Z","iopub.status.idle":"2022-03-06T14:24:25.312443Z","shell.execute_reply.started":"2022-03-06T14:23:30.946590Z","shell.execute_reply":"2022-03-06T14:24:25.311310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.array(all_mfcc).shape)\nprint(np.array(all_label).shape)\nd1=np.array(all_mfcc).shape[1]\nd2=np.array(all_mfcc).shape[2]\nd=d1*d2\nprint(d)","metadata":{"_uuid":"6ae8bdb6-d06a-4fc8-8423-fe20e71854bb","_cell_guid":"e6971de2-ed27-4ef8-a5a1-4067ced6de2f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:24:25.314203Z","iopub.execute_input":"2022-03-06T14:24:25.314818Z","iopub.status.idle":"2022-03-06T14:24:25.760818Z","shell.execute_reply.started":"2022-03-06T14:24:25.314779Z","shell.execute_reply":"2022-03-06T14:24:25.759471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(noise_path_1))","metadata":{"_uuid":"ebcaf2c6-665f-4582-926e-43018913e239","_cell_guid":"375b16e5-1d42-407d-acfb-0f12b0a34de7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:24:35.135563Z","iopub.execute_input":"2022-03-06T14:24:35.135893Z","iopub.status.idle":"2022-03-06T14:24:35.299737Z","shell.execute_reply.started":"2022-03-06T14:24:35.135863Z","shell.execute_reply":"2022-03-06T14:24:35.299001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(noise_path_2))","metadata":{"_uuid":"77a83656-edde-4bb7-baf6-0ce5b3e804c8","_cell_guid":"2d6cc11b-9cef-4016-ac5a-d7c6105bb432","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:24:35.857301Z","iopub.execute_input":"2022-03-06T14:24:35.857819Z","iopub.status.idle":"2022-03-06T14:24:35.993214Z","shell.execute_reply.started":"2022-03-06T14:24:35.857781Z","shell.execute_reply":"2022-03-06T14:24:35.992431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noise_waves_1 = os.listdir(noise_path_1)\nnoise_waves_2 =os.listdir(noise_path_2)","metadata":{"_uuid":"75f6c51f-139b-4771-abb7-f5926fa25de8","_cell_guid":"807c9d11-7867-4a3a-895d-17763961cec2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:24:39.924550Z","iopub.execute_input":"2022-03-06T14:24:39.924876Z","iopub.status.idle":"2022-03-06T14:24:39.930521Z","shell.execute_reply.started":"2022-03-06T14:24:39.924848Z","shell.execute_reply":"2022-03-06T14:24:39.929401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noise_files1=np.array(noise_waves_1[:50])\nnoise_files2=np.array(noise_waves_2[:50])","metadata":{"_uuid":"71efbaf6-3a4c-4552-9e15-b7e060a39ae9","_cell_guid":"6438fc8d-3ed9-4c7d-a7ad-89f143f8fa11","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:24:42.923840Z","iopub.execute_input":"2022-03-06T14:24:42.924258Z","iopub.status.idle":"2022-03-06T14:24:42.932487Z","shell.execute_reply.started":"2022-03-06T14:24:42.924217Z","shell.execute_reply":"2022-03-06T14:24:42.931477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noise_files=np.hstack((noise_files1, noise_files2))","metadata":{"_uuid":"2358ef46-96ca-422f-9523-9d0b3c5675b5","_cell_guid":"7435aa34-302d-4795-bf9b-c965da01c9f3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:24:46.442324Z","iopub.execute_input":"2022-03-06T14:24:46.442684Z","iopub.status.idle":"2022-03-06T14:24:46.448217Z","shell.execute_reply.started":"2022-03-06T14:24:46.442653Z","shell.execute_reply":"2022-03-06T14:24:46.447072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noise_files1[40]","metadata":{"_uuid":"11cc5e2b-f787-4a3b-892d-e5335ead53ef","_cell_guid":"a5b2c46a-7636-428e-b40b-3fb7ed13b454","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:25:06.261431Z","iopub.execute_input":"2022-03-06T14:25:06.261775Z","iopub.status.idle":"2022-03-06T14:25:06.267293Z","shell.execute_reply.started":"2022-03-06T14:25:06.261743Z","shell.execute_reply":"2022-03-06T14:25:06.266411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples, sample_rate = librosa.load(noise_path_1+noise_files1[40], sr = 16000)","metadata":{"_uuid":"f95a8883-44e7-40ac-b94c-fa336ed6af61","_cell_guid":"fc8b3695-eec0-42f4-b21a-ca59a8be018e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:25:22.116296Z","iopub.execute_input":"2022-03-06T14:25:22.116806Z","iopub.status.idle":"2022-03-06T14:25:23.063217Z","shell.execute_reply.started":"2022-03-06T14:25:22.116767Z","shell.execute_reply":"2022-03-06T14:25:23.062339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(samples, rate = 16000 )","metadata":{"_uuid":"e01b6790-0cc9-42eb-a3e6-317ad623151b","_cell_guid":"aae49c5a-2914-49da-b891-3a5ad8953183","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:25:26.613157Z","iopub.execute_input":"2022-03-06T14:25:26.613546Z","iopub.status.idle":"2022-03-06T14:25:26.632138Z","shell.execute_reply.started":"2022-03-06T14:25:26.613513Z","shell.execute_reply":"2022-03-06T14:25:26.631271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sample_rate)\nsig1=samples\nfs=sample_rate\nsr=fs","metadata":{"_uuid":"ffe1c179-d08b-4ba0-b11a-1778faccb761","_cell_guid":"93e918f7-5e21-491c-b6c0-c799e6994f1d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:25:34.305517Z","iopub.execute_input":"2022-03-06T14:25:34.305862Z","iopub.status.idle":"2022-03-06T14:25:34.310628Z","shell.execute_reply.started":"2022-03-06T14:25:34.305830Z","shell.execute_reply":"2022-03-06T14:25:34.309757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Silence Removal\nreduced_noise1 = nr.reduce_noise(y=sig1, sr=fs,stationary=True)\n\nvad = malaya_speech.vad.webrtc()\ny=reduced_noise1\ny_= malaya_speech.resample(y, sr, 16000)\ny_ = malaya_speech.astype.float_to_int(y_)\nframes = malaya_speech.generator.frames(y, 30, sr)\nframes_ = list(malaya_speech.generator.frames(y_, 30, 16000, append_ending_trail = False))\nframes_webrtc = [(frames[no], vad(frame)) for no, frame in enumerate(frames_)]\ny_ = malaya_speech.combine.without_silent(frames_webrtc)\ny_=y_[:16000]","metadata":{"_uuid":"8cba16de-9b55-428c-9cc4-977a7522faff","_cell_guid":"2e9ec931-ee7d-415c-9f96-54d77f9b73a2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:28:40.849947Z","iopub.execute_input":"2022-03-06T14:28:40.850309Z","iopub.status.idle":"2022-03-06T14:28:40.939851Z","shell.execute_reply.started":"2022-03-06T14:28:40.850274Z","shell.execute_reply":"2022-03-06T14:28:40.938998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero = np.zeros((1*sr-y_.shape[0]))\nsignal = np.concatenate((y_,zero))\nsignal.shape\ntime = np.linspace(0, len(signal - 1) / fs, len(signal - 1))","metadata":{"_uuid":"11986fb4-c678-4811-98b9-e5d118adb52a","_cell_guid":"c1e384a6-04fd-4efd-bfb8-50e896fa483a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:28:43.203058Z","iopub.execute_input":"2022-03-06T14:28:43.203420Z","iopub.status.idle":"2022-03-06T14:28:43.208907Z","shell.execute_reply.started":"2022-03-06T14:28:43.203387Z","shell.execute_reply":"2022-03-06T14:28:43.207856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(time,signal)","metadata":{"_uuid":"c7b21206-3fe8-45ae-ae09-c3bf3fb96f77","_cell_guid":"9ca9c343-5b2c-4800-b7a3-9a83ecac84fd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:28:49.411788Z","iopub.execute_input":"2022-03-06T14:28:49.412148Z","iopub.status.idle":"2022-03-06T14:28:49.567168Z","shell.execute_reply.started":"2022-03-06T14:28:49.412115Z","shell.execute_reply":"2022-03-06T14:28:49.566208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_of_other[0]","metadata":{"_uuid":"3b3c2914-fc3d-4701-b072-8e43b1d70b25","_cell_guid":"989ceaaf-bc64-440c-b789-57904cfd7dd3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:28:51.349989Z","iopub.execute_input":"2022-03-06T14:28:51.350323Z","iopub.status.idle":"2022-03-06T14:28:51.357195Z","shell.execute_reply.started":"2022-03-06T14:28:51.350288Z","shell.execute_reply":"2022-03-06T14:28:51.356098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sr=16000\nvad = malaya_speech.vad.webrtc()\nnoise_wave = []\nnoise_label = []\n\ni=0\nfor wav in noise_files:\n    if (i<50):\n        samples, sample_rate = librosa.load(noise_path_1 + '/' + wav , sr = 16000)\n    else:\n        samples, sample_rate = librosa.load(noise_path_2 + '/' + wav , sr = 16000)\n        \n    samples = nr.reduce_noise(y=samples[:16000], sr=sr,stationary=True)\n    y_= malaya_speech.resample(samples, sr, 16000)\n    y_ = malaya_speech.astype.float_to_int(y_)\n    frames = malaya_speech.generator.frames(samples, 30, sr)\n    frames_ = list(malaya_speech.generator.frames(y_, 30, 16000, append_ending_trail = False))\n    frames_webrtc = [(frames[no], vad(frame)) for no, frame in enumerate(frames_)]\n    y_ = malaya_speech.combine.without_silent(frames_webrtc)\n    zero = np.zeros(((1*sr+4000)-y_.shape[0]))\n    signal = np.concatenate((y_,zero))\n    noise_wave.append(signal)\n    noise_label.append(label_of_other[0])\n    i+=1\n\nfor label in labb:\n    print(label)\n    waves = [f for f in os.listdir(train_audio_path + '/'+ label) if f.endswith('.wav')]\n    j=0\n    for wav in waves:\n        samples, sample_rate = librosa.load(train_audio_path + '/' + label + '/' + wav, sr = 16000)\n        samples = nr.reduce_noise(y=samples, sr=sr,stationary=True)\n        y_= malaya_speech.resample(samples, sr, 16000)\n        y_ = malaya_speech.astype.float_to_int(y_)\n        frames = malaya_speech.generator.frames(samples, 30, sr)\n        frames_ = list(malaya_speech.generator.frames(y_, 30, 16000, append_ending_trail = False))\n        frames_webrtc = [(frames[no], vad(frame)) for no, frame in enumerate(frames_)]\n        y_ = malaya_speech.combine.without_silent(frames_webrtc)\n        zero = np.zeros(((1*sr+4000)-y_.shape[0]))\n        signal = np.concatenate((y_,zero))\n        noise_wave.append(signal)\n        noise_label.append(label_of_other[0])\n        j+=1\n        if j==100:\n            break","metadata":{"_uuid":"3941a08f-76e6-40c2-86af-4364224c9653","_cell_guid":"f15f2798-d73b-4d1e-a55c-5fb9671a0fed","collapsed":false,"_kg_hide-output":true,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:30:58.911943Z","iopub.execute_input":"2022-03-06T14:30:58.912293Z","iopub.status.idle":"2022-03-06T14:33:40.106256Z","shell.execute_reply.started":"2022-03-06T14:30:58.912260Z","shell.execute_reply":"2022-03-06T14:33:40.105376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"noise_mfcc=[]\nfor wave in noise_wave:\n    mfcc_feat = mfcc(wave , fs, winlen=256/fs, winstep=256/(2*fs), numcep=13, nfilt=26, nfft=256,\n                 lowfreq=0, highfreq=fs/2, preemph=0.97, ceplifter=22, appendEnergy=True, winfunc=np.hamming)\n    mfcc_feat= np.transpose(mfcc_feat)\n    all_mfcc.append(mfcc_feat)","metadata":{"_uuid":"6116fa4a-72d0-4267-ac64-7b768945c6f7","_cell_guid":"14751413-45ed-403a-82f1-466ad2e0c82f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:16.348929Z","iopub.execute_input":"2022-03-06T14:35:16.349288Z","iopub.status.idle":"2022-03-06T14:35:23.332705Z","shell.execute_reply.started":"2022-03-06T14:35:16.349254Z","shell.execute_reply":"2022-03-06T14:35:23.331635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(noise_label)","metadata":{"_uuid":"71e8245e-ac4d-4a06-9573-74f62c005409","_cell_guid":"5e7e4b29-6679-4821-a662-0e521da0cfd0","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:23.337885Z","iopub.execute_input":"2022-03-06T14:35:23.340258Z","iopub.status.idle":"2022-03-06T14:35:23.349837Z","shell.execute_reply.started":"2022-03-06T14:35:23.340210Z","shell.execute_reply":"2022-03-06T14:35:23.348832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.array(all_mfcc).shape)\nprint(np.array(noise_label).shape)\n#d1=np.array(noise_mfcc).shape[1]\n#d2=np.array(noise_mfcc).shape[2]\n#d=d1*d2\n#print(d)","metadata":{"_uuid":"ea0bd6fb-a6e1-436c-8b1e-3eca2663f9a4","_cell_guid":"b1a01bd4-bc3e-4a6f-8ab0-fb96d77e335e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:23.355140Z","iopub.execute_input":"2022-03-06T14:35:23.357455Z","iopub.status.idle":"2022-03-06T14:35:23.545065Z","shell.execute_reply.started":"2022-03-06T14:35:23.357408Z","shell.execute_reply":"2022-03-06T14:35:23.544110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"op_mfcc=np.array(all_mfcc)\nop_mfcc=op_mfcc.reshape(np.array(all_mfcc).shape[0],-1)\nop_mfcc.shape","metadata":{"_uuid":"e5828164-ddd8-4734-ab9a-ec456f331217","_cell_guid":"e7693b90-2e10-48de-ac27-5da1a4b3dadd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:32.339738Z","iopub.execute_input":"2022-03-06T14:35:32.340090Z","iopub.status.idle":"2022-03-06T14:35:32.617765Z","shell.execute_reply.started":"2022-03-06T14:35:32.340058Z","shell.execute_reply":"2022-03-06T14:35:32.616956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_label=np.hstack((all_label, noise_label))\nall_label","metadata":{"_uuid":"d51b68b5-53a2-4792-9fa9-33fe8846d31f","_cell_guid":"4e31ef6d-fbed-4310-82a1-de253aa5e688","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:35.211100Z","iopub.execute_input":"2022-03-06T14:35:35.211536Z","iopub.status.idle":"2022-03-06T14:35:35.223544Z","shell.execute_reply.started":"2022-03-06T14:35:35.211489Z","shell.execute_reply":"2022-03-06T14:35:35.222331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#all_label = all_label.tolist()\n\nle = LabelEncoder()\ny=le.fit_transform(all_label)\nclasses= list(le.classes_)","metadata":{"_uuid":"599c8614-1a42-4f26-8dda-d59c55562570","_cell_guid":"f59cb05d-7b3a-48b6-9a94-72c6931d417f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:38.402756Z","iopub.execute_input":"2022-03-06T14:35:38.403077Z","iopub.status.idle":"2022-03-06T14:35:38.415147Z","shell.execute_reply.started":"2022-03-06T14:35:38.403048Z","shell.execute_reply":"2022-03-06T14:35:38.414255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Model based on ANN**","metadata":{"_uuid":"1336c48e-7cda-499e-a413-b3614d269852","_cell_guid":"a648bf72-1775-4d25-846b-25a922541a9b","trusted":true}},{"cell_type":"code","source":"classes","metadata":{"_uuid":"7d5d07de-3165-44c5-8f95-93c9f5869ab8","_cell_guid":"294e8951-cec8-4f40-a1a3-cb2c9f357d70","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:45.039242Z","iopub.execute_input":"2022-03-06T14:35:45.039652Z","iopub.status.idle":"2022-03-06T14:35:45.045275Z","shell.execute_reply.started":"2022-03-06T14:35:45.039615Z","shell.execute_reply":"2022-03-06T14:35:45.044443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install --upgrade tensorflow\n! pip install --upgrade tensorflow-gpu\n! pip install keras==2.3.1","metadata":{"_uuid":"634e711a-13b6-4c4d-8a99-0a16861a8341","_cell_guid":"5bfc91c1-9704-40c2-9fef-1fedcaafcf27","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:35:46.988391Z","iopub.execute_input":"2022-03-06T14:35:46.988722Z","iopub.status.idle":"2022-03-06T14:36:09.202600Z","shell.execute_reply.started":"2022-03-06T14:35:46.988688Z","shell.execute_reply":"2022-03-06T14:36:09.201599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import SGD\nfrom keras.constraints import maxnorm\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D, Flatten, Dense,Dropout\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint","metadata":{"_uuid":"e83e1891-a452-4d18-8929-2bfb97c82111","_cell_guid":"ad5a1d56-3298-4699-b743-6592a9d2437e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:36:09.204341Z","iopub.execute_input":"2022-03-06T14:36:09.204707Z","iopub.status.idle":"2022-03-06T14:36:09.212036Z","shell.execute_reply.started":"2022-03-06T14:36:09.204670Z","shell.execute_reply":"2022-03-06T14:36:09.211188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=tensorflow.keras.utils.to_categorical(y, num_classes=len(classes), dtype='float32')\ny.shape","metadata":{"_uuid":"a43c5ec2-2f4d-4ecf-a2f4-c7e165b1213b","_cell_guid":"118474f9-4593-4fc3-8210-722f081d3c08","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:37:16.401711Z","iopub.execute_input":"2022-03-06T14:37:16.402047Z","iopub.status.idle":"2022-03-06T14:37:16.410444Z","shell.execute_reply.started":"2022-03-06T14:37:16.402017Z","shell.execute_reply":"2022-03-06T14:37:16.409451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_tr, x_val, y_tr, y_val= train_test_split(op_mfcc,np.array(y),stratify=y,test_size = 0.2,random_state=777,shuffle=True)","metadata":{"_uuid":"bcdae490-844c-4567-9687-5b9affdaf17a","_cell_guid":"475c173e-8d5e-4c5b-a9d4-f41490c0f625","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:37:19.462835Z","iopub.execute_input":"2022-03-06T14:37:19.463170Z","iopub.status.idle":"2022-03-06T14:37:19.842009Z","shell.execute_reply.started":"2022-03-06T14:37:19.463142Z","shell.execute_reply":"2022-03-06T14:37:19.841150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_tr.shape)\nprint(y_tr.shape)\nprint(x_val.shape)\nprint(y_val.shape)","metadata":{"_uuid":"3e8b60b0-b24c-4354-aec7-302264e9bc7e","_cell_guid":"e0e8eb0d-92cd-4cc4-bdec-31cb0cea9d39","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:37:25.166150Z","iopub.execute_input":"2022-03-06T14:37:25.166511Z","iopub.status.idle":"2022-03-06T14:37:25.172187Z","shell.execute_reply.started":"2022-03-06T14:37:25.166479Z","shell.execute_reply":"2022-03-06T14:37:25.171064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Model Architecture**","metadata":{"_uuid":"1b55e565-3949-4ac7-93f9-01b3be4df31d","_cell_guid":"ea904dd1-ea41-4bf3-8d49-e78262884cbd","trusted":true}},{"cell_type":"code","source":"#Model Architecture\nmodel = Sequential()\nmodel.add(Dense(100, activation='relu', input_shape=(d,), kernel_constraint=maxnorm(3)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(80, activation='relu', kernel_constraint=maxnorm(3)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(len(classes), activation='softmax' , kernel_constraint=maxnorm(3)))","metadata":{"_uuid":"dc19c43c-9192-41b3-b84f-0634852430c2","_cell_guid":"27674ef4-4c25-4e86-87f9-e500e0379246","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:38:36.379177Z","iopub.execute_input":"2022-03-06T14:38:36.379552Z","iopub.status.idle":"2022-03-06T14:38:36.421857Z","shell.execute_reply.started":"2022-03-06T14:38:36.379523Z","shell.execute_reply":"2022-03-06T14:38:36.421102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensorflow.keras.utils.plot_model(model, 'model.png',show_shapes=True)","metadata":{"_uuid":"b0fb8610-8e74-4533-8e2f-24184db85c78","_cell_guid":"6bfa439d-3153-44c7-927b-f549bd4abbd9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:38:39.290306Z","iopub.execute_input":"2022-03-06T14:38:39.290665Z","iopub.status.idle":"2022-03-06T14:38:39.700259Z","shell.execute_reply.started":"2022-03-06T14:38:39.290635Z","shell.execute_reply":"2022-03-06T14:38:39.699337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adamax',metrics=['accuracy'])","metadata":{"_uuid":"8b9cf746-5d09-44f9-8db9-eb86efbbab25","_cell_guid":"5a704c23-9163-403c-ab06-18fa9316ede0","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:38:57.709076Z","iopub.execute_input":"2022-03-06T14:38:57.709446Z","iopub.status.idle":"2022-03-06T14:38:57.724769Z","shell.execute_reply.started":"2022-03-06T14:38:57.709408Z","shell.execute_reply":"2022-03-06T14:38:57.723919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10, min_delta=0.0001) \nmc = ModelCheckpoint('best_model.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')","metadata":{"_uuid":"69d184e7-1581-41be-9a77-2cc4abadf420","_cell_guid":"182861da-9966-48ca-a584-805189d25254","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:39:00.037976Z","iopub.execute_input":"2022-03-06T14:39:00.038468Z","iopub.status.idle":"2022-03-06T14:39:00.043435Z","shell.execute_reply.started":"2022-03-06T14:39:00.038432Z","shell.execute_reply":"2022-03-06T14:39:00.042150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x_tr, y_tr,validation_data=(x_val,y_val), epochs=300, batch_size=65)","metadata":{"_uuid":"339adb27-4da4-46ec-a1fe-fc173b88b27c","_cell_guid":"0a634a76-ca54-4c5e-8353-7a24c5c78f14","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:39:05.777220Z","iopub.execute_input":"2022-03-06T14:39:05.777581Z","iopub.status.idle":"2022-03-06T14:43:24.536211Z","shell.execute_reply.started":"2022-03-06T14:39:05.777550Z","shell.execute_reply":"2022-03-06T14:43:24.535400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_score = model.evaluate(x_tr, y_tr, batch_size=12)\nprint(train_score)\n\nprint('----------------Training Complete-----------------')\n\ntest_score = model.evaluate(x_val, y_val, batch_size = 12)\nprint(test_score)","metadata":{"_uuid":"cece8b44-cb3a-4d33-8d68-97e3d46e32cf","_cell_guid":"883d0952-0ccd-4c47-b395-08719b30939c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:43:47.809891Z","iopub.execute_input":"2022-03-06T14:43:47.810284Z","iopub.status.idle":"2022-03-06T14:43:51.209090Z","shell.execute_reply.started":"2022-03-06T14:43:47.810253Z","shell.execute_reply":"2022-03-06T14:43:51.208121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"_uuid":"2676d20f-c646-42d0-8961-82211997482a","_cell_guid":"38165d2c-6f8d-43d6-b103-5b38e0b00ff0","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:43:54.191515Z","iopub.execute_input":"2022-03-06T14:43:54.191885Z","iopub.status.idle":"2022-03-06T14:43:54.197904Z","shell.execute_reply.started":"2022-03-06T14:43:54.191836Z","shell.execute_reply":"2022-03-06T14:43:54.196892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot\npyplot.plot(history.history['loss'], label='train')\npyplot.plot(history.history['val_loss'], label='test')\npyplot.legend()\npyplot.show()","metadata":{"_uuid":"92a77823-e410-41b3-9e53-fa3c132be234","_cell_guid":"b73f6669-2ebb-46a9-86d7-778c1c35fa2b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:43:56.170903Z","iopub.execute_input":"2022-03-06T14:43:56.171235Z","iopub.status.idle":"2022-03-06T14:43:56.334648Z","shell.execute_reply.started":"2022-03-06T14:43:56.171205Z","shell.execute_reply":"2022-03-06T14:43:56.333768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"_uuid":"f3f9ed39-44ba-4470-956e-b533535a0e57","_cell_guid":"40ec4787-70cc-40f8-8d12-a7fc7f90a31c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:43:58.348570Z","iopub.execute_input":"2022-03-06T14:43:58.348899Z","iopub.status.idle":"2022-03-06T14:43:58.491870Z","shell.execute_reply.started":"2022-03-06T14:43:58.348870Z","shell.execute_reply":"2022-03-06T14:43:58.491000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict=model.predict(x_val)\nconf_mat=tensorflow.math.confusion_matrix(np.argmax(y_val,axis=1) , np.argmax(y_predict,axis=1))","metadata":{"_uuid":"e083f0ea-390d-4265-b9ec-562f2c4e8997","_cell_guid":"d3870f27-7529-4d2f-9e64-3ed41f489451","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:44:04.684620Z","iopub.execute_input":"2022-03-06T14:44:04.684963Z","iopub.status.idle":"2022-03-06T14:44:04.900996Z","shell.execute_reply.started":"2022-03-06T14:44:04.684934Z","shell.execute_reply":"2022-03-06T14:44:04.900018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cm = pd.DataFrame(np.array(conf_mat), index = [i for i in classes],\n                  columns = [i for i in classes])\nplt.figure(figsize = (13,7))\nax = sn.heatmap(df_cm, annot=True)\nplt.title(\"Confusion Matrix\", fontsize=20)\nplt.ylabel(\"True Class\"     , fontsize=20)\nplt.xlabel(\"Predicted Class\", fontsize=20)\nplt.show()","metadata":{"_uuid":"b32cf146-f5eb-49d2-ad41-3e3bd9eb29fa","_cell_guid":"7c79a951-4bc4-4dfc-bc55-fb28465051db","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:44:07.767993Z","iopub.execute_input":"2022-03-06T14:44:07.768305Z","iopub.status.idle":"2022-03-06T14:44:08.366032Z","shell.execute_reply.started":"2022-03-06T14:44:07.768277Z","shell.execute_reply":"2022-03-06T14:44:08.365188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val[1].shape\nmodel.predict(x_val[1].reshape((1,d)))","metadata":{"_uuid":"28c1700e-8da2-4536-a33f-48922c347cfa","_cell_guid":"5521c380-4b50-4d9d-a0d6-7cfedca8baf4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:45:18.927361Z","iopub.execute_input":"2022-03-06T14:45:18.927728Z","iopub.status.idle":"2022-03-06T14:45:18.971983Z","shell.execute_reply.started":"2022-03-06T14:45:18.927698Z","shell.execute_reply":"2022-03-06T14:45:18.971226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define the function that predicts text for the given audio:","metadata":{"_uuid":"324ecd54-52ba-4e52-bf69-2259cd714390","_cell_guid":"1886ec1b-82a3-481f-a2c5-73683efb0240","trusted":true}},{"cell_type":"code","source":"def predict(audio):\n    print(samples.shape)\n    prob=model.predict(audio)\n    index=np.argmax(prob[0])\n    return classes[index]","metadata":{"_uuid":"500a63a1-7868-4749-8510-0a616f263ed9","_cell_guid":"6dc2a647-e41c-4ea1-a3b0-2aab15ebe006","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:45:23.949989Z","iopub.execute_input":"2022-03-06T14:45:23.950338Z","iopub.status.idle":"2022-03-06T14:45:23.955207Z","shell.execute_reply.started":"2022-03-06T14:45:23.950306Z","shell.execute_reply":"2022-03-06T14:45:23.954168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prediction time! Make predictions on the validation data:","metadata":{"_uuid":"b4f73b68-405c-464b-b770-a419a17d06ee","_cell_guid":"6d1d8b28-ca72-4b17-b155-354e58c6e7bf","trusted":true}},{"cell_type":"code","source":"import random\nindex=random.randint(0,len(x_val)-1)\nprint(index)\nsamples=x_val[index]\nprint(\"Audio:\",classes[np.argmax(y_val[index])])\n#ipd.Audio(np.array(all_wave)[index,:], rate=16000)","metadata":{"_uuid":"e95c2ce6-8675-448b-9872-07361c4b9284","_cell_guid":"d52111df-fc73-4cd1-901d-cff989fef2bd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:46:19.549879Z","iopub.execute_input":"2022-03-06T14:46:19.550213Z","iopub.status.idle":"2022-03-06T14:46:19.556525Z","shell.execute_reply.started":"2022-03-06T14:46:19.550176Z","shell.execute_reply":"2022-03-06T14:46:19.555131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Text:\",predict(samples.reshape(1,d)))","metadata":{"_uuid":"993bb26f-9168-4d71-b648-ee9f6d1ea5d8","_cell_guid":"4f6d2064-ce70-488e-9185-4d32f6aba050","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:46:20.749733Z","iopub.execute_input":"2022-03-06T14:46:20.750051Z","iopub.status.idle":"2022-03-06T14:46:20.822834Z","shell.execute_reply.started":"2022-03-06T14:46:20.750023Z","shell.execute_reply":"2022-03-06T14:46:20.821013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nmodel.save(\"8_words_neew.h5\")","metadata":{"_uuid":"b6f43442-e853-42c6-8178-6b315756a150","_cell_guid":"25ad6ef7-3657-4f7b-aaa7-e0c4d5216811","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-06T14:46:21.694639Z","iopub.execute_input":"2022-03-06T14:46:21.694965Z","iopub.status.idle":"2022-03-06T14:46:21.721897Z","shell.execute_reply.started":"2022-03-06T14:46:21.694936Z","shell.execute_reply":"2022-03-06T14:46:21.720971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"be06b3ac-8380-446e-a608-6a8fc5623a4f","_cell_guid":"af6a7594-6fc9-40ba-8512-44b8ab70d10e","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"a7d5dbd5-bbb9-4bee-9acc-c89d6b97bff6","_cell_guid":"f2c9b7a3-4d20-4c60-b0bb-b370689f103f","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}