{"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":"> This notebook follows the [fastai style conventions](https://docs.fast.ai/dev/style.html#style-guide).","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"## Setup","metadata":{}},{"cell_type":"code","source":"try: from fastkaggle import *\nexcept ModuleNotFoundError:\n    ! pip install -Uqq fastkaggle\n    from fastkaggle import *\n\niskaggle","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:35:58.859842Z","iopub.execute_input":"2023-04-01T08:35:58.860928Z","iopub.status.idle":"2023-04-01T08:36:13.644297Z","shell.execute_reply.started":"2023-04-01T08:35:58.860880Z","shell.execute_reply":"2023-04-01T08:36:13.642470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"comp = 'birdclef-2023'\nd_path = setup_comp(comp)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:36:13.647563Z","iopub.execute_input":"2023-04-01T08:36:13.648468Z","iopub.status.idle":"2023-04-01T08:36:13.654305Z","shell.execute_reply.started":"2023-04-01T08:36:13.648421Z","shell.execute_reply":"2023-04-01T08:36:13.652782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.imports import *\nfrom fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:36:13.655818Z","iopub.execute_input":"2023-04-01T08:36:13.656236Z","iopub.status.idle":"2023-04-01T08:36:19.350945Z","shell.execute_reply.started":"2023-04-01T08:36:13.656191Z","shell.execute_reply":"2023-04-01T08:36:19.349315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data","metadata":{}},{"cell_type":"markdown","source":"### Paths","metadata":{}},{"cell_type":"code","source":"d_path.ls()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:36:25.201649Z","iopub.execute_input":"2023-04-01T08:36:25.202180Z","iopub.status.idle":"2023-04-01T08:36:25.212245Z","shell.execute_reply.started":"2023-04-01T08:36:25.202137Z","shell.execute_reply":"2023-04-01T08:36:25.210573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aud_files = d_path/'train_audio'","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:36:25.552763Z","iopub.execute_input":"2023-04-01T08:36:25.553235Z","iopub.status.idle":"2023-04-01T08:36:25.558929Z","shell.execute_reply.started":"2023-04-01T08:36:25.553197Z","shell.execute_reply":"2023-04-01T08:36:25.557655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir('/kaggle/train_images', exist_ok=True); Path('/kaggle/train_images').exists()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:36:30.712784Z","iopub.execute_input":"2023-04-01T08:36:30.713293Z","iopub.status.idle":"2023-04-01T08:36:30.724793Z","shell.execute_reply.started":"2023-04-01T08:36:30.713250Z","shell.execute_reply":"2023-04-01T08:36:30.723232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Single Image","metadata":{}},{"cell_type":"code","source":"aud = aud_files.ls()[0].ls()[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-30T07:55:40.637720Z","iopub.execute_input":"2023-03-30T07:55:40.638178Z","iopub.status.idle":"2023-03-30T07:55:40.681195Z","shell.execute_reply.started":"2023-03-30T07:55:40.638092Z","shell.execute_reply":"2023-03-30T07:55:40.679874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Frequency Masking","metadata":{}},{"cell_type":"code","source":"import torchaudio\nimport torchaudio.transforms as T\nwvfrm, sr = torchaudio.load(aud)\nstart_sec = 0\nend_sec = 5\nwvfrm = wvfrm[:, start_sec*sr:end_sec*sr]\nspec = T.Spectrogram()(wvfrm)\nspec = T.AmplitudeToDB()(spec); spec","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:02.816744Z","iopub.execute_input":"2023-03-30T08:00:02.817164Z","iopub.status.idle":"2023-03-30T08:00:02.856027Z","shell.execute_reply.started":"2023-03-30T08:00:02.817123Z","shell.execute_reply":"2023-03-30T08:00:02.855003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = T.FrequencyMasking(freq_mask_param=80)(spec)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:03.055077Z","iopub.execute_input":"2023-03-30T08:00:03.055915Z","iopub.status.idle":"2023-03-30T08:00:03.062669Z","shell.execute_reply.started":"2023-03-30T08:00:03.055859Z","shell.execute_reply":"2023-03-30T08:00:03.061637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = spec.squeeze().numpy()\nspec = (spec - spec.min()) / (spec.max() - spec.min()) * 255; spec\nspec = spec.astype('uint8')","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:03.337213Z","iopub.execute_input":"2023-03-30T08:00:03.337948Z","iopub.status.idle":"2023-03-30T08:00:03.345998Z","shell.execute_reply.started":"2023-03-30T08:00:03.337907Z","shell.execute_reply":"2023-03-30T08:00:03.344448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.fromarray(spec)\nprint(img.shape)\nimg","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:03.843118Z","iopub.execute_input":"2023-03-30T08:00:03.843588Z","iopub.status.idle":"2023-03-30T08:00:03.867735Z","shell.execute_reply.started":"2023-03-30T08:00:03.843545Z","shell.execute_reply":"2023-03-30T08:00:03.866551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.resize((128, 128))","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:04.925753Z","iopub.execute_input":"2023-03-30T08:00:04.926170Z","iopub.status.idle":"2023-03-30T08:00:04.937767Z","shell.execute_reply.started":"2023-03-30T08:00:04.926131Z","shell.execute_reply":"2023-03-30T08:00:04.936191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.resize((256, 256))","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:07.137614Z","iopub.execute_input":"2023-03-30T08:00:07.138063Z","iopub.status.idle":"2023-03-30T08:00:07.153093Z","shell.execute_reply.started":"2023-03-30T08:00:07.138027Z","shell.execute_reply":"2023-03-30T08:00:07.151526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.resize((512, 512))","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:08.471828Z","iopub.execute_input":"2023-03-30T08:00:08.472886Z","iopub.status.idle":"2023-03-30T08:00:08.518362Z","shell.execute_reply.started":"2023-03-30T08:00:08.472843Z","shell.execute_reply":"2023-03-30T08:00:08.517109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.resize((512, 512)).save('test.png')","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:00:16.155784Z","iopub.execute_input":"2023-03-30T08:00:16.156545Z","iopub.status.idle":"2023-03-30T08:00:16.195417Z","shell.execute_reply.started":"2023-03-30T08:00:16.156501Z","shell.execute_reply":"2023-03-30T08:00:16.194454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Time Masking","metadata":{}},{"cell_type":"code","source":"def create_spec():\n    wvfrm, sr = torchaudio.load(aud)\n    start_sec = 0\n    end_sec = 5\n    wvfrm = wvfrm[:, start_sec*sr:end_sec*sr]\n    spec = T.Spectrogram()(wvfrm)\n    spec = T.AmplitudeToDB()(spec)\n    return spec\n\ndef create_img(spec):\n    spec = spec.squeeze().numpy()\n    spec = np.real(spec)\n    spec = (spec - spec.min()) / (spec.max() - spec.min()) * 255\n    spec = spec.astype('uint8')\n    img = Image.fromarray(spec)\n    print(img.shape)\n    display(img)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:31:24.366881Z","iopub.execute_input":"2023-03-30T08:31:24.368256Z","iopub.status.idle":"2023-03-30T08:31:24.375861Z","shell.execute_reply.started":"2023-03-30T08:31:24.368206Z","shell.execute_reply":"2023-03-30T08:31:24.374852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = create_spec()\nspec = T.TimeMasking(time_mask_param=100)(spec)\ncreate_img(spec)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:21:55.050513Z","iopub.execute_input":"2023-03-30T08:21:55.050915Z","iopub.status.idle":"2023-03-30T08:21:55.100560Z","shell.execute_reply.started":"2023-03-30T08:21:55.050879Z","shell.execute_reply":"2023-03-30T08:21:55.099073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Time Stretch","metadata":{}},{"cell_type":"code","source":"spec = create_spec()\nspec = T.TimeStretch()(spec, 1.5)\ncreate_img(spec)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T08:59:32.050671Z","iopub.execute_input":"2023-03-30T08:59:32.051171Z","iopub.status.idle":"2023-03-30T08:59:32.123635Z","shell.execute_reply.started":"2023-03-30T08:59:32.051124Z","shell.execute_reply":"2023-03-30T08:59:32.121856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create Augmented Spectrograms","metadata":{}},{"cell_type":"code","source":"import torchaudio\nimport torchaudio.transforms as T","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:37:00.580155Z","iopub.execute_input":"2023-04-01T08:37:00.581492Z","iopub.status.idle":"2023-04-01T08:37:01.734032Z","shell.execute_reply.started":"2023-04-01T08:37:00.581428Z","shell.execute_reply":"2023-04-01T08:37:01.732432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_images(duration, f):\n    for step in range(0, duration, 5):\n        wvfrm, sr = torchaudio.load(f)\n        wvfrm = cut_wvfrm(wvfrm, sr, step)\n        spec = create_spec(wvfrm)\n        end_sec = step + 5\n        for x in range(1, 4):\n            spec_ = time_mask(spec)\n            img = spec2img(spec)\n            img.save(f'/kaggle/train_images/{bird.stem}/{f.stem}_tm{x}_{end_sec}.png')\n            \n#             spec_ = freq_mask(spec)\n#             img = spec2img(spec)\n#             img.save(f'/kaggle/train_images/{bird.stem}/{f.stem}_fm{x}_{end_sec}.png')\n            \n#             spec_ = time_stch(spec)\n#             img = spec2img(spec)\n#             img.save(f'/kaggle/train_images/{bird.stem}/{f.stem}_ts{x}_{end_sec}.png')\n            \n#             spec_ = time_mask(spec)\n#             spec__ = freq_mask(spec_)\n#             img = spec2img(spec)\n#             img.save(f'/kaggle/train_images/{bird.stem}/{f.stem}_tm-fm{x}_{end_sec}.png')\n            \n#             spec_ = time_mask(spec)\n#             spec__ = time_stch(spec_)\n#             img = spec2img(spec)\n#             img.save(f'/kaggle/train_images/{bird.stem}/{f.stem}_tm-ts{x}_{end_sec}.png')\n            \n#             spec_ = freq_mask(spec)\n#             spec__ = time_stch(spec_)\n#             img = spec2img(spec)\n#             img.save(f'/kaggle/train_images/{bird.stem}/{f.stem}_fm-ts{x}_{end_sec}.png')\n            \n#             spec_ = time_mask(spec)\n#             spec__ = freq_mask(spec_)\n#             spec___ = time_stch(spec__)\n#             img = spec2img(spec)\n#             img.save(f'/kaggle/train_images/{bird.stem}/{f.stem}_tm{x}_{end_sec}.png')\n\ndef cut_wvfrm(wvfrm, sr, step):\n    start_sec, end_sec = step, step + 5\n    return wvfrm[:, start_sec * sr: end_sec * sr]\n            \ndef create_spec(wvfrm):\n    spec = T.Spectrogram()(wvfrm)\n    return T.AmplitudeToDB()(spec)\n        \ndef time_mask(spec, param=90):\n    return T.TimeMasking(time_mask_param=param)(spec)\n\ndef freq_mask(spec, param=90):\n    return T.FrequencyMasking(freq_mask_param=param)(spec)\n\ndef time_stch(spec, up_rate=2):\n    rate = torch.rand(1) * up_rate\n    while rate == 0 or rate == 1 or rate == 2:\n        rate = torch.rand(1) * up_rate\n    return T.TimeStretch()(spec, rate.item())\n        \ndef spec2img(spec, img_size=(512, 512)):\n    spec = np.real(spec.squeeze().numpy())\n    spec = ((spec - spec.min()) / (spec.max() - spec.min()) * 255).astype('uint8')\n    return Image.fromarray(spec).resize(img_size)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T08:50:02.952212Z","iopub.execute_input":"2023-04-01T08:50:02.952645Z","iopub.status.idle":"2023-04-01T08:50:02.968387Z","shell.execute_reply.started":"2023-04-01T08:50:02.952609Z","shell.execute_reply":"2023-04-01T08:50:02.966830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nfor bird in aud_files.ls().sorted():\n    mkdir(f'/kaggle/train_images/{bird.stem}', exist_ok=True)\n    Path(f'/kaggle/train_images/').ls()\n    for f in bird.ls().sorted():\n        info = torchaudio.info(f)\n        duration = info.num_frames / info.sample_rate\n        if duration >= 15:\n            create_images(15, f)\n        elif duration >= 10:\n            create_images(10, f)\n        elif duration >= 5:\n            create_images(5, f)\n        else: continue\n    gc.collect()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## API Setup","metadata":{}},{"cell_type":"code","source":"import os\nfrom kaggle_secrets import UserSecretsClient","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"secrets = UserSecretsClient()\nos.environ['KAGGLE_USERNAME'] = secrets.get_secret('KAGGLE_USERNAME')\nos.environ['KAGGLE_KEY'] = secrets.get_secret('KAGGLE_KEY')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Push Dataset","metadata":{}},{"cell_type":"code","source":"mk_dataset('/kaggle/train_images', 'spectrograms-birdclef-2023', force=True, upload=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! cat /kaggle/train_images/dataset-metadata.json","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}