{"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":"## Spectogram generator\n\nA simple notebook to generate spectograms and save them as images so as to make them work with CNN's.","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport librosa\nimport librosa.display\nfrom joblib import Parallel, delayed\nfrom tqdm.notebook import tqdm\nimport glob\nimport numpy as np\nimport soundfile as sf","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:00:00.023272Z","iopub.execute_input":"2022-03-14T09:00:00.023897Z","iopub.status.idle":"2022-03-14T09:00:02.765133Z","shell.execute_reply.started":"2022-03-14T09:00:00.023803Z","shell.execute_reply":"2022-03-14T09:00:02.763978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create sample spectogram","metadata":{}},{"cell_type":"code","source":"generate_numpy = False","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clip, sample_rate = librosa.load('../input/kaggle-pog-series-s01e02/train/000002.ogg', sr=None)\nS = librosa.feature.melspectrogram(y=clip, sr=sample_rate)\nmelspec = librosa.power_to_db(S, ref=np.max)\nlibrosa.display.specshow(melspec)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:00:02.767406Z","iopub.execute_input":"2022-03-14T09:00:02.767743Z","iopub.status.idle":"2022-03-14T09:00:03.562313Z","shell.execute_reply.started":"2022-03-14T09:00:02.767700Z","shell.execute_reply":"2022-03-14T09:00:03.561455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generating Spectograms for training and testing data","metadata":{}},{"cell_type":"code","source":"\ndef 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\ndef create_numpy(filename, dest):\n    name = filename.split('/')[-1].split('.')[0]\n    melspec = create_melspec(filename)\n    np.save(dest  + '/' + name + \".npy\", melspec)\n\ndef 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","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:00:03.563358Z","iopub.execute_input":"2022-03-14T09:00:03.564233Z","iopub.status.idle":"2022-03-14T09:00:03.721085Z","shell.execute_reply.started":"2022-03-14T09:00:03.564180Z","shell.execute_reply":"2022-03-14T09:00:03.720021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Benchmarking time","metadata":{}},{"cell_type":"code","source":"%load_ext line_profiler","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:00:03.723292Z","iopub.execute_input":"2022-03-14T09:00:03.723627Z","iopub.status.idle":"2022-03-14T09:00:03.743345Z","shell.execute_reply.started":"2022-03-14T09:00:03.723574Z","shell.execute_reply":"2022-03-14T09:00:03.742555Z"},"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":{"execution":{"iopub.status.busy":"2022-03-14T09:00:03.747426Z","iopub.execute_input":"2022-03-14T09:00:03.748895Z","iopub.status.idle":"2022-03-14T09:00:06.414663Z","shell.execute_reply.started":"2022-03-14T09:00:03.748834Z","shell.execute_reply":"2022-03-14T09:00:06.413245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It's the saving that takes the most of the time.","metadata":{}},{"cell_type":"code","source":"!mkdir -p /kaggle/spectograms/train\n!mkdir -p /kaggle/spectograms/test\n\n\ntrain_files = glob.glob('../input/kaggle-pog-series-s01e02/train/*.ogg')\ntest_files = glob.glob('../input/kaggle-pog-series-s01e02/test/*.ogg')","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:00:11.519700Z","iopub.execute_input":"2022-03-14T09:00:11.520022Z","iopub.status.idle":"2022-03-14T09:00:15.438234Z","shell.execute_reply.started":"2022-03-14T09:00:11.519990Z","shell.execute_reply":"2022-03-14T09:00:15.437228Z"},"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":{"execution":{"iopub.status.busy":"2022-03-14T09:00:15.440378Z","iopub.execute_input":"2022-03-14T09:00:15.440909Z","iopub.status.idle":"2022-03-14T09:00:22.602072Z","shell.execute_reply.started":"2022-03-14T09:00:15.440875Z","shell.execute_reply":"2022-03-14T09:00:22.600942Z"},"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":{"execution":{"iopub.status.busy":"2022-03-14T09:00:22.603577Z","iopub.execute_input":"2022-03-14T09:00:22.604470Z","iopub.status.idle":"2022-03-14T09:00:25.359427Z","shell.execute_reply.started":"2022-03-14T09:00:22.604400Z","shell.execute_reply":"2022-03-14T09:00:25.358755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Zip dataset ","metadata":{}},{"cell_type":"code","source":"!zip -rq spectograms.zip /kaggle/spectograms/\n!rm -rf /kaggle/spectograms/","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:15:46.974315Z","iopub.execute_input":"2022-03-14T09:15:46.975714Z","iopub.status.idle":"2022-03-14T09:15:50.218202Z","shell.execute_reply.started":"2022-03-14T09:15:46.975622Z","shell.execute_reply":"2022-03-14T09:15:50.216599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Numpy (optional)","metadata":{}},{"cell_type":"code","source":"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":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The numpy and spectograms can be clubbed together as well, but the kaggle kernel has an output space limitation and numpy files take a lot of space hence I am keeping both the functions separate.","metadata":{}}]}