{"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":"## Setup","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"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-02-21T05:03:17.659702Z","iopub.execute_input":"2023-02-21T05:03:17.660377Z","iopub.status.idle":"2023-02-21T05:03:31.575974Z","shell.execute_reply.started":"2023-02-21T05:03:17.660287Z","shell.execute_reply":"2023-02-21T05:03:31.57469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"comp = 'birdclef-2022'\nd_path = setup_comp(comp, install='fastai nbdev timm torch torchvision torchaudio')","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:03:31.57817Z","iopub.execute_input":"2023-02-21T05:03:31.578973Z","iopub.status.idle":"2023-02-21T05:05:25.288988Z","shell.execute_reply.started":"2023-02-21T05:03:31.578936Z","shell.execute_reply":"2023-02-21T05:05:25.28805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.imports import *\nfrom fastai.vision.all import *\n\nnp.set_printoptions(linewidth=140)\ntorch.set_printoptions(linewidth=140, sci_mode=False, edgeitems=7)\npd.set_option('display.width', 140)\npd.options.display.float_format = '{:.2f}'.format","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:05:25.290366Z","iopub.execute_input":"2023-02-21T05:05:25.290709Z","iopub.status.idle":"2023-02-21T05:05:29.300649Z","shell.execute_reply.started":"2023-02-21T05:05:25.290678Z","shell.execute_reply":"2023-02-21T05:05:29.299505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if torch.backends.mps.is_available():\n    device = torch.device('mps')\nelif torch.cuda.is_available():\n    device = torch.device('cuda')\nelse:\n    device = torch.device('cpu')\n\ndefault_device(device)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:05:29.30287Z","iopub.execute_input":"2023-02-21T05:05:29.303257Z","iopub.status.idle":"2023-02-21T05:05:29.312139Z","shell.execute_reply.started":"2023-02-21T05:05:29.303225Z","shell.execute_reply":"2023-02-21T05:05:29.311091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Augment all Audio Files","metadata":{}},{"cell_type":"code","source":"mkdir('/kaggle/train_specs')\nimg_path = Path('/kaggle/train_specs'); img_path.exists()","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:51.784648Z","iopub.execute_input":"2023-02-21T05:16:51.785111Z","iopub.status.idle":"2023-02-21T05:16:51.79947Z","shell.execute_reply.started":"2023-02-21T05:16:51.785069Z","shell.execute_reply":"2023-02-21T05:16:51.798418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_path = d_path/'train_audio'; trn_path","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:51.801814Z","iopub.execute_input":"2023-02-21T05:16:51.802449Z","iopub.status.idle":"2023-02-21T05:16:51.810461Z","shell.execute_reply.started":"2023-02-21T05:16:51.802412Z","shell.execute_reply":"2023-02-21T05:16:51.809645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for p in trn_path.ls().sorted():\n    mkdir(img_path/p.stem, parents=True, exist_ok=True, overwrite=True)\n\nimg_path.ls()","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:51.811608Z","iopub.execute_input":"2023-02-21T05:16:51.812265Z","iopub.status.idle":"2023-02-21T05:16:51.8446Z","shell.execute_reply.started":"2023-02-21T05:16:51.812231Z","shell.execute_reply":"2023-02-21T05:16:51.843746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchaudio\nimport torchaudio.transforms as T","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:51.845807Z","iopub.execute_input":"2023-02-21T05:16:51.846817Z","iopub.status.idle":"2023-02-21T05:16:52.562918Z","shell.execute_reply.started":"2023-02-21T05:16:51.846782Z","shell.execute_reply":"2023-02-21T05:16:52.561289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_spec(wvfrm):\n    s = T.Spectrogram(n_fft=2048)(wvfrm)\n    s_db = T.AmplitudeToDB()(s)\n    return (s_db + torch.abs(s_db.min())).log1p()","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:52.565236Z","iopub.execute_input":"2023-02-21T05:16:52.565711Z","iopub.status.idle":"2023-02-21T05:16:52.5735Z","shell.execute_reply.started":"2023-02-21T05:16:52.565659Z","shell.execute_reply":"2023-02-21T05:16:52.571765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time_stretch(spec):\n    rate = 0.\n    while rate == 0.:\n        rate = (0 - 2) * torch.rand(1) + 2\n    \n    trans = T.TimeStretch(n_freq=spec.shape[1])\n    return trans(spec, rate.item()).real","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:52.57548Z","iopub.execute_input":"2023-02-21T05:16:52.576301Z","iopub.status.idle":"2023-02-21T05:16:52.586367Z","shell.execute_reply.started":"2023-02-21T05:16:52.576252Z","shell.execute_reply":"2023-02-21T05:16:52.58506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time_mask(spec):\n    mask = T.TimeMasking(time_mask_param=torch.randint(10, 101, (1, 1)).item())\n    return mask(spec)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:52.588419Z","iopub.execute_input":"2023-02-21T05:16:52.589237Z","iopub.status.idle":"2023-02-21T05:16:52.600177Z","shell.execute_reply.started":"2023-02-21T05:16:52.589187Z","shell.execute_reply":"2023-02-21T05:16:52.598738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def freq_mask(spec):\n    mask = T.FrequencyMasking(freq_mask_param=torch.randint(10, 101, (1, 1)).item())\n    return mask(spec)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:52.605075Z","iopub.execute_input":"2023-02-21T05:16:52.606063Z","iopub.status.idle":"2023-02-21T05:16:52.612691Z","shell.execute_reply.started":"2023-02-21T05:16:52.605987Z","shell.execute_reply":"2023-02-21T05:16:52.611553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def aug_spec(spec, aud_file, path):\n    # 2 random time stretches.\n    for i in range(1, 3):\n        torch.save(f'{path}/{aud_file.stem}-time_stretch_{i}.pt', time_stretch(spec)[0, :, :])\n    \n    # 2 random time masks.\n    for i in range(1, 3):\n        torch.save(f'{path}/{aud_file.stem}-time_mask_{i}.pt', time_mask(spec)[0, :, :])\n        \n    # 2 random frequency masks.\n    for i in range(1, 3):\n        torch.save(f'{path}/{aud_file.stem}-freq_mask_{i}.pt', freq_mask(spec)[0, :, :])\n    \n    # 3 random combos of all augmentations.\n    for i in range(1, 4):\n        torch.save(f'{path}/{aud_file.stem}-all_{i}.pt', freq_mask(time_mask(time_stretch(spec)))[0, :, :])","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:52.614484Z","iopub.execute_input":"2023-02-21T05:16:52.615233Z","iopub.status.idle":"2023-02-21T05:16:52.627046Z","shell.execute_reply.started":"2023-02-21T05:16:52.615187Z","shell.execute_reply":"2023-02-21T05:16:52.625842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def aud2spec(aud):\n    wvfrm, _ = torchaudio.load(aud)\n    s = create_spec(wvfrm)\n    p = Path(img_path/aud.parent.stem)\n    aug_spec(s, aud, p)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:16:52.628456Z","iopub.execute_input":"2023-02-21T05:16:52.629445Z","iopub.status.idle":"2023-02-21T05:16:52.644391Z","shell.execute_reply.started":"2023-02-21T05:16:52.629387Z","shell.execute_reply":"2023-02-21T05:16:52.643146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Dataset","metadata":{}},{"cell_type":"code","source":"birds = (file for file in trn_path.ls().sorted())","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:18:59.385374Z","iopub.execute_input":"2023-02-21T05:18:59.385918Z","iopub.status.idle":"2023-02-21T05:18:59.397671Z","shell.execute_reply.started":"2023-02-21T05:18:59.385827Z","shell.execute_reply":"2023-02-21T05:18:59.39648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for bird in birds:\n    for file in Path(bird).ls().sorted():\n        aud2spec(file)","metadata":{"execution":{"iopub.status.busy":"2023-02-21T05:19:02.664203Z","iopub.execute_input":"2023-02-21T05:19:02.665207Z","iopub.status.idle":"2023-02-21T05:21:28.311845Z","shell.execute_reply.started":"2023-02-21T05:19:02.665159Z","shell.execute_reply":"2023-02-21T05:21:28.31003Z"},"trusted":true},"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(img_path, 'augmented-logarithmic-spectrograms-birdclef-2022', force=True, upload=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! cat /kaggle/train_specs/dataset-metadata.json","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}