{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":44224,"databundleVersionId":5188730,"sourceType":"competition"},{"sourceId":5148212,"sourceType":"datasetVersion","datasetId":2991134},{"sourceId":121796742,"sourceType":"kernelVersion"},{"sourceId":160024677,"sourceType":"kernelVersion"}],"dockerImageVersionId":30635,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport torch\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms\nimport torchvision.transforms as transforms\nfrom torch import nn, optim\nfrom torch.utils.data import DataLoader\nfrom tqdm import tqdm\nfrom torch.utils.data import random_split\nimport pytorch_lightning as pl\nimport torchmetrics\nfrom torchmetrics import Metric\nimport timm\nimport torch.nn as nn\nimport pandas as pd\nimport numpy as np\nfrom pathlib import Path","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-22T18:42:07.053861Z","iopub.execute_input":"2024-01-22T18:42:07.054491Z","iopub.status.idle":"2024-01-22T18:42:07.068770Z","shell.execute_reply.started":"2024-01-22T18:42:07.054443Z","shell.execute_reply":"2024-01-22T18:42:07.066829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    num_classes = 264\n    batch_size = 12\n    PRECISION = 16    \n    seed = 2023\n    model = \"resnet50\"\n    pretrained = False\n    use_mixup = False\n    mixup_alpha = 0.2   \n    DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')    \n\n    data_root = \"/kaggle/input/birdclef-2023/\"\n    train_images = \"/kaggle/input/split-creating-melspecs-stage-1/specs/train/\"\n    valid_images = \"/kaggle/input/split-creating-melspecs-stage-1/specs/valid/\"\n    train_path = \"/kaggle/input/bc2023-train-val-df/train.csv\"\n    valid_path = \"/kaggle/input/bc2023-train-val-df/valid.csv\"\n    \n    test_path = '/kaggle/input/birdclef-2023/test_soundscapes/'\n    SR = 32000\n    DURATION = 5\n    LR = 5e-4\n    \n    model_ckpt = '/kaggle/input/birdclef23-supervised-contrastive-loss-training/birdclef_supconmodel.ckpt'","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.073049Z","iopub.execute_input":"2024-01-22T18:42:07.073769Z","iopub.status.idle":"2024-01-22T18:42:07.086368Z","shell.execute_reply.started":"2024-01-22T18:42:07.073715Z","shell.execute_reply":"2024-01-22T18:42:07.084662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pl.seed_everything(Config.seed, workers=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.089229Z","iopub.execute_input":"2024-01-22T18:42:07.090286Z","iopub.status.idle":"2024-01-22T18:42:07.107286Z","shell.execute_reply.started":"2024-01-22T18:42:07.090235Z","shell.execute_reply":"2024-01-22T18:42:07.105871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def config_to_dict(cfg):\n    return dict((name, getattr(cfg, name)) for name in dir(cfg) if not name.startswith('__'))","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.111237Z","iopub.execute_input":"2024-01-22T18:42:07.112443Z","iopub.status.idle":"2024-01-22T18:42:07.120250Z","shell.execute_reply.started":"2024-01-22T18:42:07.112395Z","shell.execute_reply":"2024-01-22T18:42:07.118530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def compute_melspec(y, sr, n_mels, fmin, fmax):\n    \"\"\"\n    Computes a mel-spectrogram and puts it at decibel scale\n    Arguments:\n        y {np array} -- signal\n        params {AudioParams} -- Parameters to use for the spectrogram. Expected to have the attributes sr, n_mels, f_min, f_max\n    Returns:\n        np array -- Mel-spectrogram\n    \"\"\"\n    melspec = lb.feature.melspectrogram(\n        y=y, sr=sr, n_mels=n_mels, fmin=fmin, fmax=fmax,\n    )\n\n    melspec = lb.power_to_db(melspec).astype(np.float32)\n    return melspec\n\ndef mono_to_color(X, eps=1e-6, mean=None, std=None):\n    mean = mean or X.mean()\n    std = std or X.std()\n    X = (X - mean) / (std + eps)\n    \n    _min, _max = X.min(), X.max()\n\n    if (_max - _min) > eps:\n        V = np.clip(X, _min, _max)\n        V = 255 * (V - _min) / (_max - _min)\n        V = V.astype(np.uint8)\n    else:\n        V = np.zeros_like(X, dtype=np.uint8)\n\n    return V\n\ndef crop_or_pad(y, length, is_train=True, start=None):\n    if len(y) < length:\n        y = np.concatenate([y, np.zeros(length - len(y))])\n        \n        n_repeats = length // len(y)\n        epsilon = length % len(y)\n        \n        y = np.concatenate([y]*n_repeats + [y[:epsilon]])\n        \n    elif len(y) > length:\n        if not is_train:\n            start = start or 0\n        else:\n            start = start or np.random.randint(len(y) - length)\n\n        y = y[start:start + length]\n\n    return y","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.121902Z","iopub.execute_input":"2024-01-22T18:42:07.123108Z","iopub.status.idle":"2024-01-22T18:42:07.140159Z","shell.execute_reply.started":"2024-01-22T18:42:07.123062Z","shell.execute_reply":"2024-01-22T18:42:07.138864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(Config.train_path)\nConfig.num_classes = len(df_train.primary_label.unique())","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.143912Z","iopub.execute_input":"2024-01-22T18:42:07.145066Z","iopub.status.idle":"2024-01-22T18:42:07.290008Z","shell.execute_reply.started":"2024-01-22T18:42:07.145016Z","shell.execute_reply":"2024-01-22T18:42:07.288547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for path in Path(Config.test_path).glob(\"*.ogg\"):\n    print(path)\n    print(path.stem)\n    print(path.stem.split(\"_\"))","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.291614Z","iopub.execute_input":"2024-01-22T18:42:07.292008Z","iopub.status.idle":"2024-01-22T18:42:07.301487Z","shell.execute_reply.started":"2024-01-22T18:42:07.291976Z","shell.execute_reply":"2024-01-22T18:42:07.300349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.DataFrame(\n     [(path.stem, *path.stem.split(\"_\"), path) for path in Path(Config.test_path).glob(\"*.ogg\")],\n    columns = [\"filename\", \"name\" ,\"id\", \"path\"]\n)\nprint(df_test.shape)\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.303803Z","iopub.execute_input":"2024-01-22T18:42:07.304623Z","iopub.status.idle":"2024-01-22T18:42:07.331486Z","shell.execute_reply.started":"2024-01-22T18:42:07.304579Z","shell.execute_reply":"2024-01-22T18:42:07.329861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa as lb\nimport librosa.display as lbd\nimport soundfile as sf\nfrom  soundfile import SoundFile \nfrom torch.utils.data import Dataset, DataLoader\n\nclass BirdDataset(Dataset):\n    def __init__(self, data, sr=Config.SR, n_mels=128, fmin=0, fmax=None, duration=Config.DURATION, step=None, res_type=\"kaiser_fast\", resample=True):\n        \n        self.data = data\n        \n        self.sr = sr\n        self.n_mels = n_mels\n        self.fmin = fmin\n        self.fmax = fmax or self.sr//2\n\n        self.duration = duration\n        self.audio_length = self.duration*self.sr\n        self.step = step or self.audio_length\n        \n        self.res_type = res_type\n        self.resample = resample\n\n    def __len__(self):\n        return len(self.data)\n    \n    @staticmethod\n    def normalize(image):\n        image = image.astype(\"float32\", copy=False) / 255.0\n        image = np.stack([image, image, image])\n        return image\n    \n    \n    def audio_to_image(self, audio):\n        melspec = compute_melspec(audio, self.sr, self.n_mels, self.fmin, self.fmax) \n        image = mono_to_color(melspec)\n        image = self.normalize(image)\n        return image\n\n    def read_file(self, filepath):\n        audio, orig_sr = sf.read(filepath, dtype=\"float32\")\n\n        if self.resample and orig_sr != self.sr:\n            audio = lb.resample(audio, orig_sr, self.sr, res_type=self.res_type)\n          \n        audios = []\n        for i in range(self.audio_length, len(audio) + self.step, self.step):\n            start = max(0, i - self.audio_length)\n            end = start + self.audio_length\n            audios.append(audio[start:end])\n            \n        if len(audios[-1]) < self.audio_length:\n            audios = audios[:-1]\n            \n        images = [self.audio_to_image(audio) for audio in audios]\n        images = np.stack(images)\n        \n        return images\n    \n        \n    def __getitem__(self, idx):\n        return self.read_file(self.data.loc[idx, \"path\"])","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.334059Z","iopub.execute_input":"2024-01-22T18:42:07.334575Z","iopub.status.idle":"2024-01-22T18:42:07.352330Z","shell.execute_reply.started":"2024-01-22T18:42:07.334529Z","shell.execute_reply":"2024-01-22T18:42:07.351483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_test = BirdDataset(\n    df_test, \n    sr = Config.SR,\n    duration = Config.DURATION,\n)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.356034Z","iopub.execute_input":"2024-01-22T18:42:07.357407Z","iopub.status.idle":"2024-01-22T18:42:07.369525Z","shell.execute_reply.started":"2024-01-22T18:42:07.357318Z","shell.execute_reply":"2024-01-22T18:42:07.368426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_test[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:07.370838Z","iopub.execute_input":"2024-01-22T18:42:07.372283Z","iopub.status.idle":"2024-01-22T18:42:12.627781Z","shell.execute_reply.started":"2024-01-22T18:42:07.372235Z","shell.execute_reply":"2024-01-22T18:42:12.626108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.metrics\n\ndef padded_cmap(solution, submission, padding_factor=5):\n    solution = solution#.drop(['row_id'], axis=1, errors='ignore')\n    submission = submission#.drop(['row_id'], axis=1, errors='ignore')\n    new_rows = []\n    for i in range(padding_factor):\n        new_rows.append([1 for i in range(len(solution.columns))])\n    new_rows = pd.DataFrame(new_rows)\n    new_rows.columns = solution.columns\n    padded_solution = pd.concat([solution, new_rows]).reset_index(drop=True).copy()\n    padded_submission = pd.concat([submission, new_rows]).reset_index(drop=True).copy()\n    score = sklearn.metrics.average_precision_score(\n        padded_solution.values,\n        padded_submission.values,\n        average='macro',\n    )\n    return score","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:12.630184Z","iopub.execute_input":"2024-01-22T18:42:12.641775Z","iopub.status.idle":"2024-01-22T18:42:12.668852Z","shell.execute_reply.started":"2024-01-22T18:42:12.641665Z","shell.execute_reply":"2024-01-22T18:42:12.667053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SupConLoss: https://github.com/HobbitLong/SupContrast/blob/master/losses.py\nclass SupConLoss(nn.Module):\n    \"\"\"Supervised Contrastive Learning: https://arxiv.org/pdf/2004.11362.pdf.\n    It also supports the unsupervised contrastive loss in SimCLR\"\"\"\n    def __init__(self, temperature=0.07, contrast_mode='all',\n                 base_temperature=0.07):\n        super(SupConLoss, self).__init__()\n        self.temperature = temperature\n        self.contrast_mode = contrast_mode\n        self.base_temperature = base_temperature\n\n    def forward(self, features, labels=None, mask=None):\n        \"\"\"Compute loss for model. If both `labels` and `mask` are None,\n        it degenerates to SimCLR unsupervised loss:\n        https://arxiv.org/pdf/2002.05709.pdf\n\n        Args:\n            features: hidden vector of shape [bsz, n_views, ...].\n            labels: ground truth of shape [bsz].\n            mask: contrastive mask of shape [bsz, bsz], mask_{i,j}=1 if sample j\n                has the same class as sample i. Can be asymmetric.\n        Returns:\n            A loss scalar.\n        \"\"\"\n        device = (torch.device('cuda')\n                  if features.is_cuda\n                  else torch.device('cpu'))\n\n        if len(features.shape) < 3:\n            raise ValueError('`features` needs to be [bsz, n_views, ...],'\n                             'at least 3 dimensions are required')\n        if len(features.shape) > 3:\n            features = features.view(features.shape[0], features.shape[1], -1)\n\n        batch_size = features.shape[0]\n        if labels is not None and mask is not None:\n            raise ValueError('Cannot define both `labels` and `mask`')\n        elif labels is None and mask is None:\n            mask = torch.eye(batch_size, dtype=torch.float32).to(device)\n        elif labels is not None:\n            labels = labels.contiguous().view(-1, 1)\n            if labels.shape[0] != batch_size:\n                raise ValueError('Num of labels does not match num of features')\n            mask = torch.eq(labels, labels.T).float().to(device)\n        else:\n            mask = mask.float().to(device)\n\n        contrast_count = features.shape[1]\n        contrast_feature = torch.cat(torch.unbind(features, dim=1), dim=0)\n        if self.contrast_mode == 'one':\n            anchor_feature = features[:, 0]\n            anchor_count = 1\n        elif self.contrast_mode == 'all':\n            anchor_feature = contrast_feature\n            anchor_count = contrast_count\n        else:\n            raise ValueError('Unknown mode: {}'.format(self.contrast_mode))\n\n        # compute logits\n        anchor_dot_contrast = torch.div(\n            torch.matmul(anchor_feature, contrast_feature.T),\n            self.temperature)\n        # for numerical stability\n        logits_max, _ = torch.max(anchor_dot_contrast, dim=1, keepdim=True)\n        logits = anchor_dot_contrast - logits_max.detach()\n\n        # tile mask\n        mask = mask.repeat(anchor_count, contrast_count)\n        # mask-out self-contrast cases\n        logits_mask = torch.scatter(\n            torch.ones_like(mask),\n            1,\n            torch.arange(batch_size * anchor_count).view(-1, 1).to(device),\n            0\n        )\n        mask = mask * logits_mask\n\n        # compute log_prob\n        exp_logits = torch.exp(logits) * logits_mask\n        log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True))\n\n        # compute mean of log-likelihood over positive\n        # modified to handle edge cases when there is no positive pair\n        # for an anchor point.\n        # Edge case e.g.:-\n        # features of shape: [4,1,...]\n        # labels:            [0,1,1,2]\n        # loss before mean:  [nan, ..., ..., nan]\n        mask_pos_pairs = mask.sum(1)\n        mask_pos_pairs = torch.where(mask_pos_pairs < 1e-6, 1, mask_pos_pairs)\n        mean_log_prob_pos = (mask * log_prob).sum(1) / mask_pos_pairs\n\n        # loss\n        loss = - (self.temperature / self.base_temperature) * mean_log_prob_pos\n        loss = loss.view(anchor_count, batch_size).mean()\n\n        return loss","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:12.671605Z","iopub.execute_input":"2024-01-22T18:42:12.674748Z","iopub.status.idle":"2024-01-22T18:42:12.712704Z","shell.execute_reply.started":"2024-01-22T18:42:12.674630Z","shell.execute_reply":"2024-01-22T18:42:12.711184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Encoder(pl.LightningModule):\n    def __init__(self, model_name, emb_dim):\n        super().__init__()\n        self.backbone = timm.create_model(\"resnet50\", pretrained = False)\n        self.in_features = self.backbone.fc.in_features\n        self.backbone.fc = nn.Linear(self.in_features, emb_dim)\n        self.loss_fn = SupConLoss(0.07, 'one', 0.07)\n\n\n    def forward(self, x):\n        emb = self.backbone(x)\n        return emb\n\n    # Difference between Normal and Lightning: The train, valid and test steps is written here inside the class\n    def training_step(self, batch, batch_idx):\n        images, labels = batch\n\n        bsz = len(labels)\n\n        images = torch.cat([images[0], images[1]], dim=0)\n\n        #print(images.shape)\n\n        features = self.forward(images)\n\n        # Manipulating the features for SupConLoss\n        f1, f2 = torch.split(features, [bsz, bsz], dim=0)\n        features = torch.cat([f1.unsqueeze(1), f2.unsqueeze(1)], dim=1)\n\n        # Calculating SupConLoss\n        loss = self.loss_fn(features,labels)\n\n        return loss\n\n    # We have training_epoch_end function\n    # def on_train_epoch_end(self):\n    #     #print(\"Epoch Done\")\n\n    def validation_step(self, batch , batch_idx):\n\n        images, labels = batch\n\n        bsz = len(labels)\n\n        images = torch.cat([images[0], images[1]], dim=0)\n        \n        #print(images.shape)\n\n        features = self.forward(images)\n\n        # Manipulating the arrangment of features for SupConLoss\n        f1, f2 = torch.split(features, [bsz, bsz], dim=0)\n\n        features = torch.cat([f1.unsqueeze(1), f2.unsqueeze(1)], dim=1)\n\n        # Calculating SupConLoss\n        loss = self.loss_fn(features,labels)\n\n        return loss\n\n    def test_step(self, batch, batch_idx):\n        images, labels = batch\n        \n        bsz = len(labels)\n\n        images = torch.cat([images[0], images[1]], dim=0)\n        \n\n        features = self.forward(images)\n\n        # Manipulating the arrangment of features for SupConLoss\n        f1, f2 = torch.split(features, [bsz, bsz], dim=0)\n\n        features = torch.cat([f1.unsqueeze(1), f2.unsqueeze(1)], dim=1)\n\n        # Calculating SupConLoss\n        loss = self.loss_fn(features,labels)\n        return loss\n\n    # We can add schedulers to this method\n    def configure_optimizers(self):\n        return optim.Adam(self.parameters(), lr = 0.001)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:12.715424Z","iopub.execute_input":"2024-01-22T18:42:12.716398Z","iopub.status.idle":"2024-01-22T18:42:12.738814Z","shell.execute_reply.started":"2024-01-22T18:42:12.716346Z","shell.execute_reply":"2024-01-22T18:42:12.737301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport pickle\nimport pytorch_lightning as pl\nfrom torch.optim import Adam\n\n\n\nclass SupConCE(pl.LightningModule):\n    def __init__(self,):\n        super().__init__()\n        self.loss_fn = nn.CrossEntropyLoss()\n        self.accuracy = torchmetrics.Accuracy(task = 'multiclass', num_classes = 264)\n        self.f1_score = torchmetrics.F1Score(task = 'multiclass', num_classes = 264)\n        backbone = 'resnet50'\n        model_path = '/kaggle/input/birdclef23-supervised-contrastive-loss-training/birdclef_supconencoder.ckpt'\n        pretrained_model = Encoder.load_from_checkpoint(model_path, model_name = backbone, emb_dim = 128)\n\n\n        #Freezing all the encoder layers\n        for param in pretrained_model.parameters():\n            param.requires_grad = False\n\n\n        #Trainging only the last layer\n        pretrained_model.backbone.fc = nn.Linear(in_features=pretrained_model.backbone.fc.in_features, out_features=264)\n\n        pretrained_model.backbone.fc.requires_grad = True\n\n        self.model = pretrained_model\n\n\n    def forward(self, x):\n        logits = self.model(x)\n        return logits\n\n    def training_step(self, batch, batch_idx):\n        images, labels = batch\n        y_pred = self.forward(images)\n        loss = self.loss_fn(y_pred,labels)\n        accuracy = self.accuracy(y_pred,labels)\n        f1_score = self.f1_score(y_pred,labels)\n        self.log_dict({'train_loss': loss, 'train_accuracy': accuracy, 'train_f1_score': f1_score},\n                      on_step = False, on_epoch = True, prog_bar = True)\n        return loss\n\n    def validation_step(self, batch, batch_idx):\n        images, labels = batch\n        y_pred = self.forward(images)\n        loss = self.loss_fn(y_pred,labels)\n        accuracy = self.accuracy(y_pred,labels)\n        f1_score = self.f1_score(y_pred,labels)\n        \n        one_hot_target = F.one_hot(labels, num_classes=264)\n        \n        y_pred = pd.DataFrame(y_pred.cpu().detach().numpy())\n        y_true = pd.DataFrame(one_hot_target.cpu().detach().numpy())\n        \n        cmap_score = padded_cmap(y_true, y_pred)\n        \n        self.log_dict({'valid_loss': loss, 'valid_accuracy': accuracy, 'valid_f1_score': f1_score, 'cmap_score': cmap_score},\n                      on_step = False, on_epoch = True, prog_bar = True)\n        return loss\n\n    def configure_optimizers(self):\n        return optim.Adam(self.parameters(), lr = 0.001)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:12.740508Z","iopub.execute_input":"2024-01-22T18:42:12.740874Z","iopub.status.idle":"2024-01-22T18:42:12.760007Z","shell.execute_reply.started":"2024-01-22T18:42:12.740844Z","shell.execute_reply":"2024-01-22T18:42:12.758670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(data_loader, model):\n        \n    model.to('cpu')\n    model.eval()    \n    predictions = []\n    for en in range(len(ds_test)):\n        print(en)\n        images = torch.from_numpy(ds_test[en])\n        print(images.shape)\n        with torch.no_grad():\n            outputs = model(images).sigmoid().detach().cpu().numpy()\n            print(outputs.shape)\n#             pred_batch.extend(outputs.detach().cpu().numpy())\n#         pred_batch = np.vstack(pred_batch)\n        predictions.append(outputs)\n            \n    \n    return predictions","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:12.762028Z","iopub.execute_input":"2024-01-22T18:42:12.762696Z","iopub.status.idle":"2024-01-22T18:42:12.779714Z","shell.execute_reply.started":"2024-01-22T18:42:12.762647Z","shell.execute_reply":"2024-01-22T18:42:12.778383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\n\nprint(f\"Create Dataloader...\")\n\nds_test = BirdDataset(\n    df_test, \n    sr = Config.SR,\n    duration = Config.DURATION,\n)\n\n\naudio_model = SupConCE()\n\nprint(\"Model Creation\")\n\nmodel = SupConCE.load_from_checkpoint(Config.model_ckpt, train_dataloader=None,validation_dataloader=None) \nprint(\"Running Inference..\")\n\npreds = predict(ds_test, model)   \n\n#gc.collect()\n#torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:12.781343Z","iopub.execute_input":"2024-01-22T18:42:12.781706Z","iopub.status.idle":"2024-01-22T18:42:36.443527Z","shell.execute_reply.started":"2024-01-22T18:42:12.781676Z","shell.execute_reply":"2024-01-22T18:42:36.441990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = df_test.filename.values.tolist()\n\nbird_cols = list(pd.get_dummies(df_train['primary_label']).columns)\nsub_df = pd.DataFrame(columns=['row_id']+bird_cols)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:36.446926Z","iopub.execute_input":"2024-01-22T18:42:36.448352Z","iopub.status.idle":"2024-01-22T18:42:36.503454Z","shell.execute_reply.started":"2024-01-22T18:42:36.448283Z","shell.execute_reply":"2024-01-22T18:42:36.502487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:36.504950Z","iopub.execute_input":"2024-01-22T18:42:36.505845Z","iopub.status.idle":"2024-01-22T18:42:36.523739Z","shell.execute_reply.started":"2024-01-22T18:42:36.505802Z","shell.execute_reply":"2024-01-22T18:42:36.522319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, file in enumerate(filenames):\n    pred = preds[i]\n    num_rows = len(pred)\n    row_ids = [f'{file}_{(i+1)*5}' for i in range(num_rows)]\n    df = pd.DataFrame(columns=['row_id']+bird_cols)\n    \n    df['row_id'] = row_ids\n    df[bird_cols] = pred\n    \n    sub_df = pd.concat([sub_df,df]).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:36.525201Z","iopub.execute_input":"2024-01-22T18:42:36.526659Z","iopub.status.idle":"2024-01-22T18:42:36.631098Z","shell.execute_reply.started":"2024-01-22T18:42:36.526621Z","shell.execute_reply":"2024-01-22T18:42:36.629516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:36.633216Z","iopub.execute_input":"2024-01-22T18:42:36.633649Z","iopub.status.idle":"2024-01-22T18:42:36.675032Z","shell.execute_reply.started":"2024-01-22T18:42:36.633614Z","shell.execute_reply":"2024-01-22T18:42:36.673449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T18:42:36.676602Z","iopub.execute_input":"2024-01-22T18:42:36.676981Z","iopub.status.idle":"2024-01-22T18:42:36.737886Z","shell.execute_reply.started":"2024-01-22T18:42:36.676949Z","shell.execute_reply":"2024-01-22T18:42:36.736392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}