{"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":"from fastai.vision.all import *\nimport torchaudio\nfrom joblib import Parallel, delayed\nfrom tqdm.notebook import tqdm\nfrom torchvision.utils import save_image","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:30.890854Z","iopub.execute_input":"2022-05-13T10:50:30.891634Z","iopub.status.idle":"2022-05-13T10:50:30.899378Z","shell.execute_reply.started":"2022-05-13T10:50:30.891552Z","shell.execute_reply":"2022-05-13T10:50:30.898354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_folder = Path('../input/birdclef-2022')","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:54.375963Z","iopub.execute_input":"2022-05-13T10:50:54.376450Z","iopub.status.idle":"2022-05-13T10:50:54.380241Z","shell.execute_reply.started":"2022-05-13T10:50:54.376401Z","shell.execute_reply":"2022-05-13T10:50:54.379357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/birdclef-2022/train_metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:54.730141Z","iopub.execute_input":"2022-05-13T10:50:54.730580Z","iopub.status.idle":"2022-05-13T10:50:54.812687Z","shell.execute_reply.started":"2022-05-13T10:50:54.730533Z","shell.execute_reply":"2022-05-13T10:50:54.811669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items = get_files(base_folder, extensions='.ogg')","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:54.934765Z","iopub.execute_input":"2022-05-13T10:50:54.935068Z","iopub.status.idle":"2022-05-13T10:50:55.189968Z","shell.execute_reply.started":"2022-05-13T10:50:54.935038Z","shell.execute_reply":"2022-05-13T10:50:55.189201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:55.191987Z","iopub.execute_input":"2022-05-13T10:50:55.192259Z","iopub.status.idle":"2022-05-13T10:50:55.198732Z","shell.execute_reply.started":"2022-05-13T10:50:55.192228Z","shell.execute_reply":"2022-05-13T10:50:55.197748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_FFT = 2048\nHOP_LEN = 1024","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:55.389469Z","iopub.execute_input":"2022-05-13T10:50:55.389938Z","iopub.status.idle":"2022-05-13T10:50:55.394113Z","shell.execute_reply.started":"2022-05-13T10:50:55.389902Z","shell.execute_reply":"2022-05-13T10:50:55.393292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_spectrogram(filename):\n    audio, sr = torchaudio.load(filename)\n    specgram = torchaudio.transforms.MelSpectrogram(sample_rate=sr, \n                                                    n_fft=N_FFT, \n                                                    win_length=N_FFT, \n                                                    hop_length=HOP_LEN\n                                                    ,\n                                                    center=True,\n                                                    pad_mode=\"reflect\",\n                                                    power=2.0,\n                                                    norm='slaney',\n                                                    onesided=True,\n                                                    n_mels=224,\n                                                    mel_scale=\"htk\"\n                                                   )(audio).mean(axis=0)\n    specgram = torchaudio.transforms.AmplitudeToDB()(specgram)\n    specgram = specgram - specgram.min()\n    specgram = specgram/specgram.max()\n    \n    \n    return specgram","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:55.731152Z","iopub.execute_input":"2022-05-13T10:50:55.732003Z","iopub.status.idle":"2022-05-13T10:50:55.742159Z","shell.execute_reply.started":"2022-05-13T10:50:55.731944Z","shell.execute_reply":"2022-05-13T10:50:55.741189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = items[2]\nspec_default = create_spectrogram(filename)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:50:55.859986Z","iopub.execute_input":"2022-05-13T10:50:55.860961Z","iopub.status.idle":"2022-05-13T10:50:55.969185Z","shell.execute_reply.started":"2022-05-13T10:50:55.860916Z","shell.execute_reply":"2022-05-13T10:50:55.968437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename.parent","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:52:24.455975Z","iopub.execute_input":"2022-05-13T10:52:24.456299Z","iopub.status.idle":"2022-05-13T10:52:24.462772Z","shell.execute_reply.started":"2022-05-13T10:52:24.456263Z","shell.execute_reply":"2022-05-13T10:52:24.461911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Path('spectograms/train_img').mkdir(parents=True, exist_ok=True)\nPath('spectograms/test_img').mkdir(parents=True, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:54:28.709915Z","iopub.execute_input":"2022-05-13T10:54:28.710383Z","iopub.status.idle":"2022-05-13T10:54:28.715814Z","shell.execute_reply.started":"2022-05-13T10:54:28.710349Z","shell.execute_reply":"2022-05-13T10:54:28.714672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.primary_label.unique()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:54:28.935179Z","iopub.execute_input":"2022-05-13T10:54:28.935501Z","iopub.status.idle":"2022-05-13T10:54:28.944897Z","shell.execute_reply.started":"2022-05-13T10:54:28.935468Z","shell.execute_reply":"2022-05-13T10:54:28.944158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_image(filename):\n    specgram = create_spectrogram(filename)\n    if filename.parent.name in train.primary_label.unique():\n        Path('spectograms/train_img/{}'.format(filename.parent.name)).mkdir(parents=True, exist_ok=True)\n        dest = Path('spectograms/train_img/{}'.format(filename.parent.name))/f'{filename.stem}.png'\n    else:\n         dest = Path('spectograms/test_img')/f'{filename.stem}.png'\n    print(dest)\n    save_image(specgram, dest)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:55:14.225140Z","iopub.execute_input":"2022-05-13T10:55:14.225479Z","iopub.status.idle":"2022-05-13T10:55:14.234005Z","shell.execute_reply.started":"2022-05-13T10:55:14.225445Z","shell.execute_reply":"2022-05-13T10:55:14.232977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = Parallel(n_jobs=-1)(delayed(create_image)(file) for file in tqdm(items))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T10:55:15.429441Z","iopub.execute_input":"2022-05-13T10:55:15.430469Z","iopub.status.idle":"2022-05-13T10:55:37.999241Z","shell.execute_reply.started":"2022-05-13T10:55:15.430418Z","shell.execute_reply":"2022-05-13T10:55:37.997526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}