{"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":"!pip install /kaggle/input/birdclef-wheel/torch-1.13.0cpu-cp37-cp37m-linux_x86_64.whl /kaggle/input/birdclef-wheel/torchaudio-0.13.0cpu-cp37-cp37m-linux_x86_64.whl\n# !pip install ../input/birds-inference-pip-wheels/timm-0.4.8.zip --no-index --no-deps\n# !pip install /kaggle/input/timm-pytorch-image-models/pytorch-image-models-master --no-index --no-deps\n!pip install /kaggle/input/birdclef-wheel/onnx-1.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl --no-index --no-deps\n!pip install /kaggle/input/birdclef-wheel/openvino_dev-2022.3.0-9052-py3-none-any.whl --no-index --no-deps","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-22T18:58:24.239174Z","iopub.execute_input":"2023-05-22T18:58:24.239831Z","iopub.status.idle":"2023-05-22T18:58:58.999037Z","shell.execute_reply.started":"2023-05-22T18:58:24.2397Z","shell.execute_reply":"2023-05-22T18:58:58.997902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/birdclef-wheel/openvino-2022.3.0-9052-cp37-cp37m-manylinux_2_17_x86_64.whl --no-index --no-deps","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:58:59.000895Z","iopub.execute_input":"2023-05-22T18:58:59.001203Z","iopub.status.idle":"2023-05-22T18:59:01.431612Z","shell.execute_reply.started":"2023-05-22T18:58:59.001162Z","shell.execute_reply":"2023-05-22T18:59:01.430619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip uninstall --y torch torchvision\n# !pip install --pre torch==1.13.0 torchvision torchaudio -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:01.433512Z","iopub.execute_input":"2023-05-22T18:59:01.43403Z","iopub.status.idle":"2023-05-22T18:59:01.439974Z","shell.execute_reply.started":"2023-05-22T18:59:01.433975Z","shell.execute_reply":"2023-05-22T18:59:01.438789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/convmelspec  .\n!pip install /kaggle/working/convmelspec --no-index --no-deps ","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:01.444145Z","iopub.execute_input":"2023-05-22T18:59:01.444938Z","iopub.status.idle":"2023-05-22T18:59:06.99745Z","shell.execute_reply.started":"2023-05-22T18:59:01.444882Z","shell.execute_reply":"2023-05-22T18:59:06.996159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install onnx==1.12.0 onnxruntime --no-deps ","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:07.000162Z","iopub.execute_input":"2023-05-22T18:59:07.00209Z","iopub.status.idle":"2023-05-22T18:59:07.007914Z","shell.execute_reply.started":"2023-05-22T18:59:07.002032Z","shell.execute_reply":"2023-05-22T18:59:07.006797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install openvino-dev[pytorch,onnx]","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:07.009671Z","iopub.execute_input":"2023-05-22T18:59:07.010144Z","iopub.status.idle":"2023-05-22T18:59:07.022206Z","shell.execute_reply.started":"2023-05-22T18:59:07.010088Z","shell.execute_reply":"2023-05-22T18:59:07.021182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/birdclef21trainmeta/timm-0.4.9  .\n!pip install /kaggle/working/timm-0.4.9/pytorch-image-models-0.4.9 --no-index --no-deps ","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:07.024416Z","iopub.execute_input":"2023-05-22T18:59:07.024735Z","iopub.status.idle":"2023-05-22T18:59:14.11874Z","shell.execute_reply.started":"2023-05-22T18:59:07.024699Z","shell.execute_reply":"2023-05-22T18:59:14.11757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm\ntimm.__version__","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:14.120991Z","iopub.execute_input":"2023-05-22T18:59:14.121342Z","iopub.status.idle":"2023-05-22T18:59:16.02047Z","shell.execute_reply.started":"2023-05-22T18:59:14.121297Z","shell.execute_reply":"2023-05-22T18:59:16.019626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numba\nnumba.__version__","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:16.022764Z","iopub.execute_input":"2023-05-22T18:59:16.023054Z","iopub.status.idle":"2023-05-22T18:59:16.251381Z","shell.execute_reply.started":"2023-05-22T18:59:16.023017Z","shell.execute_reply":"2023-05-22T18:59:16.250376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\nlibrosa.__version__","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:16.252822Z","iopub.execute_input":"2023-05-22T18:59:16.253174Z","iopub.status.idle":"2023-05-22T18:59:17.056172Z","shell.execute_reply.started":"2023-05-22T18:59:16.253137Z","shell.execute_reply":"2023-05-22T18:59:17.054692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport os\nimport importlib\nimport multiprocessing as mp\n\nfrom tqdm import tqdm\nimport numpy as np\nimport pandas as pd\nimport glob\nimport torch\nfrom copy import copy\n\nfrom torch.utils.data import DataLoader\n\nimport pandas as pd\nimport timm\nfrom torch import nn\nimport torch\nimport torchaudio as ta\nfrom torch.cuda.amp import autocast\nimport random\n\nfrom torch.nn import functional as F\nfrom torch.distributions import Beta\nfrom torch.nn.parameter import Parameter\nfrom torch.utils.data import Dataset\n\nimport numpy as np\nimport librosa\nimport ast\n\nimport os\nfrom types import SimpleNamespace\nimport numpy as np\n\nimport numpy as np\nimport pandas as pd\nimport importlib\nimport sys\nimport random\nfrom tqdm import tqdm\nimport gc\nimport argparse\nimport torch\nfrom torch import optim\nfrom torch.cuda.amp import GradScaler, autocast\nfrom collections import defaultdict\nimport cv2\nfrom copy import copy\nimport os\nfrom transformers import get_cosine_schedule_with_warmup\nfrom torch.utils.data import SequentialSampler, DataLoader\n\nfrom convmelspec.stft import ConvertibleSpectrogram as Spectrogram","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:17.057956Z","iopub.execute_input":"2023-05-22T18:59:17.059738Z","iopub.status.idle":"2023-05-22T18:59:20.285817Z","shell.execute_reply.started":"2023-05-22T18:59:17.059684Z","shell.execute_reply":"2023-05-22T18:59:20.284766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=1234):\n    random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = False\n    torch.backends.cudnn.benchmark = True","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.287385Z","iopub.execute_input":"2023-05-22T18:59:20.287738Z","iopub.status.idle":"2023-05-22T18:59:20.293591Z","shell.execute_reply.started":"2023-05-22T18:59:20.287699Z","shell.execute_reply":"2023-05-22T18:59:20.292639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config","metadata":{}},{"cell_type":"code","source":"cfg = SimpleNamespace()\n\n# paths\ncfg.data_folder = ''\ncfg.name = \"julian\"\ncfg.data_dir = \"../input/birdclef-2023/\"\ncfg.train_data_folder = cfg.data_dir + \"train_audio/\"\ncfg.val_data_folder = cfg.data_dir + \"train_audio/\"\ncfg.output_dir = \"first_model\"\n\n# dataset\ncfg.dataset = \"base_ds\"\ncfg.min_rating = 0\ncfg.val_df = None\ncfg.batch_size_val = 1\ncfg.train_aug = None\ncfg.val_aug = None\ncfg.test_augs = None\ncfg.wav_len_val = 5  # seconds\n\n# audio\ncfg.window_size = 1024\ncfg.hop_size = 512\ncfg.sample_rate = 32000\ncfg.fmin = 16\ncfg.fmax = 16386\ncfg.power = 2\ncfg.mel_bins = 128\ncfg.top_db = 80.0\n\n# img model\ncfg.backbone = \"resnet18\"\ncfg.pretrained = True\ncfg.pretrained_weights = None\ncfg.train = True\ncfg.val = False\ncfg.in_chans = 1\n\ncfg.alpha = 1\ncfg.eval_epochs = 1\ncfg.eval_train_epochs = 1\ncfg.warmup = 0\n\ncfg.mel_norm = True\n\ncfg.label_smoothing = 0\n\ncfg.remove_pretrained = []\n\n# training\ncfg.seed = 123\ncfg.save_val_data = True\n\n# ressources\ncfg.mixed_precision = False\ncfg.gpu = 0\ncfg.num_workers = 4 # 18\ncfg.drop_last = True \n\ncfg.mixup2 = 0\n\ncfg.label_smoothing = 0\n\ncfg.mixup_2x = False\n\ncfg.birds = np.array(['abethr1', 'abhori1', 'abythr1', 'afbfly1', 'afdfly1', 'afecuc1', 'affeag1',\n 'afgfly1', 'afghor1', 'afmdov1', 'afpfly1', 'afpkin1', 'afpwag1', 'afrgos1',\n 'afrgrp1', 'afrjac1', 'afrthr1', 'amesun2', 'augbuz1', 'bagwea1', 'barswa',\n 'bawhor2', 'bawman1', 'bcbeat1', 'beasun2', 'bkctch1', 'bkfruw1', 'blacra1',\n 'blacuc1', 'blakit1', 'blaplo1', 'blbpuf2', 'blcapa2', 'blfbus1', 'blhgon1',\n 'blhher1', 'blksaw1', 'blnmou1', 'blnwea1', 'bltapa1', 'bltbar1', 'bltori1',\n 'blwlap1', 'brcale1', 'brcsta1', 'brctch1', 'brcwea1', 'brican1', 'brobab1',\n 'broman1', 'brosun1', 'brrwhe3', 'brtcha1', 'brubru1', 'brwwar1', 'bswdov1',\n 'btweye2', 'bubwar2', 'butapa1', 'cabgre1', 'carcha1', 'carwoo1', 'categr',\n 'ccbeat1', 'chespa1', 'chewea1', 'chibat1', 'chtapa3', 'chucis1', 'cibwar1',\n 'cohmar1', 'colsun2', 'combul2', 'combuz1', 'comsan', 'crefra2', 'crheag1',\n 'crohor1', 'darbar1', 'darter3', 'didcuc1', 'dotbar1', 'dutdov1', 'easmog1',\n 'eaywag1', 'edcsun3', 'egygoo', 'equaka1', 'eswdov1', 'eubeat1', 'fatrav1',\n 'fatwid1', 'fislov1', 'fotdro5', 'gabgos2', 'gargan', 'gbesta1', 'gnbcam2',\n 'gnhsun1', 'gobbun1', 'gobsta5', 'gobwea1', 'golher1', 'grbcam1', 'grccra1',\n 'grecor', 'greegr', 'grewoo2', 'grwpyt1', 'gryapa1', 'grywrw1', 'gybfis1',\n 'gycwar3', 'gyhbus1', 'gyhkin1', 'gyhneg1', 'gyhspa1', 'gytbar1', 'hadibi1',\n 'hamerk1', 'hartur1', 'helgui', 'hipbab1', 'hoopoe', 'huncis1', 'hunsun2',\n 'joygre1', 'kerspa2', 'klacuc1', 'kvbsun1', 'laudov1', 'lawgol', 'lesmaw1',\n 'lessts1', 'libeat1', 'litegr', 'litswi1', 'litwea1', 'loceag1', 'lotcor1',\n 'lotlap1', 'luebus1', 'mabeat1', 'macshr1', 'malkin1', 'marsto1', 'marsun2',\n 'mcptit1', 'meypar1', 'moccha1', 'mouwag1', 'ndcsun2', 'nobfly1', 'norbro1',\n 'norcro1', 'norfis1', 'norpuf1', 'nubwoo1', 'pabspa1', 'palfly2', 'palpri1',\n 'piecro1', 'piekin1', 'pitwhy', 'purgre2', 'pygbat1', 'quailf1', 'ratcis1',\n 'raybar1', 'rbsrob1', 'rebfir2', 'rebhor1', 'reboxp1', 'reccor', 'reccuc1',\n 'reedov1', 'refbar2', 'refcro1', 'reftin1', 'refwar2', 'rehblu1', 'rehwea1',\n 'reisee2', 'rerswa1', 'rewsta1', 'rindov', 'rocmar2', 'rostur1', 'ruegls1',\n 'rufcha2', 'sacibi2', 'sccsun2', 'scrcha1', 'scthon1', 'shesta1', 'sichor1',\n 'sincis1', 'slbgre1', 'slcbou1', 'sltnig1', 'sobfly1', 'somgre1', 'somtit4',\n 'soucit1', 'soufis1', 'spemou2', 'spepig1', 'spewea1', 'spfbar1', 'spfwea1',\n 'spmthr1', 'spwlap1', 'squher1', 'strher', 'strsee1', 'stusta1', 'subbus1',\n 'supsta1', 'tacsun1', 'tafpri1', 'tamdov1', 'thrnig1', 'trobou1', 'varsun2',\n 'vibsta2', 'vilwea1', 'vimwea1', 'walsta1', 'wbgbir1', 'wbrcha2', 'wbswea1',\n 'wfbeat1', 'whbcan1', 'whbcou1', 'whbcro2', 'whbtit5', 'whbwea1', 'whbwhe3',\n 'whcpri2', 'whctur2', 'wheslf1', 'whhsaw1', 'whihel1', 'whrshr1', 'witswa1',\n 'wlwwar', 'wookin1', 'woosan', 'wtbeat1', 'yebapa1', 'yebbar1', 'yebduc1',\n 'yebere1', 'yebgre1', 'yebsto1', 'yeccan1', 'yefcan', 'yelbis1', 'yenspu1',\n 'yertin1', 'yesbar1', 'yespet1', 'yetgre1', 'yewgre1'])\n\n# cfg.birds = np.array(['afrsil1', 'akekee', 'akepa1', 'akiapo', 'akikik', 'amewig',\n#        'aniani', 'apapan', 'arcter', 'barpet', 'bcnher', 'belkin1',\n#        'bkbplo', 'bknsti', 'bkwpet', 'blkfra', 'blknod', 'bongul',\n#        'brant', 'brnboo', 'brnnod', 'brnowl', 'brtcur', 'bubsan',\n#        'buffle', 'bulpet', 'burpar', 'buwtea', 'cacgoo1', 'calqua',\n#        'cangoo', 'canvas', 'caster1', 'categr', 'chbsan', 'chemun',\n#        'chukar', 'cintea', 'comgal1', 'commyn', 'compea', 'comsan',\n#        'comwax', 'coopet', 'crehon', 'dunlin', 'elepai', 'ercfra',\n#        'eurwig', 'fragul', 'gadwal', 'gamqua', 'glwgul', 'gnwtea',\n#        'golphe', 'grbher3', 'grefri', 'gresca', 'gryfra', 'gwfgoo',\n#        'hawama', 'hawcoo', 'hawcre', 'hawgoo', 'hawhaw', 'hawpet1',\n#        'hoomer', 'houfin', 'houspa', 'hudgod', 'iiwi', 'incter1',\n#        'jabwar', 'japqua', 'kalphe', 'kauama', 'laugul', 'layalb',\n#        'lcspet', 'leasan', 'leater1', 'lessca', 'lesyel', 'lobdow',\n#        'lotjae', 'madpet', 'magpet1', 'mallar3', 'masboo', 'mauala',\n#        'maupar', 'merlin', 'mitpar', 'moudov', 'norcar', 'norhar2',\n#        'normoc', 'norpin', 'norsho', 'nutman', 'oahama', 'omao', 'osprey',\n#        'pagplo', 'palila', 'parjae', 'pecsan', 'peflov', 'perfal',\n#        'pibgre', 'pomjae', 'puaioh', 'reccar', 'redava', 'redjun',\n#        'redpha1', 'refboo', 'rempar', 'rettro', 'ribgul', 'rinduc',\n#        'rinphe', 'rocpig', 'rorpar', 'rudtur', 'ruff', 'saffin', 'sander',\n#        'semplo', 'sheowl', 'shtsan', 'skylar', 'snogoo', 'sooshe',\n#        'sooter1', 'sopsku1', 'sora', 'spodov', 'sposan', 'towsol',\n#        'wantat1', 'warwhe1', 'wesmea', 'wessan', 'wetshe', 'whfibi',\n#        'whiter', 'whttro', 'wiltur', 'yebcar', 'yefcan', 'zebdov'])\n\n\ncfg.n_classes = len(cfg.birds)\n# dataset\ncfg.min_rating = 2.0\n\ncfg.wav_crop_len = 30  # seconds\n\ncfg.lr = 0.0001\ncfg.epochs = 20\ncfg.batch_size = 64 # 64\ncfg.batch_size_val = 64\ncfg.backbone = \"seresnext26t_32x4d\"\n\n\ncfg.save_val_data = True\ncfg.mixed_precision = True\n\ncfg.mixup = True\ncfg.mix_beta = 1\n\n\ncfg.train_df1 = \"../input/birdclef-2022/train_metadata.csv\"\ncfg.train_df2 = \"../input/birdclef-2022-df-train-with-durations/df-with-durations.csv\"\n\n\ncfg.device = 'cuda' if torch.cuda.is_available() else 'cpu'\n\ncfg.tr_collate_fn = None\ncfg.val_collate_fn = None\ncfg.val = False\n\ncfg.dev = False\n\ncfg.model = \"RN34\"\n\ncfg","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.296972Z","iopub.execute_input":"2023-05-22T18:59:20.297259Z","iopub.status.idle":"2023-05-22T18:59:20.34045Z","shell.execute_reply.started":"2023-05-22T18:59:20.297227Z","shell.execute_reply":"2023-05-22T18:59:20.339032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_AUDIO_ROOT = \"/kaggle/input/birdclef-2023/test_soundscapes\"\ncfg.val_data_folder = TEST_AUDIO_ROOT\ncfg.pretrained = False\n\n\nprint(cfg.model, cfg.dataset, cfg.backbone, cfg.pretrained_weights, cfg.mel_norm)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.342256Z","iopub.execute_input":"2023-05-22T18:59:20.342939Z","iopub.status.idle":"2023-05-22T18:59:20.365099Z","shell.execute_reply.started":"2023-05-22T18:59:20.342889Z","shell.execute_reply":"2023-05-22T18:59:20.364206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os.path import join, abspath\n\ndef batch_to_device(batch, device):\n    return {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in batch.items()}\n\nclass TestDataset(Dataset):\n    def __init__(self, test_dir):\n        self.audio_dir = TEST_AUDIO_ROOT\n        self.audio_list = os.listdir(test_dir)\n\n        self.num_parts = 120\n        self.duration = 5\n\n    def __getitem__(self, index):\n        audio_name = self.audio_list[index]\n        ## load whole audio\n        audio, sr = librosa.load(join(self.audio_dir, audio_name), sr=None, offset=0)\n\n        sr = 32000\n        ## not a whole n-second audio\n        if audio.shape[0] % sr != 0:\n            audio = np.pad(audio, (0, sr - audio.shape[0] % sr))\n\n        audio = torch.Tensor(audio)\n        audio = audio.reshape(self.num_parts, len(audio) // self.num_parts)\n        return {\n            'audio' : torch.Tensor(audio),\n            'filename' : audio_name\n        }\n    def __len__(self) -> int:\n        return len(self.audio_list)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.366989Z","iopub.execute_input":"2023-05-22T18:59:20.369687Z","iopub.status.idle":"2023-05-22T18:59:20.381134Z","shell.execute_reply.started":"2023-05-22T18:59:20.369598Z","shell.execute_reply":"2023-05-22T18:59:20.380068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gem(x, p=3, eps=1e-6):\n#     size = (x.size(-2), x.size(-1))\n#     return F.avg_pool2d(x.clamp(min=eps).pow(p), (10, 4)).pow(1.0 / p) ## config B\n    return F.avg_pool2d(x.clamp(min=eps).pow(p), (16, 2)).pow(1.0 / p) ## config A\n\n\n\nclass GeM(nn.Module):\n    # Generalized mean: https://arxiv.org/abs/1711.02512\n    def __init__(self, p=3, eps=1e-6):\n        super(GeM, self).__init__()\n        self.p = Parameter(torch.ones(1) * p)\n#         self.p = p\n        self.eps = eps\n\n    def forward(self, x):\n        ret = gem(x, p=self.p, eps=self.eps)\n        return ret\n\n    def __repr__(self):\n        return (self.__class__.__name__+ \"(p=\"+ \"{:.4f}\".format(self.p.data.tolist()[0])+ \", eps=\"+ str(self.eps)+ \")\")\n\n\nclass Mixup(nn.Module):\n    def __init__(self, mix_beta):\n\n        super(Mixup, self).__init__()\n        self.beta_distribution = Beta(mix_beta, mix_beta)\n\n    def forward(self, X, Y, weight=None):\n\n        bs = X.shape[0]\n        n_dims = len(X.shape)\n        perm = torch.randperm(bs)\n        coeffs = self.beta_distribution.rsample(torch.Size((bs,))).to(X.device)\n\n        if n_dims == 2:\n            X = coeffs.view(-1, 1) * X + (1 - coeffs.view(-1, 1)) * X[perm]\n        elif n_dims == 3:\n            X = coeffs.view(-1, 1, 1) * X + (1 - coeffs.view(-1, 1, 1)) * X[perm]\n        else:\n            X = coeffs.view(-1, 1, 1, 1) * X + (1 - coeffs.view(-1, 1, 1, 1)) * X[perm]\n\n        Y = coeffs.view(-1, 1) * Y + (1 - coeffs.view(-1, 1)) * Y[perm]\n\n        if weight is None:\n            return X, Y\n        else:\n            weight = coeffs.view(-1) * weight + (1 - coeffs.view(-1)) * weight[perm]\n            return X, Y, weight\n\n        \n        \nclass Net(nn.Module):\n    def __init__(self, cfg):\n        super(Net, self).__init__()\n\n        self.cfg = cfg\n\n        self.n_classes = cfg.n_classes\n        \n        self.mel_spec = Spectrogram(\n            sr=cfg.sample_rate,\n            n_fft=cfg.window_size,\n            hop_size=cfg.hop_size,\n            fmin=cfg.fmin,\n            fmax=cfg.fmax,\n            padding=0,\n            n_mel=cfg.mel_bins,\n#             power=cfg.power,\n            norm=None,\n            spec_mode='DFT',\n            dft_mode='store',\n            mel_scale=\"htk\",\n        )\n\n#         self.mel_spec = ta.transforms.MelSpectrogram(\n#             sample_rate=cfg.sample_rate,\n#             n_fft=cfg.window_size,\n#             win_length=cfg.window_size,\n#             hop_length=cfg.hop_size,\n#             f_min=cfg.fmin,\n#             f_max=cfg.fmax,\n#             pad=0,\n#             n_mels=cfg.mel_bins,\n#             power=cfg.power,\n#             normalized=False,\n#         )\n\n        self.amplitude_to_db = ta.transforms.AmplitudeToDB(top_db=cfg.top_db)\n        \n        self.frequencymask = ta.transforms.FrequencyMasking(20)\n        \n        self.timemask = ta.transforms.TimeMasking(50)\n        \n        self.wav2img_val = torch.nn.Sequential(self.mel_spec, self.amplitude_to_db)\n        \n        self.wav2img_train = torch.nn.Sequential(self.mel_spec, self.amplitude_to_db, self.frequencymask, self.timemask)\n\n\n        self.backbone = timm.create_model(\n            cfg.backbone,\n            pretrained=cfg.pretrained,\n            num_classes=0,\n            global_pool=\"\",\n            in_chans=cfg.in_chans,\n        )\n\n        if \"efficientnet\" in cfg.backbone:\n            backbone_out = self.backbone.num_features\n        else:\n            backbone_out = self.backbone.feature_info[-1][\"num_chs\"]\n\n        self.global_pool = GeM()\n\n        self.head = nn.Linear(backbone_out, self.n_classes)\n\n        if cfg.pretrained_weights is not None:\n            sd = torch.load(cfg.pretrained_weights, map_location=\"cpu\")[\"model\"]\n            sd = {k.replace(\"module.\", \"\"): v for k, v in sd.items()}\n            self.load_state_dict(sd, strict=True)\n            print(\"weights loaded from\", cfg.pretrained_weights)\n        self.loss_fn = nn.BCEWithLogitsLoss(reduction=\"none\")\n\n        self.mixup = Mixup(mix_beta=cfg.mix_beta)\n\n        self.factor = int(cfg.wav_crop_len / 5.0)\n\n    def forward(self, audio, target, weight):\n\n        if not self.training:\n            x = audio ## (bs * num_parts, sr * 5)\n            y = target\n#             weight = batch['weight']\n#             bs, parts, time = x.shape\n#             x = x.reshape(parts, time)\n#             y = batch[\"target\"]\n#             y = y[0]\n        else:\n            x = audio\n            y = target\n            bs, time = x.shape\n            x = x.reshape(bs * self.factor, time // self.factor)\n\n        with autocast(enabled=False):\n            if not self.training:\n                x = self.wav2img_val(x)  # (bs * num_parts, mel, sr * 5)\n            else:\n                x = self.wav2img_train(x)  # (bs, mel, time)\n            if self.cfg.mel_norm:\n                x = (x + 80) / 80\n                if not self.training:\n                    x = torch.pow(x, 2)\n#                 else:\n#                     x = self.random_power(x)\n\n        x = x.permute(0, 2, 1) ## (bs * num_parts, sr * 5, mel)\n        x = x[:, None, :, :] ## [bs * num_parts, channel, sr * 5, mel]\n\n#         weight = batch[\"weight\"]\n\n        if self.training:\n            b, c, t, f = x.shape\n            x = x.permute(0, 2, 1, 3)\n            x = x.reshape(b // self.factor, self.factor * t, c, f)\n\n            if self.cfg.mixup:\n                x, y, weight = self.mixup(x, y, weight)\n            if self.cfg.mixup2:\n                x, y, weight = self.mixup(x, y, weight)\n\n            x = x.reshape(b, t, c, f)\n            x = x.permute(0, 2, 1, 3)\n            \n        bs, channels, time, mels = x.shape\n\n        x = self.backbone(x)\n\n        if self.training:\n            b, c, t, f = x.shape\n            x = x.permute(0, 2, 1, 3)\n            x = x.reshape(b // self.factor, self.factor * t, c, f)\n            x = x.permute(0, 2, 1, 3)\n#         print(x.size())\n        x = self.global_pool(x)\n        x = x[:, :, 0, 0]\n        logits = self.head(x)\n\n#         loss = self.loss_fn(logits, y)\n#         loss = (loss.mean(dim=1) * weight) / weight.sum()\n#         loss = loss.sum()\n\n        return {\"logits\": logits.sigmoid(), \"logits_raw\": logits, \"target\": y}\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.383698Z","iopub.execute_input":"2023-05-22T18:59:20.384247Z","iopub.status.idle":"2023-05-22T18:59:20.421097Z","shell.execute_reply.started":"2023-05-22T18:59:20.384197Z","shell.execute_reply":"2023-05-22T18:59:20.420068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_state_dict(sd_fp):\n    print(list(torch.load(sd_fp, map_location=\"cpu\")))\n    sd = torch.load(sd_fp, map_location=\"cpu\")['model']\n    sd = {k.replace(\"module.\", \"\"):v for k,v in sd.items()}\n    return sd\n\nfrom scipy.stats.mstats import gmean","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.42232Z","iopub.execute_input":"2023-05-22T18:59:20.423525Z","iopub.status.idle":"2023-05-22T18:59:20.440536Z","shell.execute_reply.started":"2023-05-22T18:59:20.42342Z","shell.execute_reply":"2023-05-22T18:59:20.439726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from joblib import Parallel, delayed\nfrom joblib.externals.loky.backend.context import get_context\nN_CORES = 4\ncfg.batch_size = 1\n\n# TEST_AUDIO_ROOT = '/kaggle/input/birdclef-2023-test/test_soundscapes'\n\ndataset = TestDataset(TEST_AUDIO_ROOT)\ndataloader = DataLoader(\n    dataset,\n    batch_size=cfg.batch_size,\n    shuffle=False,\n    drop_last=False,\n    num_workers=N_CORES,\n#     multiprocessing_context=get_context('loky')\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.441603Z","iopub.execute_input":"2023-05-22T18:59:20.442312Z","iopub.status.idle":"2023-05-22T18:59:20.456614Z","shell.execute_reply.started":"2023-05-22T18:59:20.442259Z","shell.execute_reply":"2023-05-22T18:59:20.455788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEVICE = \"cuda\" if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.458134Z","iopub.execute_input":"2023-05-22T18:59:20.45856Z","iopub.status.idle":"2023-05-22T18:59:20.466408Z","shell.execute_reply.started":"2023-05-22T18:59:20.458525Z","shell.execute_reply":"2023-05-22T18:59:20.465529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.window_size = 1024\ncfg.hop_size = 320\ncfg.sample_rate = 32000\ncfg.fmin = 50\ncfg.fmax = 14000\ncfg.power = 2\ncfg.mel_bins = 64\ncfg.top_db = None","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.518818Z","iopub.execute_input":"2023-05-22T18:59:20.519265Z","iopub.status.idle":"2023-05-22T18:59:20.530685Z","shell.execute_reply.started":"2023-05-22T18:59:20.519231Z","shell.execute_reply":"2023-05-22T18:59:20.529675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\ntorch.set_flush_denormal(True)\n#state_dict = \"../input/mel-gem-resnet-from-2021-2nd-place/first_model/checkpoint_last_seed123.pth\"\n#state_dict = \"../input/mel-gem-resnet/first_model/checkpoint_last_seed123.pth\"\nstate_dict = \"/kaggle/input/22-3rd-place-model/nfn0_A_sfm_seed42_46.pth\"\n# cfg.backbone = \"seresnext26t_32x4d\"\n# cfg.backbone = 'efficientnet_b0'\n# cfg.backbone = 'tf_efficientnet_b3_ns'\n# cfg.backbone = 'tf_efficientnetv2_s_in21k'\ncfg.backbone = 'eca_nfnet_l0'\n# cfg.backbone = 'resnet34'\nnet = Net(cfg).eval().to(DEVICE)\nsd = torch.load(state_dict, map_location=\"cpu\")['model']\n#sd = get_state_dict(state_dict)\nprint(\"loading dict\")\nnet.load_state_dict(sd, strict=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T18:59:20.532102Z","iopub.execute_input":"2023-05-22T18:59:20.53275Z","iopub.status.idle":"2023-05-22T18:59:21.415151Z","shell.execute_reply.started":"2023-05-22T18:59:20.532714Z","shell.execute_reply":"2023-05-22T18:59:21.414203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nimport warnings\n\nnet.eval()\n\nweights_path = \"/kaggle/working/22-3rd-place-model/\" + state_dict.split('/')[-1].replace('.pth', '')\n# weights_path = \"/kaggle/working/22-3rd-place-model/sup3_efnv2_cmap\"\nonnx_path = Path(weights_path).with_suffix('.onnx')\nif not onnx_path.parent.exists():\n    onnx_path.parent.mkdir()\nprint(onnx_path)\n\nwith warnings.catch_warnings():\n    warnings.filterwarnings(\"ignore\")\n    if not onnx_path.exists():\n        dummy_input = torch.randn(1, 120, 160000)\n        bs, num_parts, time = dummy_input.shape\n        dummy_input = dummy_input.reshape(bs * num_parts, time)\n        torch.onnx.export(\n            net,\n            (dummy_input, -1, -1),\n            onnx_path,\n            opset_version=14,\n        )\n        print(f\"ONNX model exported to {onnx_path}.\")\n    else:\n        print(f\"ONNX model {onnx_path} already exists.\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Markdown, display\nir_path = onnx_path.with_suffix(\".xml\")\n# Construct the command for Model Optimizer.\nmo_command = f\"\"\"mo\n                 --input_model \"{onnx_path}\"\n                 --output_dir \"{ir_path.parent}\"\n                 \"\"\"\nmo_command = \" \".join(mo_command.split())\nprint(\"Model Optimizer command to convert the ONNX model to OpenVINO:\")\ndisplay(Markdown(f\"`{mo_command}`\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not ir_path.exists():\n    print(\"Exporting ONNX model to IR... This may take a few minutes.\")\n    mo_result = %sx $mo_command\n    print(\"\\n\".join(mo_result))\nelse:\n    print(f\"IR model {ir_path} already exists.\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from openvino.runtime import Core\nimport os\n# Load the network in OpenVINO Runtime.\nie = Core()\nmodel_ir = ie.read_model(model=ir_path)\ncompiled_model_ir = ie.compile_model(model=model_ir, device_name=\"CPU\", config={\"INFERENCE_NUM_THREADS\": os.cpu_count()})\n\n# Get input and output layers.\noutput_layer_ir = compiled_model_ir.output(0)\n\n# dummy_input = torch.randn(1, 120, 160000)\n# bs, num_parts, time = dummy_input.shape\n# dummy_input = dummy_input.reshape(bs * num_parts, time)\n# res = compiled_model_ir([dummy_input, -1])[output_layer_ir]\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from joblib import Parallel, delayed\nfrom joblib.externals.loky.backend.context import get_context\nfrom tqdm import tqdm\nimport openvino\npred = {'row_id' : [], 'score' : []}\nsmooth = 1\n\nfor data in tqdm(dataloader):\n# def infer(data):\n#     with torch.no_grad():\n    data = batch_to_device(data, DEVICE)\n    ## data['audio'].shape == (bs, num_parts, sr * 5) ->\n    ## data['audio'].shape == (bs * num_parts, sr * 5) ## inference on 5 sec crops\n    bs, num_parts, time = data['audio'].shape\n    audio = data['audio'].reshape(bs * num_parts, time)\n    c_input = np.ascontiguousarray(audio, dtype=np.float32)\n    input_tensor = openvino.runtime.Tensor(c_input, shared_memory=True)\n    prediction = compiled_model_ir([input_tensor, -1])[output_layer_ir]\n\n    ## prediction for every offset, for every bird\n    prediction = prediction.reshape(bs, num_parts, -1)\n\n    for batch_idx, (filename, score) in enumerate(zip(data['filename'],  prediction)):\n        soundscape_name = filename.split('.')[0]\n        for offset_index, offset in enumerate(range(0, 600, 5)):\n#                 for bird, index in zip(bird_names, scored_birds):\n            pred_name = f'{soundscape_name}_{offset + 5}'\n            pred['row_id'].append(pred_name)\n            if offset == 0:\n                pred['score'].append((1 + smooth) / 2 * score[offset_index] + (1 - smooth) / 2 * score[offset_index + 1])\n            elif offset == 595:\n                pred['score'].append((1 + smooth) / 2 * score[offset_index] + (1 - smooth) / 2 * score[offset_index - 1])\n            else:\n                pred['score'].append((1 - smooth) / 2 * score[offset_index - 1] + smooth * score[offset_index] + (1 - smooth) / 2 * score[offset_index + 1])\n    \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds = pd.DataFrame(pred['score'], columns=cfg.birds)\ndf_preds.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame()\ntest_df['row_id'] = pred['row_id']\ntest_df = test_df.join(df_preds)\ntest_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['fn'] = test_df['row_id'].apply(lambda x: x.split('_')[1])\ntest_df['ind'] = test_df.index\ntest_df.sort_values(['fn', 'ind'], inplace=True, ascending=True)\ntest_df.drop(['fn', 'ind'], axis=1, inplace=True)\ntest_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}