{"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":"# **Music_genre_classificaion_Competition------> Take some audio data and Apply Signal Process Perspective**\n\n<img src='https://i.pinimg.com/originals/92/a6/95/92a6952be8747f7896441369dd17b5f4.jpg'>\n\n\n## ***I'm beginner in audio data! So I'm trying basics (Valerio Velardro-Sound of AI)***\n\n\n**Audio_Signal_Processing using ML Series:[https://www.youtube.com/playlist?list=PL-wATfeyAMNqIee7cH3q1bh4QJFAaeNv0](http://)**\n\n\n### **Learn and Apply this data(Audio_data file taken)**\n\n## **STEPS --> Learn/Day**\n\n### 1. Import Library, Load the Data\n### 2. Visualize Input\n### 3. Amplitude Enevlops\n### 4. RMS\n### 5. Zero Crossing Rate\n### 6. Fourier Transform-Basics\n### 7. Magnitude Spectrum\n### 8. Spectrogram\n### 9. Mel_Spectrogram\n### 10. MFCC\n### 11. Band Energy Ratio\n### 12. Spectral Centroid and Bandwidth\n### 13. Augmentaion(Apply various type)\n### 14. SpectrogramGenerator ","metadata":{}},{"cell_type":"markdown","source":"# **1. Import Necessary Library**","metadata":{}},{"cell_type":"code","source":"import math\nimport numpy as np\nimport pandas as pd\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\nimport IPython.display as ipd\nimport scipy as sp\nimport soundfile as sf\nimport random\nimport glob\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel,delayed","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-04-03T13:56:47.349837Z","iopub.execute_input":"2022-04-03T13:56:47.350233Z","iopub.status.idle":"2022-04-03T13:56:49.392887Z","shell.execute_reply.started":"2022-04-03T13:56:47.350149Z","shell.execute_reply":"2022-04-03T13:56:49.391801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input/kaggle-pog-series-s01e02","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:49.397615Z","iopub.execute_input":"2022-04-03T13:56:49.3993Z","iopub.status.idle":"2022-04-03T13:56:50.096471Z","shell.execute_reply.started":"2022-04-03T13:56:49.399252Z","shell.execute_reply":"2022-04-03T13:56:50.095694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1 = '../input/kaggle-pog-series-s01e02/train/000000.ogg'\nf2 = '../input/kaggle-pog-series-s01e02/train/000001.ogg'\nf3 = '../input/kaggle-pog-series-s01e02/train/000002.ogg'","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:50.09797Z","iopub.execute_input":"2022-04-03T13:56:50.098262Z","iopub.status.idle":"2022-04-03T13:56:50.103154Z","shell.execute_reply.started":"2022-04-03T13:56:50.098225Z","shell.execute_reply":"2022-04-03T13:56:50.101966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Audio input**","metadata":{}},{"cell_type":"code","source":"ipd.Audio(f1)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:50.10533Z","iopub.execute_input":"2022-04-03T13:56:50.105748Z","iopub.status.idle":"2022-04-03T13:56:50.139272Z","shell.execute_reply.started":"2022-04-03T13:56:50.105713Z","shell.execute_reply":"2022-04-03T13:56:50.13868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(f2)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:50.140033Z","iopub.execute_input":"2022-04-03T13:56:50.140282Z","iopub.status.idle":"2022-04-03T13:56:50.17992Z","shell.execute_reply.started":"2022-04-03T13:56:50.140248Z","shell.execute_reply":"2022-04-03T13:56:50.179174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(f3)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:50.181068Z","iopub.execute_input":"2022-04-03T13:56:50.181647Z","iopub.status.idle":"2022-04-03T13:56:50.22179Z","shell.execute_reply.started":"2022-04-03T13:56:50.181613Z","shell.execute_reply":"2022-04-03T13:56:50.221053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **2. Load the data**","metadata":{}},{"cell_type":"code","source":"#load\nf1,sr = librosa.load(f1)\nf2,_ = librosa.load(f2)\nf3,_ = librosa.load(f3)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:50.223052Z","iopub.execute_input":"2022-04-03T13:56:50.223314Z","iopub.status.idle":"2022-04-03T13:56:54.531787Z","shell.execute_reply.started":"2022-04-03T13:56:50.22328Z","shell.execute_reply":"2022-04-03T13:56:54.530976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample duration\nsample_duration = 1/sr\nprint(f\"Duration of 1 sample is:{sample_duration: .6f} seconds\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:54.535009Z","iopub.execute_input":"2022-04-03T13:56:54.535315Z","iopub.status.idle":"2022-04-03T13:56:54.540654Z","shell.execute_reply.started":"2022-04-03T13:56:54.535284Z","shell.execute_reply":"2022-04-03T13:56:54.53995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# duration of audio signal\nduration = sample_duration * len(f1)\nprint(f\"Duration of 1 sample is:{duration: .6f} seconds\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:54.542023Z","iopub.execute_input":"2022-04-03T13:56:54.542905Z","iopub.status.idle":"2022-04-03T13:56:54.551665Z","shell.execute_reply.started":"2022-04-03T13:56:54.542866Z","shell.execute_reply":"2022-04-03T13:56:54.550778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualize the Input data**","metadata":{}},{"cell_type":"code","source":"#Visualize\nplt.figure(figsize=(15,17))\n\nplt.subplot(3,1,1)\nlibrosa.display.waveshow(f1,alpha=0.5)\nplt.ylim((-1,1))\nplt.title('first_file')\n\nplt.subplot(3,1,2)\nlibrosa.display.waveshow(f2,alpha=0.5)\nplt.title('second_file')\nplt.ylim((-1,1))\n\nplt.subplot(3,1,3)\nlibrosa.display.waveshow(f3,alpha=0.5)\nplt.title('third_file')\nplt.ylim((-1,1))\n\nplt.tight_layout()\n\nplt.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:54.55547Z","iopub.execute_input":"2022-04-03T13:56:54.556071Z","iopub.status.idle":"2022-04-03T13:56:56.728032Z","shell.execute_reply.started":"2022-04-03T13:56:54.556033Z","shell.execute_reply":"2022-04-03T13:56:56.727415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **3. Amplitude Enevlops**\n\n## **Build Amplitude envelops function and apply**\n\n**Amplitude Envelops:** Amplitude envelope refers to the changes in the amplitude of a sound over time, and is an influential property as it affects our perception of timbre. This is an important property of sound, because it is what allows us to effortlessly identify sounds, and uniquely distinguish them from other sounds\n\n[https://maplelab.net/overview/amplitude-envelope/](http://)","metadata":{}},{"cell_type":"code","source":"FRAME_SIZE = 1024\nHOP_LENGTH = 512\n\ndef amplitude_envelope(signal, frame_size, hop_length):\n    \"\"\"Calculate the amplitude envelope of a signal with a given frame size nad hop length.\"\"\"\n    amplitude_envelope = []\n    \n    # calculate amplitude envelope for each frame\n    for i in range(0, len(signal), hop_length): \n        amplitude_envelope_current_frame = max(signal[i:i+frame_size]) \n        amplitude_envelope.append(amplitude_envelope_current_frame)\n    \n    return np.array(amplitude_envelope)    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:56.729367Z","iopub.execute_input":"2022-04-03T13:56:56.729809Z","iopub.status.idle":"2022-04-03T13:56:56.736197Z","shell.execute_reply.started":"2022-04-03T13:56:56.729773Z","shell.execute_reply":"2022-04-03T13:56:56.735485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fancy_amplitude_envelope(signal, frame_size, hop_length):\n    \"\"\"Fancier Python code to calculate the amplitude envelope of a signal with a given frame size.\"\"\"\n    return np.array([max(signal[i:i+frame_size]) for i in range(0, len(signal), hop_length)])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:56.737433Z","iopub.execute_input":"2022-04-03T13:56:56.737889Z","iopub.status.idle":"2022-04-03T13:56:56.747088Z","shell.execute_reply.started":"2022-04-03T13:56:56.737856Z","shell.execute_reply":"2022-04-03T13:56:56.746198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of frames in amplitude envelope\nae_f1 = amplitude_envelope(f1, FRAME_SIZE, HOP_LENGTH)\nlen(ae_f1)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:56.74876Z","iopub.execute_input":"2022-04-03T13:56:56.749338Z","iopub.status.idle":"2022-04-03T13:56:56.923661Z","shell.execute_reply.started":"2022-04-03T13:56:56.7493Z","shell.execute_reply":"2022-04-03T13:56:56.922952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ae_f2 = amplitude_envelope(f2, FRAME_SIZE, HOP_LENGTH)\nae_f3 = amplitude_envelope(f3, FRAME_SIZE, HOP_LENGTH)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:56.924901Z","iopub.execute_input":"2022-04-03T13:56:56.925174Z","iopub.status.idle":"2022-04-03T13:56:57.254144Z","shell.execute_reply.started":"2022-04-03T13:56:56.925126Z","shell.execute_reply":"2022-04-03T13:56:57.253517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualize_AE**","metadata":{}},{"cell_type":"code","source":"frames = range(len(ae_f1))\nt = librosa.frames_to_time(frames, hop_length=HOP_LENGTH)\nprint(f'frames:{frames},time:{t[1]}')","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:57.255293Z","iopub.execute_input":"2022-04-03T13:56:57.255515Z","iopub.status.idle":"2022-04-03T13:56:57.260155Z","shell.execute_reply.started":"2022-04-03T13:56:57.255482Z","shell.execute_reply":"2022-04-03T13:56:57.259185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames2 = range(len(ae_f2))\nt2 = librosa.frames_to_time(frames2, hop_length=HOP_LENGTH)\nframes3= range(len(ae_f3))\nt3 = librosa.frames_to_time(frames3, hop_length=HOP_LENGTH)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:57.261387Z","iopub.execute_input":"2022-04-03T13:56:57.261808Z","iopub.status.idle":"2022-04-03T13:56:57.270901Z","shell.execute_reply.started":"2022-04-03T13:56:57.261751Z","shell.execute_reply":"2022-04-03T13:56:57.270166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# amplitude envelope is graphed in red\n\nplt.figure(figsize=(15, 17))\n\nax = plt.subplot(3, 1, 1)\nlibrosa.display.waveshow(f1, alpha=0.5)\nplt.plot(t, ae_f1, color=\"r\")\nplt.ylim((-1, 1))\nplt.title(\"first_file_AE\")\n\nplt.subplot(3, 1, 2)\nlibrosa.display.waveshow(f2, alpha=0.5)\nplt.plot(t2, ae_f2, color=\"r\")\nplt.ylim((-1, 1))\nplt.title(\"second_file_AE\")\n\nplt.subplot(3, 1, 3)\nlibrosa.display.waveshow(f3, alpha=0.5)\nplt.plot(t3, ae_f3, color=\"r\")\nplt.ylim((-1, 1))\nplt.title(\"Third_file_AE\")\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:57.272388Z","iopub.execute_input":"2022-04-03T13:56:57.272679Z","iopub.status.idle":"2022-04-03T13:56:59.425525Z","shell.execute_reply.started":"2022-04-03T13:56:57.27264Z","shell.execute_reply":"2022-04-03T13:56:59.424924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **4. RMS**\n\nThe square root of the mean of the square. RMS is (to engineers anyway) a meaningful way of calculating the average of values over a period of time. With audio, the signal value (amplitude) is squared, averaged over a period of time, then the square root of the result is calculated. The result is a value, that when squared, is related (proportional) to the effective power of the signal. ","metadata":{}},{"cell_type":"code","source":"#Extract RMSE with Librosa\n\nFRAME_LENGTH = 1024\nHOP_LENGTH = 512\nrms_f1 = librosa.feature.rms(f1, FRAME_SIZE, HOP_LENGTH)[0]\nrms_f2 = librosa.feature.rms(f2, FRAME_SIZE, HOP_LENGTH)[0]\nrms_f3 = librosa.feature.rms(f3, FRAME_SIZE, HOP_LENGTH)[0]\n\nprint(rms_f1.shape,rms_f2.shape,rms_f3.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:56:59.426649Z","iopub.execute_input":"2022-04-03T13:56:59.426992Z","iopub.status.idle":"2022-04-03T13:56:59.440899Z","shell.execute_reply.started":"2022-04-03T13:56:59.426954Z","shell.execute_reply":"2022-04-03T13:56:59.440189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Visualize RMS**","metadata":{}},{"cell_type":"code","source":"# RMS is graphed in red\n\nplt.figure(figsize=(15, 17))\n\nax = plt.subplot(3, 1, 1)\nlibrosa.display.waveshow(f1, alpha=0.5)\nplt.plot(t,rms_f1, color=\"r\")\nplt.ylim((-1, 1))\nplt.title(\"first_file_RMS\")\n\nplt.subplot(3, 1, 2)\nlibrosa.display.waveshow(f2, alpha=0.5)\nplt.plot(t2,rms_f2, color=\"r\")\nplt.ylim((-1, 1))\nplt.title(\"second_file_RMS\")\n\nplt.subplot(3, 1, 3)\nlibrosa.display.waveshow(f3, alpha=0.5)\nplt.plot(t3, rms_f3, color=\"r\")\nplt.ylim((-1, 1))\nplt.title(\"Third_file_RMS\")\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:56:59.442507Z","iopub.execute_input":"2022-04-03T13:56:59.44328Z","iopub.status.idle":"2022-04-03T13:57:01.448024Z","shell.execute_reply.started":"2022-04-03T13:56:59.443243Z","shell.execute_reply":"2022-04-03T13:57:01.447443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Create own scratch RMS_scratch vs librosa**","metadata":{}},{"cell_type":"code","source":"def rms(signal,frame_length,hop_length):\n    res = []\n    \n    for i in range(0,len(signal),hop_length):\n        rms_current_frame = np.sqrt(np.sum(signal[i:i+frame_length]**2)/frame_length)\n        res.append(rms_current_frame)\n        \n    return np.array(res)","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-04-03T13:57:01.449193Z","iopub.execute_input":"2022-04-03T13:57:01.449616Z","iopub.status.idle":"2022-04-03T13:57:01.45568Z","shell.execute_reply.started":"2022-04-03T13:57:01.449578Z","shell.execute_reply":"2022-04-03T13:57:01.455036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rms1_f1 = rms(f1,frame_length=FRAME_SIZE, hop_length=HOP_LENGTH)\nrms1_f2 = rms(f2,frame_length=FRAME_SIZE,hop_length=HOP_LENGTH)\nrms1_f3 = rms(f3,frame_length=FRAME_SIZE,hop_length=HOP_LENGTH)\nprint(rms1_f1.shape,rms1_f2.shape,rms1_f3.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:01.456794Z","iopub.execute_input":"2022-04-03T13:57:01.457624Z","iopub.status.idle":"2022-04-03T13:57:01.533105Z","shell.execute_reply.started":"2022-04-03T13:57:01.457589Z","shell.execute_reply":"2022-04-03T13:57:01.532449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RMS is graphed in red and green differentiated own function\n\nplt.figure(figsize=(15, 17))\n\nax = plt.subplot(3, 1, 1)\nlibrosa.display.waveshow(f1, alpha=0.5)\nplt.plot(t,rms_f1, color=\"r\")\nplt.plot(t,rms1_f1, color=\"g\")\nplt.ylim((-1, 1))\nplt.title(\"first_file_RMS v Librosa_func\")\n\nplt.subplot(3, 1, 2)\nlibrosa.display.waveshow(f2, alpha=0.5)\nplt.plot(t2,rms_f2, color=\"r\")\nplt.plot(t2,rms1_f2, color=\"g\")\nplt.ylim((-1, 1))\nplt.title(\"second_file_RMS v Librosa_func\")\n\nplt.subplot(3, 1, 3)\nlibrosa.display.waveshow(f3, alpha=0.5)\nplt.plot(t3, rms_f3, color=\"r\")\nplt.plot(t3, rms1_f3, color=\"g\")\nplt.ylim((-1, 1))\nplt.title(\"Third_file_RMS v Librosa_func\")\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:01.534473Z","iopub.execute_input":"2022-04-03T13:57:01.534731Z","iopub.status.idle":"2022-04-03T13:57:03.789752Z","shell.execute_reply.started":"2022-04-03T13:57:01.534697Z","shell.execute_reply":"2022-04-03T13:57:03.78918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **5. Zero_Crossing_Rate**\n\nThe Zero-Crossing Rate (ZCR) of an audio frame is the rate of sign-changes of the signal during the frame. In other words, it is the number of times the signal changes value, from positive to negative and vice versa, divided by the length of the frame","metadata":{}},{"cell_type":"code","source":"zcr_f1 = librosa.feature.zero_crossing_rate(f1, FRAME_SIZE, HOP_LENGTH)[0]\nzcr_f2 = librosa.feature.zero_crossing_rate(f2, FRAME_SIZE, HOP_LENGTH)[0]\nzcr_f3 = librosa.feature.zero_crossing_rate(f3, FRAME_SIZE, HOP_LENGTH)[0]\n\nplt.figure(figsize=(15, 17))\nplt.plot(t,zcr_f1*FRAME_LENGTH, color=\"r\")\nplt.plot(t2,zcr_f2*FRAME_LENGTH, color=\"g\")\nplt.plot(t3,zcr_f3*FRAME_LENGTH, color=\"b\")\nplt.ylim((0, 400))\nplt.title(\"Zero_Crossing_Rate | Three_Audio(f1,f2,f3)\")","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:03.790731Z","iopub.execute_input":"2022-04-03T13:57:03.791055Z","iopub.status.idle":"2022-04-03T13:57:04.215595Z","shell.execute_reply.started":"2022-04-03T13:57:03.791024Z","shell.execute_reply":"2022-04-03T13:57:04.214986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **6. Fourier_Transform_All about basic understanding**\n\n**Fourier_Transform:** The Fourier Transform is an important image processing tool which is used to decompose an image into its sine and cosine components. The output of the transformation represents the image in the Fourier or frequency domain, while the input image is the spatial domain equivalent","metadata":{}},{"cell_type":"code","source":"# load audio file in the player\nf4 = \"../input/kaggle-pog-series-s01e02/train/000004.ogg\"\nipd.Audio(f4)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:04.216721Z","iopub.execute_input":"2022-04-03T13:57:04.217165Z","iopub.status.idle":"2022-04-03T13:57:04.238658Z","shell.execute_reply.started":"2022-04-03T13:57:04.217108Z","shell.execute_reply":"2022-04-03T13:57:04.238078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load audio file\nsignal, sr = librosa.load(f4)\n# plot waveform\nplt.figure(figsize=(15, 17))\nlibrosa.display.waveshow(signal, sr=sr, alpha=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:04.239752Z","iopub.execute_input":"2022-04-03T13:57:04.24023Z","iopub.status.idle":"2022-04-03T13:57:06.48662Z","shell.execute_reply.started":"2022-04-03T13:57:04.240194Z","shell.execute_reply":"2022-04-03T13:57:06.485963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# derive spectrum using FT\nft = sp.fft.fft(signal)\nmagnitude = np.absolute(ft)\nfrequency = np.linspace(0, sr, len(magnitude)) ","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:06.487977Z","iopub.execute_input":"2022-04-03T13:57:06.488397Z","iopub.status.idle":"2022-04-03T13:57:06.521293Z","shell.execute_reply.started":"2022-04-03T13:57:06.488361Z","shell.execute_reply":"2022-04-03T13:57:06.52063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot spectrum\nplt.figure(figsize=(18, 8))\nplt.plot(frequency[:5000], magnitude[:5000]) # magnitude spectrum\nplt.xlabel(\"Frequency (Hz)\")\nplt.ylabel(\"Magnitude\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:06.522582Z","iopub.execute_input":"2022-04-03T13:57:06.522839Z","iopub.status.idle":"2022-04-03T13:57:06.741651Z","shell.execute_reply.started":"2022-04-03T13:57:06.522803Z","shell.execute_reply":"2022-04-03T13:57:06.740976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('length of signal:',len(signal))\nd =  1 / sr\nprint('d:',d)\nd_523 = 1 / 523\nprint('d_523:',d_523)\nd_400_samples = 400 * d\nprint('d_400:',d_400_samples)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:06.747156Z","iopub.execute_input":"2022-04-03T13:57:06.747714Z","iopub.status.idle":"2022-04-03T13:57:06.755764Z","shell.execute_reply.started":"2022-04-03T13:57:06.747684Z","shell.execute_reply":"2022-04-03T13:57:06.755074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# zomm in to the waveform\nsamples = range(len(signal))\nt = librosa.samples_to_time(samples, sr=sr)\n\nplt.figure(figsize=(18, 8))\nplt.plot(t[10000:10400], signal[10000:10400]) \nplt.xlabel(\"Time (s)\")\nplt.ylabel(\"Amplitude\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:06.757304Z","iopub.execute_input":"2022-04-03T13:57:06.757618Z","iopub.status.idle":"2022-04-03T13:57:07.061619Z","shell.execute_reply.started":"2022-04-03T13:57:06.757583Z","shell.execute_reply":"2022-04-03T13:57:07.060968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Basic understanding of fourier transform**","metadata":{}},{"cell_type":"code","source":"# create a sinusoid\n\nf = 523\nphase = 0\nphase2 = 0.2\n\nsin = 0.5 * np.sin(2*np.pi * (f * t - phase))\nsin2 = 0.5 * np.sin(2*np.pi * (f * t - phase2))\n\nplt.figure(figsize=(18, 8))\nplt.plot(t[10000:10400], sin[10000:10400], color=\"r\")\nplt.plot(t[10000:10400], sin2[10000:10400], color=\"y\")\n\n\nplt.xlabel(\"Time (s)\")\nplt.ylabel(\"Amplitude\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:07.062838Z","iopub.execute_input":"2022-04-03T13:57:07.063249Z","iopub.status.idle":"2022-04-03T13:57:07.296653Z","shell.execute_reply.started":"2022-04-03T13:57:07.063209Z","shell.execute_reply":"2022-04-03T13:57:07.295989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compare signal and sinusoids\n\nf = 523\nphase = 0.55\n\nsin = 0.1 * np.sin(2*np.pi * (f * t - phase))\n\nplt.figure(figsize=(18, 8))\nplt.plot(t[10000:10400], signal[10000:10400]) \nplt.plot(t[10000:10400], sin[10000:10400], color=\"r\")\n\nplt.fill_between(t[10000:10400], sin[10000:10400]*signal[10000:10400], color=\"y\")\n\nplt.xlabel(\"Time (s)\")\nplt.ylabel(\"Amplitude\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:07.297844Z","iopub.execute_input":"2022-04-03T13:57:07.298353Z","iopub.status.idle":"2022-04-03T13:57:07.524688Z","shell.execute_reply.started":"2022-04-03T13:57:07.298314Z","shell.execute_reply":"2022-04-03T13:57:07.524039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot spectrum\nplt.figure(figsize=(18, 8))\nplt.plot(frequency[:5000], magnitude[:5000]) # magnitude spectrum\nplt.xlabel(\"Frequency (Hz)\")\nplt.ylabel(\"Magnitude\")\nplt.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:07.525718Z","iopub.execute_input":"2022-04-03T13:57:07.526082Z","iopub.status.idle":"2022-04-03T13:57:07.738591Z","shell.execute_reply.started":"2022-04-03T13:57:07.526039Z","shell.execute_reply":"2022-04-03T13:57:07.737969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# superimposing pure tones\nf = 1\nt = np.linspace(0, 10, 10000)\n\nsin = np.sin(2*np.pi * (f * t))\nsin2 = np.sin(2*np.pi * (2*f * t))\nsin3 = np.sin(2*np.pi * (3*f * t))\n\nsum_signal = sin + sin2 + sin3\n\nplt.figure(figsize=(15, 10))\n\nplt.subplot(4, 1, 1)\nplt.plot(t, sum_signal, color=\"r\")\n\nplt.subplot(4, 1, 2)\nplt.plot(t, sin)\n\nplt.subplot(4, 1, 3)\nplt.plot(t, sin2)\n\nplt.subplot(4, 1, 4)\nplt.plot(t, sin3)\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:07.739828Z","iopub.execute_input":"2022-04-03T13:57:07.741494Z","iopub.status.idle":"2022-04-03T13:57:08.16761Z","shell.execute_reply.started":"2022-04-03T13:57:07.741453Z","shell.execute_reply":"2022-04-03T13:57:08.166993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ***7. Magnitude Spectrum***\n\nThe Fourier transform can be used to construct the power spectrum of a signal by taking the square of the magnitude spectrum. The power spectral curve shows signal power as a function of frequency. The power spectrum is particularly useful for noisy or random data where phase characteristics have little meaning","metadata":{}},{"cell_type":"code","source":"# load audio file in the player\nf5 = \"../input/kaggle-pog-series-s01e02/train/000005.ogg\"\nipd.Audio(f5)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:08.168779Z","iopub.execute_input":"2022-04-03T13:57:08.169555Z","iopub.status.idle":"2022-04-03T13:57:08.192579Z","shell.execute_reply.started":"2022-04-03T13:57:08.169519Z","shell.execute_reply":"2022-04-03T13:57:08.19194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load audio file\nsignal_f5, sr = librosa.load(f5)\nprint('Length of f5_Signal:', len(signal_f5))\n#FFT\nX = np.fft.fft(signal_f5)\nprint('Length of fft_X:',len(X))\n\n# Plot the Magnitude_Spectrum\ndef plot_magnitude_spectrum(signal, sr, title, f_ratio=1):\n    X = np.fft.fft(signal)\n    X_mag = np.absolute(X)\n    \n    plt.figure(figsize=(18, 5))\n    \n    f = np.linspace(0, sr, len(X_mag))\n    f_bins = int(len(X_mag)*f_ratio)  \n    \n    plt.plot(f[:f_bins], X_mag[:f_bins])\n    plt.xlabel('Frequency (Hz)')\n    plt.title(title)","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-04-03T13:57:08.193646Z","iopub.execute_input":"2022-04-03T13:57:08.194352Z","iopub.status.idle":"2022-04-03T13:57:09.631559Z","shell.execute_reply.started":"2022-04-03T13:57:08.194316Z","shell.execute_reply":"2022-04-03T13:57:09.629221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Visualize the F5_Audio file**","metadata":{}},{"cell_type":"code","source":"plot_magnitude_spectrum(signal_f5, sr, \"F5_AudioFile\", 2.9)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:09.635352Z","iopub.execute_input":"2022-04-03T13:57:09.637412Z","iopub.status.idle":"2022-04-03T13:57:10.164433Z","shell.execute_reply.started":"2022-04-03T13:57:09.637373Z","shell.execute_reply":"2022-04-03T13:57:10.1638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **8. Spectrogram**\n\n**Spectrogram:** A spectrogram is a visual way of representing the signal strength, or “loudness”, of a signal over time at various frequencies present in a particular waveform. Not only can one see whether there is more or less energy at, for example, 2 Hz vs 10 Hz, but one can also see how energy levels vary over time\n\n**STFT:** The Short-time Fourier transform (STFT), is a Fourier-related transform used to determine the sinusoidal frequency and phase content of local sections of a signal as it changes over time.\n\n**STFT VS Spectrogram:** stft focuses on the FT of windowed and segmented (overlaped) data and the output can be used to reconstruct the original (under certain condition). spectrogram focuses on the spectral estimation based on STFT. It has the options for power spectrum or power spectrum density.","metadata":{}},{"cell_type":"code","source":"#Extracting Short-Time Fourier Transform\nFRAME_SIZE = 2048\nHOP_SIZE = 512\n#Apply stft\nf1_scale = librosa.stft(f1, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)\n#Shape and Type\nprint('Shape of the f1_scale:',f1_scale.shape)\nprint('Type of f1_scale:', type(f1_scale[0][0]))\nprint('')\nprint('*****************************************')\n#Calculating the Spectrogram\nyf1_scale = np.abs(f1_scale)**2\nprint('Shape of yf1_scale:',yf1_scale.shape)\nprint('Type of yf1_scale:',type(yf1_scale[0][0]))","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:10.16803Z","iopub.execute_input":"2022-04-03T13:57:10.168624Z","iopub.status.idle":"2022-04-03T13:57:10.21804Z","shell.execute_reply.started":"2022-04-03T13:57:10.168587Z","shell.execute_reply":"2022-04-03T13:57:10.217391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Visualizing (Spectrogram)**","metadata":{}},{"cell_type":"code","source":"def plot_spectrogram(Y, sr, hop_length, y_axis=\"linear\"):\n    plt.figure(figsize=(25, 10))\n    librosa.display.specshow(Y, \n                             sr=sr, \n                             hop_length=hop_length, \n                             x_axis=\"time\", \n                             y_axis=y_axis)\n    plt.colorbar(format=\"%+2.f\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:10.219041Z","iopub.execute_input":"2022-04-03T13:57:10.219413Z","iopub.status.idle":"2022-04-03T13:57:10.229987Z","shell.execute_reply.started":"2022-04-03T13:57:10.219379Z","shell.execute_reply":"2022-04-03T13:57:10.229303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrogram(yf1_scale, sr, HOP_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:10.23169Z","iopub.execute_input":"2022-04-03T13:57:10.232153Z","iopub.status.idle":"2022-04-03T13:57:11.657785Z","shell.execute_reply.started":"2022-04-03T13:57:10.232107Z","shell.execute_reply":"2022-04-03T13:57:11.657118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **8.1. Log-Amplitude Spectrogram**","metadata":{}},{"cell_type":"code","source":"yf1_log_scale = librosa.power_to_db(yf1_scale)\nplot_spectrogram(yf1_log_scale, sr, HOP_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:11.658854Z","iopub.execute_input":"2022-04-03T13:57:11.659281Z","iopub.status.idle":"2022-04-03T13:57:13.334914Z","shell.execute_reply.started":"2022-04-03T13:57:11.659242Z","shell.execute_reply":"2022-04-03T13:57:13.334008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **8.1.2 Log-Frequency Spectrogram**","metadata":{}},{"cell_type":"code","source":"plot_spectrogram(yf1_log_scale, sr, HOP_SIZE, y_axis=\"log\")","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:13.336514Z","iopub.execute_input":"2022-04-03T13:57:13.336762Z","iopub.status.idle":"2022-04-03T13:57:15.084649Z","shell.execute_reply.started":"2022-04-03T13:57:13.33673Z","shell.execute_reply":"2022-04-03T13:57:15.083526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#stft\nS_f2 = librosa.stft(f2, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)\nS_f3 = librosa.stft(f3, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)\n\n#power to db(log-amplitude)\nY_f2 = librosa.power_to_db(np.abs(S_f2) ** 2)\nY_f3 = librosa.power_to_db(np.abs(S_f3) ** 2)\n# log-frequency\nplot_spectrogram(Y_f2, sr, HOP_SIZE, y_axis=\"log\")\nplot_spectrogram(Y_f3, sr, HOP_SIZE, y_axis=\"log\")\n\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:15.085932Z","iopub.execute_input":"2022-04-03T13:57:15.086355Z","iopub.status.idle":"2022-04-03T13:57:18.539291Z","shell.execute_reply.started":"2022-04-03T13:57:15.08632Z","shell.execute_reply":"2022-04-03T13:57:18.538649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **9. Mel Spectrogram**\n\nThe Mel spectrogram is used to provide our models with sound information similar to what a human would perceive. The raw audio waveforms are passed through filter banks to obtain the Mel spectrogram.\n\n**STEPS:**\n\n1. Choose number of mel bands\n2. Construct mel filter banks\n    1. Convert lowest/highest frequency to Mel\n    2. Create bands equally space points\n    3. Convert points back to Hertz\n    4. Round to nearest frequency bin\n    5. Create triangular filters\n3. Apply mel filter banks to spectrogram","metadata":{}},{"cell_type":"code","source":"# load audio file in the player\nf5 = \"../input/kaggle-pog-series-s01e02/train/000010.ogg\"\n# load audio files with librosa\nscale, sr = librosa.load(f5)\n#display\nipd.Audio(f5)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:18.540446Z","iopub.execute_input":"2022-04-03T13:57:18.540803Z","iopub.status.idle":"2022-04-03T13:57:19.698074Z","shell.execute_reply.started":"2022-04-03T13:57:18.540771Z","shell.execute_reply":"2022-04-03T13:57:19.697435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **9.1. Mel filter banks**\n\nMel Filter Banks is a triangular filter bank that works similar to the human ears perception of sound which is more discriminative at lower frequencies and less discriminative at higher frequencies. Mel Filter Banks are used to provide a better resolution at low frequencies and less resolution at high frequencies.\n\n<img src='https://siggigue.github.io/pyfilterbank//melbank-1_00.png'>\n\n","metadata":{}},{"cell_type":"code","source":"filter_banks = librosa.filters.mel(n_fft=2048, sr=22050, n_mels=10)\nfilter_banks.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:19.699122Z","iopub.execute_input":"2022-04-03T13:57:19.700287Z","iopub.status.idle":"2022-04-03T13:57:19.70765Z","shell.execute_reply.started":"2022-04-03T13:57:19.700247Z","shell.execute_reply":"2022-04-03T13:57:19.70697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Visualization Mel_Filter_Banks**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nlibrosa.display.specshow(filter_banks, \n                         sr=sr, \n                         x_axis=\"linear\")\nplt.colorbar(format=\"%+2.f\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:19.70879Z","iopub.execute_input":"2022-04-03T13:57:19.709606Z","iopub.status.idle":"2022-04-03T13:57:19.947164Z","shell.execute_reply.started":"2022-04-03T13:57:19.70957Z","shell.execute_reply":"2022-04-03T13:57:19.946514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **9.1.2. Extracting Mel Spectrogram**","metadata":{}},{"cell_type":"code","source":"#Apply mel spectrogram function using librosa\nmel_spectrogram = librosa.feature.melspectrogram(scale, sr=sr, n_fft=2048, hop_length=512, n_mels=10)\nprint('Mel_Spectrogram:',mel_spectrogram.shape)\n\n# Log_mel_spectrogram--> clear and understanding visualize the low frequency\nlog_mel_spectrogram = librosa.power_to_db(mel_spectrogram)\nprint('Log_Mel_Spectrogram:',log_mel_spectrogram.shape)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:19.948419Z","iopub.execute_input":"2022-04-03T13:57:19.948703Z","iopub.status.idle":"2022-04-03T13:57:19.989104Z","shell.execute_reply.started":"2022-04-03T13:57:19.948667Z","shell.execute_reply":"2022-04-03T13:57:19.988377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Visualize the Log_Mel_Spectrogram**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nlibrosa.display.specshow(log_mel_spectrogram, \n                         x_axis=\"time\",\n                         y_axis=\"mel\", \n                         sr=sr)\nplt.colorbar(format=\"%+2.f\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:19.993247Z","iopub.execute_input":"2022-04-03T13:57:19.993685Z","iopub.status.idle":"2022-04-03T13:57:20.315341Z","shell.execute_reply.started":"2022-04-03T13:57:19.99364Z","shell.execute_reply":"2022-04-03T13:57:20.3147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **10. MFCC**\n\nMel-frequency cepstral coefficients (MFCCs) are coefficients that collectively make up an MFC.They are derived from a type of cepstral representation of the audio clip (a nonlinear \"spectrum-of-a-spectrum\"). The difference between the cepstrum and the mel-frequency cepstrum is that in the MFC, the frequency bands are equally spaced on the mel scale, which approximates the human auditory system's response more closely than the linearly-spaced frequency bands used in the normal spectrum. This frequency warping can allow for better representation of sound, for example, in audio compression.\n\nMFCCs are commonly derived as follows:\n1. Take the Fourier transform of (a windowed excerpt of) a signal.\n2. Map the powers of the spectrum obtained above onto the mel scale, using triangular overlapping windows or alternatively, cosine          overlapping windows.\n3. Take the logs of the powers at each of the mel frequencies.\n4. Take the discrete cosine transform of the list of mel log powers, as if it were a signal.\n5. The MFCCs are the amplitudes of the resulting spectrum.\n\n<img src='https://www.researchgate.net/profile/Mustafa-Bingol/publication/329609829/figure/fig1/AS:703259400495105@1544681515106/The-diagram-of-MFCC-algorithm-Firstly-speech-data-is-emphasized-then-emphasized-data-is.png'>","metadata":{}},{"cell_type":"code","source":"# Extract MFCCs\nmfccs = librosa.feature.mfcc(y=scale, n_mfcc=13, sr=sr)\nprint('Shape of MFCCs:',mfccs.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:20.316364Z","iopub.execute_input":"2022-04-03T13:57:20.316751Z","iopub.status.idle":"2022-04-03T13:57:20.359172Z","shell.execute_reply.started":"2022-04-03T13:57:20.316709Z","shell.execute_reply":"2022-04-03T13:57:20.358439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualize MFCC\nplt.figure(figsize=(15, 17))\nlibrosa.display.specshow(mfccs, \n                         x_axis=\"time\", \n                         sr=sr)\nplt.colorbar(format=\"%+2.f\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:20.360649Z","iopub.execute_input":"2022-04-03T13:57:20.361147Z","iopub.status.idle":"2022-04-03T13:57:20.668074Z","shell.execute_reply.started":"2022-04-03T13:57:20.361094Z","shell.execute_reply":"2022-04-03T13:57:20.667405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ***First/Second MFCC derivatives***","metadata":{}},{"cell_type":"code","source":"#First\ndelta_mfccs = librosa.feature.delta(mfccs)\n#order 2\ndelta2_mfccs = librosa.feature.delta(mfccs, order=2)\n#shape\nprint('delta_mfccs:',delta_mfccs.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:20.669416Z","iopub.execute_input":"2022-04-03T13:57:20.669649Z","iopub.status.idle":"2022-04-03T13:57:20.699575Z","shell.execute_reply.started":"2022-04-03T13:57:20.669617Z","shell.execute_reply":"2022-04-03T13:57:20.698809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualize\nplt.figure(figsize=(25, 10))\nlibrosa.display.specshow(delta_mfccs, \n                         x_axis=\"time\", \n                         sr=sr)\nplt.colorbar(format=\"%+2.f\")\nplt.title('First_order')\nplt.show()\nprint('****************************************************')\n\nplt.figure(figsize=(25, 10))\nlibrosa.display.specshow(delta2_mfccs, \n                         x_axis=\"time\", \n                         sr=sr)\nplt.colorbar(format=\"%+2.f\")\nplt.title('second_order')\nplt.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:20.700863Z","iopub.execute_input":"2022-04-03T13:57:20.701406Z","iopub.status.idle":"2022-04-03T13:57:21.237602Z","shell.execute_reply.started":"2022-04-03T13:57:20.701359Z","shell.execute_reply":"2022-04-03T13:57:21.236958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **10.1. Concat Mfccs,first,second order mfccs**","metadata":{}},{"cell_type":"code","source":"mfccs_features = np.concatenate((mfccs, delta_mfccs, delta2_mfccs))\nprint('Concat_mfccs_features_Shape:',mfccs_features.shape)\n#Visualize\nplt.figure(figsize=(15, 10))\nlibrosa.display.specshow(mfccs_features, \n                         x_axis=\"time\", \n                         sr=sr)\nplt.colorbar(format=\"%+2.f\")\nplt.title('Concat Mfccs,First,Second order MFccs features')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:21.238962Z","iopub.execute_input":"2022-04-03T13:57:21.239421Z","iopub.status.idle":"2022-04-03T13:57:21.515557Z","shell.execute_reply.started":"2022-04-03T13:57:21.239384Z","shell.execute_reply":"2022-04-03T13:57:21.514931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **11. BandEnergyRatio**","metadata":{}},{"cell_type":"code","source":"#Already data taken(f1,f2,f3)\nFRAME_SIZE = 2048\nHOP_SIZE = 512\n\nf1_spec = librosa.stft(f1, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)\nf2_spec = librosa.stft(f2, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)\nf3_spec = librosa.stft(f3, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:21.516637Z","iopub.execute_input":"2022-04-03T13:57:21.517449Z","iopub.status.idle":"2022-04-03T13:57:21.585168Z","shell.execute_reply.started":"2022-04-03T13:57:21.517407Z","shell.execute_reply":"2022-04-03T13:57:21.584499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculate Band Energy Ratio\ndef calculate_split_frequency_bin(split_frequency, sample_rate, num_frequency_bins):\n    \"\"\"Infer the frequency bin associated to a given split frequency.\"\"\"\n    \n    frequency_range = sample_rate / 2\n    frequency_delta_per_bin = frequency_range / num_frequency_bins\n    split_frequency_bin = math.floor(split_frequency / frequency_delta_per_bin)\n    return int(split_frequency_bin)\n\nsplit_frequency_bin = calculate_split_frequency_bin(2000, 22050, 1025)\nsplit_frequency_bin","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:21.586599Z","iopub.execute_input":"2022-04-03T13:57:21.586858Z","iopub.status.idle":"2022-04-03T13:57:21.595893Z","shell.execute_reply.started":"2022-04-03T13:57:21.586822Z","shell.execute_reply":"2022-04-03T13:57:21.595101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def band_energy_ratio(spectrogram, split_frequency, sample_rate):\n    \"\"\"Calculate band energy ratio with a given split frequency.\"\"\"\n    \n    split_frequency_bin = calculate_split_frequency_bin(split_frequency, sample_rate, len(spectrogram[0]))\n    band_energy_ratio = []\n    \n    # calculate power spectrogram\n    power_spectrogram = np.abs(spectrogram) ** 2\n    power_spectrogram = power_spectrogram.T\n    \n    # calculate BER value for each frame\n    for frame in power_spectrogram:\n        sum_power_low_frequencies = frame[:split_frequency_bin].sum()\n        sum_power_high_frequencies = frame[split_frequency_bin:].sum()\n        band_energy_ratio_current_frame = sum_power_low_frequencies / sum_power_high_frequencies\n        band_energy_ratio.append(band_energy_ratio_current_frame)\n    \n    return np.array(band_energy_ratio)\n\nber_f1 = band_energy_ratio(f1_spec, 2000, sr)\nber_f2 = band_energy_ratio(f2_spec, 2000, sr)\nber_f3 = band_energy_ratio(f3_spec, 2000, sr)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:21.597108Z","iopub.execute_input":"2022-04-03T13:57:21.597438Z","iopub.status.idle":"2022-04-03T13:57:21.64863Z","shell.execute_reply.started":"2022-04-03T13:57:21.597402Z","shell.execute_reply":"2022-04-03T13:57:21.647976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Visualise Band Energy Ratio**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(25, 10))\nplt.plot(t2, ber_f2, color=\"r\")\nplt.plot(t3, ber_f3, color=\"y\")\nplt.ylim((0, 20000))\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:21.649823Z","iopub.execute_input":"2022-04-03T13:57:21.650061Z","iopub.status.idle":"2022-04-03T13:57:21.857814Z","shell.execute_reply.started":"2022-04-03T13:57:21.650028Z","shell.execute_reply":"2022-04-03T13:57:21.857193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **12. Spectral Centroid and bandwidth**","metadata":{}},{"cell_type":"code","source":"FRAME_SIZE = 1024\nHOP_LENGTH = 512\n\nsc_f2 = librosa.feature.spectral_centroid(y=f2, sr=sr, n_fft=FRAME_SIZE, hop_length=HOP_LENGTH)[0]\nsc_f3 = librosa.feature.spectral_centroid(y=f3, sr=sr, n_fft=FRAME_SIZE, hop_length=HOP_LENGTH)[0]\n\n#Visualize\nplt.figure(figsize=(15,10))\n\nplt.plot(t2, sc_f2, color='r')\nplt.plot(t3, sc_f3, color='y')\n\nplt.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:21.859064Z","iopub.execute_input":"2022-04-03T13:57:21.859473Z","iopub.status.idle":"2022-04-03T13:57:22.147601Z","shell.execute_reply.started":"2022-04-03T13:57:21.859438Z","shell.execute_reply":"2022-04-03T13:57:22.146954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Spectral Bandwidth**","metadata":{}},{"cell_type":"code","source":"sbw_f2 = librosa.feature.spectral_bandwidth(y=f2, sr=sr, n_fft=FRAME_SIZE, hop_length=HOP_LENGTH)[0]\nsbw_f3 = librosa.feature.spectral_bandwidth(y=f3, sr=sr, n_fft=FRAME_SIZE, hop_length=HOP_LENGTH)[0]\n\n#Visualize\nplt.figure(figsize=(15,10))\n\nplt.plot(t2, sbw_f2, color='r')\nplt.plot(t3, sbw_f3, color='y')\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:22.148929Z","iopub.execute_input":"2022-04-03T13:57:22.149371Z","iopub.status.idle":"2022-04-03T13:57:22.44067Z","shell.execute_reply.started":"2022-04-03T13:57:22.149337Z","shell.execute_reply":"2022-04-03T13:57:22.439955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Plot_Function**","metadata":{}},{"cell_type":"code","source":"def plot_signal_and_augmented_signal(signal, augmented_signal, sr):\n    fig, ax = plt.subplots(nrows=2)\n    librosa.display.waveshow(signal, sr=sr, ax=ax[0])\n    ax[0].set(title=\"Original signal\")\n    librosa.display.waveshow(augmented_signal, sr=sr, ax=ax[1])\n    ax[1].set(title=\"Augmented signal\")\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:22.441988Z","iopub.execute_input":"2022-04-03T13:57:22.442433Z","iopub.status.idle":"2022-04-03T13:57:22.448407Z","shell.execute_reply.started":"2022-04-03T13:57:22.442398Z","shell.execute_reply":"2022-04-03T13:57:22.447616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ***Audio Data Augmentation***\n\n## **Library:**\n\n### ***1. Librosa***\n### ***2. Audiomentations***\n### ***3. torch-audiomentations***\n### ***4. torchaudio.transform***\n","metadata":{}},{"cell_type":"markdown","source":"## **Why? DataAugmentation is important**\n\n***Mainly used for Computervision 90% important and audio data augmentaion is 50%***\n\n### **1. Reduce Overfitting**\n### **2. Save resources to collect and label data**\n### **3. Improve Model's accuracy**\n\n## **And various types:**\n\n### **1. Time shifting** \n### **2. Time stretching**\n### **3. Pitch Scaling**\n### **4. Noise addition(WhiteNoise etc..)**\n### **5. Random gain**\n### **6. Polarity inversion**\n\n## ***Go and visualise output in difference of input and apply augmentation output***\n","metadata":{}},{"cell_type":"markdown","source":"## **Input**","metadata":{}},{"cell_type":"code","source":"audio_in = \"../input/kaggle-pog-series-s01e02/train/000016.ogg\"\nipd.Audio(audio_in)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:22.449927Z","iopub.execute_input":"2022-04-03T13:57:22.450451Z","iopub.status.idle":"2022-04-03T13:57:22.477934Z","shell.execute_reply.started":"2022-04-03T13:57:22.450412Z","shell.execute_reply":"2022-04-03T13:57:22.477253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **WhiteNoise**","metadata":{}},{"cell_type":"code","source":"def add_white_noise(signal, noise_percentage_factor):\n    noise = np.random.normal(0, signal.std(), signal.size)\n    augmented_signal = signal + noise * noise_percentage_factor\n    return augmented_signal\n\n\n\nsignal, sr = librosa.load(audio_in)\naugmented_signal = add_white_noise(signal,0.5)\nsf.write(\"augmented_audio_white.wav\", augmented_signal, sr)\nplot_signal_and_augmented_signal(signal, augmented_signal, sr)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:22.479172Z","iopub.execute_input":"2022-04-03T13:57:22.479405Z","iopub.status.idle":"2022-04-03T13:57:24.470785Z","shell.execute_reply.started":"2022-04-03T13:57:22.479376Z","shell.execute_reply":"2022-04-03T13:57:24.47014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Time_Stretch**","metadata":{}},{"cell_type":"code","source":"def time_stretch(signal, time_stretch_rate):\n    return librosa.effects.time_stretch(signal, time_stretch_rate)\n\n\n\nsignal, sr = librosa.load(audio_in)\naugmented_signal = time_stretch(signal,0.5)\nsf.write(\"augmented_audio_time_stratch.wav\", augmented_signal, sr)\nplot_signal_and_augmented_signal(signal, augmented_signal, sr)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:24.473607Z","iopub.execute_input":"2022-04-03T13:57:24.474252Z","iopub.status.idle":"2022-04-03T13:57:27.292964Z","shell.execute_reply.started":"2022-04-03T13:57:24.474208Z","shell.execute_reply":"2022-04-03T13:57:27.292288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Pitch_Scale**","metadata":{}},{"cell_type":"code","source":"def pitch_scale(signal, sr, num_semitones):\n    return librosa.effects.pitch_shift(signal, sr, num_semitones)\n\n\nsignal, sr = librosa.load(audio_in)\naugmented_signal = pitch_scale(signal,sr,24)\nsf.write(\"augmented_audio_PitchScale.wav\", augmented_signal, sr)\nplot_signal_and_augmented_signal(signal, augmented_signal, sr)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:27.294059Z","iopub.execute_input":"2022-04-03T13:57:27.295274Z","iopub.status.idle":"2022-04-03T13:57:32.242654Z","shell.execute_reply.started":"2022-04-03T13:57:27.295233Z","shell.execute_reply":"2022-04-03T13:57:32.241991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Random_gain**","metadata":{}},{"cell_type":"code","source":"def random_gain(signal, min_factor=0.1, max_factor=0.12):\n    gain_rate = random.uniform(min_factor, max_factor)\n    augmented_signal = signal * gain_rate\n    return augmented_signal\n\nsignal, sr = librosa.load(audio_in)\naugmented_signal = random_gain(signal,2,4)\nsf.write(\"augmented_audio_Random_Gain.wav\", augmented_signal, sr)\nplot_signal_and_augmented_signal(signal, augmented_signal, sr)\n\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:32.243925Z","iopub.execute_input":"2022-04-03T13:57:32.244359Z","iopub.status.idle":"2022-04-03T13:57:34.203397Z","shell.execute_reply.started":"2022-04-03T13:57:32.244319Z","shell.execute_reply":"2022-04-03T13:57:34.202708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Invert_Polarity**","metadata":{}},{"cell_type":"code","source":"\ndef invert_polarity(signal):\n    return signal * -1\n\nsignal, sr = librosa.load(audio_in)\naugmented_signal = invert_polarity(signal)\nsf.write(\"augmented_audio_InvertPolarity.wav\", augmented_signal, sr)\nplot_signal_and_augmented_signal(signal, augmented_signal, sr)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:34.204647Z","iopub.execute_input":"2022-04-03T13:57:34.205036Z","iopub.status.idle":"2022-04-03T13:57:36.212462Z","shell.execute_reply.started":"2022-04-03T13:57:34.204998Z","shell.execute_reply":"2022-04-03T13:57:36.211788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Spectrogram Generator**\n\n**Reference: https://www.kaggle.com/code/harveenchadha/pog-spectogram-generator (Full_credit and Acknowledge)**","metadata":{}},{"cell_type":"code","source":"# # Create sample spectogram\n\n# generate_numpy = False\n\n# clip,sample_rate = librosa.load('../input/kaggle-pog-series-s01e02/train/000005.ogg',sr=None)\n# s = librosa.feature.melspectrogram(y=clip,sr=sample_rate)\n# melspec = librosa.power_to_db(s,ref=np.max)\n# librosa.display.specshow(melspec)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.213722Z","iopub.execute_input":"2022-04-03T13:57:36.214112Z","iopub.status.idle":"2022-04-03T13:57:36.218723Z","shell.execute_reply.started":"2022-04-03T13:57:36.214075Z","shell.execute_reply":"2022-04-03T13:57:36.218094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Generating Spectrograms for training and testing data\n\n# def create_melspec(filename):\n#     data,sample_rate = librosa.load(filename,sr=None)\n#     S = librosa.feature.melspectrogram(y=data,sr=sample_rate)\n#     melspec= librosa.power_to_db(S,ref=np.max)\n#     return melspec\n\n# def create_numpy(filename,dest):\n#     name = filename.split('/')[-1].split('.')[0]\n#     melspec = create_melspec(filename)\n#     np.save(dest + '/' + name +\".npy\",melspec)\n    \n# def create_spectrogram(filename,dest):\n#     name = filename.split('/')[-1].split('.')[0]\n#     plt.interactive(False)\n#     fig = plt.figure()\n#     ax = fig.add_subplot(111)\n#     ax.axes.get_xaxis().set_visible(False)\n#     ax.axes.get_yaxis().set_visible(False)\n#     ax.set_frame_on(False)\n#     melspec = create_melspec(filename)\n#     librosa.display.specshow(melspec)\n#     filename = f\"{dest}/{name}.jpg\"\n#     plt.savefig(filename,dpi=400,bbox_inches='tight',pad_inches=0)\n#     plt.close()\n#     fig.clf()\n#     plt.close(fig)\n#     plt.close('all')\n#     del fig,ax\n    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.219944Z","iopub.execute_input":"2022-04-03T13:57:36.220852Z","iopub.status.idle":"2022-04-03T13:57:36.228897Z","shell.execute_reply.started":"2022-04-03T13:57:36.220811Z","shell.execute_reply":"2022-04-03T13:57:36.228166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Benchmarking time\n# %load_ext line_profiler","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.231827Z","iopub.execute_input":"2022-04-03T13:57:36.232055Z","iopub.status.idle":"2022-04-03T13:57:36.241858Z","shell.execute_reply.started":"2022-04-03T13:57:36.232031Z","shell.execute_reply":"2022-04-03T13:57:36.241178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %lprun -f create_spectrogram  create_spectrogram('../input/kaggle-pog-series-s01e02/train/000002.ogg', '/kaggle/')\n# !rm /kaggle/000002.jpg\n# !rm /kaggle/000002.npy","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.242842Z","iopub.execute_input":"2022-04-03T13:57:36.243019Z","iopub.status.idle":"2022-04-03T13:57:36.250926Z","shell.execute_reply.started":"2022-04-03T13:57:36.242998Z","shell.execute_reply":"2022-04-03T13:57:36.250055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !mkdir -p /kaggle/spectograms/train\n# !mkdir -p /kaggle/spectograms/test\n\n\n# train_files = glob.glob('../input/kaggle-pog-series-s01e02/train/*.ogg')\n# test_files = glob.glob('../input/kaggle-pog-series-s01e02/test/*.ogg')\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.25225Z","iopub.execute_input":"2022-04-03T13:57:36.252642Z","iopub.status.idle":"2022-04-03T13:57:36.260067Z","shell.execute_reply.started":"2022-04-03T13:57:36.252608Z","shell.execute_reply":"2022-04-03T13:57:36.259355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# _ = Parallel(n_jobs=-1)(delayed(create_spectrogram)(file, '/kaggle/spectograms/train') for file in tqdm(train_files))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.261407Z","iopub.execute_input":"2022-04-03T13:57:36.261731Z","iopub.status.idle":"2022-04-03T13:57:36.268599Z","shell.execute_reply.started":"2022-04-03T13:57:36.261699Z","shell.execute_reply":"2022-04-03T13:57:36.26772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# _ = Parallel(n_jobs=-1)(delayed(create_spectrogram)(file, '/kaggle/spectograms/test') for file in tqdm(test_files))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.269924Z","iopub.execute_input":"2022-04-03T13:57:36.27041Z","iopub.status.idle":"2022-04-03T13:57:36.278267Z","shell.execute_reply.started":"2022-04-03T13:57:36.270375Z","shell.execute_reply":"2022-04-03T13:57:36.277596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !zip -rq spectograms.zip /kaggle/spectograms/\n# !rm -rf /kaggle/spectograms/","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:36.279594Z","iopub.execute_input":"2022-04-03T13:57:36.28005Z","iopub.status.idle":"2022-04-03T13:57:36.287604Z","shell.execute_reply.started":"2022-04-03T13:57:36.280015Z","shell.execute_reply":"2022-04-03T13:57:36.286919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #generate numpy\n# if generate_numpy:\n#     !mkdir -p /kaggle/npy/train\n#     !mkdir -p /kaggle/npy/test\n    \n    \n#     _ = Parallel(n_jobs=-1)(delayed(create_numpy)(file, '/kaggle/npy/train') for file in tqdm(train_files))\n#     _ = Parallel(n_jobs=-1)(delayed(create_numpy)(file, '/kaggle/npy/test') for file in tqdm(test_files))\n    \n#     !zip -rq npy.zip /kaggle/npy/\n#     !rm -rf /kaggle/npy/","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-03T13:57:36.288527Z","iopub.execute_input":"2022-04-03T13:57:36.289258Z","iopub.status.idle":"2022-04-03T13:57:36.297533Z","shell.execute_reply.started":"2022-04-03T13:57:36.289223Z","shell.execute_reply":"2022-04-03T13:57:36.296874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **2D_CNN**\n\n#### ***https://www.kaggle.com/code/harveenchadha/pog-1-spect-2d-cnn-keras-baseline***","metadata":{}},{"cell_type":"code","source":"import os\nos.environ[\"KMP_AFFINITY\"] = \"noverbose\"\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n\n\nimport numpy as np\nimport tensorflow as tf\ntf.get_logger().setLevel('ERROR')\n\nimport glob\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport warnings\n\nwarnings.filterwarnings('ignore')\n\nconfig = {\n    'SEED' : 42,\n    'DEBUG': False,\n    'test_size':0.1,\n    'img_size':256,\n    'batch_size':16,\n    'num_labels':0,\n    'epochs':100,\n    'device':'GPU'\n}","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:36.298794Z","iopub.execute_input":"2022-04-03T13:57:36.299039Z","iopub.status.idle":"2022-04-03T13:57:40.403822Z","shell.execute_reply.started":"2022-04-03T13:57:36.299007Z","shell.execute_reply":"2022-04-03T13:57:40.403071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(SEED):\n    os.environ['PYTHONHASHSEED'] = str(SEED)\n    np.random.seed(SEED)\n    tf.random.set_seed(SEED)\n    \nset_seed(config['SEED'])\n\ndef get_device(device):\n    if device == 'TPU':\n        try: # detect TPUs\n            tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect() # TPU detection\n            strategy = tf.distribute.TPUStrategy(tpu)\n        except ValueError: # detect GPUs\n            print('Cannot initialize TPU')\n    if device == 'GPU':\n        strategy = tf.distribute.MirroredStrategy() \n\n    print(\"Number of accelerators: \", strategy.num_replicas_in_sync)\n    return strategy\n\nstrategy= get_device(config['device'])\nconfig['batch_size'] = config['batch_size'] * strategy.num_replicas_in_sync","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:40.405247Z","iopub.execute_input":"2022-04-03T13:57:40.405484Z","iopub.status.idle":"2022-04-03T13:57:42.762578Z","shell.execute_reply.started":"2022-04-03T13:57:40.40545Z","shell.execute_reply":"2022-04-03T13:57:42.761814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/kaggle-pog-series-s01e02/train.csv')\ndf_test = pd.read_csv('../input/kaggle-pog-series-s01e02/test.csv')\ndf_train.sample(2)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:42.763875Z","iopub.execute_input":"2022-04-03T13:57:42.764282Z","iopub.status.idle":"2022-04-03T13:57:42.82618Z","shell.execute_reply.started":"2022-04-03T13:57:42.764243Z","shell.execute_reply":"2022-04-03T13:57:42.82541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Reading spectograms and ignoring files not present\ndf_train['ID'] = df_train['filename'].str.split('.').str[0]\ndf_test['ID'] = df_test['filename'].str.split('.').str[0]\ndf_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:42.827568Z","iopub.execute_input":"2022-04-03T13:57:42.827825Z","iopub.status.idle":"2022-04-03T13:57:42.8867Z","shell.execute_reply.started":"2022-04-03T13:57:42.82779Z","shell.execute_reply":"2022-04-03T13:57:42.886058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = pd.DataFrame(glob.glob('../input/pog-train-and-test-spectograms/kaggle/spectograms/train/*.jpg'), columns=['spec_path'])\ntest_files = pd.DataFrame(glob.glob('../input/pog-train-and-test-spectograms/kaggle/spectograms/test/*.jpg'), columns=['spec_path'])\n#train_files.sample(2)\ntrain_files['ID'] = train_files['spec_path'].str.split('/').str[-1].str.split('.').str[0]\ntest_files['ID'] = test_files['spec_path'].str.split('/').str[-1].str.split('.').str[0]\n#train_files.sample(2)\n\ndf_train_spec = pd.merge(df_train, train_files, how='right', on='ID')\ndf_test_spec = pd.merge(df_test, test_files, how='right', on='ID')\ndf_train_spec.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:42.887866Z","iopub.execute_input":"2022-04-03T13:57:42.888167Z","iopub.status.idle":"2022-04-03T13:57:43.910685Z","shell.execute_reply.started":"2022-04-03T13:57:42.888113Z","shell.execute_reply":"2022-04-03T13:57:43.909998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config['num_labels'] = df_train_spec['genre_id'].nunique()\ndf_train_spec.genre_id.value_counts(normalize=True) * 100","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:43.911758Z","iopub.execute_input":"2022-04-03T13:57:43.911996Z","iopub.status.idle":"2022-04-03T13:57:43.921824Z","shell.execute_reply.started":"2022-04-03T13:57:43.911963Z","shell.execute_reply":"2022-04-03T13:57:43.921194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid = train_test_split(df_train_spec, test_size = config['test_size'], random_state=config['SEED'], shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:43.923071Z","iopub.execute_input":"2022-04-03T13:57:43.92379Z","iopub.status.idle":"2022-04-03T13:57:43.938889Z","shell.execute_reply.started":"2022-04-03T13:57:43.923752Z","shell.execute_reply":"2022-04-03T13:57:43.938169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:43.945915Z","iopub.execute_input":"2022-04-03T13:57:43.946111Z","iopub.status.idle":"2022-04-03T13:57:43.949714Z","shell.execute_reply.started":"2022-04-03T13:57:43.946087Z","shell.execute_reply":"2022-04-03T13:57:43.948899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_data_train(image_path, label):\n    img = tf.io.read_file(image_path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.random_brightness(img, 0.1)\n    img = tf.image.resize(img,[config['img_size'], config['img_size']])\n    return img, label\n\ndef process_data_valid(image_path, label):\n    img = tf.io.read_file(image_path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.resize(img,[config['img_size'], config['img_size']])\n    return img, label\n\ndef configure_for_performance(ds, batch_size = 32):\n    ds = ds.cache('/kaggle/dump.tfcache') \n    ds = ds.shuffle(buffer_size=32)\n    ds = ds.batch(batch_size)\n    ds = ds.prefetch(buffer_size=AUTOTUNE)\n    return ds\n\n\ntrain_ds = tf.data.Dataset.from_tensor_slices((X_train.spec_path.values, X_train.genre_id.values))\nvalid_ds = tf.data.Dataset.from_tensor_slices((X_valid.spec_path.values, X_valid.genre_id.values))\n\n\ntrain_ds = train_ds.map(process_data_train, num_parallel_calls=AUTOTUNE)\nvalid_ds = valid_ds.map(process_data_valid, num_parallel_calls=AUTOTUNE)\n\ntrain_ds_batch = configure_for_performance(train_ds, config['batch_size'])\nvalid_ds_batch = valid_ds.batch(config['batch_size']*2)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:43.951147Z","iopub.execute_input":"2022-04-03T13:57:43.951401Z","iopub.status.idle":"2022-04-03T13:57:44.320758Z","shell.execute_reply.started":"2022-04-03T13:57:43.951368Z","shell.execute_reply":"2022-04-03T13:57:44.320048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, label_batch = next(iter(train_ds_batch))\nplt.figure(figsize=(14, 14))\nfor i in range(8):\n    ax = plt.subplot(4, 4, i + 1)\n    plt.imshow(image_batch[i].numpy().astype(\"uint8\"))\n    label = label_batch[i].numpy()\n    plt.title(label)\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:44.322018Z","iopub.execute_input":"2022-04-03T13:57:44.322273Z","iopub.status.idle":"2022-04-03T13:57:48.729863Z","shell.execute_reply.started":"2022-04-03T13:57:44.322239Z","shell.execute_reply":"2022-04-03T13:57:48.729021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    model = tf.keras.models.Sequential(\n        [\n            tf.keras.layers.Rescaling(1./255, input_shape = (config['img_size'], config['img_size'], 3)),\n            tf.keras.layers.Conv2D(16, kernel_size=5,  activation='relu'),\n            tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'),\n            tf.keras.layers.Conv2D(64, kernel_size=3, strides= 2, activation='relu'),\n            tf.keras.layers.Conv2D(128, kernel_size=3,  activation='relu'),\n            tf.keras.layers.Conv2D(256, kernel_size=3, strides= 2,  activation='relu'),\n            tf.keras.layers.Conv2D(512, kernel_size=3, strides= 2, activation='relu'),\n            tf.keras.layers.GlobalAveragePooling2D(),\n            tf.keras.layers.Flatten(),\n            tf.keras.layers.Dense(128, activation='relu'),\n            tf.keras.layers.Dense(64, activation='relu'),\n            tf.keras.layers.Dropout(0.2),\n            tf.keras.layers.Dense(config['num_labels'], activation='softmax')\n        ]\n    )\n\n    model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(),\n                 optimizer='adam',\n                 metrics='sparse_categorical_accuracy')\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:48.732662Z","iopub.execute_input":"2022-04-03T13:57:48.732888Z","iopub.status.idle":"2022-04-03T13:57:49.795499Z","shell.execute_reply.started":"2022-04-03T13:57:48.732861Z","shell.execute_reply":"2022-04-03T13:57:49.79477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:49.79672Z","iopub.execute_input":"2022-04-03T13:57:49.796956Z","iopub.status.idle":"2022-04-03T13:57:49.816406Z","shell.execute_reply.started":"2022-04-03T13:57:49.796924Z","shell.execute_reply":"2022-04-03T13:57:49.815677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_weight_path = 'best_model.hdf5'\nlast_weight_path = 'last_model.hdf5'\n\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(best_weight_path, \n                             monitor= 'val_loss', \n                             verbose=1, \n                             save_best_only=True, \n                             mode= 'min', \n                             save_weights_only = False)\ncheckpoint_last = tf.keras.callbacks.ModelCheckpoint(last_weight_path, \n                             monitor= 'val_loss', \n                             verbose=1, \n                             save_best_only=False, \n                             mode= 'min', \n                             save_weights_only = False)\n\n\nearly = tf.keras.callbacks.EarlyStopping(monitor= 'val_loss', \n                      mode= 'min', \n                      patience=4)\n\nreduceLROnPlat = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.8, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=5, min_lr=0.00001)\ncallbacks_list = [checkpoint, checkpoint_last, early, reduceLROnPlat]","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:49.819116Z","iopub.execute_input":"2022-04-03T13:57:49.819361Z","iopub.status.idle":"2022-04-03T13:57:49.826617Z","shell.execute_reply.started":"2022-04-03T13:57:49.81933Z","shell.execute_reply":"2022-04-03T13:57:49.825792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:49.827659Z","iopub.execute_input":"2022-04-03T13:57:49.828124Z","iopub.status.idle":"2022-04-03T13:57:50.544976Z","shell.execute_reply.started":"2022-04-03T13:57:49.828089Z","shell.execute_reply":"2022-04-03T13:57:50.544182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_ds_batch, callbacks=callbacks_list, epochs=config['epochs'], validation_data=valid_ds_batch)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-03T13:57:50.547545Z","iopub.execute_input":"2022-04-03T13:57:50.548086Z","iopub.status.idle":"2022-04-03T15:24:13.919861Z","shell.execute_reply.started":"2022-04-03T13:57:50.548041Z","shell.execute_reply":"2022-04-03T15:24:13.917025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_hist(hist):\n    plt.figure(figsize=(15,5))\n    local_epochs = len(hist.history[\"sparse_categorical_accuracy\"])\n    plt.plot(np.arange(local_epochs, step=1), hist.history[\"sparse_categorical_accuracy\"], '-o', label='Train Accuracy',color='#ff7f0e')\n    plt.plot(np.arange(local_epochs, step=1), hist.history[\"val_sparse_categorical_accuracy\"], '-o',label='Val Accuracy',color='#1f77b4')\n    plt.xlabel('Epoch',size=14)\n    plt.ylabel('Accuracy',size=14)\n    plt.legend(loc=2)\n    \n    plt2 = plt.gca().twinx()\n    plt2.plot(np.arange(local_epochs, step=1) ,history.history['loss'],'-o',label='Train Loss',color='#2ca02c')\n    plt2.plot(np.arange(local_epochs, step=1) ,history.history['val_loss'],'-o',label='Val Loss',color='#d62728')\n    plt.legend(loc=3)\n    plt.ylabel('Loss',size=14)\n    plt.title(\"Model Accuracy and loss\")\n    \n    plt.savefig('loss.png')\n    plt.show()\n    \nplot_hist(history)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T15:24:13.924697Z","iopub.execute_input":"2022-04-03T15:24:13.925754Z","iopub.status.idle":"2022-04-03T15:24:14.394117Z","shell.execute_reply.started":"2022-04-03T15:24:13.925714Z","shell.execute_reply":"2022-04-03T15:24:14.39343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('./best_model.hdf5')\npred_valid_y = model.predict(valid_ds_batch, workers=4, verbose = True)\npred_valid_y_labels = np.argmax(pred_valid_y, axis=-1)\n\nvalid_labels = np.concatenate([y.numpy() for x, y in valid_ds_batch], axis=0)\nprint(classification_report(valid_labels, pred_valid_y_labels ))","metadata":{"execution":{"iopub.status.busy":"2022-04-03T15:24:14.395412Z","iopub.execute_input":"2022-04-03T15:24:14.395828Z","iopub.status.idle":"2022-04-03T15:24:54.870159Z","shell.execute_reply.started":"2022-04-03T15:24:14.395789Z","shell.execute_reply":"2022-04-03T15:24:54.868966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = tf.data.Dataset.from_tensor_slices((df_test_spec.ID.values, df_test_spec.spec_path.values))\n\n\ndef process_test(id, image_path):\n    img = tf.io.read_file(image_path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    \n    img = tf.image.resize(img, size=[config['img_size'], config['img_size']])\n    return id, img\n    \ntest_ds = test_ds.map(process_test, num_parallel_calls=AUTOTUNE).batch(config['batch_size']*2)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T15:24:54.874684Z","iopub.execute_input":"2022-04-03T15:24:54.875055Z","iopub.status.idle":"2022-04-03T15:24:55.013211Z","shell.execute_reply.started":"2022-04-03T15:24:54.875017Z","shell.execute_reply":"2022-04-03T15:24:55.012319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\nfor id, imgl in tqdm(test_ds):\n    local_preds = model.predict(imgl, workers=4)\n    \n    for idx, local_id in enumerate(id.numpy()):\n        preds.append({'song_id':local_id.decode(\"utf-8\") , 'genre_id': np.argmax(local_preds[idx], axis=-1)})","metadata":{"execution":{"iopub.status.busy":"2022-04-03T15:24:55.014776Z","iopub.execute_input":"2022-04-03T15:24:55.015338Z","iopub.status.idle":"2022-04-03T15:27:16.993814Z","shell.execute_reply.started":"2022-04-03T15:24:55.015299Z","shell.execute_reply":"2022-04-03T15:27:16.993058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.append({'song_id':'022612', 'genre_id':1})\npreds.append({'song_id':'024013', 'genre_id':0})\n\ntest_df = pd.DataFrame.from_dict(preds)\ntest_df['song_id'] = test_df['song_id'].astype('int')\n\nassert(len(test_df) == 5078)\ntest_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T15:27:16.995332Z","iopub.execute_input":"2022-04-03T15:27:16.995755Z","iopub.status.idle":"2022-04-03T15:27:17.033304Z","shell.execute_reply.started":"2022-04-03T15:27:16.995717Z","shell.execute_reply":"2022-04-03T15:27:17.032605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **After the know basic concept Audio data!** \n#### ***Go to the data? Identify Supervised or Unsupervised? ---> Apply above concept's***\n#### **NextProcess ---> Preprocessing(Identify the features) and Build the model**\n\n\n## **Reference: BirdCLEF22-Preprocessing and Build LSTM,CNN Model notebook**\n#### ***[https://www.kaggle.com/code/venkatkumar001/birdclef22-lstm/notebook](http://)***\n\n\n## **🎛🔥⭐️Thankyou Guys🎛🔥⭐️**\n#### ***reference: [https://www.youtube.com/playlist?list=PL-wATfeyAMNqIee7cH3q1bh4QJFAaeNv0](http://)*** (Full_Credit)\n\n\n\n## **Next_Process ComingSoon..........................................**\n","metadata":{}}]}