{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyunpack\n!pip install patool","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:48:29.589890Z","iopub.execute_input":"2022-12-17T21:48:29.590825Z","iopub.status.idle":"2022-12-17T21:48:55.566520Z","shell.execute_reply.started":"2022-12-17T21:48:29.590724Z","shell.execute_reply":"2022-12-17T21:48:55.565352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom pyunpack import Archive\nimport shutil\nif not os.path.exists('/kaggle/working/train/'):\n    os.makedirs('/kaggle/working/train/')\nArchive('/kaggle/input/tensorflow-speech-recognition-challenge/train.7z').extractall('/kaggle/working/train/')\nfor dirname, _, filenames in os.walk('/kaggle/working/train/'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:48:55.569279Z","iopub.execute_input":"2022-12-17T21:48:55.569718Z","iopub.status.idle":"2022-12-17T21:50:45.842442Z","shell.execute_reply.started":"2022-12-17T21:48:55.569672Z","shell.execute_reply":"2022-12-17T21:50:45.841433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"words=os.listdir('/kaggle/working/train/train/audio')\nwords","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:50:45.843850Z","iopub.execute_input":"2022-12-17T21:50:45.845377Z","iopub.status.idle":"2022-12-17T21:50:45.857352Z","shell.execute_reply.started":"2022-12-17T21:50:45.845332Z","shell.execute_reply":"2022-12-17T21:50:45.856112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\ntrain_audio_path='/kaggle/working/train/train/audio'\nlabels=os.listdir(train_audio_path)\n\n#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":{"execution":{"iopub.status.busy":"2022-12-17T21:50:45.860331Z","iopub.execute_input":"2022-12-17T21:50:45.863437Z","iopub.status.idle":"2022-12-17T21:50:46.359952Z","shell.execute_reply.started":"2022-12-17T21:50:45.863400Z","shell.execute_reply":"2022-12-17T21:50:46.358984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\nall_wave = []\nall_label = []\n\nfor label in labels[:6]:\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 = 8000)\n        #samples = librosa.resample(samples, sample_rate, 8000)\n        if(len(samples)== 8000) : \n            all_wave.append(samples)\n            all_label.append(label)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:50:46.361788Z","iopub.execute_input":"2022-12-17T21:50:46.362510Z","iopub.status.idle":"2022-12-17T21:53:42.457508Z","shell.execute_reply.started":"2022-12-17T21:50:46.362471Z","shell.execute_reply":"2022-12-17T21:53:42.456370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ny=le.fit_transform(all_label)\nclasses= list(le.classes_)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:42.458888Z","iopub.execute_input":"2022-12-17T21:53:42.459689Z","iopub.status.idle":"2022-12-17T21:53:42.469187Z","shell.execute_reply.started":"2022-12-17T21:53:42.459652Z","shell.execute_reply":"2022-12-17T21:53:42.468139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\ny=np_utils.to_categorical(y, num_classes=len(labels))","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:42.470413Z","iopub.execute_input":"2022-12-17T21:53:42.471559Z","iopub.status.idle":"2022-12-17T21:53:52.363559Z","shell.execute_reply.started":"2022-12-17T21:53:42.471522Z","shell.execute_reply":"2022-12-17T21:53:52.362277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_wave","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:52.368801Z","iopub.execute_input":"2022-12-17T21:53:52.371741Z","iopub.status.idle":"2022-12-17T21:53:52.715170Z","shell.execute_reply.started":"2022-12-17T21:53:52.371699Z","shell.execute_reply":"2022-12-17T21:53:52.713965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_wave = np.array(all_wave).reshape(-1,8000)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:52.719061Z","iopub.execute_input":"2022-12-17T21:53:52.720379Z","iopub.status.idle":"2022-12-17T21:53:52.826749Z","shell.execute_reply.started":"2022-12-17T21:53:52.720333Z","shell.execute_reply":"2022-12-17T21:53:52.825620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_wave.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:52.831407Z","iopub.execute_input":"2022-12-17T21:53:52.831712Z","iopub.status.idle":"2022-12-17T21:53:52.840364Z","shell.execute_reply.started":"2022-12-17T21:53:52.831684Z","shell.execute_reply":"2022-12-17T21:53:52.839227Z"},"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(np.array(all_wave),np.array(y),stratify=y,test_size = 0.2,random_state=777,shuffle=True)\nx_te, x_val, y_te, y_val = train_test_split(x_val,y_val,stratify=y_val,test_size = 0.5,random_state=777,shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:52.842189Z","iopub.execute_input":"2022-12-17T21:53:52.842892Z","iopub.status.idle":"2022-12-17T21:53:53.324569Z","shell.execute_reply.started":"2022-12-17T21:53:52.842853Z","shell.execute_reply":"2022-12-17T21:53:53.323345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense, Dropout, Flatten, Conv1D, Input, MaxPooling1D\nfrom keras.models import Model\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras import backend as K\nK.clear_session()\n\ninputs = Input(shape=(8000,1))\n\n#First Conv1D layer\nconv = Conv1D(8,13, padding='valid', activation='relu', strides=1)(inputs)\nconv = MaxPooling1D(3)(conv)\nconv = Dropout(0.3)(conv)\n\n#Second Conv1D layer\nconv = Conv1D(16, 11, padding='valid', activation='relu', strides=1)(conv)\nconv = MaxPooling1D(3)(conv)\nconv = Dropout(0.3)(conv)\n\n#Third Conv1D layer\nconv = Conv1D(32, 9, padding='valid', activation='relu', strides=1)(conv)\nconv = MaxPooling1D(3)(conv)\nconv = Dropout(0.3)(conv)\n\n#Fourth Conv1D layer\nconv = Conv1D(64, 7, padding='valid', activation='relu', strides=1)(conv)\nconv = MaxPooling1D(3)(conv)\nconv = Dropout(0.3)(conv)\n\n#Flatten layer\nconv = Flatten()(conv)\n\n#Dense Layer 1\nconv = Dense(256, activation='relu')(conv)\nconv = Dropout(0.3)(conv)\n\n#Dense Layer 2\nconv = Dense(128, activation='relu')(conv)\nconv = Dropout(0.3)(conv)\n\noutputs = Dense(len(labels), activation='softmax')(conv)\n\nmodel = Model(inputs, outputs)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:53.326350Z","iopub.execute_input":"2022-12-17T21:53:53.327430Z","iopub.status.idle":"2022-12-17T21:53:59.104998Z","shell.execute_reply.started":"2022-12-17T21:53:53.327380Z","shell.execute_reply":"2022-12-17T21:53:59.103815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10, min_delta=0.0001) \n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:59.108406Z","iopub.execute_input":"2022-12-17T21:53:59.108745Z","iopub.status.idle":"2022-12-17T21:53:59.123745Z","shell.execute_reply.started":"2022-12-17T21:53:59.108718Z","shell.execute_reply":"2022-12-17T21:53:59.122733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x_tr, y_tr ,epochs=100,callbacks=es,  batch_size=32, validation_data=(x_val,y_val))\n","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:53:59.125588Z","iopub.execute_input":"2022-12-17T21:53:59.125950Z","iopub.status.idle":"2022-12-17T21:55:11.918838Z","shell.execute_reply.started":"2022-12-17T21:53:59.125903Z","shell.execute_reply":"2022-12-17T21:55:11.917756Z"},"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-12-17T21:55:11.922303Z","iopub.execute_input":"2022-12-17T21:55:11.922690Z","iopub.status.idle":"2022-12-17T21:55:12.160963Z","shell.execute_reply.started":"2022-12-17T21:55:11.922660Z","shell.execute_reply":"2022-12-17T21:55:12.160008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import save\nmodel.save('best_model.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:55:12.162538Z","iopub.execute_input":"2022-12-17T21:55:12.162910Z","iopub.status.idle":"2022-12-17T21:55:12.243007Z","shell.execute_reply.started":"2022-12-17T21:55:12.162873Z","shell.execute_reply":"2022-12-17T21:55:12.242046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(audio):\n    prob=model.predict(audio.reshape(1,8000))\n    index=np.argmax(prob[0])\n    return classes[index]","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:55:12.244748Z","iopub.execute_input":"2022-12-17T21:55:12.245188Z","iopub.status.idle":"2022-12-17T21:55:12.252298Z","shell.execute_reply.started":"2022-12-17T21:55:12.245147Z","shell.execute_reply":"2022-12-17T21:55:12.251134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nindex=random.randint(0,len(x_val)-1)\nsamples=x_val[index]\nprint(\"Audio:\",classes[np.argmax(y_val[index])])\n#ipd.Audio(samples, rate=8000)\nprint(\"Text:\",predict(samples))","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:55:12.253761Z","iopub.execute_input":"2022-12-17T21:55:12.254188Z","iopub.status.idle":"2022-12-17T21:55:12.463040Z","shell.execute_reply.started":"2022-12-17T21:55:12.254148Z","shell.execute_reply":"2022-12-17T21:55:12.462136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x=x_te, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:55:12.464280Z","iopub.execute_input":"2022-12-17T21:55:12.464580Z","iopub.status.idle":"2022-12-17T21:55:12.614791Z","shell.execute_reply.started":"2022-12-17T21:55:12.464553Z","shell.execute_reply":"2022-12-17T21:55:12.613661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_te.shape,predictions.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:55:12.616164Z","iopub.execute_input":"2022-12-17T21:55:12.616501Z","iopub.status.idle":"2022-12-17T21:55:12.621646Z","shell.execute_reply.started":"2022-12-17T21:55:12.616466Z","shell.execute_reply":"2022-12-17T21:55:12.620667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\n\nconfusion_matrix = confusion_matrix(y_te.argmax(axis=1),predictions.argmax(axis=1))\nconfusion_matrix\nprint(classification_report(y_te.argmax(axis=1), predictions.argmax(axis=1)))","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:55:12.623384Z","iopub.execute_input":"2022-12-17T21:55:12.624110Z","iopub.status.idle":"2022-12-17T21:55:12.644986Z","shell.execute_reply.started":"2022-12-17T21:55:12.624038Z","shell.execute_reply":"2022-12-17T21:55:12.643753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sn\n\nsn.heatmap(confusion_matrix, annot=True, cmap=plt.cm.Blues)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T21:55:12.646401Z","iopub.execute_input":"2022-12-17T21:55:12.647263Z","iopub.status.idle":"2022-12-17T21:55:13.154267Z","shell.execute_reply.started":"2022-12-17T21:55:12.647216Z","shell.execute_reply":"2022-12-17T21:55:13.153069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}