{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-31T13:35:26.439357Z","iopub.execute_input":"2022-03-31T13:35:26.440020Z","iopub.status.idle":"2022-03-31T13:35:26.445441Z","shell.execute_reply.started":"2022-03-31T13:35:26.439980Z","shell.execute_reply":"2022-03-31T13:35:26.444617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_folder = Path('../input/kaggle-pog-series-s01e02')","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:34:04.239953Z","iopub.execute_input":"2022-03-31T13:34:04.240242Z","iopub.status.idle":"2022-03-31T13:34:04.245352Z","shell.execute_reply.started":"2022-03-31T13:34:04.240206Z","shell.execute_reply":"2022-03-31T13:34:04.244209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items = get_files(base_folder, extensions='.ogg')","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:34:08.411625Z","iopub.execute_input":"2022-03-31T13:34:08.411979Z","iopub.status.idle":"2022-03-31T13:34:27.521440Z","shell.execute_reply.started":"2022-03-31T13:34:08.411945Z","shell.execute_reply":"2022-03-31T13:34:27.520508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:34:27.523125Z","iopub.execute_input":"2022-03-31T13:34:27.523422Z","iopub.status.idle":"2022-03-31T13:34:27.533307Z","shell.execute_reply.started":"2022-03-31T13:34:27.523383Z","shell.execute_reply":"2022-03-31T13:34:27.532363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_FFT = 2048\nHOP_LEN = 1024","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:34:36.510175Z","iopub.execute_input":"2022-03-31T13:34:36.510830Z","iopub.status.idle":"2022-03-31T13:34:36.514658Z","shell.execute_reply.started":"2022-03-31T13:34:36.510787Z","shell.execute_reply":"2022-03-31T13:34:36.513840Z"},"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-03-31T13:34:42.169716Z","iopub.execute_input":"2022-03-31T13:34:42.169978Z","iopub.status.idle":"2022-03-31T13:34:42.177258Z","shell.execute_reply.started":"2022-03-31T13:34:42.169951Z","shell.execute_reply":"2022-03-31T13:34:42.176247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = items[2]\nspec_default = create_spectrogram(filename)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:34:57.239248Z","iopub.execute_input":"2022-03-31T13:34:57.239546Z","iopub.status.idle":"2022-03-31T13:34:57.642494Z","shell.execute_reply.started":"2022-03-31T13:34:57.239504Z","shell.execute_reply":"2022-03-31T13:34:57.641827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec_default","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:35:01.208961Z","iopub.execute_input":"2022-03-31T13:35:01.209380Z","iopub.status.idle":"2022-03-31T13:35:01.226456Z","shell.execute_reply.started":"2022-03-31T13:35:01.209349Z","shell.execute_reply":"2022-03-31T13:35:01.225798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(spec_default)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:35:13.814283Z","iopub.execute_input":"2022-03-31T13:35:13.814554Z","iopub.status.idle":"2022-03-31T13:35:14.075520Z","shell.execute_reply.started":"2022-03-31T13:35:13.814526Z","shell.execute_reply":"2022-03-31T13:35:14.074666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Path('spectograms/train').mkdir(parents=True, exist_ok=True)\nPath('spectograms/test').mkdir(parents=True, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:44:49.235730Z","iopub.execute_input":"2022-03-31T13:44:49.236590Z","iopub.status.idle":"2022-03-31T13:44:49.242264Z","shell.execute_reply.started":"2022-03-31T13:44:49.236535Z","shell.execute_reply":"2022-03-31T13:44:49.241652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items.shuffle()","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:50:46.673642Z","iopub.execute_input":"2022-03-31T13:50:46.674065Z","iopub.status.idle":"2022-03-31T13:50:46.708917Z","shell.execute_reply.started":"2022-03-31T13:50:46.674028Z","shell.execute_reply":"2022-03-31T13:50:46.707830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_image(filename):\n    specgram = create_spectrogram(filename)\n    if filename.parent.name == 'train':\n        dest = Path('spectograms/train')/f'{filename.stem}.png'\n    else:\n         dest = Path('spectograms/test')/f'{filename.stem}.png'\n    print(dest)\n    save_image(specgram, dest)","metadata":{"execution":{"iopub.status.busy":"2022-03-31T13:50:50.961306Z","iopub.execute_input":"2022-03-31T13:50:50.961593Z","iopub.status.idle":"2022-03-31T13:50:50.966820Z","shell.execute_reply.started":"2022-03-31T13:50:50.961544Z","shell.execute_reply":"2022-03-31T13:50:50.965850Z"},"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-03-31T13:50:51.260423Z","iopub.execute_input":"2022-03-31T13:50:51.261323Z","iopub.status.idle":"2022-03-31T13:50:52.726444Z","shell.execute_reply.started":"2022-03-31T13:50:51.261278Z","shell.execute_reply":"2022-03-31T13:50:52.725734Z"},"trusted":true},"execution_count":null,"outputs":[]}]}