{"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":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-10-24T05:09:28.299641Z","iopub.execute_input":"2021-10-24T05:09:28.299905Z","iopub.status.idle":"2021-10-24T05:09:28.307638Z","shell.execute_reply.started":"2021-10-24T05:09:28.299879Z","shell.execute_reply":"2021-10-24T05:09:28.306725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os.path import isdir, join\nfrom pathlib import Path\nimport pandas as pd\n\n# Math\nimport numpy as np\nfrom scipy.fftpack import fft\nfrom scipy import signal\nfrom scipy.io import wavfile\nimport librosa\n\nfrom sklearn.decomposition import PCA\n\n# Visualization\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport IPython.display as ipd\nimport librosa.display\n\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nimport plotly.graph_objs as go\nimport plotly.tools as tls\nimport pandas as pd\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:09:28.338513Z","iopub.execute_input":"2021-10-24T05:09:28.338801Z","iopub.status.idle":"2021-10-24T05:09:30.230338Z","shell.execute_reply.started":"2021-10-24T05:09:28.338775Z","shell.execute_reply":"2021-10-24T05:09:30.229582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport librosa   #for audio processing\nimport IPython.display as ipd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.io import wavfile #for audio processing\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2021-10-24T05:09:30.232337Z","iopub.execute_input":"2021-10-24T05:09:30.232699Z","iopub.status.idle":"2021-10-24T05:09:30.238557Z","shell.execute_reply.started":"2021-10-24T05:09:30.23266Z","shell.execute_reply":"2021-10-24T05:09:30.237771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt-get install -y p7zip-full\n!7z x ../input/tensorflow-speech-recognition-challenge/train.7z","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:09:30.240837Z","iopub.execute_input":"2021-10-24T05:09:30.24136Z","iopub.status.idle":"2021-10-24T05:11:32.245773Z","shell.execute_reply.started":"2021-10-24T05:09:30.241323Z","shell.execute_reply":"2021-10-24T05:11:32.244868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio_path = '../input/tensorflow-speech-recognition-challenge/train/audio/'","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:11:32.247288Z","iopub.execute_input":"2021-10-24T05:11:32.247559Z","iopub.status.idle":"2021-10-24T05:11:32.252339Z","shell.execute_reply.started":"2021-10-24T05:11:32.247531Z","shell.execute_reply":"2021-10-24T05:11:32.251169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We'll need numpy for some mathematical operations\nimport numpy as np\n\n# matplotlib for displaying the output\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\n# and IPython.display for audio output\nimport IPython.display\n\n# Librosa for audio\nimport librosa\n# And the display module for visualization\nimport librosa.display","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:11:32.256501Z","iopub.execute_input":"2021-10-24T05:11:32.257183Z","iopub.status.idle":"2021-10-24T05:11:32.272551Z","shell.execute_reply.started":"2021-10-24T05:11:32.25714Z","shell.execute_reply":"2021-10-24T05:11:32.271506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\naudio_path = librosa.util.example_audio_file()\n\n# or uncomment the line below and point it at your favorite song:\n#\n# audio_path = '/path/to/your/favorite/song.mp3'\n\ny, sr = librosa.load(audio_path)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:11:32.286294Z","iopub.execute_input":"2021-10-24T05:11:32.286976Z","iopub.status.idle":"2021-10-24T05:11:36.084993Z","shell.execute_reply.started":"2021-10-24T05:11:32.286939Z","shell.execute_reply":"2021-10-24T05:11:36.084062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Let's make and display a mel-scaled power (energy-squared) spectrogram\nS = librosa.feature.melspectrogram(y, sr=sr, n_mels=128)\n\n# Convert to log scale (dB). We'll use the peak power (max) as reference.\nlog_S = librosa.power_to_db(S, ref=np.max)\n\n# Make a new figure\nplt.figure(figsize=(12,4))\n\n# Display the spectrogram on a mel scale\n# sample rate and hop length parameters are used to render the time axis\nlibrosa.display.specshow(log_S, sr=sr, x_axis='time', y_axis='mel')\n\n# Put a descriptive title on the plot\nplt.title('mel power spectrogram')\n\n# draw a color bar\nplt.colorbar(format='%+02.0f dB')\n\n# Make the figure layout compact\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:11:36.086333Z","iopub.execute_input":"2021-10-24T05:11:36.086707Z","iopub.status.idle":"2021-10-24T05:11:36.658631Z","shell.execute_reply.started":"2021-10-24T05:11:36.086672Z","shell.execute_reply":"2021-10-24T05:11:36.657644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio_path = 'train/audio/'\nfilename = 'yes/0a7c2a8d_nohash_0.wav'\nsample_rate, samples = wavfile.read(str(train_audio_path) + filename)\nipd.Audio(samples, rate=sample_rate)\nsamples_cut = samples[4000:13000]\nipd.Audio(samples_cut, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:18:22.856529Z","iopub.execute_input":"2021-10-24T05:18:22.856949Z","iopub.status.idle":"2021-10-24T05:18:22.868853Z","shell.execute_reply.started":"2021-10-24T05:18:22.856919Z","shell.execute_reply":"2021-10-24T05:18:22.867725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_fft(y, fs):\n    T = 1.0 / fs\n    N = y.shape[0]\n    yf = fft(y)\n    xf = np.linspace(0.0, 1.0/(2.0*T), N//2)\n    vals = 2.0/N * np.abs(yf[0:N//2])  # FFT is simmetrical, so we take just the first half\n    # FFT is also complex, to we take just the real part (abs)\n    return xf, vals","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:18:28.130985Z","iopub.execute_input":"2021-10-24T05:18:28.131427Z","iopub.status.idle":"2021-10-24T05:18:28.138347Z","shell.execute_reply.started":"2021-10-24T05:18:28.131383Z","shell.execute_reply":"2021-10-24T05:18:28.136895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/happy/0b09edd3_nohash_0.wav'\nnew_sample_rate = 8000\n\nsample_rate, samples = wavfile.read(str(train_audio_path) + filename)\nresampled = signal.resample(samples, int(new_sample_rate/sample_rate * samples.shape[0]))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:18:29.419187Z","iopub.execute_input":"2021-10-24T05:18:29.419565Z","iopub.status.idle":"2021-10-24T05:18:29.427889Z","shell.execute_reply.started":"2021-10-24T05:18:29.419507Z","shell.execute_reply":"2021-10-24T05:18:29.426822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(samples, rate=sample_rate)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:19:04.400619Z","iopub.execute_input":"2021-10-24T05:19:04.400947Z","iopub.status.idle":"2021-10-24T05:19:04.408433Z","shell.execute_reply.started":"2021-10-24T05:19:04.400916Z","shell.execute_reply":"2021-10-24T05:19:04.40748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(resampled, rate=new_sample_rate)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:19:04.821653Z","iopub.execute_input":"2021-10-24T05:19:04.821946Z","iopub.status.idle":"2021-10-24T05:19:04.830803Z","shell.execute_reply.started":"2021-10-24T05:19:04.821918Z","shell.execute_reply":"2021-10-24T05:19:04.829527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xf, vals = custom_fft(samples, sample_rate)\nplt.figure(figsize=(12, 4))\nplt.title('FFT of recording sampled with ' + str(sample_rate) + ' Hz')\nplt.plot(xf, vals)\nplt.xlabel('Frequency')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:19:05.973863Z","iopub.execute_input":"2021-10-24T05:19:05.974184Z","iopub.status.idle":"2021-10-24T05:19:06.134468Z","shell.execute_reply.started":"2021-10-24T05:19:05.974154Z","shell.execute_reply":"2021-10-24T05:19:06.13355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xf, vals = custom_fft(resampled, new_sample_rate)\nplt.figure(figsize=(12, 4))\nplt.title('FFT of recording sampled with ' + str(new_sample_rate) + ' Hz')\nplt.plot(xf, vals)\nplt.xlabel('Frequency')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:19:11.721937Z","iopub.execute_input":"2021-10-24T05:19:11.722252Z","iopub.status.idle":"2021-10-24T05:19:11.875971Z","shell.execute_reply.started":"2021-10-24T05:19:11.722223Z","shell.execute_reply":"2021-10-24T05:19:11.875121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = ['on/004ae714_nohash_0.wav', 'on/0137b3f4_nohash_0.wav']\nfor filename in filenames:\n    sample_rate, samples = wavfile.read(str(train_audio_path) + filename)\n    xf, vals = custom_fft(samples, sample_rate)\n    plt.figure(figsize=(12, 4))\n    plt.title('FFT of speaker ' + filename[4:11])\n    plt.plot(xf, vals)\n    plt.xlabel('Frequency')\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:21:54.784364Z","iopub.execute_input":"2021-10-24T05:21:54.78472Z","iopub.status.idle":"2021-10-24T05:21:55.106726Z","shell.execute_reply.started":"2021-10-24T05:21:54.784688Z","shell.execute_reply":"2021-10-24T05:21:55.105788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Speaker ' + filenames[0][4:11])\nipd.Audio(join(train_audio_path, filenames[0]))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:26:42.07708Z","iopub.execute_input":"2021-10-24T05:26:42.077409Z","iopub.status.idle":"2021-10-24T05:26:42.087176Z","shell.execute_reply.started":"2021-10-24T05:26:42.077378Z","shell.execute_reply":"2021-10-24T05:26:42.086006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Speaker ' + filenames[1][4:11])\nipd.Audio(join(train_audio_path, filenames[1]))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:26:43.437357Z","iopub.execute_input":"2021-10-24T05:26:43.437694Z","iopub.status.idle":"2021-10-24T05:26:43.447618Z","shell.execute_reply.started":"2021-10-24T05:26:43.437663Z","shell.execute_reply":"2021-10-24T05:26:43.446533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirs = [f for f in os.listdir(train_audio_path) if isdir(join(train_audio_path, f))]\ndirs.sort()\nprint('Number of labels: ' + str(len(dirs)))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:26:46.58715Z","iopub.execute_input":"2021-10-24T05:26:46.587472Z","iopub.status.idle":"2021-10-24T05:26:46.594092Z","shell.execute_reply.started":"2021-10-24T05:26:46.587443Z","shell.execute_reply":"2021-10-24T05:26:46.593206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirs","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:27:03.129203Z","iopub.execute_input":"2021-10-24T05:27:03.129523Z","iopub.status.idle":"2021-10-24T05:27:03.135387Z","shell.execute_reply.started":"2021-10-24T05:27:03.129492Z","shell.execute_reply":"2021-10-24T05:27:03.134555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_shorter = 0\nfor direct in dirs:\n    waves = [f for f in os.listdir(join(train_audio_path, direct)) if f.endswith('.wav')]\n    for wav in waves:\n        sample_rate, samples = wavfile.read(train_audio_path + direct + '/' + wav)\n        if samples.shape[0] < sample_rate:\n            num_of_shorter += 1\nprint('Number of recordings shorter than 1 second: ' + str(num_of_shorter))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:27:56.452851Z","iopub.execute_input":"2021-10-24T05:27:56.453179Z","iopub.status.idle":"2021-10-24T05:27:58.926966Z","shell.execute_reply.started":"2021-10-24T05:27:56.45315Z","shell.execute_reply":"2021-10-24T05:27:58.926044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_keep = 'yes no'.split()\ndirs = [d for d in dirs if d in to_keep]","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:28:41.574202Z","iopub.execute_input":"2021-10-24T05:28:41.57452Z","iopub.status.idle":"2021-10-24T05:28:41.580442Z","shell.execute_reply.started":"2021-10-24T05:28:41.57449Z","shell.execute_reply":"2021-10-24T05:28:41.57955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fft_all = []\nnames = []\nfor direct in dirs:\n    waves = [f for f in os.listdir(join(train_audio_path, direct)) if f.endswith('.wav')]\n    for wav in waves:\n        sample_rate, samples = wavfile.read(train_audio_path + direct + '/' + wav)\n        if samples.shape[0] != sample_rate:\n            samples = np.append(samples, np.zeros((sample_rate - samples.shape[0], )))\n        x, val = custom_fft(samples, sample_rate)\n        fft_all.append(val)\n        names.append(direct + '/' + wav)\n\nfft_all = np.array(fft_all)\n\n# Normalization\nfft_all = (fft_all - np.mean(fft_all, axis=0)) / np.std(fft_all, axis=0)\n\n# Dim reduction\npca = PCA(n_components=3)\nfft_all = pca.fit_transform(fft_all)\n\n# print(names)\ndef interactive_3d_plot(data, names):\n    scatt = go.Scatter3d(x=data[:, 0], y=data[:, 1], z=data[:, 2], mode='markers', text=names)\n    data = go.Data([scatt])\n    layout = go.Layout(title=\"Anomaly detection\")\n    figure = go.Figure(data=data, layout=layout)\n    py.iplot(figure)\n    \ninteractive_3d_plot(fft_all, names)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:42:58.875337Z","iopub.execute_input":"2021-10-24T05:42:58.875749Z","iopub.status.idle":"2021-10-24T05:43:03.952766Z","shell.execute_reply.started":"2021-10-24T05:42:58.875714Z","shell.execute_reply":"2021-10-24T05:43:03.951974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Recording yes/5165cf0a_nohash_0.wav')\nipd.Audio(join(train_audio_path, 'yes/5165cf0a_nohash_0.wav'))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:45:13.718642Z","iopub.execute_input":"2021-10-24T05:45:13.718963Z","iopub.status.idle":"2021-10-24T05:45:13.729519Z","shell.execute_reply.started":"2021-10-24T05:45:13.718933Z","shell.execute_reply":"2021-10-24T05:45:13.728362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Recording yes/e4b02540_nohash_0.wav')\nipd.Audio(join(train_audio_path, 'yes/e4b02540_nohash_0.wav'))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T05:45:55.165732Z","iopub.execute_input":"2021-10-24T05:45:55.166064Z","iopub.status.idle":"2021-10-24T05:45:55.175914Z","shell.execute_reply.started":"2021-10-24T05:45:55.166033Z","shell.execute_reply":"2021-10-24T05:45:55.174945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples=np.array(samples, dtype='float64')\n\nsamples = librosa.resample(samples, sample_rate, 8000)\nipd.Audio(samples, rate=8000)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:06:37.710888Z","iopub.execute_input":"2021-10-24T06:06:37.711205Z","iopub.status.idle":"2021-10-24T06:06:38.083324Z","shell.execute_reply.started":"2021-10-24T06:06:37.711174Z","shell.execute_reply":"2021-10-24T06:06:38.082477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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()\n\nlabels=[\"yes\", \"no\"]","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:09:06.961237Z","iopub.execute_input":"2021-10-24T06:09:06.961644Z","iopub.status.idle":"2021-10-24T06:09:07.287429Z","shell.execute_reply.started":"2021-10-24T06:09:06.961608Z","shell.execute_reply":"2021-10-24T06:09:07.286555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nduration_of_recordings=[]\nfor label in labels:\n    waves = [f for f in os.listdir(train_audio_path + '/'+ label) if f.endswith('.wav')]\n    for wav in waves:\n        sample_rate, samples = wavfile.read(train_audio_path + '/' + label + '/' + wav)\n        duration_of_recordings.append(float(len(samples)/sample_rate))\n    \nplt.hist(np.array(duration_of_recordings))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:09:08.902804Z","iopub.execute_input":"2021-10-24T06:09:08.903132Z","iopub.status.idle":"2021-10-24T06:09:09.219749Z","shell.execute_reply.started":"2021-10-24T06:09:08.903102Z","shell.execute_reply":"2021-10-24T06:09:09.218879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio_path = 'train/audio/'\n\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 = 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":"2021-10-24T06:09:37.539512Z","iopub.execute_input":"2021-10-24T06:09:37.539886Z","iopub.status.idle":"2021-10-24T06:10:49.512321Z","shell.execute_reply.started":"2021-10-24T06:09:37.539855Z","shell.execute_reply":"2021-10-24T06:10:49.511554Z"},"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":"2021-10-24T06:13:44.812495Z","iopub.execute_input":"2021-10-24T06:13:44.812878Z","iopub.status.idle":"2021-10-24T06:13:44.819796Z","shell.execute_reply.started":"2021-10-24T06:13:44.812847Z","shell.execute_reply":"2021-10-24T06:13:44.8185Z"},"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":"2021-10-24T06:13:46.547902Z","iopub.execute_input":"2021-10-24T06:13:46.548224Z","iopub.status.idle":"2021-10-24T06:13:50.673632Z","shell.execute_reply.started":"2021-10-24T06:13:46.548192Z","shell.execute_reply":"2021-10-24T06:13:50.672796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:13:54.553282Z","iopub.execute_input":"2021-10-24T06:13:54.553643Z","iopub.status.idle":"2021-10-24T06:13:54.559656Z","shell.execute_reply.started":"2021-10-24T06:13:54.553607Z","shell.execute_reply":"2021-10-24T06:13:54.558518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_wave = np.array(all_wave).reshape(-1,8000,1)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:13:59.741099Z","iopub.execute_input":"2021-10-24T06:13:59.741418Z","iopub.status.idle":"2021-10-24T06:13:59.781701Z","shell.execute_reply.started":"2021-10-24T06:13:59.741388Z","shell.execute_reply":"2021-10-24T06:13:59.780897Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:14:01.998815Z","iopub.execute_input":"2021-10-24T06:14:01.999144Z","iopub.status.idle":"2021-10-24T06:14:02.12794Z","shell.execute_reply.started":"2021-10-24T06:14:01.999112Z","shell.execute_reply":"2021-10-24T06:14:02.127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_tr.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:29:43.932139Z","iopub.execute_input":"2021-10-24T06:29:43.93264Z","iopub.status.idle":"2021-10-24T06:29:43.943255Z","shell.execute_reply.started":"2021-10-24T06:29:43.93259Z","shell.execute_reply":"2021-10-24T06:29:43.94218Z"},"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.2)(conv)\n\n#Second Conv1D layer\nconv = Conv1D(16, 11, padding='valid', activation='relu', strides=1)(conv)\nconv = MaxPooling1D(3)(conv)\nconv = Dropout(0.2)(conv)\n\n#Third Conv1D layer\nconv = Conv1D(32, 9, padding='valid', activation='relu', strides=1)(conv)\nconv = MaxPooling1D(3)(conv)\nconv = Dropout(0.2)(conv)\n\n#Fourth Conv1D layer\nconv = Conv1D(64, 7, padding='valid', activation='relu', strides=1)(conv)\nconv = MaxPooling1D(3)(conv)\nconv = Dropout(0.2)(conv)\n\n#Flatten layer\nconv = Flatten()(conv)\n\n#Dense Layer 1\nconv = Dense(256, activation='relu')(conv)\nconv = Dropout(0.2)(conv)\n\n#Dense Layer 2\nconv = Dense(128, activation='relu')(conv)\nconv = Dropout(0.2)(conv)\n\noutputs = Dense(len(labels), activation='softmax')(conv)\n\nmodel = Model(inputs, outputs)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:27:56.918165Z","iopub.execute_input":"2021-10-24T06:27:56.918528Z","iopub.status.idle":"2021-10-24T06:27:59.04595Z","shell.execute_reply.started":"2021-10-24T06:27:56.918495Z","shell.execute_reply":"2021-10-24T06:27:59.045207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:37:40.612318Z","iopub.execute_input":"2021-10-24T06:37:40.612688Z","iopub.status.idle":"2021-10-24T06:37:40.635587Z","shell.execute_reply.started":"2021-10-24T06:37:40.612653Z","shell.execute_reply":"2021-10-24T06:37:40.634419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10, min_delta=0.00001) \nmc = ModelCheckpoint('best_model.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:37:46.182482Z","iopub.execute_input":"2021-10-24T06:37:46.182832Z","iopub.status.idle":"2021-10-24T06:37:46.188122Z","shell.execute_reply.started":"2021-10-24T06:37:46.182801Z","shell.execute_reply":"2021-10-24T06:37:46.18703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x_tr, y_tr ,epochs=100, callbacks=[es,mc], batch_size=32, validation_data=(x_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:37:48.698978Z","iopub.execute_input":"2021-10-24T06:37:48.6993Z","iopub.status.idle":"2021-10-24T06:38:56.579777Z","shell.execute_reply.started":"2021-10-24T06:37:48.699269Z","shell.execute_reply":"2021-10-24T06:38:56.578663Z"},"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":"2021-10-24T06:39:20.733105Z","iopub.execute_input":"2021-10-24T06:39:20.733455Z","iopub.status.idle":"2021-10-24T06:39:20.890294Z","shell.execute_reply.started":"2021-10-24T06:39:20.733404Z","shell.execute_reply":"2021-10-24T06:39:20.889265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(audio):\n    prob=model.predict(audio.reshape(1,8000,1))\n    index=np.argmax(prob[0])\n    return classes[index]","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:39:27.880179Z","iopub.execute_input":"2021-10-24T06:39:27.880519Z","iopub.status.idle":"2021-10-24T06:39:27.885754Z","shell.execute_reply.started":"2021-10-24T06:39:27.880487Z","shell.execute_reply":"2021-10-24T06:39:27.884601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nindex=random.randint(0,len(x_val)-1)\nsamples=x_val[index].ravel()\nprint(\"Audio:\",classes[np.argmax(y_val[index])])\nipd.Audio(samples, rate=8000)\nprint(\"Text:\",predict(samples))","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:39:31.137852Z","iopub.execute_input":"2021-10-24T06:39:31.138269Z","iopub.status.idle":"2021-10-24T06:39:31.317207Z","shell.execute_reply.started":"2021-10-24T06:39:31.138222Z","shell.execute_reply":"2021-10-24T06:39:31.316282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_audio = './train/audio/yes/e4b02540_nohash_0.wav'\nsamples_test, sample_rate_test = librosa.load(test_audio, sr = 16000)\nsamples_test = librosa.resample(samples_test, sample_rate_test, 8000)\n\npredict(samples_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:39:34.115848Z","iopub.execute_input":"2021-10-24T06:39:34.116176Z","iopub.status.idle":"2021-10-24T06:39:34.179821Z","shell.execute_reply.started":"2021-10-24T06:39:34.116144Z","shell.execute_reply":"2021-10-24T06:39:34.178753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(samples_test, rate=8000)","metadata":{"execution":{"iopub.status.busy":"2021-10-24T06:39:44.796616Z","iopub.execute_input":"2021-10-24T06:39:44.796959Z","iopub.status.idle":"2021-10-24T06:39:44.804778Z","shell.execute_reply.started":"2021-10-24T06:39:44.79693Z","shell.execute_reply":"2021-10-24T06:39:44.803793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples_test.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# recorder.save('test')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}