{"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":"### **Install the required libraries** 🔽","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2022-03-24T09:49:56.875027Z","iopub.execute_input":"2022-03-24T09:49:56.875441Z","iopub.status.idle":"2022-03-24T09:51:57.116858Z","shell.execute_reply.started":"2022-03-24T09:49:56.875401Z","shell.execute_reply":"2022-03-24T09:51:57.115686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Import the required libraries** 📌","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2022-03-24T09:55:09.262076Z","iopub.execute_input":"2022-03-24T09:55:09.262422Z","iopub.status.idle":"2022-03-24T09:55:13.850435Z","shell.execute_reply.started":"2022-03-24T09:55:09.262387Z","shell.execute_reply":"2022-03-24T09:55:13.849391Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T09:55:25.944352Z","iopub.execute_input":"2022-03-24T09:55:25.944725Z","iopub.status.idle":"2022-03-24T09:58:01.918399Z","shell.execute_reply.started":"2022-03-24T09:55:25.944693Z","shell.execute_reply":"2022-03-24T09:58:01.917487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove unnecessary labels data\nimport os\nmain_dataset_path = \"./tensorflow-speech-recognition-challenge/train/train/audio\"\n\ndataset_labels_folders = os.listdir(main_dataset_path)\nfor folder in dataset_labels_folders:\n    if folder not in [\"right\", \"up\", \"left\", \"down\"]:\n        shutil.rmtree(main_dataset_path+\"/\"+folder, ignore_errors=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:58:51.69291Z","iopub.execute_input":"2022-03-24T09:58:51.693236Z","iopub.status.idle":"2022-03-24T09:58:53.419094Z","shell.execute_reply.started":"2022-03-24T09:58:51.693204Z","shell.execute_reply":"2022-03-24T09:58:53.418202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio_path = '/kaggle/working/tensorflow-speech-recognition-challenge/train/train/audio/'","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:58:55.560099Z","iopub.execute_input":"2022-03-24T09:58:55.56044Z","iopub.status.idle":"2022-03-24T09:58:55.566183Z","shell.execute_reply.started":"2022-03-24T09:58:55.560408Z","shell.execute_reply":"2022-03-24T09:58:55.5654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples, sample_rate = librosa.load(train_audio_path+'right/49af4432_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/right/49af4432_nohash_1.wav')\nax1.set_xlabel('time')\nax1.set_ylabel('Amplitude')\nax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:58:59.210671Z","iopub.execute_input":"2022-03-24T09:58:59.211098Z","iopub.status.idle":"2022-03-24T09:58:59.563583Z","shell.execute_reply.started":"2022-03-24T09:58:59.211062Z","shell.execute_reply":"2022-03-24T09:58:59.562657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(samples, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:05.255244Z","iopub.execute_input":"2022-03-24T09:59:05.255651Z","iopub.status.idle":"2022-03-24T09:59:05.264739Z","shell.execute_reply.started":"2022-03-24T09:59:05.255617Z","shell.execute_reply":"2022-03-24T09:59:05.263384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sample_rate)\nsig1=samples\nfs=sample_rate\nsr=fs","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:18.228084Z","iopub.execute_input":"2022-03-24T09:59:18.228409Z","iopub.status.idle":"2022-03-24T09:59:18.233791Z","shell.execute_reply.started":"2022-03-24T09:59:18.228378Z","shell.execute_reply":"2022-03-24T09:59:18.232737Z"},"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\nplt.xlabel(\"Time [seconds]\")\nplt.ylabel(\"Voice amplitude\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:19.983614Z","iopub.execute_input":"2022-03-24T09:59:19.983937Z","iopub.status.idle":"2022-03-24T09:59:20.695347Z","shell.execute_reply.started":"2022-03-24T09:59:19.983908Z","shell.execute_reply":"2022-03-24T09:59:20.694378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(reduced_noise1, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:23.804819Z","iopub.execute_input":"2022-03-24T09:59:23.805147Z","iopub.status.idle":"2022-03-24T09:59:23.813626Z","shell.execute_reply.started":"2022-03-24T09:59:23.805117Z","shell.execute_reply":"2022-03-24T09:59:23.812554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Silence Removal\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_","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:28.579256Z","iopub.execute_input":"2022-03-24T09:59:28.579619Z","iopub.status.idle":"2022-03-24T09:59:28.744201Z","shell.execute_reply.started":"2022-03-24T09:59:28.579588Z","shell.execute_reply":"2022-03-24T09:59:28.743333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(y_, rate = sr )","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:31.378558Z","iopub.execute_input":"2022-03-24T09:59:31.381028Z","iopub.status.idle":"2022-03-24T09:59:31.393831Z","shell.execute_reply.started":"2022-03-24T09:59:31.380983Z","shell.execute_reply":"2022-03-24T09:59:31.392167Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T09:59:39.29323Z","iopub.execute_input":"2022-03-24T09:59:39.293577Z","iopub.status.idle":"2022-03-24T09:59:39.300796Z","shell.execute_reply.started":"2022-03-24T09:59:39.293541Z","shell.execute_reply":"2022-03-24T09:59:39.298917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(time,signal)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:41.44823Z","iopub.execute_input":"2022-03-24T09:59:41.448632Z","iopub.status.idle":"2022-03-24T09:59:41.599241Z","shell.execute_reply.started":"2022-03-24T09:59:41.4486Z","shell.execute_reply":"2022-03-24T09:59:41.598158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = os.listdir(train_audio_path)\nlabels","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:45.321638Z","iopub.execute_input":"2022-03-24T09:59:45.321968Z","iopub.status.idle":"2022-03-24T09:59:45.328256Z","shell.execute_reply.started":"2022-03-24T09:59:45.32194Z","shell.execute_reply":"2022-03-24T09:59:45.327436Z"},"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,10))\nindex = np.arange(len(labels))\nplt.bar(index, no_of_recordings)\nplt.xlabel('Commands', fontsize=20)\nplt.ylabel('No of recordings', fontsize=20)\nplt.xticks(index, labels, fontsize=20, rotation=60)\nplt.title('No. of recordings for each command')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:47.885928Z","iopub.execute_input":"2022-03-24T09:59:47.886274Z","iopub.status.idle":"2022-03-24T09:59:48.107983Z","shell.execute_reply.started":"2022-03-24T09:59:47.886244Z","shell.execute_reply":"2022-03-24T09:59:48.106883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Preform Noise Reduction & Silence Removal** ⚙️","metadata":{}},{"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    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)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T09:59:53.597353Z","iopub.execute_input":"2022-03-24T09:59:53.597709Z","iopub.status.idle":"2022-03-24T10:04:23.671406Z","shell.execute_reply.started":"2022-03-24T09:59:53.59768Z","shell.execute_reply":"2022-03-24T10:04:23.670562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.array(all_wave).shape)\nprint(np.array(all_label).shape)\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":{"execution":{"iopub.status.busy":"2022-03-24T10:04:27.602941Z","iopub.execute_input":"2022-03-24T10:04:27.603269Z","iopub.status.idle":"2022-03-24T10:04:31.097392Z","shell.execute_reply.started":"2022-03-24T10:04:27.603238Z","shell.execute_reply":"2022-03-24T10:04:31.096367Z"},"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)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:04:37.149049Z","iopub.execute_input":"2022-03-24T10:04:37.149405Z","iopub.status.idle":"2022-03-24T10:05:05.579286Z","shell.execute_reply.started":"2022-03-24T10:04:37.149373Z","shell.execute_reply":"2022-03-24T10:05:05.578144Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:05:10.910277Z","iopub.execute_input":"2022-03-24T10:05:10.910671Z","iopub.status.idle":"2022-03-24T10:05:11.108771Z","shell.execute_reply.started":"2022-03-24T10:05:10.910634Z","shell.execute_reply":"2022-03-24T10:05:11.107738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"op_mfcc=np.array(all_mfcc)\nop_mfcc=op_mfcc.reshape(9454, -1)\nop_mfcc.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:05:32.720038Z","iopub.execute_input":"2022-03-24T10:05:32.720387Z","iopub.status.idle":"2022-03-24T10:05:32.790899Z","shell.execute_reply.started":"2022-03-24T10:05:32.720356Z","shell.execute_reply":"2022-03-24T10:05:32.789914Z"},"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_)\nclasses","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:05:38.482206Z","iopub.execute_input":"2022-03-24T10:05:38.482549Z","iopub.status.idle":"2022-03-24T10:05:38.493013Z","shell.execute_reply.started":"2022-03-24T10:05:38.482518Z","shell.execute_reply":"2022-03-24T10:05:38.491999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Model based on ANN** ","metadata":{}},{"cell_type":"code","source":"! pip install --upgrade tensorflow\n! pip install --upgrade tensorflow-gpu\n! pip install keras==2.3.1","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:05:41.774167Z","iopub.execute_input":"2022-03-24T10:05:41.774603Z","iopub.status.idle":"2022-03-24T10:07:48.436886Z","shell.execute_reply.started":"2022-03-24T10:05:41.774548Z","shell.execute_reply":"2022-03-24T10:07:48.435872Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:07:52.85781Z","iopub.execute_input":"2022-03-24T10:07:52.858175Z","iopub.status.idle":"2022-03-24T10:07:52.907353Z","shell.execute_reply.started":"2022-03-24T10:07:52.858138Z","shell.execute_reply":"2022-03-24T10:07:52.906375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=tensorflow.keras.utils.to_categorical(y, num_classes=len(labels), dtype='float32')\ny.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:07:58.194417Z","iopub.execute_input":"2022-03-24T10:07:58.194876Z","iopub.status.idle":"2022-03-24T10:07:58.20934Z","shell.execute_reply.started":"2022-03-24T10:07:58.194835Z","shell.execute_reply":"2022-03-24T10:07:58.208572Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:08:01.848595Z","iopub.execute_input":"2022-03-24T10:08:01.848922Z","iopub.status.idle":"2022-03-24T10:08:01.983108Z","shell.execute_reply.started":"2022-03-24T10:08:01.848894Z","shell.execute_reply":"2022-03-24T10:08:01.982258Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:08:09.437955Z","iopub.execute_input":"2022-03-24T10:08:09.438278Z","iopub.status.idle":"2022-03-24T10:08:09.444934Z","shell.execute_reply.started":"2022-03-24T10:08:09.438247Z","shell.execute_reply":"2022-03-24T10:08:09.443956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Model Architecture**","metadata":{}},{"cell_type":"code","source":"#from keras.models import Sequential\n#from keras.layers import Dense, Dropout, Activation\n\n#Model Architecture\nmodel = Sequential()\nmodel.add(Dense(100, activation='sigmoid', input_shape=(d,), kernel_constraint=maxnorm(3)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(4, activation='softmax' , kernel_constraint=maxnorm(3)))","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:13:08.49008Z","iopub.execute_input":"2022-03-24T10:13:08.490424Z","iopub.status.idle":"2022-03-24T10:13:08.519109Z","shell.execute_reply.started":"2022-03-24T10:13:08.490393Z","shell.execute_reply":"2022-03-24T10:13:08.51837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensorflow.keras.utils.plot_model(model, 'model.png',show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:13:13.148364Z","iopub.execute_input":"2022-03-24T10:13:13.148753Z","iopub.status.idle":"2022-03-24T10:13:13.359151Z","shell.execute_reply.started":"2022-03-24T10:13:13.148721Z","shell.execute_reply":"2022-03-24T10:13:13.358211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:13:22.775991Z","iopub.execute_input":"2022-03-24T10:13:22.776336Z","iopub.status.idle":"2022-03-24T10:13:22.790787Z","shell.execute_reply.started":"2022-03-24T10:13:22.776298Z","shell.execute_reply":"2022-03-24T10:13:22.789794Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:13:25.230382Z","iopub.execute_input":"2022-03-24T10:13:25.230745Z","iopub.status.idle":"2022-03-24T10:13:25.235576Z","shell.execute_reply.started":"2022-03-24T10:13:25.230716Z","shell.execute_reply":"2022-03-24T10:13:25.234573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x_tr, y_tr,validation_data=(x_val,y_val), epochs=120, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:13:28.451693Z","iopub.execute_input":"2022-03-24T10:13:28.452016Z","iopub.status.idle":"2022-03-24T10:14:54.775734Z","shell.execute_reply.started":"2022-03-24T10:13:28.451988Z","shell.execute_reply":"2022-03-24T10:14:54.774906Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:15:03.956295Z","iopub.execute_input":"2022-03-24T10:15:03.956644Z","iopub.status.idle":"2022-03-24T10:15:05.514361Z","shell.execute_reply.started":"2022-03-24T10:15:03.956613Z","shell.execute_reply":"2022-03-24T10:15:05.513337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:15:19.886779Z","iopub.execute_input":"2022-03-24T10:15:19.887098Z","iopub.status.idle":"2022-03-24T10:15:19.894682Z","shell.execute_reply.started":"2022-03-24T10:15:19.88707Z","shell.execute_reply":"2022-03-24T10:15:19.893549Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:15:49.233661Z","iopub.execute_input":"2022-03-24T10:15:49.23403Z","iopub.status.idle":"2022-03-24T10:15:49.390337Z","shell.execute_reply.started":"2022-03-24T10:15:49.233998Z","shell.execute_reply":"2022-03-24T10:15:49.389513Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:15:55.076237Z","iopub.execute_input":"2022-03-24T10:15:55.076601Z","iopub.status.idle":"2022-03-24T10:15:55.249347Z","shell.execute_reply.started":"2022-03-24T10:15:55.076567Z","shell.execute_reply":"2022-03-24T10:15:55.24842Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:16:06.544971Z","iopub.execute_input":"2022-03-24T10:16:06.545305Z","iopub.status.idle":"2022-03-24T10:16:07.067804Z","shell.execute_reply.started":"2022-03-24T10:16:06.545268Z","shell.execute_reply":"2022-03-24T10:16:07.066659Z"},"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":{"execution":{"iopub.status.busy":"2022-03-24T10:16:17.494327Z","iopub.execute_input":"2022-03-24T10:16:17.494673Z","iopub.status.idle":"2022-03-24T10:16:17.759138Z","shell.execute_reply.started":"2022-03-24T10:16:17.494644Z","shell.execute_reply":"2022-03-24T10:16:17.758346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val[1].shape\nmodel.predict(x_val[1].reshape((1,d)))","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:16:33.387885Z","iopub.execute_input":"2022-03-24T10:16:33.388203Z","iopub.status.idle":"2022-03-24T10:16:33.437317Z","shell.execute_reply.started":"2022-03-24T10:16:33.388175Z","shell.execute_reply":"2022-03-24T10:16:33.436401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2022-03-24T10:16:49.13607Z","iopub.execute_input":"2022-03-24T10:16:49.136411Z","iopub.status.idle":"2022-03-24T10:16:49.143037Z","shell.execute_reply.started":"2022-03-24T10:16:49.136379Z","shell.execute_reply":"2022-03-24T10:16:49.142041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2022-03-24T10:17:25.109065Z","iopub.execute_input":"2022-03-24T10:17:25.109395Z","iopub.status.idle":"2022-03-24T10:17:25.115892Z","shell.execute_reply.started":"2022-03-24T10:17:25.109363Z","shell.execute_reply":"2022-03-24T10:17:25.115017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Text:\",predict(samples.reshape(1,d)))","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:17:27.375113Z","iopub.execute_input":"2022-03-24T10:17:27.375428Z","iopub.status.idle":"2022-03-24T10:17:27.415764Z","shell.execute_reply.started":"2022-03-24T10:17:27.3754Z","shell.execute_reply":"2022-03-24T10:17:27.414911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nmodel.save(\"CommandsRecognitionModel\")","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:24:54.272807Z","iopub.execute_input":"2022-03-24T10:24:54.273161Z","iopub.status.idle":"2022-03-24T10:24:54.828344Z","shell.execute_reply.started":"2022-03-24T10:24:54.27313Z","shell.execute_reply":"2022-03-24T10:24:54.827555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# Convert the model\nconverter = tf.lite.TFLiteConverter.from_saved_model(\"./CommandsRecognitionModel\") # path to the SavedModel directory\ntflite_model = converter.convert()\n\n# Save the model.\nwith open('CommandsRecognitionModel.tflite', 'wb') as f:\n  f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:25:28.630117Z","iopub.execute_input":"2022-03-24T10:25:28.630463Z","iopub.status.idle":"2022-03-24T10:25:28.915333Z","shell.execute_reply.started":"2022-03-24T10:25:28.630434Z","shell.execute_reply":"2022-03-24T10:25:28.914441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center> <h1><a href=\"./CommandsRecognitionModel.tflite\"> Download TensorFlow Lite Model</a></h1>","metadata":{}}]}