{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport matplotlib.pyplot as plt\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-16T16:46:58.446201Z","iopub.execute_input":"2022-02-16T16:46:58.446504Z","iopub.status.idle":"2022-02-16T16:46:58.451322Z","shell.execute_reply.started":"2022-02-16T16:46:58.446472Z","shell.execute_reply":"2022-02-16T16:46:58.450418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch import nn, optim\nimport torchvision\nimport torchaudio\nfrom torchvision import transforms\nfrom torch.utils.data import DataLoader, Dataset, random_split\nimport glob","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:02:25.803279Z","iopub.execute_input":"2022-02-16T17:02:25.803624Z","iopub.status.idle":"2022-02-16T17:02:25.809636Z","shell.execute_reply.started":"2022-02-16T17:02:25.803579Z","shell.execute_reply":"2022-02-16T17:02:25.809069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfms = [\n    torchaudio.transforms.MelSpectrogram(n_mels=64),\n    transforms.Resize((64, 512))\n]","metadata":{"execution":{"iopub.status.busy":"2022-02-16T16:59:47.111884Z","iopub.execute_input":"2022-02-16T16:59:47.112218Z","iopub.status.idle":"2022-02-16T16:59:47.117673Z","shell.execute_reply.started":"2022-02-16T16:59:47.112186Z","shell.execute_reply":"2022-02-16T16:59:47.117066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/birdclef-2022/train_audio/\"","metadata":{"execution":{"iopub.status.busy":"2022-02-16T16:59:47.933219Z","iopub.execute_input":"2022-02-16T16:59:47.933512Z","iopub.status.idle":"2022-02-16T16:59:47.937784Z","shell.execute_reply.started":"2022-02-16T16:59:47.933478Z","shell.execute_reply":"2022-02-16T16:59:47.936688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = glob.glob(path + \"*/*\")\nlen(files)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T16:59:48.082434Z","iopub.execute_input":"2022-02-16T16:59:48.082758Z","iopub.status.idle":"2022-02-16T16:59:48.396238Z","shell.execute_reply.started":"2022-02-16T16:59:48.082728Z","shell.execute_reply":"2022-02-16T16:59:48.395564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AudioDataset(Dataset):\n    def __init__(self, root, transforms=None):\n        self.root = root\n        self.transforms = transforms\n        self.classes = os.listdir(root)\n        self.files = glob.glob(path + \"*/*\")\n    def __len__(self):\n        return len(self.files)\n    def __getitem__(self, idx):\n        file = self.files[idx]\n        class_ = file.split(\"/\")[-2]\n        audio_mono = torchaudio.load(file, normalize = True)[0]\n        audio_mono = torch.mean(audio_mono, dim=0).unsqueeze(0)\n        if self.transforms is not None:\n            for tf in self.transforms:\n                audio_mono = tf(audio_mono)\n        return audio_mono, class_\n        ","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:00:12.454733Z","iopub.execute_input":"2022-02-16T17:00:12.455059Z","iopub.status.idle":"2022-02-16T17:00:12.464089Z","shell.execute_reply.started":"2022-02-16T17:00:12.455022Z","shell.execute_reply":"2022-02-16T17:00:12.462912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = AudioDataset(path, transforms = tfms)\ntrain_data_len = int(len(dataset)*0.75)\nvalid_data_len = int((len(dataset) - train_data_len))","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:00:13.187763Z","iopub.execute_input":"2022-02-16T17:00:13.188218Z","iopub.status.idle":"2022-02-16T17:00:13.384021Z","shell.execute_reply.started":"2022-02-16T17:00:13.188179Z","shell.execute_reply":"2022-02-16T17:00:13.383119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[6][0].shape","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:00:22.212240Z","iopub.execute_input":"2022-02-16T17:00:22.212503Z","iopub.status.idle":"2022-02-16T17:00:22.285195Z","shell.execute_reply.started":"2022-02-16T17:00:22.212477Z","shell.execute_reply":"2022-02-16T17:00:22.284313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data, val_data = random_split(dataset, [train_data_len, valid_data_len])","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:00:23.457891Z","iopub.execute_input":"2022-02-16T17:00:23.458606Z","iopub.status.idle":"2022-02-16T17:00:23.463439Z","shell.execute_reply.started":"2022-02-16T17:00:23.458567Z","shell.execute_reply":"2022-02-16T17:00:23.462533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_data, batch_size=32, shuffle=True, num_workers=4)\nval_loader = DataLoader(val_data, batch_size=32, shuffle=True, num_workers=4)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:00:23.566653Z","iopub.execute_input":"2022-02-16T17:00:23.567460Z","iopub.status.idle":"2022-02-16T17:00:23.573406Z","shell.execute_reply.started":"2022-02-16T17:00:23.567409Z","shell.execute_reply":"2022-02-16T17:00:23.572627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch = next(iter(train_loader))","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:00:57.257772Z","iopub.execute_input":"2022-02-16T17:00:57.258328Z","iopub.status.idle":"2022-02-16T17:01:08.525475Z","shell.execute_reply.started":"2022-02-16T17:00:57.258281Z","shell.execute_reply":"2022-02-16T17:01:08.524600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_classes = len(dataset.classes)\nn_classes","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:01:24.506322Z","iopub.execute_input":"2022-02-16T17:01:24.506751Z","iopub.status.idle":"2022-02-16T17:01:24.512448Z","shell.execute_reply.started":"2022-02-16T17:01:24.506720Z","shell.execute_reply":"2022-02-16T17:01:24.511838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torchvision.models.densenet121(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:02:25.854795Z","iopub.execute_input":"2022-02-16T17:02:25.855028Z","iopub.status.idle":"2022-02-16T17:02:27.542357Z","shell.execute_reply.started":"2022-02-16T17:02:25.854986Z","shell.execute_reply":"2022-02-16T17:02:27.541404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for param in model.parameters():\n    param.requires_grad = False\nn_in = model.classifier.in_features\nmodel.classifier = nn.Linear(n_in, n_classes)\nprint(model.classifier)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:02:56.376118Z","iopub.execute_input":"2022-02-16T17:02:56.376809Z","iopub.status.idle":"2022-02-16T17:02:56.386930Z","shell.execute_reply.started":"2022-02-16T17:02:56.376770Z","shell.execute_reply":"2022-02-16T17:02:56.385938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.AdamW(model.classifier.parameters(), lr=0.001)\nlr_scheduler = optim.lr_scheduler.StepLR(optimizer, 3, gamma=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:03:51.631150Z","iopub.execute_input":"2022-02-16T17:03:51.631427Z","iopub.status.idle":"2022-02-16T17:03:51.637262Z","shell.execute_reply.started":"2022-02-16T17:03:51.631399Z","shell.execute_reply":"2022-02-16T17:03:51.636212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}