{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n!cp -r ../input/timm-pytorch-image-models /kaggle/working/\n!pip install /kaggle/working/timm-pytorch-image-models/pytorch-image-models-master/\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:52:36.010377Z","iopub.execute_input":"2023-04-17T09:52:36.010678Z","iopub.status.idle":"2023-04-17T09:53:07.252724Z","shell.execute_reply.started":"2023-04-17T09:52:36.010642Z","shell.execute_reply":"2023-04-17T09:53:07.251799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This notebook is for my graduate thesis. This implementation of Inception v3 has been done along with a literature review of 2019-2022 bird recognition studies. Link to the Inception V3 training routine will be added [[Training notebook](https://github.com/SnibsLabs/thesis_inceptionv3)]\n\nCrediting the sources that helped my code.\n\n1. Calculation of PCEN [[BirdCLEF'22: Per-channel energy normalization](https://www.kaggle.com/code/nlappalainen/birdclef-22-per-channel-energy-normalization/edit)]\n2. Inception training routine [[Fine tuning tutorial, Pytorch docs](https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html)]\n3. Dataloader tutorial by Aladdin Persson [[code](https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master/ML/Pytorch/Basics/custom_dataset/custom_dataset.py)] [[Youtube](https://www.youtube.com/watch?v=ZoZHd0Zm3RY)]\n4. Data submission example [[BirdCLEF'22 submission example](https://www.kaggle.com/code/stefankahl/how-to-submit-to-birdclef-2022)]","metadata":{}},{"cell_type":"code","source":"#import os\n#for dirname, _, filenames in os.walk('/kaggle/input/nlinceptionv3'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:07.255219Z","iopub.execute_input":"2023-04-17T09:53:07.255514Z","iopub.status.idle":"2023-04-17T09:53:07.260705Z","shell.execute_reply.started":"2023-04-17T09:53:07.255470Z","shell.execute_reply":"2023-04-17T09:53:07.259954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/nlinceptionv3 /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:07.261939Z","iopub.execute_input":"2023-04-17T09:53:07.262414Z","iopub.status.idle":"2023-04-17T09:53:08.819058Z","shell.execute_reply.started":"2023-04-17T09:53:07.262379Z","shell.execute_reply":"2023-04-17T09:53:08.817999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/birdclef22-inception-v3-pcen-weights /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:08.822851Z","iopub.execute_input":"2023-04-17T09:53:08.823611Z","iopub.status.idle":"2023-04-17T09:53:15.893862Z","shell.execute_reply.started":"2023-04-17T09:53:08.823550Z","shell.execute_reply":"2023-04-17T09:53:15.892884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"versions should be:\n### conda install pytorch==1.9.1 torchvision==0.10.1 torchaudio==0.9.1 -c pytorch\n\n## 1.9.1+cu102\n## 0.9.1\n## 0.10.1+cu102\n## 1.5.9","metadata":{}},{"cell_type":"code","source":"!python3 -c \"import torch; print(torch.__version__)\"\n!python3 -c \"import torchaudio; print(torchaudio.__version__)\"\n!python3 -c \"import torchvision ; print(torchvision .__version__)\"\n!python3 -c \"import pytorch_lightning ; print(pytorch_lightning .__version__)\"\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:15.895555Z","iopub.execute_input":"2023-04-17T09:53:15.895897Z","iopub.status.idle":"2023-04-17T09:53:24.654413Z","shell.execute_reply.started":"2023-04-17T09:53:15.895846Z","shell.execute_reply":"2023-04-17T09:53:24.653474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 0.10.1","metadata":{}},{"cell_type":"code","source":"!python3 -c \"import torchvision; print(torchvision.__version__)\" #this should be 0.10.1","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:24.656912Z","iopub.execute_input":"2023-04-17T09:53:24.657747Z","iopub.status.idle":"2023-04-17T09:53:26.434942Z","shell.execute_reply.started":"2023-04-17T09:53:24.657705Z","shell.execute_reply":"2023-04-17T09:53:26.433833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### pip install pytorch-lightning==1.5.9\n### pip install ipywidgets","metadata":{}},{"cell_type":"code","source":"from torchvision import transforms\nfrom torch.utils.data import  Dataset, DataLoader\nimport copy","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.436536Z","iopub.execute_input":"2023-04-17T09:53:26.437450Z","iopub.status.idle":"2023-04-17T09:53:26.442643Z","shell.execute_reply.started":"2023-04-17T09:53:26.437409Z","shell.execute_reply":"2023-04-17T09:53:26.441545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nimport warnings\nfrom functools import partial\nfrom contextlib import contextmanager\nimport logging\nfrom pathlib import Path\nimport torch.utils.data as torchdata\nfrom albumentations.core.transforms_interface import ImageOnlyTransform\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.443955Z","iopub.execute_input":"2023-04-17T09:53:26.444624Z","iopub.status.idle":"2023-04-17T09:53:26.458801Z","shell.execute_reply.started":"2023-04-17T09:53:26.444522Z","shell.execute_reply":"2023-04-17T09:53:26.458049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nimport soundfile as sf\nimport librosa\nfrom torchaudio.transforms import AmplitudeToDB, MelSpectrogram\nimport timm\nfrom PIL import Image\nimport torchvision.transforms as T","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.460054Z","iopub.execute_input":"2023-04-17T09:53:26.460456Z","iopub.status.idle":"2023-04-17T09:53:26.471237Z","shell.execute_reply.started":"2023-04-17T09:53:26.460422Z","shell.execute_reply":"2023-04-17T09:53:26.470546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.474917Z","iopub.execute_input":"2023-04-17T09:53:26.475305Z","iopub.status.idle":"2023-04-17T09:53:26.483811Z","shell.execute_reply.started":"2023-04-17T09:53:26.475280Z","shell.execute_reply":"2023-04-17T09:53:26.483021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batchSize():\n    return 64\ndef epocCount():\n    return 50","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.484817Z","iopub.execute_input":"2023-04-17T09:53:26.485065Z","iopub.status.idle":"2023-04-17T09:53:26.496462Z","shell.execute_reply.started":"2023-04-17T09:53:26.485028Z","shell.execute_reply":"2023-04-17T09:53:26.495822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('../input/birdclef-2022/scored_birds.json') as sbfile:\n    scored_birds = json.load(sbfile)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.497793Z","iopub.execute_input":"2023-04-17T09:53:26.498094Z","iopub.status.idle":"2023-04-17T09:53:26.510474Z","shell.execute_reply.started":"2023-04-17T09:53:26.498061Z","shell.execute_reply":"2023-04-17T09:53:26.509720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Imports ok","metadata":{}},{"cell_type":"code","source":"#model_x = timm.create_model('inception_v3', pretrained=True, num_classes=152)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.511362Z","iopub.execute_input":"2023-04-17T09:53:26.511553Z","iopub.status.idle":"2023-04-17T09:53:26.520229Z","shell.execute_reply.started":"2023-04-17T09:53:26.511522Z","shell.execute_reply":"2023-04-17T09:53:26.519573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Write import so that it can be uploaded","metadata":{}},{"cell_type":"code","source":"#DEBUG import\n#from nlinceptionv3.torchvision_2.transforms._presets import ImageClassification #kaggle.working.inceptionV3nl.torchvision_2.transforms._presets\n#from nlinceptionv3.torchvision_2.utils import _log_api_usage_once\n#from nlinceptionv3.torchvision_2.models._api import register_model, Weights, WeightsEnum\n#from nlinceptionv3.torchvision_2.models._meta import _IMAGENET_CATEGORIES\n#from nlinceptionv3.torchvision_2.models._utils import _ovewrite_named_param, handle_legacy_interface","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.521696Z","iopub.execute_input":"2023-04-17T09:53:26.521975Z","iopub.status.idle":"2023-04-17T09:53:26.534554Z","shell.execute_reply.started":"2023-04-17T09:53:26.521942Z","shell.execute_reply":"2023-04-17T09:53:26.533863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nfrom collections import namedtuple\nfrom functools import partial\nfrom typing import Any, Callable, List, Optional, Tuple\n\nimport torch\nimport torch.nn.functional as F\nfrom torch import nn, Tensor\n\nimport torchvision\n#from torchvision_2.transforms._presets import ImageClassification\n#from torchvision_2.utils import _log_api_usage_once\n#from torchvision_2.models._api import register_model, Weights, WeightsEnum\n#from torchvision_2.models._meta import _IMAGENET_CATEGORIES\n#from torchvision_2.models._utils import _ovewrite_named_param, handle_legacy_interface\nfrom nlinceptionv3.torchvision_2.transforms._presets import ImageClassification #kaggle.working.inceptionV3nl.torchvision_2.transforms._presets\nfrom nlinceptionv3.torchvision_2.utils import _log_api_usage_once\nfrom nlinceptionv3.torchvision_2.models._api import register_model, Weights, WeightsEnum\nfrom nlinceptionv3.torchvision_2.models._meta import _IMAGENET_CATEGORIES\nfrom nlinceptionv3.torchvision_2.models._utils import _ovewrite_named_param, handle_legacy_interface\n\n\n__all__ = [\"Inception3\", \"InceptionOutputs\", \"_InceptionOutputs\", \"Inception_V3_Weights\", \"inception_v3\"]\n\n\nInceptionOutputs = namedtuple(\"InceptionOutputs\", [\"logits\", \"aux_logits\"])\nInceptionOutputs.__annotations__ = {\"logits\": Tensor, \"aux_logits\": Optional[Tensor]}\n\n# Script annotations failed with _GoogleNetOutputs = namedtuple ...\n# _InceptionOutputs set here for backwards compat\n_InceptionOutputs = InceptionOutputs\n\n\nclass Inception3(nn.Module):\n    def __init__(\n        self,\n        num_classes: int = 152,\n        aux_logits: bool = True,\n        transform_input: bool = False,\n        inception_blocks: Optional[List[Callable[..., nn.Module]]] = None,\n        init_weights: Optional[bool] = None,\n        dropout: float = 0.5,\n    ) -> None:\n        super().__init__()\n        _log_api_usage_once(self)\n        if inception_blocks is None:\n            inception_blocks = [BasicConv2d, InceptionA, InceptionB, InceptionC, InceptionD, InceptionE, InceptionAux]\n        if init_weights is None:\n            warnings.warn(\n                \"The default weight initialization of inception_v3 will be changed in future releases of \"\n                \"torchvision. If you wish to keep the old behavior (which leads to long initialization times\"\n                \" due to scipy/scipy#11299), please set init_weights=True.\",\n                FutureWarning,\n            )\n            init_weights = True\n        if len(inception_blocks) != 7:\n            raise ValueError(f\"length of inception_blocks should be 7 instead of {len(inception_blocks)}\")\n        conv_block = inception_blocks[0]\n        inception_a = inception_blocks[1]\n        inception_b = inception_blocks[2]\n        inception_c = inception_blocks[3]\n        inception_d = inception_blocks[4]\n        inception_e = inception_blocks[5]\n        inception_aux = inception_blocks[6]\n\n        self.aux_logits = aux_logits\n        self.transform_input = transform_input\n        self.Conv2d_1a_3x3 = conv_block(3, 32, kernel_size=3, stride=2)\n        self.Conv2d_2a_3x3 = conv_block(32, 32, kernel_size=3)\n        self.Conv2d_2b_3x3 = conv_block(32, 64, kernel_size=3, padding=1)\n        self.maxpool1 = nn.MaxPool2d(kernel_size=3, stride=2)\n        self.Conv2d_3b_1x1 = conv_block(64, 80, kernel_size=1)\n        self.Conv2d_4a_3x3 = conv_block(80, 192, kernel_size=3)\n        self.maxpool2 = nn.MaxPool2d(kernel_size=3, stride=2)\n        self.Mixed_5b = inception_a(192, pool_features=32)\n        self.Mixed_5c = inception_a(256, pool_features=64)\n        self.Mixed_5d = inception_a(288, pool_features=64)\n        self.Mixed_6a = inception_b(288)\n        self.Mixed_6b = inception_c(768, channels_7x7=128)\n        self.Mixed_6c = inception_c(768, channels_7x7=160)\n        self.Mixed_6d = inception_c(768, channels_7x7=160)\n        self.Mixed_6e = inception_c(768, channels_7x7=192)\n        self.AuxLogits: Optional[nn.Module] = None\n        if aux_logits:\n            self.AuxLogits = inception_aux(768, num_classes)\n        self.Mixed_7a = inception_d(768)\n        self.Mixed_7b = inception_e(1280)\n        self.Mixed_7c = inception_e(2048)\n        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n        self.dropout = nn.Dropout(p=dropout)\n        self.fc = nn.Linear(2048, num_classes)\n        if init_weights:\n            for m in self.modules():\n                if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):\n                    stddev = float(m.stddev) if hasattr(m, \"stddev\") else 0.1  # type: ignore\n                    torch.nn.init.trunc_normal_(m.weight, mean=0.0, std=stddev, a=-2, b=2)\n                elif isinstance(m, nn.BatchNorm2d):\n                    nn.init.constant_(m.weight, 1)\n                    nn.init.constant_(m.bias, 0)\n\n    def _transform_input(self, x: Tensor) -> Tensor:\n        if self.transform_input:\n            x_ch0 = torch.unsqueeze(x[:, 0], 1) * (0.229 / 0.5) + (0.485 - 0.5) / 0.5\n            x_ch1 = torch.unsqueeze(x[:, 1], 1) * (0.224 / 0.5) + (0.456 - 0.5) / 0.5\n            x_ch2 = torch.unsqueeze(x[:, 2], 1) * (0.225 / 0.5) + (0.406 - 0.5) / 0.5\n            x = torch.cat((x_ch0, x_ch1, x_ch2), 1)\n        return x\n\n    def _forward(self, x: Tensor) -> Tuple[Tensor, Optional[Tensor]]:\n        # N x 3 x 299 x 299\n        x = self.Conv2d_1a_3x3(x)\n        # N x 32 x 149 x 149\n        x = self.Conv2d_2a_3x3(x)\n        # N x 32 x 147 x 147\n        x = self.Conv2d_2b_3x3(x)\n        # N x 64 x 147 x 147\n        x = self.maxpool1(x)\n        # N x 64 x 73 x 73\n        x = self.Conv2d_3b_1x1(x)\n        # N x 80 x 73 x 73\n        x = self.Conv2d_4a_3x3(x)\n        # N x 192 x 71 x 71\n        x = self.maxpool2(x)\n        # N x 192 x 35 x 35\n        x = self.Mixed_5b(x)\n        # N x 256 x 35 x 35\n        x = self.Mixed_5c(x)\n        # N x 288 x 35 x 35\n        x = self.Mixed_5d(x)\n        # N x 288 x 35 x 35\n        x = self.Mixed_6a(x)\n        # N x 768 x 17 x 17\n        x = self.Mixed_6b(x)\n        # N x 768 x 17 x 17\n        x = self.Mixed_6c(x)\n        # N x 768 x 17 x 17\n        x = self.Mixed_6d(x)\n        # N x 768 x 17 x 17\n        x = self.Mixed_6e(x)\n        # N x 768 x 17 x 17\n        aux: Optional[Tensor] = None\n        if self.AuxLogits is not None:\n            if self.training:\n                aux = self.AuxLogits(x)\n        # N x 768 x 17 x 17\n        x = self.Mixed_7a(x)\n        # N x 1280 x 8 x 8\n        x = self.Mixed_7b(x)\n        # N x 2048 x 8 x 8\n        x = self.Mixed_7c(x)\n        # N x 2048 x 8 x 8\n        # Adaptive average pooling\n        x = self.avgpool(x)\n        # N x 2048 x 1 x 1\n        x = self.dropout(x)\n        # N x 2048 x 1 x 1\n        x = torch.flatten(x, 1)\n        # N x 2048\n        x = self.fc(x)\n        # N x 1000 (num_classes)\n        return x, aux\n\n    @torch.jit.unused\n    def eager_outputs(self, x: Tensor, aux: Optional[Tensor]) -> InceptionOutputs:\n        if self.training and self.aux_logits:\n            return InceptionOutputs(x, aux)\n        else:\n            return x  # type: ignore[return-value]\n\n    def forward(self, x: Tensor) -> InceptionOutputs:\n        x = self._transform_input(x)\n        x, aux = self._forward(x)\n        aux_defined = self.training and self.aux_logits\n        if torch.jit.is_scripting():\n            if not aux_defined:\n                warnings.warn(\"Scripted Inception3 always returns Inception3 Tuple\")\n            return InceptionOutputs(x, aux)\n        else:\n            return self.eager_outputs(x, aux)\n\n\nclass InceptionA(nn.Module):\n    def __init__(\n        self, in_channels: int, pool_features: int, conv_block: Optional[Callable[..., nn.Module]] = None\n    ) -> None:\n        super().__init__()\n        if conv_block is None:\n            conv_block = BasicConv2d\n        self.branch1x1 = conv_block(in_channels, 64, kernel_size=1)\n\n        self.branch5x5_1 = conv_block(in_channels, 48, kernel_size=1)\n        self.branch5x5_2 = conv_block(48, 64, kernel_size=5, padding=2)\n\n        self.branch3x3dbl_1 = conv_block(in_channels, 64, kernel_size=1)\n        self.branch3x3dbl_2 = conv_block(64, 96, kernel_size=3, padding=1)\n        self.branch3x3dbl_3 = conv_block(96, 96, kernel_size=3, padding=1)\n\n        self.branch_pool = conv_block(in_channels, pool_features, kernel_size=1)\n\n    def _forward(self, x: Tensor) -> List[Tensor]:\n        branch1x1 = self.branch1x1(x)\n\n        branch5x5 = self.branch5x5_1(x)\n        branch5x5 = self.branch5x5_2(branch5x5)\n\n        branch3x3dbl = self.branch3x3dbl_1(x)\n        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)\n        branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)\n\n        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)\n        branch_pool = self.branch_pool(branch_pool)\n\n        outputs = [branch1x1, branch5x5, branch3x3dbl, branch_pool]\n        return outputs\n\n    def forward(self, x: Tensor) -> Tensor:\n        outputs = self._forward(x)\n        return torch.cat(outputs, 1)\n\n\nclass InceptionB(nn.Module):\n    def __init__(self, in_channels: int, conv_block: Optional[Callable[..., nn.Module]] = None) -> None:\n        super().__init__()\n        if conv_block is None:\n            conv_block = BasicConv2d\n        self.branch3x3 = conv_block(in_channels, 384, kernel_size=3, stride=2)\n\n        self.branch3x3dbl_1 = conv_block(in_channels, 64, kernel_size=1)\n        self.branch3x3dbl_2 = conv_block(64, 96, kernel_size=3, padding=1)\n        self.branch3x3dbl_3 = conv_block(96, 96, kernel_size=3, stride=2)\n\n    def _forward(self, x: Tensor) -> List[Tensor]:\n        branch3x3 = self.branch3x3(x)\n\n        branch3x3dbl = self.branch3x3dbl_1(x)\n        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)\n        branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)\n\n        branch_pool = F.max_pool2d(x, kernel_size=3, stride=2)\n\n        outputs = [branch3x3, branch3x3dbl, branch_pool]\n        return outputs\n\n    def forward(self, x: Tensor) -> Tensor:\n        outputs = self._forward(x)\n        return torch.cat(outputs, 1)\n\n\nclass InceptionC(nn.Module):\n    def __init__(\n        self, in_channels: int, channels_7x7: int, conv_block: Optional[Callable[..., nn.Module]] = None\n    ) -> None:\n        super().__init__()\n        if conv_block is None:\n            conv_block = BasicConv2d\n        self.branch1x1 = conv_block(in_channels, 192, kernel_size=1)\n\n        c7 = channels_7x7\n        self.branch7x7_1 = conv_block(in_channels, c7, kernel_size=1)\n        self.branch7x7_2 = conv_block(c7, c7, kernel_size=(1, 7), padding=(0, 3))\n        self.branch7x7_3 = conv_block(c7, 192, kernel_size=(7, 1), padding=(3, 0))\n\n        self.branch7x7dbl_1 = conv_block(in_channels, c7, kernel_size=1)\n        self.branch7x7dbl_2 = conv_block(c7, c7, kernel_size=(7, 1), padding=(3, 0))\n        self.branch7x7dbl_3 = conv_block(c7, c7, kernel_size=(1, 7), padding=(0, 3))\n        self.branch7x7dbl_4 = conv_block(c7, c7, kernel_size=(7, 1), padding=(3, 0))\n        self.branch7x7dbl_5 = conv_block(c7, 192, kernel_size=(1, 7), padding=(0, 3))\n\n        self.branch_pool = conv_block(in_channels, 192, kernel_size=1)\n\n    def _forward(self, x: Tensor) -> List[Tensor]:\n        branch1x1 = self.branch1x1(x)\n\n        branch7x7 = self.branch7x7_1(x)\n        branch7x7 = self.branch7x7_2(branch7x7)\n        branch7x7 = self.branch7x7_3(branch7x7)\n\n        branch7x7dbl = self.branch7x7dbl_1(x)\n        branch7x7dbl = self.branch7x7dbl_2(branch7x7dbl)\n        branch7x7dbl = self.branch7x7dbl_3(branch7x7dbl)\n        branch7x7dbl = self.branch7x7dbl_4(branch7x7dbl)\n        branch7x7dbl = self.branch7x7dbl_5(branch7x7dbl)\n\n        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)\n        branch_pool = self.branch_pool(branch_pool)\n\n        outputs = [branch1x1, branch7x7, branch7x7dbl, branch_pool]\n        return outputs\n\n    def forward(self, x: Tensor) -> Tensor:\n        outputs = self._forward(x)\n        return torch.cat(outputs, 1)\n\n\nclass InceptionD(nn.Module):\n    def __init__(self, in_channels: int, conv_block: Optional[Callable[..., nn.Module]] = None) -> None:\n        super().__init__()\n        if conv_block is None:\n            conv_block = BasicConv2d\n        self.branch3x3_1 = conv_block(in_channels, 192, kernel_size=1)\n        self.branch3x3_2 = conv_block(192, 320, kernel_size=3, stride=2)\n\n        self.branch7x7x3_1 = conv_block(in_channels, 192, kernel_size=1)\n        self.branch7x7x3_2 = conv_block(192, 192, kernel_size=(1, 7), padding=(0, 3))\n        self.branch7x7x3_3 = conv_block(192, 192, kernel_size=(7, 1), padding=(3, 0))\n        self.branch7x7x3_4 = conv_block(192, 192, kernel_size=3, stride=2)\n\n    def _forward(self, x: Tensor) -> List[Tensor]:\n        branch3x3 = self.branch3x3_1(x)\n        branch3x3 = self.branch3x3_2(branch3x3)\n\n        branch7x7x3 = self.branch7x7x3_1(x)\n        branch7x7x3 = self.branch7x7x3_2(branch7x7x3)\n        branch7x7x3 = self.branch7x7x3_3(branch7x7x3)\n        branch7x7x3 = self.branch7x7x3_4(branch7x7x3)\n\n        branch_pool = F.max_pool2d(x, kernel_size=3, stride=2)\n        outputs = [branch3x3, branch7x7x3, branch_pool]\n        return outputs\n\n    def forward(self, x: Tensor) -> Tensor:\n        outputs = self._forward(x)\n        return torch.cat(outputs, 1)\n\n\nclass InceptionE(nn.Module):\n    def __init__(self, in_channels: int, conv_block: Optional[Callable[..., nn.Module]] = None) -> None:\n        super().__init__()\n        if conv_block is None:\n            conv_block = BasicConv2d\n        self.branch1x1 = conv_block(in_channels, 320, kernel_size=1)\n\n        self.branch3x3_1 = conv_block(in_channels, 384, kernel_size=1)\n        self.branch3x3_2a = conv_block(384, 384, kernel_size=(1, 3), padding=(0, 1))\n        self.branch3x3_2b = conv_block(384, 384, kernel_size=(3, 1), padding=(1, 0))\n\n        self.branch3x3dbl_1 = conv_block(in_channels, 448, kernel_size=1)\n        self.branch3x3dbl_2 = conv_block(448, 384, kernel_size=3, padding=1)\n        self.branch3x3dbl_3a = conv_block(384, 384, kernel_size=(1, 3), padding=(0, 1))\n        self.branch3x3dbl_3b = conv_block(384, 384, kernel_size=(3, 1), padding=(1, 0))\n\n        self.branch_pool = conv_block(in_channels, 192, kernel_size=1)\n\n    def _forward(self, x: Tensor) -> List[Tensor]:\n        branch1x1 = self.branch1x1(x)\n\n        branch3x3 = self.branch3x3_1(x)\n        branch3x3 = [\n            self.branch3x3_2a(branch3x3),\n            self.branch3x3_2b(branch3x3),\n        ]\n        branch3x3 = torch.cat(branch3x3, 1)\n\n        branch3x3dbl = self.branch3x3dbl_1(x)\n        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)\n        branch3x3dbl = [\n            self.branch3x3dbl_3a(branch3x3dbl),\n            self.branch3x3dbl_3b(branch3x3dbl),\n        ]\n        branch3x3dbl = torch.cat(branch3x3dbl, 1)\n\n        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)\n        branch_pool = self.branch_pool(branch_pool)\n\n        outputs = [branch1x1, branch3x3, branch3x3dbl, branch_pool]\n        return outputs\n\n    def forward(self, x: Tensor) -> Tensor:\n        outputs = self._forward(x)\n        return torch.cat(outputs, 1)\n\n\nclass InceptionAux(nn.Module):\n    def __init__(\n        self, in_channels: int, num_classes: int, conv_block: Optional[Callable[..., nn.Module]] = None\n    ) -> None:\n        super().__init__()\n        if conv_block is None:\n            conv_block = BasicConv2d\n        self.conv0 = conv_block(in_channels, 128, kernel_size=1)\n        self.conv1 = conv_block(128, 768, kernel_size=5)\n        self.conv1.stddev = 0.01  # type: ignore[assignment]\n        self.fc = nn.Linear(768, num_classes)\n        self.fc.stddev = 0.001  # type: ignore[assignment]\n\n    def forward(self, x: Tensor) -> Tensor:\n        # N x 768 x 17 x 17\n        x = F.avg_pool2d(x, kernel_size=5, stride=3)\n        # N x 768 x 5 x 5\n        x = self.conv0(x)\n        # N x 128 x 5 x 5\n        x = self.conv1(x)\n        # N x 768 x 1 x 1\n        # Adaptive average pooling\n        x = F.adaptive_avg_pool2d(x, (1, 1))\n        # N x 768 x 1 x 1\n        x = torch.flatten(x, 1)\n        # N x 768\n        x = self.fc(x)\n        # N x 1000\n        return x\n\n\nclass BasicConv2d(nn.Module):\n    def __init__(self, in_channels: int, out_channels: int, **kwargs: Any) -> None:\n        super().__init__()\n        self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)\n        self.bn = nn.BatchNorm2d(out_channels, eps=0.001)\n\n    def forward(self, x: Tensor) -> Tensor:\n        x = self.conv(x)\n        x = self.bn(x)\n        return F.relu(x, inplace=True)\n\n\nclass Inception_V3_Weights(WeightsEnum):\n    IMAGENET1K_V1 = Weights(\n        url=\"https://download.pytorch.org/models/inception_v3_google-0cc3c7bd.pth\",\n        transforms=partial(ImageClassification, crop_size=299, resize_size=342),\n        meta={\n            \"num_params\": 27161264,\n            \"min_size\": (75, 75),\n            \"categories\": _IMAGENET_CATEGORIES,\n            \"recipe\": \"https://github.com/pytorch/vision/tree/main/references/classification#inception-v3\",\n            \"_metrics\": {\n                \"ImageNet-1K\": {\n                    \"acc@1\": 77.294,\n                    \"acc@5\": 93.450,\n                }\n            },\n            \"_ops\": 5.713,\n            \"_file_size\": 103.903,\n            \"_docs\": \"\"\"These weights are ported from the original paper.\"\"\",\n        },\n    )\n    DEFAULT = IMAGENET1K_V1\n\n\n#@register_model()\n#@handle_legacy_interface(weights=(\"pretrained\", Inception_V3_Weights.IMAGENET1K_V1))\ndef inception_v3(*, weights: Optional[Inception_V3_Weights] = None, progress: bool = True, **kwargs: Any) -> Inception3:\n    \"\"\"\n    Inception v3 model architecture from\n    `Rethinking the Inception Architecture for Computer Vision <http://arxiv.org/abs/1512.00567>`_.\n    .. note::\n        **Important**: In contrast to the other models the inception_v3 expects tensors with a size of\n        N x 3 x 299 x 299, so ensure your images are sized accordingly.\n    Args:\n        weights (:class:`~torchvision.models.Inception_V3_Weights`, optional): The\n            pretrained weights for the model. See\n            :class:`~torchvision.models.Inception_V3_Weights` below for\n            more details, and possible values. By default, no pre-trained\n            weights are used.\n        progress (bool, optional): If True, displays a progress bar of the\n            download to stderr. Default is True.\n        **kwargs: parameters passed to the ``torchvision.models.Inception3``\n            base class. Please refer to the `source code\n            <https://github.com/pytorch/vision/blob/main/torchvision/models/inception.py>`_\n            for more details about this class.\n    .. autoclass:: torchvision.models.Inception_V3_Weights\n        :members:\n    \"\"\"\n    weights = Inception_V3_Weights.verify(weights)\n\n    original_aux_logits = kwargs.get(\"aux_logits\", True)\n    if weights is not None:\n        if \"transform_input\" not in kwargs:\n            _ovewrite_named_param(kwargs, \"transform_input\", True)\n        _ovewrite_named_param(kwargs, \"aux_logits\", True)\n        _ovewrite_named_param(kwargs, \"init_weights\", False)\n        _ovewrite_named_param(kwargs, \"num_classes\", len(weights.meta[\"categories\"]))\n\n    model = Inception3(**kwargs)\n\n    if weights is not None:\n        model.load_state_dict(weights.get_state_dict(progress=progress))\n        if not original_aux_logits:\n            model.aux_logits = False\n            model.AuxLogits = None\n\n    return model\n\n\n# The dictionary below is internal implementation detail and will be removed in v0.15\n# from torchvision.models._utils import _ModelURLs\n\n\n# model_urls = _ModelURLs(\n#     {\n#         # Inception v3 ported from TensorFlow\n#         \"inception_v3_google\": Inception_V3_Weights.IMAGENET1K_V1.url,\n#     }\n# )","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.536159Z","iopub.execute_input":"2023-04-17T09:53:26.536487Z","iopub.status.idle":"2023-04-17T09:53:26.615706Z","shell.execute_reply.started":"2023-04-17T09:53:26.536447Z","shell.execute_reply":"2023-04-17T09:53:26.614640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = batchSize()\nnewmodel = Inception3(init_weights=True, transform_input=[batch_size,3,299,299])\n\npath_to_weights = '/kaggle/working/birdclef22-inception-v3-pcen-weights/bird_weights_new.pt'#/bird_weights_new.pt' \nnewmodel.load_state_dict(torch.load(path_to_weights))\nnewmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:53:26.616956Z","iopub.execute_input":"2023-04-17T09:53:26.617334Z","iopub.status.idle":"2023-04-17T09:53:27.272098Z","shell.execute_reply.started":"2023-04-17T09:53:26.617298Z","shell.execute_reply":"2023-04-17T09:53:27.271427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed: int = 42):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)  # type: ignore\n    torch.backends.cudnn.deterministic = True  # type: ignore\n    torch.backends.cudnn.benchmark = True  # type: ignore\n    \n    \ndef get_logger(out_file=None):\n    logger = logging.getLogger()\n    formatter = logging.Formatter(\"%(asctime)s - %(levelname)s - %(message)s\")\n    logger.handlers = []\n    logger.setLevel(logging.INFO)\n\n    handler = logging.StreamHandler()\n    handler.setFormatter(formatter)\n    handler.setLevel(logging.INFO)\n    logger.addHandler(handler)\n\n    if out_file is not None:\n        fh = logging.FileHandler(out_file)\n        fh.setFormatter(formatter)\n        fh.setLevel(logging.INFO)\n        logger.addHandler(fh)\n    logger.info(\"logger set up\")\n    return logger\n    \n    \n@contextmanager\ndef timer(name: str, logger: Optional[logging.Logger] = None):\n    t0 = time.time()\n    msg = f\"[{name}] start\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)\n    yield\n\n    msg = f\"[{name}] done in {time.time() - t0:.2f} s\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:51.997562Z","iopub.execute_input":"2023-04-17T09:46:51.998380Z","iopub.status.idle":"2023-04-17T09:46:52.009030Z","shell.execute_reply.started":"2023-04-17T09:46:51.998330Z","shell.execute_reply":"2023-04-17T09:46:52.008155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logger = get_logger(\"main.log\")\nset_seed(1213)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.010320Z","iopub.execute_input":"2023-04-17T09:46:52.010717Z","iopub.status.idle":"2023-04-17T09:46:52.026015Z","shell.execute_reply.started":"2023-04-17T09:46:52.010680Z","shell.execute_reply":"2023-04-17T09:46:52.025281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    ######################\n    # Globals #\n    ######################\n    seed = 1213\n\n    ######################\n    # Data #\n    ######################\n    train_datadir = Path(\"../input/birdclef-2022/train_audio\")\n    train_csv = \"../input/birdclef-2022/train_metadata.csv\"\n    #train_soundscape = \"../input/birdclef-2022/train_soundscape_labels.csv\"\n\n    ######################\n    # Dataset #\n    ######################\n    transforms = {\n        \"train\": [{\"name\": \"Normalize\"}],\n        \"valid\": [{\"name\": \"Normalize\"}],\n        \"test\": [{\"name\": \"Normalize\"}]\n    }\n    period = 30\n    n_mels = 128\n    \n    sample_rate = 32000\n    target_columns = [\n        \"afrsil1\",\n        \"akekee\",\n        \"akepa1\",\n        \"akiapo\",\n        \"akikik\",\n        \"amewig\",\n        \"aniani\",\n        \"apapan\",\n        \"arcter\",\n        \"barpet\",\n        \"bcnher\",\n        \"belkin1\",\n        \"bkbplo\",\n        \"bknsti\",\n        \"bkwpet\",\n        \"blkfra\",\n        \"blknod\",\n        \"bongul\",\n        \"brant\",\n        \"brnboo\",\n        \"brnnod\",\n        \"brnowl\",\n        \"brtcur\",\n        \"bubsan\",\n        \"buffle\",\n        \"bulpet\",\n        \"burpar\",\n        \"buwtea\",\n        \"cacgoo1\",\n        \"calqua\",\n        \"cangoo\",\n        \"canvas\",\n        \"caster1\",\n        \"categr\",\n        \"chbsan\",\n        \"chemun\",\n        \"chukar\",\n        \"cintea\",\n        \"comgal1\",\n        \"commyn\",\n        \"compea\",\n        \"comsan\",\n        \"comwax\",\n        \"coopet\",\n        \"crehon\",\n        \"dunlin\",\n        \"elepai\",\n        \"ercfra\",\n        \"eurwig\",\n        \"fragul\",\n        \"gadwal\",\n        \"gamqua\",\n        \"glwgul\",\n        \"gnwtea\",\n        \"golphe\",\n        \"grbher3\",\n        \"grefri\",\n        \"gresca\",\n        \"gryfra\",\n        \"gwfgoo\",\n        \"hawama\",\n        \"hawcoo\",\n        \"hawcre\",\n        \"hawgoo\",\n        \"hawhaw\",\n        \"hawpet1\",\n        \"hoomer\",\n        \"houfin\",\n        \"houspa\",\n        \"hudgod\",\n        \"iiwi\",\n        \"incter1\",\n        \"jabwar\",\n        \"japqua\",\n        \"kalphe\",\n        \"kauama\",\n        \"laugul\",\n        \"layalb\",\n        \"lcspet\",\n        \"leasan\",\n        \"leater1\",\n        \"lessca\",\n        \"lesyel\",\n        \"lobdow\",\n        \"lotjae\",\n        \"madpet\",\n        \"magpet1\",\n        \"mallar3\",\n        \"masboo\",\n        \"mauala\",\n        \"maupar\",\n        \"merlin\",\n        \"mitpar\",\n        \"moudov\",\n        \"norcar\",\n        \"norhar2\",\n        \"normoc\",\n        \"norpin\",\n        \"norsho\",\n        \"nutman\",\n        \"oahama\",\n        \"omao\",\n        \"osprey\",\n        \"pagplo\",\n        \"palila\",\n        \"parjae\",\n        \"pecsan\",\n        \"peflov\",\n        \"perfal\",\n        \"pibgre\",\n        \"pomjae\",\n        \"puaioh\",\n        \"reccar\",\n        \"redava\",\n        \"redjun\",\n        \"redpha1\",\n        \"refboo\",\n        \"rempar\",\n        \"rettro\",\n        \"ribgul\",\n        \"rinduc\",\n        \"rinphe\",\n        \"rocpig\",\n        \"rorpar\",\n        \"rudtur\",\n        \"ruff\",\n        \"saffin\",\n        \"sander\",\n        \"semplo\",\n        \"sheowl\",\n        \"shtsan\",\n        \"skylar\",\n        \"snogoo\",\n        \"sooshe\",\n        \"sooter1\",\n        \"sopsku1\",\n        \"sora\",\n        \"spodov\",\n        \"sposan\",\n        \"towsol\",\n        \"wantat1\",\n        \"warwhe1\",\n        \"wesmea\",\n        \"wessan\",\n        \"wetshe\",\n        \"whfibi\",\n        \"whiter\",\n        \"whttro\",\n        \"wiltur\",\n        \"yebcar\",\n        \"yefcan\",\n        \"zebdov\",\n    ]\n    bird2id = {b: i for i, b in enumerate(target_columns)}\n    id2bird = {i: b for i, b in enumerate(target_columns)}\n    scored_birds = [\"akiapo\", \"aniani\", \"apapan\", \"barpet\", \"crehon\", \"elepai\", \"ercfra\", \"hawama\", \"hawcre\", \"hawgoo\", \"hawhaw\", \"hawpet1\", \"houfin\", \"iiwi\", \"jabwar\", \"maupar\", \"omao\", \"puaioh\", \"skylar\", \"warwhe1\", \"yefcan\"]\n    loader_params = {\n        \"train\": {\n            \"batch_size\": 64,\n            \"num_workers\": 20,\n            \"shuffle\": True\n        },\n        \"valid\": {\n            \"batch_size\": 64,\n            \"num_workers\": 20,\n            \"shuffle\": False\n        },\n        \"test\": {\n            \"batch_size\": 64,\n            \"num_workers\": 20,\n            \"shuffle\": False\n        }\n    }\n    models_cfg =  [{\"inception_v3\": \"tester2.pth\" }]  \n    \n    #[{\"inception_v3\": \"/kaggle/input/birdclef22-inception-v3-pcen-weights/bird_model.pt\"}]\n    #[{\"inception_v3\": \"/kaggle/input/birdclef22-inception-v3-pcen-weights/inception_v3_google-1a9a5a14.pth\" }]  \n    #[{\"inception_v3\": \"/kaggle/input/birdclef22-inception-v3-pcen-weights/bird_model.pt\",pretrained=True}] \n    #[{\"resnest26d\": \"../input/birdclef2022-weights/resnest26d/fold0/birdclef_2022/10p941rk/checkpoints/best_f1.ckpt\",}]\n    num_classes = len(target_columns)\nprint(f\"model_num: {len(CFG.models_cfg)}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.027578Z","iopub.execute_input":"2023-04-17T09:46:52.028096Z","iopub.status.idle":"2023-04-17T09:46:52.046559Z","shell.execute_reply.started":"2023-04-17T09:46:52.028061Z","shell.execute_reply":"2023-04-17T09:46:52.045786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(CFG.models_cfg)\n#models = CFG.models_cfg\nmodels = newmodel","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.048020Z","iopub.execute_input":"2023-04-17T09:46:52.048510Z","iopub.status.idle":"2023-04-17T09:46:52.061747Z","shell.execute_reply.started":"2023-04-17T09:46:52.048476Z","shell.execute_reply":"2023-04-17T09:46:52.060920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SR = 32000\nDATADIR = Path(\"../input/birdclef-2022/test_soundscapes/\") #ORIGINAL\n#DATADIR = Path(\"birdclef-2022/test_soundscapes/\") ","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.063186Z","iopub.execute_input":"2023-04-17T09:46:52.063444Z","iopub.status.idle":"2023-04-17T09:46:52.072716Z","shell.execute_reply.started":"2023-04-17T09:46:52.063401Z","shell.execute_reply":"2023-04-17T09:46:52.072017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_audios = list(DATADIR.glob(\"*.ogg\"))\nsample_submission = pd.read_csv('../input/birdclef-2022/sample_submission.csv')\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.074116Z","iopub.execute_input":"2023-04-17T09:46:52.074445Z","iopub.status.idle":"2023-04-17T09:46:52.094199Z","shell.execute_reply.started":"2023-04-17T09:46:52.074335Z","shell.execute_reply":"2023-04-17T09:46:52.093539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(torchdata.Dataset):\n    def __init__(self, df: pd.DataFrame, clip: np.ndarray, train_period=30, \n                 waveform_transforms=None):\n        self.df = df\n        self.clip = np.concatenate([clip, clip, clip])\n        self.train_period = train_period\n        self.waveform_transforms=waveform_transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx: int):\n        SR = 32000\n        sample = self.df.loc[idx, :]\n        row_id = sample.row_id\n\n        end_seconds = int(sample.seconds)\n        start_seconds = int(end_seconds - 5)\n        \n        end_index = int(SR * (end_seconds + (self.train_period - 5) / 2) + len(self.clip) // 3)\n        start_index = int(SR * (start_seconds - (self.train_period - 5) / 2) + len(self.clip) // 3)\n        \n        y = self.clip[start_index:end_index].astype(np.float32)\n\n        y = np.nan_to_num(y)\n\n        if self.waveform_transforms:\n            y = self.waveform_transforms(y)\n\n        y = np.nan_to_num(y)\n        \n        return y, row_id","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.095277Z","iopub.execute_input":"2023-04-17T09:46:52.095515Z","iopub.status.idle":"2023-04-17T09:46:52.104351Z","shell.execute_reply.started":"2023-04-17T09:46:52.095483Z","shell.execute_reply":"2023-04-17T09:46:52.103657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transforms(phase: str):\n    transforms = CFG.transforms\n    if transforms is None:\n        return None\n    else:\n        if transforms[phase] is None:\n            return None\n        trns_list = []\n        for trns_conf in transforms[phase]:\n            trns_name = trns_conf[\"name\"]\n            trns_params = {} if trns_conf.get(\"params\") is None else \\\n                trns_conf[\"params\"]\n            if globals().get(trns_name) is not None:\n                trns_cls = globals()[trns_name]\n                trns_list.append(trns_cls(**trns_params))\n\n        if len(trns_list) > 0:\n            return Compose(trns_list)\n        else:\n            return None\n\n\ndef get_waveform_transforms(config: dict, phase: str):\n    return get_transforms(config, phase)\n\n\ndef get_spectrogram_transforms(config: dict, phase: str):\n    transforms = config.get('spectrogram_transforms')\n    if transforms is None:\n        return None\n    else:\n        if transforms[phase] is None:\n            return None\n        trns_list = []\n        for trns_conf in transforms[phase]:\n            trns_name = trns_conf[\"name\"]\n            trns_params = {} if trns_conf.get(\"params\") is None else \\\n                trns_conf[\"params\"]\n            if hasattr(A, trns_name):\n                trns_cls = A.__getattribute__(trns_name)\n                trns_list.append(trns_cls(**trns_params))\n            else:\n                trns_cls = globals().get(trns_name)\n                if trns_cls is not None:\n                    trns_list.append(trns_cls(**trns_params))\n\n        if len(trns_list) > 0:\n            return A.Compose(trns_list, p=1.0)\n        else:\n            return None\n        \nclass Normalize:\n    def __call__(self, y: np.ndarray):\n        max_vol = np.abs(y).max()\n        y_vol = y * 1 / max_vol\n        return np.asfortranarray(y_vol)\n\n\nclass NewNormalize:\n    def __call__(self, y: np.ndarray):\n        y_mm = y - y.mean()\n        return y_mm / y_mm.abs().max()\n\n\nclass Compose:\n    def __init__(self, transforms: list):\n        self.transforms = transforms\n\n    def __call__(self, y: np.ndarray):\n        for trns in self.transforms:\n            y = trns(y)\n        return y\n\n\nclass AudioTransform:\n    def __init__(self, always_apply=False, p=0.5):\n        self.always_apply = always_apply\n        self.p = p\n\n    def __call__(self, y: np.ndarray):\n        if self.always_apply:\n            return self.apply(y)\n        else:\n            if np.random.rand() < self.p:\n                return self.apply(y)\n            else:\n                return y\n\n    def apply(self, y: np.ndarray):\n        raise NotImplementedError\n        \nclass NoiseInjection(AudioTransform):\n    def __init__(self, always_apply=False, p=0.5, max_noise_level=0.5, sr=32000):\n        super().__init__(always_apply, p)\n\n        self.noise_level = (0.0, max_noise_level)\n        self.sr = sr\n\n    def apply(self, y: np.ndarray, **params):\n        noise_level = np.random.uniform(*self.noise_level)\n        noise = np.random.randn(len(y))\n        augmented = (y + noise * noise_level).astype(y.dtype)\n        return augmented\n\n\nclass GaussianNoise(AudioTransform):\n    def __init__(self, always_apply=False, p=0.5, min_snr=5, max_snr=20, sr=32000):\n        super().__init__(always_apply, p)\n\n        self.min_snr = min_snr\n        self.max_snr = max_snr\n        self.sr = sr\n\n    def apply(self, y: np.ndarray, **params):\n        snr = np.random.uniform(self.min_snr, self.max_snr)\n        a_signal = np.sqrt(y ** 2).max()\n        a_noise = a_signal / (10 ** (snr / 20))\n\n        white_noise = np.random.randn(len(y))\n        a_white = np.sqrt(white_noise ** 2).max()\n        augmented = (y + white_noise * 1 / a_white * a_noise).astype(y.dtype)\n        return augmented\n\n\nclass PinkNoise(AudioTransform):\n    def __init__(self, always_apply=False, p=0.5, min_snr=5, max_snr=20, sr=32000):\n        super().__init__(always_apply, p)\n\n        self.min_snr = min_snr\n        self.max_snr = max_snr\n        self.sr = sr\n\n    def apply(self, y: np.ndarray, **params):\n        snr = np.random.uniform(self.min_snr, self.max_snr)\n        a_signal = np.sqrt(y ** 2).max()\n        a_noise = a_signal / (10 ** (snr / 20))\n\n        pink_noise = cn.powerlaw_psd_gaussian(1, len(y))\n        a_pink = np.sqrt(pink_noise ** 2).max()\n        augmented = (y + pink_noise * 1 / a_pink * a_noise).astype(y.dtype)\n        return augmented\n\n\nclass PitchShift(AudioTransform):\n    def __init__(self, always_apply=False, p=0.5, max_range=5, sr=32000):\n        super().__init__(always_apply, p)\n        self.max_range = max_range\n        self.sr = sr\n\n    def apply(self, y: np.ndarray, **params):\n        n_steps = np.random.randint(-self.max_range, self.max_range)\n        augmented = librosa.effects.pitch_shift(y, self.sr, n_steps)\n        return augmented\n\nclass TimeStretch(AudioTransform):\n    def __init__(self, always_apply=False, p=0.5, max_rate=1, sr=32000):\n        super().__init__(always_apply, p)\n        self.max_rate = max_rate\n        self.sr = sr\n\n    def apply(self, y: np.ndarray, **params):\n        rate = np.random.uniform(0, self.max_rate)\n        augmented = librosa.effects.time_stretch(y, rate)\n        return augmented\n\n\ndef _db2float(db: float, amplitude=True):\n    if amplitude:\n        return 10**(db / 20)\n    else:\n        return 10 ** (db / 10)\n\n\ndef volume_down(y: np.ndarray, db: float):\n    \"\"\"\n    Low level API for decreasing the volume\n    Parameters\n    ----------\n    y: numpy.ndarray\n        stereo / monaural input audio\n    db: float\n        how much decibel to decrease\n    Returns\n    -------\n    applied: numpy.ndarray\n        audio with decreased volume\n    \"\"\"\n    applied = y * _db2float(-db)\n    return applied\n\n\ndef volume_up(y: np.ndarray, db: float):\n    \"\"\"\n    Low level API for increasing the volume\n    Parameters\n    ----------\n    y: numpy.ndarray\n        stereo / monaural input audio\n    db: float\n        how much decibel to increase\n    Returns\n    -------\n    applied: numpy.ndarray\n        audio with increased volume\n    \"\"\"\n    applied = y * _db2float(db)\n    return applied\n\nclass RandomVolume(AudioTransform):\n    def __init__(self, always_apply=False, p=0.5, limit=10):\n        super().__init__(always_apply, p)\n        self.limit = limit\n\n    def apply(self, y: np.ndarray, **params):\n        db = np.random.uniform(-self.limit, self.limit)\n        if db >= 0:\n            return volume_up(y, db)\n        else:\n            return volume_down(y, db)\n\n\nclass OneOf:\n    def __init__(self, transforms: list):\n        self.transforms = transforms\n\n    def __call__(self, y: np.ndarray):\n        n_trns = len(self.transforms)\n        trns_idx = np.random.choice(n_trns)\n        trns = self.transforms[trns_idx]\n        y = trns(y)\n        return y\n\n\nclass CosineVolume(AudioTransform):\n    def __init__(self, always_apply=False, p=0.5, limit=10):\n        super().__init__(always_apply, p)\n        self.limit = limit\n\n    def apply(self, y: np.ndarray, **params):\n        db = np.random.uniform(-self.limit, self.limit)\n        cosine = np.cos(np.arange(len(y)) / len(y) * np.pi * 2)\n        dbs = _db2float(cosine * db)\n        return y * dbs\n\n\ndef drop_stripes(image: np.ndarray, dim: int, drop_width: int, stripes_num: int):\n    total_width = image.shape[dim]\n    lowest_value = image.min()\n    for _ in range(stripes_num):\n        distance = np.random.randint(low=0, high=drop_width, size=(1,))[0]\n        begin = np.random.randint(\n            low=0, high=total_width - distance, size=(1,))[0]\n\n        if dim == 0:\n            image[begin:begin + distance] = lowest_value\n        elif dim == 1:\n            image[:, begin + distance] = lowest_value\n        elif dim == 2:\n            image[:, :, begin + distance] = lowest_value\n    return image\n\nclass TimeFreqMasking(ImageOnlyTransform):\n    def __init__(self,\n                 time_drop_width: int,\n                 time_stripes_num: int,\n                 freq_drop_width: int,\n                 freq_stripes_num: int,\n                 always_apply=False,\n                 p=0.5):\n        super().__init__(always_apply, p)\n        self.time_drop_width = time_drop_width\n        self.time_stripes_num = time_stripes_num\n        self.freq_drop_width = freq_drop_width\n        self.freq_stripes_num = freq_stripes_num\n\n    def apply(self, img, **params):\n        img_ = img.copy()\n        if img.ndim == 2:\n            img_ = drop_stripes(\n                img_, dim=0, drop_width=self.freq_drop_width, stripes_num=self.freq_stripes_num)\n            img_ = drop_stripes(\n                img_, dim=1, drop_width=self.time_drop_width, stripes_num=self.time_stripes_num)\n        return img_","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.105756Z","iopub.execute_input":"2023-04-17T09:46:52.106433Z","iopub.status.idle":"2023-04-17T09:46:52.148916Z","shell.execute_reply.started":"2023-04-17T09:46:52.106398Z","shell.execute_reply":"2023-04-17T09:46:52.148070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load the model here","metadata":{}},{"cell_type":"code","source":"from torch.nn.modules.batchnorm import _BatchNorm\n\ndef prepare_model_for_inference(model, path: Path):\n    if not torch.cuda.is_available():\n        ckpt = torch.load(path, map_location=\"cpu\")\n    else:\n        ckpt = torch.load(path) #model.load_state_dict(path) #ckpt = torch.load(path) model.load(path)\n    model.load_state_dict(ckpt) # #model.load_state_dict(ckpt[\"state_dict\"]) #load_state_dict(state_dict, strict=True) model.load_state_dict(torch.load(ckpt))\n    model.eval()\n\n    return model\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.150082Z","iopub.execute_input":"2023-04-17T09:46:52.150335Z","iopub.status.idle":"2023-04-17T09:46:52.164720Z","shell.execute_reply.started":"2023-04-17T09:46:52.150301Z","shell.execute_reply":"2023-04-17T09:46:52.163982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_mel_spectrogram(audio_file, **spec_params):\n    sr, hop_length, n_fft, n_mels, fmin, fmax = [\n        spec_params[k] for k in [\"sr\", \"hop_length\", \"n_fft\", \"n_mels\", \"fmin\", \"fmax\"]\n    ]\n    audio, _ = librosa.core.load(audio_file, sr=sr, mono=True)\n    melspec = librosa.feature.melspectrogram(\n        audio,\n        sr=sr,\n        n_fft=n_fft,\n        hop_length=hop_length,\n        n_mels=n_mels,\n        fmin=fmin,\n        fmax=fmax,\n        power=1,\n    )\n    return melspec\n\n\ndef pcen_bird(melspec): #(melspec, **spec_params):\n    \"\"\"\n    parameters are taken from [1]:\n        - [1] Lostanlen, et. al. Per-Channel Energy Normalization: Why and How. IEEE Signal Processing Letters, 26(1), 39-43.\n    \"\"\"\n    #sr, hop_length = [spec_params[k] for k in [\"sr\", \"hop_length\"]]\n    return librosa.pcen(\n        melspec * (2 ** 31),\n        time_constant=0.06,\n        eps=1e-6,\n        gain=0.8,\n        power=0.25,\n        bias=10,\n        sr=32000,\n        hop_length=512,\n    )\n\n\ndef mel2audio(melspec, **spec_params):\n    n_fft, sr, hop_length = [spec_params[k] for k in [\"n_fft\", \"sr\", \"hop_length\"]]\n    return librosa.feature.inverse.mel_to_audio(\n        melspec, sr=sr, n_fft=n_fft, hop_length=hop_length, power=1\n    )\n\n\ndef get_fullpath(filename, audio_path=\"../input/birdclef-2022/train_audio\"):\n    return f\"{audio_path}/{filename}\"\n\n\ndef play_audio(audio_file):\n    display(ipd.Audio(audio_file))\n\n\ndef gen_spec_and_audio(audio_name):\n    out_file = audio_name.replace(\"/\", \"_\")[:-4] + \".wav\"\n    img = plot_spectrograms(audio_name)\n    print(\"source audio:\")\n    play_audio(get_fullpath(audio_name))\n    return img #Inserted return for PCEN","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.165744Z","iopub.execute_input":"2023-04-17T09:46:52.165943Z","iopub.status.idle":"2023-04-17T09:46:52.179990Z","shell.execute_reply.started":"2023-04-17T09:46:52.165913Z","shell.execute_reply":"2023-04-17T09:46:52.179341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(\n        self,\n        backbone=\"inception_v3\", #resnet34\n        p=0.5,\n        n_mels=128,\n        num_class=CFG.num_classes,\n        train_period=CFG.period,\n        infer_period=5.0,\n        in_chans=1,\n    ):\n        super().__init__()\n        #self.model = AttModel(backbone, p, n_mels, num_class, train_period, infer_period, in_chans)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.181047Z","iopub.execute_input":"2023-04-17T09:46:52.181717Z","iopub.status.idle":"2023-04-17T09:46:52.195444Z","shell.execute_reply.started":"2023-04-17T09:46:52.181676Z","shell.execute_reply":"2023-04-17T09:46:52.194535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def softmax_stable(x):\n    return(np.exp(x - np.max(x)) / np.exp(x - np.max(x)).sum())","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.201201Z","iopub.execute_input":"2023-04-17T09:46:52.201393Z","iopub.status.idle":"2023-04-17T09:46:52.210305Z","shell.execute_reply.started":"2023-04-17T09:46:52.201364Z","shell.execute_reply":"2023-04-17T09:46:52.209628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## constructor for model + predict","metadata":{}},{"cell_type":"code","source":"def prediction_for_clip(test_df: pd.DataFrame,  #BUCKAROO\n                        clip: np.ndarray, \n                        models, \n                        threshold=0.05, \n                        threshold_long=None):\n\n    dataset = TestDataset(df=test_df, \n                          clip=clip,\n                          train_period = CFG.period, \n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = torchdata.DataLoader(dataset, batch_size=1, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    transform_spectra = T.Compose([\n        T.ToPILImage(),\n        T.Resize((299,299))\n        ])\n    transform_image = T.Compose([\n        transforms.ToTensor(),\n        T.Resize((299,299)),\n        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n        ])\n    prediction_dict = {}\n    mel_model.to(device)\n    for image, row_id in tqdm(loader):\n        row_id = row_id[0]\n        \n        image = image.to(device)\n\n        with torch.no_grad(): \n            image =  mel_model.model.logmelspec_extractor[0](image)[:, None]#mel_model[0].logmelspec_extractor(image)[:, None]#image = create_mel_spectrogram(image,spec_params) #models[0]#.logmelspec_extractor(image)[:, None]\n            #print(image.shape)\n\n            np_img = image.cpu().numpy()\n            np_img = np_img.reshape(np_img.shape[2], np_img.shape[3], np_img.shape[1])\n            np_pcen = pcen_bird(np_img)\n            \n            img3 = cv2.cvtColor(np.float32(np_pcen), cv2.COLOR_GRAY2RGB) \n            formatted = (img3 * 255 / np.max(img3)).astype('uint8')\n            PIL_image = Image.fromarray(formatted).convert('RGB')\n\n            #Device and float\n            image = transform_image(PIL_image)\n            image = image.unsqueeze(0)#.to(device)\n            #print(image.shape)\n            \n            probas = []\n            probas_long = []\n            \n            \n            #for model in models:\n            with torch.cuda.amp.autocast():\n            #    _, clipwise_pred, _, _, clipwise_pred_long = newmodel(image)\n                clipwise_pred= newmodel(image)\n            probas.append(clipwise_pred.detach().cpu().numpy().reshape(-1))\n            #print(probas)\n            #probas_long.append(clipwise_pred_long.detach().cpu().numpy().reshape(-1))\n            probas = np.array(probas)\n            probas_new= softmax_stable(probas)\n            #probas_long = np.array(probas_long)\n#             probas = np.array([model(image)[1].detach().cpu().numpy().reshape(-1) for model in models])\n        if threshold_long is None:\n            events = probas_new.mean(0) >= threshold\n        else:\n            events = probas_new.mean(0) >= threshold\n         #   events = ((probas.mean(0) >= threshold).astype(int) \\\n         #             + (probas_long.mean(0) >= threshold_long).astype(int)) >= 2\n        labels = np.argwhere(events).reshape(-1).tolist()\n        labels = labels[:2]\n        if len(labels) == 0:\n            prediction_dict[str(row_id)] = \"nocall\"\n        else:\n            labels_str_list = list(map(lambda x: CFG.target_columns[x], labels))\n            label_string = \" \".join(labels_str_list)\n            prediction_dict[str(row_id)] = label_string\n    return prediction_dict\n# def prediction_for_clip(test_df, clip, models, threshold, threshold_long):\n#     probas = []\n#     probas_long = []\n#     newmodel = models\n#     newmodel.cuda()\n#     newmodel.eval()\n#     with torch.no_grad():\n#         for i in range(test_df.shape[0]):\n#             image = test_df.iloc[i].values\n#             image = torch.Tensor(image)\n#             image = image.unsqueeze(0).unsqueeze(0)\n#             # Convert the input to float32\n#             image = image.float()\n#             image = image.cuda()\n#             with torch.cuda.amp.autocast():\n#                 clipwise_pred = newmodel(image)\n#             probas.append(clipwise_pred.detach().cpu().numpy().reshape(-1))\n#     return np.mean(probas, axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.211714Z","iopub.execute_input":"2023-04-17T09:46:52.211976Z","iopub.status.idle":"2023-04-17T09:46:52.228535Z","shell.execute_reply.started":"2023-04-17T09:46:52.211942Z","shell.execute_reply":"2023-04-17T09:46:52.227651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(backbone_name, weight_path):\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    model = Model(\n        backbone_name,\n        p=0.5,\n        n_mels=CFG.n_mels,\n        num_class=CFG.num_classes,\n        train_period=CFG.period,\n        infer_period=5,\n    )\n    model = model_x\n    #model = prepare_model_for_inference(model, weight_path).to(device)\n    #model = model.model\n    return model\n\ndef prediction(test_audios, \n               models_cfg,\n               threshold=0.05, \n               threshold_long=None):\n    \n    #models = [load_model(list(models_cfg.keys())[0], list(models_cfg.values())[0]) for models_cfg in models_cfg]\n    warnings.filterwarnings(\"ignore\")\n    prediction_dicts = {}\n    for audio_path in test_audios:\n        with timer(f\"Loading {str(audio_path)}\", logger):\n            clip, _ = sf.read(audio_path)\n        seconds = []\n        row_ids = []\n        for second in range(5, 65, 5):\n            row_id = \"_\".join(audio_path.name.split(\".\")[:-1]) + f\"_{second}\"\n            seconds.append(second)\n            row_ids.append(row_id)\n        print(row_ids)\n        test_df = pd.DataFrame({\n            \"row_id\": row_ids,\n            \"seconds\": seconds\n        })\n        with timer(f\"Prediction on {audio_path}\", logger):\n            prediction_dict = prediction_for_clip(test_df,\n                                                  clip=clip,\n                                                  models=models,\n                                                  threshold=threshold, threshold_long=threshold_long)\n#         row_id = list(prediction_dict.keys())\n#         birds = list(prediction_dict.values())\n#         prediction_df = pd.DataFrame({\n#             \"row_id\": row_id,\n#             \"birds\": birds\n#         })\n#         prediction_dfs.append(prediction_df)\n#     prediction_df = pd.concat(prediction_dfs, axis=0, sort=False).reset_index(drop=True)\n        prediction_dicts.update(prediction_dict)\n    return prediction_dicts","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.229946Z","iopub.execute_input":"2023-04-17T09:46:52.230204Z","iopub.status.idle":"2023-04-17T09:46:52.243357Z","shell.execute_reply.started":"2023-04-17T09:46:52.230163Z","shell.execute_reply":"2023-04-17T09:46:52.242634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#weights = torch.load(\"tester2.pth\")\n#state_dict = torch.load('federated_mnist_best_test.pth')\n#a = Model()#('inception_v3', \"tester2.pth\") # weights[\"state_dict\"]\n#a.eval()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.244552Z","iopub.execute_input":"2023-04-17T09:46:52.244819Z","iopub.status.idle":"2023-04-17T09:46:52.258100Z","shell.execute_reply.started":"2023-04-17T09:46:52.244785Z","shell.execute_reply":"2023-04-17T09:46:52.257407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Todo, rewrite prediction dictionary creator","metadata":{}},{"cell_type":"code","source":"# def plot_spectrograms(\n#     audio_name,\n#     spec_params=dict(\n#         sr=32_000, hop_length=320, n_fft=512, n_mels=128, fmin=20, fmax=14_000 #Changed n_fft=800, hop_lenght to frame (1 = 10ms)\n#     ),\n# ):\n#     sr, hop_length, fmin, fmax, n_mels = [\n#         spec_params[k] for k in [\"sr\", \"hop_length\", \"fmin\", \"fmax\", \"n_mels\"]\n#     ]\n#     print(f\"parameters: {spec_params}\")\n#     audio_file = get_fullpath(audio_name)\n#     if not os.path.isfile(audio_file):\n#         raise FileNotFoundError\n#     melspec = create_mel_spectrogram(audio_file, **spec_params)\n#     log_melspec = librosa.amplitude_to_db(melspec, ref=np.max)\n#     pcen_melspec = pcen_bird(melspec, **spec_params)\n    \n#     return pcen_melspec","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.259536Z","iopub.execute_input":"2023-04-17T09:46:52.259794Z","iopub.status.idle":"2023-04-17T09:46:52.271268Z","shell.execute_reply.started":"2023-04-17T09:46:52.259762Z","shell.execute_reply":"2023-04-17T09:46:52.270619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GeMFreq(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super().__init__()\n        self.p = torch.nn.Parameter(torch.ones(1) * p)\n        self.eps = eps\n\n    def forward(self, x):\n        return gem_freq(x, p=self.p, eps=self.eps)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.272496Z","iopub.execute_input":"2023-04-17T09:46:52.273006Z","iopub.status.idle":"2023-04-17T09:46:52.281758Z","shell.execute_reply.started":"2023-04-17T09:46:52.272972Z","shell.execute_reply":"2023-04-17T09:46:52.281091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass AttHead(nn.Module):\n    def __init__(\n        self, in_chans, p=0.5, num_class=152, train_period=15.0, infer_period=5.0\n    ):\n        super().__init__()\n        self.train_period = train_period\n        self.infer_period = infer_period\n        self.pooling = GeMFreq()\n\n        self.dense_layers = nn.Sequential(\n            nn.Dropout(p / 2),\n            nn.Linear(in_chans, 512),\n            nn.ReLU(),\n            nn.Dropout(p),\n        )\n        self.attention = nn.Conv1d(\n            in_channels=512,\n            out_channels=num_class,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True,\n        )\n        self.fix_scale = nn.Conv1d(\n            in_channels=512,\n            out_channels=num_class,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True,\n        )\n\n    def forward(self, feat):\n        feat = self.pooling(feat).squeeze(-2).permute(0, 2, 1)  # (bs, time, ch)\n\n        feat = self.dense_layers(feat).permute(0, 2, 1)  # (bs, 512, time)\n        time_att = torch.tanh(self.attention(feat))\n        assert self.train_period >= self.infer_period\n        if self.training or self.train_period == self.infer_period:\n\n            clipwise_pred = torch.sum(\n                torch.sigmoid(self.fix_scale(feat)) * torch.softmax(time_att, dim=-1),\n                dim=-1,\n            )  # sum((bs, 24, time), -1) -> (bs, 24)\n            logits = torch.sum(\n                self.fix_scale(feat) * torch.softmax(time_att, dim=-1),\n                dim=-1,\n            )\n        else:\n            framewise_pred_long = torch.sigmoid(self.fix_scale(feat))\n            clipwise_pred_long = torch.sum(framewise_pred_long * torch.softmax(time_att, dim=-1), dim=-1) \n            \n            feat_time = feat.size(-1)\n            start = (\n                feat_time / 2 - feat_time * (self.infer_period / self.train_period) / 2\n            )\n            end = start + feat_time * (self.infer_period / self.train_period)\n            start = int(start)\n            end = int(end)\n            feat = feat[:, :, start:end]\n            att = torch.softmax(time_att[:, :, start:end], dim=-1)\n#             print(feat_time, start, end)\n#             print(att_a.sum(), att.sum(), time_att.shape)\n            framewise_pred = torch.sigmoid(self.fix_scale(feat))\n            clipwise_pred = torch.sum(framewise_pred * att, dim=-1) \n            logits = torch.sum(\n                self.fix_scale(feat) * att,\n                dim=-1,\n            )\n            time_att = time_att[:, :, start:end]\n        return (\n            logits,\n            clipwise_pred,\n            self.fix_scale(feat).permute(0, 2, 1),\n            time_att.permute(0, 2, 1),\n            clipwise_pred_long,\n        )","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.283019Z","iopub.execute_input":"2023-04-17T09:46:52.283538Z","iopub.status.idle":"2023-04-17T09:46:52.298946Z","shell.execute_reply.started":"2023-04-17T09:46:52.283504Z","shell.execute_reply":"2023-04-17T09:46:52.298290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class NormalizeMelSpec(nn.Module):\n    def __init__(self, eps=1e-6):\n        super().__init__()\n        self.eps = eps\n\n    def forward(self, X):\n        mean = X.mean((1, 2), keepdim=True)\n        std = X.std((1, 2), keepdim=True)\n        Xstd = (X - mean) / (std + self.eps)\n        norm_min, norm_max = Xstd.min(-1)[0].min(-1)[0], Xstd.max(-1)[0].max(-1)[0]\n        fix_ind = (norm_max - norm_min) > self.eps * torch.ones_like(\n            (norm_max - norm_min)\n        )\n        V = torch.zeros_like(Xstd)\n        if fix_ind.sum():\n            V_fix = Xstd[fix_ind]\n            norm_max_fix = norm_max[fix_ind, None, None]\n            norm_min_fix = norm_min[fix_ind, None, None]\n            V_fix = torch.max(\n                torch.min(V_fix, norm_max_fix),\n                norm_min_fix,\n            )\n            # print(V_fix.shape, norm_min_fix.shape, norm_max_fix.shape)\n            V_fix = (V_fix - norm_min_fix) / (norm_max_fix - norm_min_fix)\n            V[fix_ind] = V_fix\n        return V","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.300299Z","iopub.execute_input":"2023-04-17T09:46:52.300805Z","iopub.status.idle":"2023-04-17T09:46:52.313487Z","shell.execute_reply.started":"2023-04-17T09:46:52.300772Z","shell.execute_reply":"2023-04-17T09:46:52.312958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AttModel(nn.Module):\n    def __init__(\n        self,\n        backbone=\"resnet34\",\n        p=0.5,\n        n_mels=128,\n        num_class=152,\n        train_period=15.0,\n        infer_period=5.0,\n        in_chans=1,\n    ):\n        super().__init__()\n        self.n_mels = n_mels\n        self.logmelspec_extractor = nn.Sequential(\n            MelSpectrogram(\n                32000,\n                n_mels=n_mels,\n                f_min=20,\n                n_fft=2048,\n                hop_length=512,\n                normalized=True,\n            )#,\n            #AmplitudeToDB(top_db=80.0)#,\n            #NormalizeMelSpec(),\n        )\n\n        self.backbone = timm.create_model(\n            backbone, features_only=True, pretrained=False, in_chans=in_chans\n        )\n        encoder_channels = self.backbone.feature_info.channels()\n        dense_input = encoder_channels[-1]\n        self.head = AttHead(\n            dense_input,\n            p=p,\n            num_class=num_class,\n            train_period=train_period,\n            infer_period=infer_period,\n        )\n\n    def forward(self, input):\n#         img = self.logmelspec_extractor(input)[\n#             :, None\n#         ]  # (batch_size, 1, mel_bins, time_steps)\n        feats = self.backbone(input)\n        return self.head(feats[-1])\n    \nclass Model(nn.Module):\n    def __init__(\n        self,\n        backbone=\"resnet34\",\n        p=0.5,\n        n_mels=224,\n        num_class=CFG.num_classes,\n        train_period=CFG.period,\n        infer_period=5.0,\n        in_chans=1,\n    ):\n        super().__init__()\n        self.model = AttModel(backbone, p, n_mels, num_class, train_period, infer_period, in_chans)    \n\nmel_model = Model()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.314689Z","iopub.execute_input":"2023-04-17T09:46:52.315045Z","iopub.status.idle":"2023-04-17T09:46:52.675954Z","shell.execute_reply.started":"2023-04-17T09:46:52.315011Z","shell.execute_reply":"2023-04-17T09:46:52.675208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(mel_model.model.logmelspec_extractor[0]) #this is with the template model\n\n#print(mel_model)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.677388Z","iopub.execute_input":"2023-04-17T09:46:52.677796Z","iopub.status.idle":"2023-04-17T09:46:52.684349Z","shell.execute_reply.started":"2023-04-17T09:46:52.677760Z","shell.execute_reply":"2023-04-17T09:46:52.683655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MAIN","metadata":{}},{"cell_type":"code","source":"threshold = 0.025#0.025\nthreshold_long = 0.05\n\n# def plot_spectrograms(\n#     spec_params=dict(\n#         sr=32_000, hop_length=320, n_fft=512, n_mels=128, fmin=20, fmax=14_000 #Changed n_fft=800, hop_lenght to frame (1 = 10ms)\n#     ),\n# ):\n#     sr, hop_length, fmin, fmax, n_mels = [\n#         spec_params[k] for k in [\"sr\", \"hop_length\", \"fmin\", \"fmax\", \"n_mels\"]\n#     ]\n#     print(f\"parameters: {spec_params}\")\n#     audio_file = get_fullpath(audio_name)\n#     if not os.path.isfile(audio_file):\n#         raise FileNotFoundError\n#     melspec = create_mel_spectrogram(audio_file, **spec_params)\n#     log_melspec = librosa.amplitude_to_db(melspec, ref=np.max)\n#     pcen_melspec = pcen_bird(melspec, **spec_params)\n    \n#def plot_spectrograms(\ndef gen_specs(\n    spec_params=dict(\n        sr=32_000, hop_length=320, n_fft=512, n_mels=128, fmin=20, fmax=14_000 #Changed n_fft=800, hop_lenght to frame (1 = 10ms)\n    ),\n):\n    sr, hop_length, fmin, fmax, n_mels = [\n        spec_params[k] for k in [\"sr\", \"hop_length\", \"fmin\", \"fmax\", \"n_mels\"]\n    ]\n    return spec_params\n    \nspec_params = gen_specs()\nprint(f\"parameters: {spec_params}\")    \n#print(plot_spectrograms())\n\n\nprediction_dicts = prediction(test_audios=all_audios,\n           models_cfg=CFG.models_cfg,\n           threshold=threshold, \n           threshold_long=threshold_long)\nprint(prediction_dicts)\n#prediction_dicts.to_csv(\"prediction_dicts.csv\", index=False)\n\n\n# for i in range(len(sample_submission)):\n#     sample = sample_submission.row_id[i]\n#     key = sample.split(\"_\")[0] + \"_\" + sample.split(\"_\")[1] + \"_\" + sample.split(\"_\")[3]\n#     target_bird = sample.split(\"_\")[2]\n#     print(key, target_bird)\n#     if key in prediction_dicts:\n#         sample_submission.iat[i, 1] = (target_bird in prediction_dicts[key])\n# sample_submission.to_csv(\"submission.csv\", index=False)\n\n\n# submission = prediction(test_audios=all_audios,\n#                         models_cfg=CFG.models_cfg,\n#                         threshold=threshold, \n#                         threshold_long=threshold_long)\n#prediction_df = pd.DataFrame.from_dict(submission,orient='index')#(submission, orient='index', columns=['bird'])\n\n\n#prediction_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:52.685741Z","iopub.execute_input":"2023-04-17T09:46:52.686159Z","iopub.status.idle":"2023-04-17T09:46:56.546256Z","shell.execute_reply.started":"2023-04-17T09:46:52.686124Z","shell.execute_reply":"2023-04-17T09:46:56.545530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique = 'potato'\nkey = 'defacto'\n#pred = {'row_id': [], 'target': []}\nsubmission_fin = {'row_id': [], 'target': []}\nfor x in prediction_dicts:\n    for bird in scored_birds:\n    #key = prediction_dicts\n        if unique[:-1]==x[:-2]:\n            key = x[:-2]\n            time = x[-2:]\n        else: \n            #print('new')\n            #print(unique[:-1])\n            #print(x[:-2])\n            unique = x \n            key = x[:-1]\n            time = '5'\n        row = key + bird + '_' + time\n        \n        submission_fin['row_id'].append(row)\n        if bird == prediction_dicts[x]:\n            submission_fin['target'].append(True)\n        else:\n            submission_fin['target'].append(False)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:56.547408Z","iopub.execute_input":"2023-04-17T09:46:56.547789Z","iopub.status.idle":"2023-04-17T09:46:56.556179Z","shell.execute_reply.started":"2023-04-17T09:46:56.547751Z","shell.execute_reply":"2023-04-17T09:46:56.555093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(submission_fin)\ndf.to_csv(\"submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T09:46:56.557615Z","iopub.execute_input":"2023-04-17T09:46:56.557949Z","iopub.status.idle":"2023-04-17T09:46:56.570635Z","shell.execute_reply.started":"2023-04-17T09:46:56.557915Z","shell.execute_reply":"2023-04-17T09:46:56.569978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}