{"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 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\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:15.882029Z","iopub.execute_input":"2023-05-05T09:56:15.882706Z","iopub.status.idle":"2023-05-05T09:56:30.982847Z","shell.execute_reply.started":"2023-05-05T09:56:15.882665Z","shell.execute_reply":"2023-05-05T09:56:30.981712Z"},"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-05T09:56:30.984708Z","iopub.execute_input":"2023-05-05T09:56:30.985308Z","iopub.status.idle":"2023-05-05T09:56:30.990470Z","shell.execute_reply.started":"2023-05-05T09:56:30.985279Z","shell.execute_reply":"2023-05-05T09:56:30.989480Z"},"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\ncfg.min_rating = 2.0\ncfg.wav_crop_len = 30  # seconds\n# audio\ncfg.img_height = 256\ncfg.img_weight = 512\ncfg.window_size = 2048\ncfg.sample_rate = 32000\ncfg.fmin = 16\ncfg.fmax = 16386\ncfg.power = 2\ncfg.mel_bins = cfg.img_height\ncfg.hop_size = 512 #int(cfg.sample_rate * cfg.wav_crop_len / (cfg.img_weight - 1))\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 = False\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 = True\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\n\ncfg.birds = np.array(['abethr1', 'abhori1', 'abythr1', 'afbfly1', 'afdfly1', 'afecuc1',\n       'affeag1', 'afgfly1', 'afghor1', 'afmdov1', 'afpfly1', 'afpkin1',\n       'afpwag1', 'afrgos1', 'afrgrp1', 'afrjac1', 'afrthr1', 'amesun2',\n       'augbuz1', 'bagwea1', 'barswa', 'bawhor2', 'bawman1', 'bcbeat1',\n       'beasun2', 'bkctch1', 'bkfruw1', 'blacra1', 'blacuc1', 'blakit1',\n       'blaplo1', 'blbpuf2', 'blcapa2', 'blfbus1', 'blhgon1', 'blhher1',\n       'blksaw1', 'blnmou1', 'blnwea1', 'bltapa1', 'bltbar1', 'bltori1',\n       'blwlap1', 'brcale1', 'brcsta1', 'brctch1', 'brcwea1', 'brican1',\n       'brobab1', 'broman1', 'brosun1', 'brrwhe3', 'brtcha1', 'brubru1',\n       'brwwar1', 'bswdov1', 'btweye2', 'bubwar2', 'butapa1', 'cabgre1',\n       'carcha1', 'carwoo1', 'categr', 'ccbeat1', 'chespa1', 'chewea1',\n       'chibat1', 'chtapa3', 'chucis1', 'cibwar1', 'cohmar1', 'colsun2',\n       'combul2', 'combuz1', 'comsan', 'crefra2', 'crheag1', 'crohor1',\n       'darbar1', 'darter3', 'didcuc1', 'dotbar1', 'dutdov1', 'easmog1',\n       'eaywag1', 'edcsun3', 'egygoo', 'equaka1', 'eswdov1', 'eubeat1',\n       'fatrav1', 'fatwid1', 'fislov1', 'fotdro5', 'gabgos2', 'gargan',\n       'gbesta1', 'gnbcam2', 'gnhsun1', 'gobbun1', 'gobsta5', 'gobwea1',\n       'golher1', 'grbcam1', 'grccra1', 'grecor', 'greegr', 'grewoo2',\n       'grwpyt1', 'gryapa1', 'grywrw1', 'gybfis1', 'gycwar3', 'gyhbus1',\n       'gyhkin1', 'gyhneg1', 'gyhspa1', 'gytbar1', 'hadibi1', 'hamerk1',\n       'hartur1', 'helgui', 'hipbab1', 'hoopoe', 'huncis1', 'hunsun2',\n       'joygre1', 'kerspa2', 'klacuc1', 'kvbsun1', 'laudov1', 'lawgol',\n       'lesmaw1', 'lessts1', 'libeat1', 'litegr', 'litswi1', 'litwea1',\n       'loceag1', 'lotcor1', 'lotlap1', 'luebus1', 'mabeat1', 'macshr1',\n       'malkin1', 'marsto1', 'marsun2', 'mcptit1', 'meypar1', 'moccha1',\n       'mouwag1', 'ndcsun2', 'nobfly1', 'norbro1', 'norcro1', 'norfis1',\n       'norpuf1', 'nubwoo1', 'pabspa1', 'palfly2', 'palpri1', 'piecro1',\n       'piekin1', 'pitwhy', 'purgre2', 'pygbat1', 'quailf1', 'ratcis1',\n       'raybar1', 'rbsrob1', 'rebfir2', 'rebhor1', 'reboxp1', 'reccor',\n       'reccuc1', 'reedov1', 'refbar2', 'refcro1', 'reftin1', 'refwar2',\n       'rehblu1', 'rehwea1', 'reisee2', 'rerswa1', 'rewsta1', 'rindov',\n       'rocmar2', 'rostur1', 'ruegls1', 'rufcha2', 'sacibi2', 'sccsun2',\n       'scrcha1', 'scthon1', 'shesta1', 'sichor1', 'sincis1', 'slbgre1',\n       'slcbou1', 'sltnig1', 'sobfly1', 'somgre1', 'somtit4', 'soucit1',\n       'soufis1', 'spemou2', 'spepig1', 'spewea1', 'spfbar1', 'spfwea1',\n       'spmthr1', 'spwlap1', 'squher1', 'strher', 'strsee1', 'stusta1',\n       'subbus1', 'supsta1', 'tacsun1', 'tafpri1', 'tamdov1', 'thrnig1',\n       'trobou1', 'varsun2', 'vibsta2', 'vilwea1', 'vimwea1', 'walsta1',\n       'wbgbir1', 'wbrcha2', 'wbswea1', 'wfbeat1', 'whbcan1', 'whbcou1',\n       'whbcro2', 'whbtit5', 'whbwea1', 'whbwhe3', 'whcpri2', 'whctur2',\n       'wheslf1', 'whhsaw1', 'whihel1', 'whrshr1', 'witswa1', 'wlwwar',\n       'wookin1', 'woosan', 'wtbeat1', 'yebapa1', 'yebbar1', 'yebduc1',\n       'yebere1', 'yebgre1', 'yebsto1', 'yeccan1', 'yefcan', 'yelbis1',\n       'yenspu1', 'yertin1', 'yesbar1', 'yespet1', 'yetgre1', 'yewgre1'])\n\n\ncfg.n_classes = len(cfg.birds)\n\n\n# training \ncfg.lr = 0.0001\ncfg.epochs = 20\ncfg.batch_size = 64\ncfg.batch_size_val = 64\ncfg.backbone = \"resnet34\"\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-2023/train_metadata.csv\"\ncfg.train_df2 = \"../input/birdclef-22-duration/train_metadata_with_duration.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\ncfg.model = 'RN34'\n\ncfg","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:30.994872Z","iopub.execute_input":"2023-05-05T09:56:30.995699Z","iopub.status.idle":"2023-05-05T09:56:31.023527Z","shell.execute_reply.started":"2023-05-05T09:56:30.995675Z","shell.execute_reply":"2023-05-05T09:56:31.022132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cfg = importlib.import_module('default_config')\n# importlib.reload(cfg)\n# cfg = importlib.import_module('cfg_ps_6_v2')\n# importlib.reload(cfg)\n# cfg = copy(cfg.cfg)\n\nTEST_AUDIO_ROOT = \"../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-05T09:56:31.025229Z","iopub.execute_input":"2023-05-05T09:56:31.026439Z","iopub.status.idle":"2023-05-05T09:56:31.040990Z","shell.execute_reply.started":"2023-05-05T09:56:31.026399Z","shell.execute_reply":"2023-05-05T09:56:31.039587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_to_device(batch, device):\n    batch_dict = {key: batch[key].to(device) for key in batch}\n    return batch_dict\n\nimport librosa as lb\nimport librosa.display as lbd\nimport soundfile as sf\nfrom  soundfile import SoundFile \n\nclass BirdDataset(Dataset):\n    def __init__(self, data, sr=cfg.sample_rate, duration=5, step=None, resample=True, res_type=\"kaiser_fast\"):\n\n        self.data = data\n\n        self.sr = sr\n\n        self.duration = duration\n        self.audio_length = self.duration * self.sr\n        self.step = step or self.audio_length\n        self.res_type = res_type\n        self.resample = resample\n\n    def __len__(self):\n        return len(self.data)\n\n\n    def read_file(self, filepath):\n        audio, orig_sr = sf.read(filepath, dtype=\"float32\")\n\n        if self.resample and orig_sr != self.sr:\n            audio = lb.resample(audio, orig_sr, self.sr, res_type=self.res_type)\n\n        audios = []\n        for i in range(self.audio_length, len(audio) + self.step, self.step):\n            start = max(0, i - self.audio_length)\n            end = start + self.audio_length\n            audios.append(audio[start:end])\n\n        if len(audios[-1]) < self.audio_length:\n            audios = audios[:-1]\n\n        images = [audio for audio in audios]\n        images = np.stack(images)\n\n        return images\n\n    def __getitem__(self, idx):\n        return {'input': self.read_file(self.data.loc[idx, \"path\"])}","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:31.043412Z","iopub.execute_input":"2023-05-05T09:56:31.043786Z","iopub.status.idle":"2023-05-05T09:56:31.086846Z","shell.execute_reply.started":"2023-05-05T09:56:31.043756Z","shell.execute_reply":"2023-05-05T09:56:31.085125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gem(x, p=3, eps=1e-6):\n    return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1.0 / p)\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.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 = 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        self.wav2img = torch.nn.Sequential(self.mel_spec, self.amplitude_to_db)\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_23 = 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, batch):\n\n        \n        x = batch[\"input\"]\n        bs, parts, time = x.shape\n        x = x.reshape(parts, time)\n        \n        \n        with autocast(enabled=False):\n            x = self.wav2img(x)  # (bs, mel, time)\n            if self.cfg.mel_norm:\n                x = (x + 80) / 80\n\n        x = x.permute(0, 2, 1)\n        x = x[:, None, :, :]\n\n\n        x = self.backbone(x)\n        x = self.global_pool(x)\n        x = x[:, :, 0, 0]\n        logits = self.head_23(x)\n\n\n        return logits.sigmoid()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:31.088288Z","iopub.execute_input":"2023-05-05T09:56:31.088659Z","iopub.status.idle":"2023-05-05T09:56:31.109840Z","shell.execute_reply.started":"2023-05-05T09:56:31.088627Z","shell.execute_reply":"2023-05-05T09:56:31.107938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_state_dict(sd_fp):\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-05T09:56:31.111231Z","iopub.execute_input":"2023-05-05T09:56:31.111543Z","iopub.status.idle":"2023-05-05T09:56:31.296782Z","shell.execute_reply.started":"2023-05-05T09:56:31.111520Z","shell.execute_reply":"2023-05-05T09:56:31.295617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.DataFrame(\n     [(path.stem, *path.stem.split(\"_\"), path) for path in Path(cfg.val_data_folder).glob(\"*.ogg\")],\n    columns = [\"filename\", \"name\" ,\"id\", \"path\"]\n)\nprint(df_test.shape)\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:31.298558Z","iopub.execute_input":"2023-05-05T09:56:31.298954Z","iopub.status.idle":"2023-05-05T09:56:31.336849Z","shell.execute_reply.started":"2023-05-05T09:56:31.298923Z","shell.execute_reply":"2023-05-05T09:56:31.335782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_CORES = 4\ncfg.batch_size = 1\n\naug = None\ntest_ds =  BirdDataset(df_test, sr=cfg.sample_rate, duration=5, step=None, resample=True, res_type=\"kaiser_fast\")\ntest_dl = DataLoader(test_ds, shuffle=False, batch_size = cfg.batch_size, num_workers = N_CORES)\n\ntest_ds[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:31.340050Z","iopub.execute_input":"2023-05-05T09:56:31.340295Z","iopub.status.idle":"2023-05-05T09:56:31.769408Z","shell.execute_reply.started":"2023-05-05T09:56:31.340274Z","shell.execute_reply":"2023-05-05T09:56:31.768172Z"},"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-05T09:56:31.770482Z","iopub.execute_input":"2023-05-05T09:56:31.770856Z","iopub.status.idle":"2023-05-05T09:56:31.774792Z","shell.execute_reply.started":"2023-05-05T09:56:31.770834Z","shell.execute_reply":"2023-05-05T09:56:31.773819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#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/birdclef-21-2nd-place-model-train-0-66/first_model/checkpoint_last_seed123_19.pth\"\nnet = Net(cfg).eval().to(DEVICE)\nsd = get_state_dict(state_dict)\nprint(\"loading dict\")\nnet.load_state_dict(sd, strict=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:31.776028Z","iopub.execute_input":"2023-05-05T09:56:31.776482Z","iopub.status.idle":"2023-05-05T09:56:35.258477Z","shell.execute_reply.started":"2023-05-05T09:56:31.776428Z","shell.execute_reply":"2023-05-05T09:56:35.257470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(net.global_pool.parameters())","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:35.259766Z","iopub.execute_input":"2023-05-05T09:56:35.260080Z","iopub.status.idle":"2023-05-05T09:56:35.283939Z","shell.execute_reply.started":"2023-05-05T09:56:35.260052Z","shell.execute_reply":"2023-05-05T09:56:35.282488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def flatten(l):\n    return [item for sublist in l for item in sublist]","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:35.285406Z","iopub.execute_input":"2023-05-05T09:56:35.285702Z","iopub.status.idle":"2023-05-05T09:56:35.290584Z","shell.execute_reply.started":"2023-05-05T09:56:35.285679Z","shell.execute_reply":"2023-05-05T09:56:35.289224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    preds = []\n    for batch in tqdm(test_dl):\n        batch = batch_to_device(batch, DEVICE)\n        with torch.cuda.amp.autocast():\n            out = net(batch)\n            preds += [out.cpu().numpy()]","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:35.292210Z","iopub.execute_input":"2023-05-05T09:56:35.292540Z","iopub.status.idle":"2023-05-05T09:56:49.259233Z","shell.execute_reply.started":"2023-05-05T09:56:35.292504Z","shell.execute_reply":"2023-05-05T09:56:49.258359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:49.260318Z","iopub.execute_input":"2023-05-05T09:56:49.260589Z","iopub.status.idle":"2023-05-05T09:56:49.268658Z","shell.execute_reply.started":"2023-05-05T09:56:49.260567Z","shell.execute_reply":"2023-05-05T09:56:49.267385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/birdclef-2023/train_metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:49.270028Z","iopub.execute_input":"2023-05-05T09:56:49.270416Z","iopub.status.idle":"2023-05-05T09:56:49.390212Z","shell.execute_reply.started":"2023-05-05T09:56:49.270376Z","shell.execute_reply":"2023-05-05T09:56:49.389463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = df_test.filename.values.tolist()\n\nbird_cols = list(pd.get_dummies(df_train['primary_label']).columns)\nsub_df = pd.DataFrame(columns=['row_id']+bird_cols)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:49.391425Z","iopub.execute_input":"2023-05-05T09:56:49.391875Z","iopub.status.idle":"2023-05-05T09:56:49.440189Z","shell.execute_reply.started":"2023-05-05T09:56:49.391853Z","shell.execute_reply":"2023-05-05T09:56:49.438808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:49.441448Z","iopub.execute_input":"2023-05-05T09:56:49.441752Z","iopub.status.idle":"2023-05-05T09:56:49.455823Z","shell.execute_reply.started":"2023-05-05T09:56:49.441726Z","shell.execute_reply":"2023-05-05T09:56:49.453737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, file in enumerate(filenames):\n    pred = preds[i]\n    num_rows = len(pred)\n    row_ids = [f'{file}_{(i+1)*5}' for i in range(num_rows)]\n    df = pd.DataFrame(columns=['row_id']+bird_cols)\n    \n    df['row_id'] = row_ids\n    df[bird_cols] = pred\n    \n    sub_df = pd.concat([sub_df,df]).reset_index(drop=True)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:49.457244Z","iopub.execute_input":"2023-05-05T09:56:49.457674Z","iopub.status.idle":"2023-05-05T09:56:49.568074Z","shell.execute_reply.started":"2023-05-05T09:56:49.457643Z","shell.execute_reply":"2023-05-05T09:56:49.566707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:49.569434Z","iopub.execute_input":"2023-05-05T09:56:49.569789Z","iopub.status.idle":"2023-05-05T09:56:49.600444Z","shell.execute_reply.started":"2023-05-05T09:56:49.569757Z","shell.execute_reply":"2023-05-05T09:56:49.599428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-05T09:56:49.601648Z","iopub.execute_input":"2023-05-05T09:56:49.601933Z","iopub.status.idle":"2023-05-05T09:56:49.641067Z","shell.execute_reply.started":"2023-05-05T09:56:49.601907Z","shell.execute_reply":"2023-05-05T09:56:49.639735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}