{
  "id": 575567,
  "title": "Submission Scoring Error",
  "url": "/competitions/birdclef-2025/discussion/575567",
  "author_name": "",
  "post_date": "2025-04-29T13:54:42.611972100Z",
  "votes": null,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Hey everybody, </p>\n<p>I have trained a CNN on spectrograms created by splitting each audio fragment into 5 seconds and then converting these audios into spectrograms. I load the CNN in a second notebook and then try to make the submision.</p>\n<p>I keep getting the same submission scoring error when I try to submit. Could anybody help me try and find the issue? The output file looks exactly how it is supposed to be. See the code below;</p>\n<pre><code> os\n torch\n torch.nn  nn\n librosa\n numpy  np\n pandas  pd\n torch.utils.data  Dataset, DataLoader\n tqdm  tqdm\n re\n\n\nTEST_DIR = \nMODEL_PATH = \nSUBMIT_PATH = \nSR = \nCHUNK_LEN = \nN_MELS = \nBATCH_SIZE = \ndevice = torch.device(  torch.cuda.is_available()  )\n\n\nunique_labels = [\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , \n]\n\nidx2label = {i: label  i, label  (unique_labels)}\n\n\n ():\n    samples_per_chunk = chunk_length * sr\n     [audio[i:i+samples_per_chunk]  i  (, (audio), samples_per_chunk)]\n\n ():\n    mel = librosa.feature.melspectrogram(y=chunk, sr=SR, n_mels=N_MELS)\n    mel_db = librosa.power_to_db(mel, ref=np.)\n    mel_db -= mel_db.()\n    mel_db /= mel_db.()\n     mel_db\n\n\n (nn.Module):\n     ():\n        ().__init__()\n        .cnn = nn.Sequential(\n            nn.Conv2d(, , , padding=), nn.ReLU(), nn.MaxPool2d(),\n            nn.Conv2d(, , , padding=), nn.ReLU(), nn.MaxPool2d(),\n            nn.Conv2d(, , , padding=), nn.ReLU(), nn.AdaptiveAvgPool2d((, )),\n        )\n        .fc = nn.Linear(, num_classes)\n\n     ():\n        x = .cnn(x)\n        x = x.view(x.size(), -)\n         .fc(x)\n\nmodel = CNNModel(num_classes=(unique_labels)).to(device)\nmodel.load_state_dict(torch.load(MODEL_PATH, map_location=device))\nmodel.()\n\n\nrows = []\n fname  (os.listdir(TEST_DIR)):\n      fname.endswith(): \n    path = os.path.join(TEST_DIR, fname)\n    audio, _ = librosa.load(path, sr=SR)\n    chunks = split_audio(audio)\n\n     i, chunk  (chunks):\n         (chunk) &lt; CHUNK_LEN * SR:\n            chunk = np.pad(chunk, (, CHUNK_LEN*SR - (chunk)))\n\n        mel = to_mel_spectrogram(chunk)\n        mel_tensor = torch.tensor(mel).unsqueeze().unsqueeze().().to(device)  \n\n         torch.no_grad():\n            logits = model(mel_tensor)\n            probs = torch.softmax(logits, dim=).cpu().numpy().flatten()\n\n        row_id = re.split(, fname)[]\n        row_id = \n        row = [row_id] + probs.tolist()\n        rows.append(row)\n\n\ncolumns = [] + unique_labels\ndf = pd.DataFrame(rows, columns=columns)\ndf.to_csv(SUBMIT_PATH, index=)\n()\n</code></pre>",
  "messages": [
    {
      "id": "3189630",
      "postDate": "04/29/2025 13:54:42",
      "content": "<p>Hey everybody, </p>\n<p>I have trained a CNN on spectrograms created by splitting each audio fragment into 5 seconds and then converting these audios into spectrograms. I load the CNN in a second notebook and then try to make the submision.</p>\n<p>I keep getting the same submission scoring error when I try to submit. Could anybody help me try and find the issue? The output file looks exactly how it is supposed to be. See the code below;</p>\n<pre><code> os\n torch\n torch.nn  nn\n librosa\n numpy  np\n pandas  pd\n torch.utils.data  Dataset, DataLoader\n tqdm  tqdm\n re\n\n\nTEST_DIR = \nMODEL_PATH = \nSUBMIT_PATH = \nSR = \nCHUNK_LEN = \nN_MELS = \nBATCH_SIZE = \ndevice = torch.device(  torch.cuda.is_available()  )\n\n\nunique_labels = [\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , , , , , ,\n    , , , , , \n]\n\nidx2label = {i: label  i, label  (unique_labels)}\n\n\n ():\n    samples_per_chunk = chunk_length * sr\n     [audio[i:i+samples_per_chunk]  i  (, (audio), samples_per_chunk)]\n\n ():\n    mel = librosa.feature.melspectrogram(y=chunk, sr=SR, n_mels=N_MELS)\n    mel_db = librosa.power_to_db(mel, ref=np.)\n    mel_db -= mel_db.()\n    mel_db /= mel_db.()\n     mel_db\n\n\n (nn.Module):\n     ():\n        ().__init__()\n        .cnn = nn.Sequential(\n            nn.Conv2d(, , , padding=), nn.ReLU(), nn.MaxPool2d(),\n            nn.Conv2d(, , , padding=), nn.ReLU(), nn.MaxPool2d(),\n            nn.Conv2d(, , , padding=), nn.ReLU(), nn.AdaptiveAvgPool2d((, )),\n        )\n        .fc = nn.Linear(, num_classes)\n\n     ():\n        x = .cnn(x)\n        x = x.view(x.size(), -)\n         .fc(x)\n\nmodel = CNNModel(num_classes=(unique_labels)).to(device)\nmodel.load_state_dict(torch.load(MODEL_PATH, map_location=device))\nmodel.()\n\n\nrows = []\n fname  (os.listdir(TEST_DIR)):\n      fname.endswith(): \n    path = os.path.join(TEST_DIR, fname)\n    audio, _ = librosa.load(path, sr=SR)\n    chunks = split_audio(audio)\n\n     i, chunk  (chunks):\n         (chunk) &lt; CHUNK_LEN * SR:\n            chunk = np.pad(chunk, (, CHUNK_LEN*SR - (chunk)))\n\n        mel = to_mel_spectrogram(chunk)\n        mel_tensor = torch.tensor(mel).unsqueeze().unsqueeze().().to(device)  \n\n         torch.no_grad():\n            logits = model(mel_tensor)\n            probs = torch.softmax(logits, dim=).cpu().numpy().flatten()\n\n        row_id = re.split(, fname)[]\n        row_id = \n        row = [row_id] + probs.tolist()\n        rows.append(row)\n\n\ncolumns = [] + unique_labels\ndf = pd.DataFrame(rows, columns=columns)\ndf.to_csv(SUBMIT_PATH, index=)\n()\n</code></pre>",
      "rawMarkdown": "Hey everybody, \n\nI have trained a CNN on spectrograms created by splitting each audio fragment into 5 seconds and then converting these audios into spectrograms. I load the CNN in a second notebook and then try to make the submision.\n\nI keep getting the same submission scoring error when I try to submit. Could anybody help me try and find the issue? The output file looks exactly how it is supposed to be. See the code below;\n\n```python\nimport os\nimport torch\nimport torch.nn as nn\nimport librosa\nimport numpy as np\nimport pandas as pd\nfrom torch.utils.data import Dataset, DataLoader\nfrom tqdm import tqdm\nimport re\n\n# --- Settings ---\nTEST_DIR = '/kaggle/input/birdclef-2025/test_soundscapes/'\nMODEL_PATH = '/kaggle/input/birclefcnnmodel/pytorch/default/1/birdclef_cnn.pth'\nSUBMIT_PATH = '/kaggle/working/submission.csv'\nSR = 32000\nCHUNK_LEN = 5\nN_MELS = 128\nBATCH_SIZE = 32\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# --- Load label mapping from training ---\nunique_labels = [\n    \"1139490\", \"1192948\", \"1194042\", \"126247\", \"1346504\", \"134933\", \"135045\", \"1462711\", \"1462737\", \"1564122\",\n    \"21038\", \"21116\", \"21211\", \"22333\", \"22973\", \"22976\", \"24272\", \"24292\", \"24322\", \"41663\",\n    \"41778\", \"41970\", \"42007\", \"42087\", \"42113\", \"46010\", \"47067\", \"476537\", \"476538\", \"48124\",\n    \"50186\", \"517119\", \"523060\", \"528041\", \"52884\", \"548639\", \"555086\", \"555142\", \"566513\", \"64862\",\n    \"65336\", \"65344\", \"65349\", \"65373\", \"65419\", \"65448\", \"65547\", \"65962\", \"66016\", \"66531\",\n    \"66578\", \"66893\", \"67082\", \"67252\", \"714022\", \"715170\", \"787625\", \"81930\", \"868458\", \"963335\",\n    \"amakin1\", \"amekes\", \"ampkin1\", \"anhing\", \"babwar\", \"bafibi1\", \"banana\", \"baymac\", \"bbwduc\", \"bicwre1\",\n    \"bkcdon\", \"bkmtou1\", \"blbgra1\", \"blbwre1\", \"blcant4\", \"blchaw1\", \"blcjay1\", \"blctit1\", \"blhpar1\", \"blkvul\",\n    \"bobfly1\", \"bobher1\", \"brtpar1\", \"bubcur1\", \"bubwre1\", \"bucmot3\", \"bugtan\", \"butsal1\", \"cargra1\", \"cattyr\",\n    \"chbant1\", \"chfmac1\", \"cinbec1\", \"cocher1\", \"cocwoo1\", \"colara1\", \"colcha1\", \"compau\", \"compot1\", \"cotfly1\",\n    \"crbtan1\", \"crcwoo1\", \"crebob1\", \"cregua1\", \"creoro1\", \"eardov1\", \"fotfly\", \"gohman1\", \"grasal4\", \"grbhaw1\",\n    \"greani1\", \"greegr\", \"greibi1\", \"grekis\", \"grepot1\", \"gretin1\", \"grnkin\", \"grysee1\", \"gybmar\", \"gycwor1\",\n    \"labter1\", \"laufal1\", \"leagre\", \"linwoo1\", \"littin1\", \"mastit1\", \"neocor\", \"norscr1\", \"olipic1\", \"orcpar\",\n    \"palhor2\", \"paltan1\", \"pavpig2\", \"piepuf1\", \"pirfly1\", \"piwtyr1\", \"plbwoo1\", \"plctan1\", \"plukit1\", \"purgal2\",\n    \"ragmac1\", \"rebbla1\", \"recwoo1\", \"rinkin1\", \"roahaw\", \"rosspo1\", \"royfly1\", \"rtlhum\", \"rubsee1\", \"rufmot1\",\n    \"rugdov\", \"rumfly1\", \"ruther1\", \"rutjac1\", \"rutpuf1\", \"saffin\", \"sahpar1\", \"savhaw1\", \"secfly1\", \"shghum1\",\n    \"shtfly1\", \"smbani\", \"snoegr\", \"sobtyr1\", \"socfly1\", \"solsan\", \"soulap1\", \"spbwoo1\", \"speowl1\", \"spepar1\",\n    \"srwswa1\", \"stbwoo2\", \"strcuc1\", \"strfly1\", \"strher\", \"strowl1\", \"tbsfin1\", \"thbeup1\", \"thlsch3\", \"trokin\",\n    \"tropar\", \"trsowl\", \"turvul\", \"verfly\", \"watjac1\", \"wbwwre1\", \"whbant1\", \"whbman1\", \"whfant1\", \"whmtyr1\",\n    \"whtdov\", \"whttro1\", \"whwswa1\", \"woosto\", \"y00678\", \"yebela1\", \"yebfly1\", \"yebsee1\", \"yecspi2\", \"yectyr1\",\n    \"yehbla2\", \"yehcar1\", \"yelori1\", \"yeofly1\", \"yercac1\", \"ywcpar\"\n]\n\nidx2label = {i: label for i, label in enumerate(unique_labels)}\n\n# --- Define preprocessing ---\ndef split_audio(audio, sr=SR, chunk_length=CHUNK_LEN):\n    samples_per_chunk = chunk_length * sr\n    return [audio[i:i+samples_per_chunk] for i in range(0, len(audio), samples_per_chunk)]\n\ndef to_mel_spectrogram(chunk):\n    mel = librosa.feature.melspectrogram(y=chunk, sr=SR, n_mels=N_MELS)\n    mel_db = librosa.power_to_db(mel, ref=np.max)\n    mel_db -= mel_db.min()\n    mel_db /= mel_db.max()\n    return mel_db\n\n# --- Load model ---\nclass CNNModel(nn.Module):\n    def __init__(self, num_classes):\n        super().__init__()\n        self.cnn = nn.Sequential(\n            nn.Conv2d(1, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),\n            nn.Conv2d(16, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),\n            nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d((1, 1)),\n        )\n        self.fc = nn.Linear(64, num_classes)\n\n    def forward(self, x):\n        x = self.cnn(x)\n        x = x.view(x.size(0), -1)\n        return self.fc(x)\n\nmodel = CNNModel(num_classes=len(unique_labels)).to(device)\nmodel.load_state_dict(torch.load(MODEL_PATH, map_location=device))\nmodel.eval()\n\n# --- Process test files ---\nrows = []\nfor fname in sorted(os.listdir(TEST_DIR)):\n    if not fname.endswith('.ogg'): continue\n    path = os.path.join(TEST_DIR, fname)\n    audio, _ = librosa.load(path, sr=SR)\n    chunks = split_audio(audio)\n\n    for i, chunk in enumerate(chunks):\n        if len(chunk) < CHUNK_LEN * SR:\n            chunk = np.pad(chunk, (0, CHUNK_LEN*SR - len(chunk)))\n\n        mel = to_mel_spectrogram(chunk)\n        mel_tensor = torch.tensor(mel).unsqueeze(0).unsqueeze(0).float().to(device)  # (1, 1, n_mels, time)\n\n        with torch.no_grad():\n            logits = model(mel_tensor)\n            probs = torch.softmax(logits, dim=1).cpu().numpy().flatten()\n\n        row_id = re.split(\"_\", fname)[1]\n        row_id = f\"soundscape_{fname.split('_')[1]}_{(i + 1) * CHUNK_LEN}\"\n        row = [row_id] + probs.tolist()\n        rows.append(row)\n\n# --- Create submission DataFrame ---\ncolumns = ['row_id'] + unique_labels\ndf = pd.DataFrame(rows, columns=columns)\ndf.to_csv(SUBMIT_PATH, index=False)\nprint(f\"✅ Submission saved to {SUBMIT_PATH}\")\n```",
      "votes": null
    },
    {
      "id": "3190458",
      "postDate": "04/30/2025 17:25:54",
      "content": "<p>Does it run in interactive mode / when you save it and only breaks when submitting?  What type of error do you receive?  Make sure the model exists as a dataset in your environment.  Run locally and let us know the error :)</p>",
      "rawMarkdown": "Does it run in interactive mode / when you save it and only breaks when submitting?  What type of error do you receive?  Make sure the model exists as a dataset in your environment.  Run locally and let us know the error :)",
      "votes": null
    },
    {
      "id": "3191030",
      "postDate": "05/01/2025 09:39:27",
      "content": "<p>Apparently there was something wrong with the labeling but I managed to fix it now thanks for your reply!</p>",
      "rawMarkdown": "Apparently there was something wrong with the labeling but I managed to fix it now thanks for your reply!",
      "votes": null
    },
    {
      "id": "3200725",
      "postDate": "05/13/2025 00:58:56",
      "content": "<p>Hello Sem,</p>\n<p>I think I also run into the same issues that you just had here. I trained a model on one notebook, saved the model, and then used that model for inference in the submission notebook. It still failed even the logs show that it run successfully. Curious on what you did to fix it. Here's my notebook: <a href=\"https://www.kaggle.com/code/locnguyen14/birdclef-2025-submission\" target=\"_blank\">https://www.kaggle.com/code/locnguyen14/birdclef-2025-submission</a></p>\n<p>Thank you so much! </p>",
      "rawMarkdown": "Hello Sem,\n\nI think I also run into the same issues that you just had here. I trained a model on one notebook, saved the model, and then used that model for inference in the submission notebook. It still failed even the logs show that it run successfully. Curious on what you did to fix it. Here's my notebook: https://www.kaggle.com/code/locnguyen14/birdclef-2025-submission\n\nThank you so much!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 3190458,
      "author_name": "connorjd",
      "author_url": "",
      "post_date": "04/30/2025 17:25:54",
      "content": "<p>Does it run in interactive mode / when you save it and only breaks when submitting?  What type of error do you receive?  Make sure the model exists as a dataset in your environment.  Run locally and let us know the error :)</p>",
      "votes": null,
      "replies": [
        {
          "id": 3191030,
          "author_name": "semverstappen",
          "author_url": "",
          "post_date": "05/01/2025 09:39:27",
          "content": "<p>Apparently there was something wrong with the labeling but I managed to fix it now thanks for your reply!</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 3200725,
      "author_name": "locnguyen14",
      "author_url": "",
      "post_date": "05/13/2025 00:58:56",
      "content": "<p>Hello Sem,</p>\n<p>I think I also run into the same issues that you just had here. I trained a model on one notebook, saved the model, and then used that model for inference in the submission notebook. It still failed even the logs show that it run successfully. Curious on what you did to fix it. Here's my notebook: <a href=\"https://www.kaggle.com/code/locnguyen14/birdclef-2025-submission\" target=\"_blank\">https://www.kaggle.com/code/locnguyen14/birdclef-2025-submission</a></p>\n<p>Thank you so much! </p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "3189630": "Hey everybody, \n\nI have trained a CNN on spectrograms created by splitting each audio fragment into 5 seconds and then converting these audios into spectrograms. I load the CNN in a second notebook and then try to make the submision.\n\nI keep getting the same submission scoring error when I try to submit. Could anybody help me try and find the issue? The output file looks exactly how it is supposed to be. See the code below;\n\n```python\nimport os\nimport torch\nimport torch.nn as nn\nimport librosa\nimport numpy as np\nimport pandas as pd\nfrom torch.utils.data import Dataset, DataLoader\nfrom tqdm import tqdm\nimport re\n\n# --- Settings ---\nTEST_DIR = '/kaggle/input/birdclef-2025/test_soundscapes/'\nMODEL_PATH = '/kaggle/input/birclefcnnmodel/pytorch/default/1/birdclef_cnn.pth'\nSUBMIT_PATH = '/kaggle/working/submission.csv'\nSR = 32000\nCHUNK_LEN = 5\nN_MELS = 128\nBATCH_SIZE = 32\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# --- Load label mapping from training ---\nunique_labels = [\n    \"1139490\", \"1192948\", \"1194042\", \"126247\", \"1346504\", \"134933\", \"135045\", \"1462711\", \"1462737\", \"1564122\",\n    \"21038\", \"21116\", \"21211\", \"22333\", \"22973\", \"22976\", \"24272\", \"24292\", \"24322\", \"41663\",\n    \"41778\", \"41970\", \"42007\", \"42087\", \"42113\", \"46010\", \"47067\", \"476537\", \"476538\", \"48124\",\n    \"50186\", \"517119\", \"523060\", \"528041\", \"52884\", \"548639\", \"555086\", \"555142\", \"566513\", \"64862\",\n    \"65336\", \"65344\", \"65349\", \"65373\", \"65419\", \"65448\", \"65547\", \"65962\", \"66016\", \"66531\",\n    \"66578\", \"66893\", \"67082\", \"67252\", \"714022\", \"715170\", \"787625\", \"81930\", \"868458\", \"963335\",\n    \"amakin1\", \"amekes\", \"ampkin1\", \"anhing\", \"babwar\", \"bafibi1\", \"banana\", \"baymac\", \"bbwduc\", \"bicwre1\",\n    \"bkcdon\", \"bkmtou1\", \"blbgra1\", \"blbwre1\", \"blcant4\", \"blchaw1\", \"blcjay1\", \"blctit1\", \"blhpar1\", \"blkvul\",\n    \"bobfly1\", \"bobher1\", \"brtpar1\", \"bubcur1\", \"bubwre1\", \"bucmot3\", \"bugtan\", \"butsal1\", \"cargra1\", \"cattyr\",\n    \"chbant1\", \"chfmac1\", \"cinbec1\", \"cocher1\", \"cocwoo1\", \"colara1\", \"colcha1\", \"compau\", \"compot1\", \"cotfly1\",\n    \"crbtan1\", \"crcwoo1\", \"crebob1\", \"cregua1\", \"creoro1\", \"eardov1\", \"fotfly\", \"gohman1\", \"grasal4\", \"grbhaw1\",\n    \"greani1\", \"greegr\", \"greibi1\", \"grekis\", \"grepot1\", \"gretin1\", \"grnkin\", \"grysee1\", \"gybmar\", \"gycwor1\",\n    \"labter1\", \"laufal1\", \"leagre\", \"linwoo1\", \"littin1\", \"mastit1\", \"neocor\", \"norscr1\", \"olipic1\", \"orcpar\",\n    \"palhor2\", \"paltan1\", \"pavpig2\", \"piepuf1\", \"pirfly1\", \"piwtyr1\", \"plbwoo1\", \"plctan1\", \"plukit1\", \"purgal2\",\n    \"ragmac1\", \"rebbla1\", \"recwoo1\", \"rinkin1\", \"roahaw\", \"rosspo1\", \"royfly1\", \"rtlhum\", \"rubsee1\", \"rufmot1\",\n    \"rugdov\", \"rumfly1\", \"ruther1\", \"rutjac1\", \"rutpuf1\", \"saffin\", \"sahpar1\", \"savhaw1\", \"secfly1\", \"shghum1\",\n    \"shtfly1\", \"smbani\", \"snoegr\", \"sobtyr1\", \"socfly1\", \"solsan\", \"soulap1\", \"spbwoo1\", \"speowl1\", \"spepar1\",\n    \"srwswa1\", \"stbwoo2\", \"strcuc1\", \"strfly1\", \"strher\", \"strowl1\", \"tbsfin1\", \"thbeup1\", \"thlsch3\", \"trokin\",\n    \"tropar\", \"trsowl\", \"turvul\", \"verfly\", \"watjac1\", \"wbwwre1\", \"whbant1\", \"whbman1\", \"whfant1\", \"whmtyr1\",\n    \"whtdov\", \"whttro1\", \"whwswa1\", \"woosto\", \"y00678\", \"yebela1\", \"yebfly1\", \"yebsee1\", \"yecspi2\", \"yectyr1\",\n    \"yehbla2\", \"yehcar1\", \"yelori1\", \"yeofly1\", \"yercac1\", \"ywcpar\"\n]\n\nidx2label = {i: label for i, label in enumerate(unique_labels)}\n\n# --- Define preprocessing ---\ndef split_audio(audio, sr=SR, chunk_length=CHUNK_LEN):\n    samples_per_chunk = chunk_length * sr\n    return [audio[i:i+samples_per_chunk] for i in range(0, len(audio), samples_per_chunk)]\n\ndef to_mel_spectrogram(chunk):\n    mel = librosa.feature.melspectrogram(y=chunk, sr=SR, n_mels=N_MELS)\n    mel_db = librosa.power_to_db(mel, ref=np.max)\n    mel_db -= mel_db.min()\n    mel_db /= mel_db.max()\n    return mel_db\n\n# --- Load model ---\nclass CNNModel(nn.Module):\n    def __init__(self, num_classes):\n        super().__init__()\n        self.cnn = nn.Sequential(\n            nn.Conv2d(1, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),\n            nn.Conv2d(16, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),\n            nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d((1, 1)),\n        )\n        self.fc = nn.Linear(64, num_classes)\n\n    def forward(self, x):\n        x = self.cnn(x)\n        x = x.view(x.size(0), -1)\n        return self.fc(x)\n\nmodel = CNNModel(num_classes=len(unique_labels)).to(device)\nmodel.load_state_dict(torch.load(MODEL_PATH, map_location=device))\nmodel.eval()\n\n# --- Process test files ---\nrows = []\nfor fname in sorted(os.listdir(TEST_DIR)):\n    if not fname.endswith('.ogg'): continue\n    path = os.path.join(TEST_DIR, fname)\n    audio, _ = librosa.load(path, sr=SR)\n    chunks = split_audio(audio)\n\n    for i, chunk in enumerate(chunks):\n        if len(chunk) < CHUNK_LEN * SR:\n            chunk = np.pad(chunk, (0, CHUNK_LEN*SR - len(chunk)))\n\n        mel = to_mel_spectrogram(chunk)\n        mel_tensor = torch.tensor(mel).unsqueeze(0).unsqueeze(0).float().to(device)  # (1, 1, n_mels, time)\n\n        with torch.no_grad():\n            logits = model(mel_tensor)\n            probs = torch.softmax(logits, dim=1).cpu().numpy().flatten()\n\n        row_id = re.split(\"_\", fname)[1]\n        row_id = f\"soundscape_{fname.split('_')[1]}_{(i + 1) * CHUNK_LEN}\"\n        row = [row_id] + probs.tolist()\n        rows.append(row)\n\n# --- Create submission DataFrame ---\ncolumns = ['row_id'] + unique_labels\ndf = pd.DataFrame(rows, columns=columns)\ndf.to_csv(SUBMIT_PATH, index=False)\nprint(f\"✅ Submission saved to {SUBMIT_PATH}\")\n```",
    "3190458": "Does it run in interactive mode / when you save it and only breaks when submitting?  What type of error do you receive?  Make sure the model exists as a dataset in your environment.  Run locally and let us know the error :)",
    "3191030": "Apparently there was something wrong with the labeling but I managed to fix it now thanks for your reply!",
    "3200725": "Hello Sem,\n\nI think I also run into the same issues that you just had here. I trained a model on one notebook, saved the model, and then used that model for inference in the submission notebook. It still failed even the logs show that it run successfully. Curious on what you did to fix it. Here's my notebook: https://www.kaggle.com/code/locnguyen14/birdclef-2025-submission\n\nThank you so much!"
  },
  "source": "meta"
}