{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8284541,"sourceType":"datasetVersion","datasetId":3014066},{"sourceId":10821447,"sourceType":"datasetVersion","datasetId":6629712,"isSourceIdPinned":true}],"dockerImageVersionId":30407,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Baseline for Pytorch Lightning based submission \n\n**Step 1: Training Notebook with Pytorch:** https://www.kaggle.com/code/hakandogan/bclef-24-pytorch-cnn-train\n\n","metadata":{}},{"cell_type":"code","source":"!pip install --no-index --find-links /kaggle/input/onnxruntime/ onnxruntime","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:46:55.717809Z","iopub.execute_input":"2025-02-22T09:46:55.718221Z","iopub.status.idle":"2025-02-22T09:47:05.918169Z","shell.execute_reply.started":"2025-02-22T09:46:55.718178Z","shell.execute_reply":"2025-02-22T09:47:05.916769Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport torch\nimport os\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn import model_selection\nimport torchaudio\nimport torchaudio.functional as F\nimport torch.nn.functional as Fnn\nimport librosa\nfrom PIL import Image\nimport albumentations as alb\nimport torch.multiprocessing as mp\nimport warnings\n\nwarnings.filterwarnings('ignore')\n\nimport torch.nn as nn\nfrom torch.nn.functional import cross_entropy\nimport torchmetrics\nimport timm\nfrom pathlib import Path\n\nfrom tqdm.notebook import tqdm\nimport onnxruntime\nimport concurrent\nfrom glob import glob","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:05.921000Z","iopub.execute_input":"2025-02-22T09:47:05.921400Z","iopub.status.idle":"2025-02-22T09:47:05.930378Z","shell.execute_reply.started":"2025-02-22T09:47:05.921358Z","shell.execute_reply":"2025-02-22T09:47:05.929180Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import gc","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:05.931801Z","iopub.execute_input":"2025-02-22T09:47:05.932144Z","iopub.status.idle":"2025-02-22T09:47:05.951598Z","shell.execute_reply.started":"2025-02-22T09:47:05.932110Z","shell.execute_reply":"2025-02-22T09:47:05.950345Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import librosa as lb\nimport librosa.display as lbd\nimport soundfile as sf\nfrom  soundfile import SoundFile ","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:05.954203Z","iopub.execute_input":"2025-02-22T09:47:05.954567Z","iopub.status.idle":"2025-02-22T09:47:05.965591Z","shell.execute_reply.started":"2025-02-22T09:47:05.954530Z","shell.execute_reply":"2025-02-22T09:47:05.964223Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Config:\n    num_classes = 182\n    batch_size = 48\n    PRECISION = 32    \n    seed = 2024\n    #model = \"tf_efficientnet_b0_ns\"\n    #model = \"mobilenetv3_small_075\"\n    DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')    \n    \n    nfft = 1024\n    power = 2\n    hop = nfft // 2\n    fmin = 50\n    fmax = None\n    constPres = 2e-3\n    normalize_wrt = \"meanPresBand\"\n    normalize_spec = True\n\n    bandpass = True\n    bandpass_min = 400\n    bandpass_max = 15000\n    bandpass_order = 1\n\n    data_root = \"/kaggle/input/birdclef-2024/\"\n    train_path = \"/kaggle/input/birdclef-2024/train_metadata.csv\"\n    \n    test_path = '/kaggle/input/birdclef-2024/test_soundscapes/'\n    SR = 32000\n    DURATION = 5\n    Audio_length = SR * DURATION\n    LR = 5e-4\n    \n    #model_output = ['/kaggle/input/bc24-sed-callsong-onnx/offset_v15.onnx']\n    model_output = ['/kaggle/input/bc24-cnn-onnx/best_model.onnx']\n    #model_ckpt = '/kaggle/input/b23-sed-selfsuper-v3/exp1/tf_efficientnet_b1_ns_ap.ckpt'\n    ","metadata":{"papermill":{"duration":0.099568,"end_time":"2022-04-22T06:00:18.542447","exception":false,"start_time":"2022-04-22T06:00:18.442879","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2025-02-22T09:47:05.967245Z","iopub.execute_input":"2025-02-22T09:47:05.967622Z","iopub.status.idle":"2025-02-22T09:47:05.978101Z","shell.execute_reply.started":"2025-02-22T09:47:05.967587Z","shell.execute_reply":"2025-02-22T09:47:05.976834Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def config_to_dict(cfg):\n    return dict((name, getattr(cfg, name)) for name in dir(cfg) if not name.startswith('__'))","metadata":{"papermill":{"duration":0.033041,"end_time":"2022-04-22T06:00:18.664481","exception":false,"start_time":"2022-04-22T06:00:18.63144","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2025-02-22T09:47:05.979562Z","iopub.execute_input":"2025-02-22T09:47:05.979950Z","iopub.status.idle":"2025-02-22T09:47:05.991951Z","shell.execute_reply.started":"2025-02-22T09:47:05.979914Z","shell.execute_reply":"2025-02-22T09:47:05.990722Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv(Config.train_path)","metadata":{"papermill":{"duration":0.035353,"end_time":"2022-04-22T06:01:17.283888","exception":false,"start_time":"2022-04-22T06:01:17.248535","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2025-02-22T09:47:05.993408Z","iopub.execute_input":"2025-02-22T09:47:05.993770Z","iopub.status.idle":"2025-02-22T09:47:06.122236Z","shell.execute_reply.started":"2025-02-22T09:47:05.993734Z","shell.execute_reply":"2025-02-22T09:47:06.120977Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#test_files = [Config.test_path+f for f in sorted(os.listdir(Config.test_path))]\n\n#if len(test_files) == 1:\n#    Config.test_path = '/kaggle/input/birdclef-2024/unlabeled_soundscapes/'\n\n#Config.test_path = '/kaggle/input/birdclef-2024/unlabeled_soundscapes/'\n    ","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.123624Z","iopub.execute_input":"2025-02-22T09:47:06.123988Z","iopub.status.idle":"2025-02-22T09:47:06.128720Z","shell.execute_reply.started":"2025-02-22T09:47:06.123956Z","shell.execute_reply":"2025-02-22T09:47:06.127554Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if len(glob(f'{Config.data_root}/test_soundscapes/*.ogg')) > 0:\n    ogg_file_paths = glob(f'{Config.data_root}/test_soundscapes/*.ogg')\nelse:\n    ogg_file_paths = sorted(glob(f'{Config.data_root}/unlabeled_soundscapes/*.ogg'))[:20]","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.130132Z","iopub.execute_input":"2025-02-22T09:47:06.130467Z","iopub.status.idle":"2025-02-22T09:47:06.176467Z","shell.execute_reply.started":"2025-02-22T09:47:06.130435Z","shell.execute_reply":"2025-02-22T09:47:06.175280Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test = pd.DataFrame(\n     [(path.split('/')[-1][:-4], path.split('/')[-1], path) for path in ogg_file_paths],\n    columns = [\"fileid\", \"filename\" , \"path\"]\n)\nprint(df_test.shape)\n\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.181791Z","iopub.execute_input":"2025-02-22T09:47:06.182822Z","iopub.status.idle":"2025-02-22T09:47:06.207986Z","shell.execute_reply.started":"2025-02-22T09:47:06.182763Z","shell.execute_reply":"2025-02-22T09:47:06.206450Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.fileid[0]","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.209880Z","iopub.execute_input":"2025-02-22T09:47:06.210351Z","iopub.status.idle":"2025-02-22T09:47:06.219183Z","shell.execute_reply.started":"2025-02-22T09:47:06.210302Z","shell.execute_reply":"2025-02-22T09:47:06.217620Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df_test = pd.concat([df_test,df_test,df_test]).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.220525Z","iopub.execute_input":"2025-02-22T09:47:06.221919Z","iopub.status.idle":"2025-02-22T09:47:06.230049Z","shell.execute_reply.started":"2025-02-22T09:47:06.221865Z","shell.execute_reply":"2025-02-22T09:47:06.228504Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.signal import butter, sosfilt, lfilter\n\ndef butter_bandpass(lowcut, highcut, fs, order=5):\n    nyq = 0.5 * fs\n    low = lowcut / nyq\n    high = highcut / nyq\n    b, a = butter(order, [low, high], btype='band')\n    return b, a\n\n\ndef butter_bandpass_filter(data, lowcut, highcut, fs, order=5):\n    b, a = butter_bandpass(lowcut, highcut, fs, order=order)\n    y = lfilter(b, a, data)\n    return y","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.231831Z","iopub.execute_input":"2025-02-22T09:47:06.232413Z","iopub.status.idle":"2025-02-22T09:47:06.244596Z","shell.execute_reply.started":"2025-02-22T09:47:06.232363Z","shell.execute_reply":"2025-02-22T09:47:06.243086Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def compute_complex_spec(y, sr, n_mels, fmin, fmax, power, nfft, hoplength):\n    \"\"\"\n    Computes STFT\n    Arguments:\n        y {np array} -- signal\n        params {AudioParams} -- Parameters to use for the spectrogram. Expected to have the attributes sr, n_mels, f_min, f_max\n    Returns:\n        - Absolute-spectrogram\n    \"\"\"\n    \n    #spec = librosa.stft(y=y, n_fft=nfft, hop_length=hoplength, win_length=nfft, window='hann')\n    window = torch.hann_window(nfft)\n    \n    spec = torch.stft(input=y, n_fft=nfft, hop_length=hoplength, win_length=nfft, window=window, return_complex=True)\n    \n    return torch.abs(spec)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:47:06.246626Z","iopub.execute_input":"2025-02-22T09:47:06.247099Z","iopub.status.idle":"2025-02-22T09:47:06.264571Z","shell.execute_reply.started":"2025-02-22T09:47:06.247052Z","shell.execute_reply":"2025-02-22T09:47:06.263427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def normalize_spec(spec, constPres, normalize_wrt):\n    \"\"\"\n    Computes normalization of spectragramm\n    Arguments:\n        spec {np array} -- stft values\n        params {} -- absolute pressure, reference to choose from: mean, median, bandpassMean, bandpassMedian\n    Returns:\n        - Normalized spectrogram\n    \"\"\"\n\n    fbins, tbins = spec.shape\n    #print(spec.shape)\n\n    if normalize_wrt == \"meanPresBand\":\n        normalization_value = torch.mean(spec[fbins // 2: 3*fbins//4,:])\n        spec = spec * constPres / normalization_value\n    elif normlaize_wrt == \"mean\":\n        pass\n    elif normlaize_wrt == \"median\":\n        pass\n        \n    return spec","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:47:06.266067Z","iopub.execute_input":"2025-02-22T09:47:06.266468Z","iopub.status.idle":"2025-02-22T09:47:06.280414Z","shell.execute_reply.started":"2025-02-22T09:47:06.266434Z","shell.execute_reply":"2025-02-22T09:47:06.279149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class BirdDataset(torch.utils.data.Dataset):\n\n    def __init__(self, data, sr = Config.SR, duration = Config.DURATION, cfg = Config, train = False,\n                res_type=\"kaiser_fast\", resample=True,):\n        \n        self.data = data\n        self.sr = sr  \n        \n        self.res_type = res_type\n        self.resample = resample\n\n        self.duration = duration\n        self.audio_length = Config.Audio_length\n        self.step = self.audio_length\n        \n        self.n_mels = 128\n        self.fmin = cfg.fmin\n        self.fmax = cfg.fmax or self.sr//2\n        self.power = cfg.power\n        self.nfft = cfg.nfft\n        self.hoplength = cfg.hop\n        self.constPres = cfg.constPres\n        self.normalize_wrt = cfg.normalize_wrt\n        self.normalize_spec = cfg.normalize_spec\n        \n\n    def __len__(self):\n        return len(self.data)\n\n    @staticmethod\n    def normalize(image):\n        image = image / 255.0\n        #image = torch.stack([image, image, image])\n        return image\n    \n    def audio_to_image(self, audio):\n        \n        spec = compute_complex_spec(audio,self.sr,self.n_mels,self.fmin,self.fmax,self.power,self.nfft, self.hoplength) \n\n        if self.normalize_spec:\n            spec = normalize_spec(spec, self.constPres, self.normalize_wrt)\n        \n        return spec\n    \n    def read_file(self, filepath):\n        audio, orig_sr = sf.read(filepath, dtype=\"float32\")\n        #audio, orig_sr = torchaudio.load(filepath, frame_offset=0)\n        #print(audio.shape)\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        if Config.bandpass:\n            audio = butter_bandpass_filter(audio,Config.bandpass_min,Config.bandpass_max,fs=orig_sr,order=Config.bandpass_order)\n        \n        audio = torch.Tensor(audio)\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 = []\n            \n        for audio in audios:\n            #image = self.audio_to_image(torch.from_numpy(audio))\n            image = self.audio_to_image(audio)\n            #print(image.shape)\n            image = torch.stack([image, image, image])\n            #print(image.shape)\n            images.append(image)\n\n        images = np.stack(images)\n        \n        \n        return images\n    \n        \n    def __getitem__(self, idx):\n        return self.read_file(self.data.loc[idx, \"path\"])","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.282296Z","iopub.execute_input":"2025-02-22T09:47:06.282792Z","iopub.status.idle":"2025-02-22T09:47:06.301789Z","shell.execute_reply.started":"2025-02-22T09:47:06.282739Z","shell.execute_reply":"2025-02-22T09:47:06.300400Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ds_test = BirdDataset(\n    df_test, \n    sr = Config.SR,\n    duration = Config.DURATION,\n    cfg = Config,\n)","metadata":{"papermill":{"duration":0.036289,"end_time":"2022-04-22T06:01:17.539606","exception":false,"start_time":"2022-04-22T06:01:17.503317","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2025-02-22T09:47:06.303374Z","iopub.execute_input":"2025-02-22T09:47:06.303731Z","iopub.status.idle":"2025-02-22T09:47:06.320172Z","shell.execute_reply.started":"2025-02-22T09:47:06.303680Z","shell.execute_reply":"2025-02-22T09:47:06.318917Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"en = range(len(ds_test))\nen","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:47:06.321883Z","iopub.execute_input":"2025-02-22T09:47:06.322346Z","iopub.status.idle":"2025-02-22T09:47:06.334933Z","shell.execute_reply.started":"2025-02-22T09:47:06.322297Z","shell.execute_reply":"2025-02-22T09:47:06.333714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def my_sigmoid(x):\n    return 1 / (1 + np.exp(-x))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:47:06.336256Z","iopub.execute_input":"2025-02-22T09:47:06.336583Z","iopub.status.idle":"2025-02-22T09:47:06.348312Z","shell.execute_reply.started":"2025-02-22T09:47:06.336550Z","shell.execute_reply":"2025-02-22T09:47:06.347207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_batch(img_ds, num_items, num_rows, num_cols, predict_arr=None):\n    fig = plt.figure(figsize=(12, 6))    \n    img_index = np.random.randint(0, 48, size=num_items)\n    index=0\n    print(img_index)\n    for ind in img_index:  # list first 9 images\n        img = img_ds[0][ind]\n        \n        ax = fig.add_subplot(num_rows, num_cols, index + 1, xticks=[], yticks=[])\n        if isinstance(img, torch.Tensor):\n            img = img.detach().numpy()\n        if isinstance(img, np.ndarray):\n            #img = img.transpose(1, 2, 0)\n            #ax.imshow(img)     \n            #print(img.shape)\n            ax.pcolormesh(img[0,:,:])\n            \n        title = f\"Spec\"\n        ax.set_title(title)  \n        index+=1","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.349629Z","iopub.execute_input":"2025-02-22T09:47:06.349995Z","iopub.status.idle":"2025-02-22T09:47:06.362458Z","shell.execute_reply.started":"2025-02-22T09:47:06.349952Z","shell.execute_reply":"2025-02-22T09:47:06.361182Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#show_batch(ds_test, 4, 2, 2)","metadata":{"papermill":{"duration":0.584852,"end_time":"2022-04-22T06:01:18.338238","exception":false,"start_time":"2022-04-22T06:01:17.753386","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2025-02-22T09:47:06.363763Z","iopub.execute_input":"2025-02-22T09:47:06.364071Z","iopub.status.idle":"2025-02-22T09:47:06.380053Z","shell.execute_reply.started":"2025-02-22T09:47:06.364043Z","shell.execute_reply":"2025-02-22T09:47:06.378704Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\ntick = time.time()","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:06.381803Z","iopub.execute_input":"2025-02-22T09:47:06.382854Z","iopub.status.idle":"2025-02-22T09:47:06.390983Z","shell.execute_reply.started":"2025-02-22T09:47:06.382801Z","shell.execute_reply":"2025-02-22T09:47:06.389933Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import onnxruntime as ort\nimport torch\nimport numpy as np\n\n# Load ONNX model\nsession = ort.InferenceSession(Config.model_output[0], providers=[\"CPUExecutionProvider\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:47:06.392421Z","iopub.execute_input":"2025-02-22T09:47:06.392850Z","iopub.status.idle":"2025-02-22T09:47:06.660401Z","shell.execute_reply.started":"2025-02-22T09:47:06.392806Z","shell.execute_reply":"2025-02-22T09:47:06.659280Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get model input details\ninput_name = session.get_inputs()[0].name\ninput_shape = session.get_inputs()[0].shape\nprint(f\"Model expects input name: {input_name}\")\nprint(f\"Model expects input shape: {input_shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:47:06.662473Z","iopub.execute_input":"2025-02-22T09:47:06.663086Z","iopub.status.idle":"2025-02-22T09:47:06.670732Z","shell.execute_reply.started":"2025-02-22T09:47:06.663021Z","shell.execute_reply":"2025-02-22T09:47:06.669536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = []\nfor i in range(len(ds_test)):\n    spec = ds_test[i].astype(np.float32) \n    outputs = session.run(None, {'input': spec})\n    preds.append(my_sigmoid(np.array(outputs).squeeze(0)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T09:47:06.672067Z","iopub.execute_input":"2025-02-22T09:47:06.672368Z","iopub.status.idle":"2025-02-22T09:47:50.475111Z","shell.execute_reply.started":"2025-02-22T09:47:06.672339Z","shell.execute_reply":"2025-02-22T09:47:50.473901Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tock = time.time()","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:50.476442Z","iopub.execute_input":"2025-02-22T09:47:50.476819Z","iopub.status.idle":"2025-02-22T09:47:50.482704Z","shell.execute_reply.started":"2025-02-22T09:47:50.476783Z","shell.execute_reply":"2025-02-22T09:47:50.481446Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_time = (tock - tick) * 55  # Calculate estimated submission time for ~1100 recordings\nsub_time = time.gmtime(sub_time)  # Convert seconds to a time tuple\nsub_time = time.strftime(\"%H hr: %M min : %S sec\", sub_time)  # Format time tuple as string\nprint(f\">> Time for prediction: ~ {sub_time}\")  # Print estimated submission time","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:50.484253Z","iopub.execute_input":"2025-02-22T09:47:50.484698Z","iopub.status.idle":"2025-02-22T09:47:50.496207Z","shell.execute_reply.started":"2025-02-22T09:47:50.484660Z","shell.execute_reply":"2025-02-22T09:47:50.494990Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fileids = df_test.fileid.values.tolist()\n\nbird_cols = list(pd.get_dummies(df_train['primary_label']).columns) \nclmns = ['row_id'] + bird_cols\nsub_df = pd.DataFrame(columns=clmns)","metadata":{"papermill":{"duration":0.052364,"end_time":"2022-04-22T06:01:22.708806","exception":false,"start_time":"2022-04-22T06:01:22.656442","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2025-02-22T09:47:50.500881Z","iopub.execute_input":"2025-02-22T09:47:50.501881Z","iopub.status.idle":"2025-02-22T09:47:50.524207Z","shell.execute_reply.started":"2025-02-22T09:47:50.501840Z","shell.execute_reply":"2025-02-22T09:47:50.523012Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:50.525438Z","iopub.execute_input":"2025-02-22T09:47:50.525814Z","iopub.status.idle":"2025-02-22T09:47:50.540375Z","shell.execute_reply.started":"2025-02-22T09:47:50.525779Z","shell.execute_reply":"2025-02-22T09:47:50.539107Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Generate Submission csv","metadata":{}},{"cell_type":"code","source":"for i, file in enumerate(fileids):\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=clmns)\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":"2025-02-22T09:47:50.542209Z","iopub.execute_input":"2025-02-22T09:47:50.542681Z","iopub.status.idle":"2025-02-22T09:47:51.404103Z","shell.execute_reply.started":"2025-02-22T09:47:50.542613Z","shell.execute_reply":"2025-02-22T09:47:51.402853Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:51.405570Z","iopub.execute_input":"2025-02-22T09:47:51.405973Z","iopub.status.idle":"2025-02-22T09:47:51.438769Z","shell.execute_reply.started":"2025-02-22T09:47:51.405936Z","shell.execute_reply":"2025-02-22T09:47:51.437598Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2025-02-22T09:47:51.440277Z","iopub.execute_input":"2025-02-22T09:47:51.440617Z","iopub.status.idle":"2025-02-22T09:47:51.641430Z","shell.execute_reply.started":"2025-02-22T09:47:51.440582Z","shell.execute_reply":"2025-02-22T09:47:51.640201Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}