{"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":"!pip install noisereduce","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:45:56.566251Z","iopub.execute_input":"2022-03-09T05:45:56.567120Z","iopub.status.idle":"2022-03-09T05:46:04.090451Z","shell.execute_reply.started":"2022-03-09T05:45:56.567067Z","shell.execute_reply":"2022-03-09T05:46:04.089491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport IPython.display as ipd\nimport librosa.display\nimport folium\nfrom folium import plugins\nimport torch\nimport torchaudio\nfrom math import ceil\nimport noisereduce as nr\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-09T05:46:04.094274Z","iopub.execute_input":"2022-03-09T05:46:04.094647Z","iopub.status.idle":"2022-03-09T05:46:04.102437Z","shell.execute_reply.started":"2022-03-09T05:46:04.094584Z","shell.execute_reply":"2022-03-09T05:46:04.101505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/birdclef-2022/train_metadata.csv')\ntrain[0:3].T","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:46:04.104040Z","iopub.execute_input":"2022-03-09T05:46:04.105094Z","iopub.status.idle":"2022-03-09T05:46:04.196798Z","shell.execute_reply.started":"2022-03-09T05:46:04.105039Z","shell.execute_reply":"2022-03-09T05:46:04.195825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio_dir='../input/birdclef-2022/train_audio'","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:46:04.198441Z","iopub.execute_input":"2022-03-09T05:46:04.198775Z","iopub.status.idle":"2022-03-09T05:46:04.203152Z","shell.execute_reply.started":"2022-03-09T05:46:04.198733Z","shell.execute_reply":"2022-03-09T05:46:04.202352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths=[]\nfor i in range(len(train)):\n    paths+=[os.path.join(train_audio_dir,train.loc[i,'filename'])]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:46:04.205698Z","iopub.execute_input":"2022-03-09T05:46:04.206274Z","iopub.status.idle":"2022-03-09T05:46:04.370511Z","shell.execute_reply.started":"2022-03-09T05:46:04.206233Z","shell.execute_reply":"2022-03-09T05:46:04.369616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_spectrogram(\n    fname: str,\n    reduce_noise: bool = False,\n    frame_size: int = 5,\n    frame_step: int = 2,\n    channel: int = 0,\n    device = \"cpu\",\n) -> list:\n    waveform, sample_rate = torchaudio.load(fname)\n    \n    transform = torchaudio.transforms.Spectrogram(n_fft=1800, win_length=512).to(device)\n    if reduce_noise:\n        waveform = torch.tensor(nr.reduce_noise(\n            y=waveform,\n            sr=sample_rate,\n            win_length=transform.win_length,\n            use_tqdm=False,\n            n_jobs=2,\n        ))\n    step = int(frame_step * sample_rate)\n    size = int(frame_size * sample_rate)\n    spectrograms = []\n    for i in range(ceil((waveform.size()[-1] - size) / step)):\n        begin = i * step\n        frame = waveform[channel][begin:begin + size]\n        if len(frame) < size:\n            if i == 0:\n                rep = round(float(size) / len(frame))\n                frame = frame.repeat(int(rep))\n            elif len(frame) < (size * 0.33):\n                continue\n            else:\n                frame = waveform[channel][-size:]\n        sg = transform(frame.to(device))\n        spectrograms.append(np.nan_to_num(torch.log(sg).numpy()))\n        # spectrograms.append(np.nan_to_num(sg.numpy()))\n    return spectrograms","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:46:04.372238Z","iopub.execute_input":"2022-03-09T05:46:04.372925Z","iopub.status.idle":"2022-03-09T05:46:04.383865Z","shell.execute_reply.started":"2022-03-09T05:46:04.372879Z","shell.execute_reply":"2022-03-09T05:46:04.383034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=8 #set number\ndisplay(train.iloc[i])\nprint()\nprint(paths[i])\nprint()\nipd.Audio(paths[i])","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:46:04.385124Z","iopub.execute_input":"2022-03-09T05:46:04.385797Z","iopub.status.idle":"2022-03-09T05:46:04.451296Z","shell.execute_reply.started":"2022-03-09T05:46:04.385754Z","shell.execute_reply":"2022-03-09T05:46:04.450294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_audio = paths[i]\nsgs = create_spectrogram(path_audio, reduce_noise=True)\nprint(len(sgs))\n\nfig, axarr = plt.subplots(ncols=4, figsize=(16,4))\nfor i, sg in enumerate(sgs[0:4]):\n    ax = axarr[i].imshow(sg, vmin=-50, vmax=10)\n    \nplt.colorbar(ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:46:04.452538Z","iopub.execute_input":"2022-03-09T05:46:04.453343Z","iopub.status.idle":"2022-03-09T05:46:07.516465Z","shell.execute_reply.started":"2022-03-09T05:46:04.453300Z","shell.execute_reply":"2022-03-09T05:46:07.515860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=train.iloc[i:i+1,3:5]\neq_map = folium.Map(location=[20,0],tiles='Stamen Terrain',zoom_start=1,min_zoom=2.0)\neq_map.add_child(plugins.HeatMap(data))\neq_map","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:46:07.517335Z","iopub.execute_input":"2022-03-09T05:46:07.518032Z","iopub.status.idle":"2022-03-09T05:46:07.533577Z","shell.execute_reply.started":"2022-03-09T05:46:07.517998Z","shell.execute_reply":"2022-03-09T05:46:07.532655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}