{
  "id": 479712,
  "title": "Notebook Threw Exception. Need help",
  "url": "/competitions/hms-harmful-brain-activity-classification/discussion/479712",
  "author_name": "",
  "post_date": "2024-02-25T17:01:09.569236800Z",
  "votes": null,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Hi! I am getting \"Notebook Threw Exception\" while I am doing the inference on the test data. I don't know what is causing this issue. The inference code I took it from here and changed it a bit:<br>\n<a href=\"https://www.kaggle.com/code/alejopaullier/hms-efficientnetb0-pytorch-inference\" target=\"_blank\">HMS | EfficientNetB0 PyTorch [Inference]</a></p>\n<pre><code> ():\n    model.()\n    softmax = nn.Softmax(dim=)\n    prediction_dict = {}\n    preds = []\n    eeg_ids = []\n     tqdm(test_loader, unit=, desc=)  tqdm_test_loader:\n         step, (eeg_id, X)  (tqdm_test_loader):\n            X = X.to(device)\n             torch.no_grad():\n                y_preds = model(X)\n            y_preds = softmax(y_preds)\n            preds.append(y_preds.to().numpy())\n            eeg_ids.append(eeg_id)\n\n    prediction_dict[] = np.concatenate(preds)\n    prediction_dict[] = np.array(eeg_ids)\n     prediction_dict\n\n\n\npredictions = []\neeg_ids = []\n\n f_model  fold_models:\n    test_dataset = SpectrogramDataset(test_df=test_csv)\n    test_dataloader = DataLoader(\n        test_dataset,\n        batch_size=,\n        shuffle=\n    )\n    model = torch.load(f_model)\n    model.to(device)\n    prediction_dict = inference_function(test_dataloader, model, device)\n    predictions.append(prediction_dict[])\n    eeg_ids.append(prediction_dict[])\n    torch.cuda.empty_cache()\n     model, test_dataset, test_dataloader\n    gc.collect()\n\npredictions = np.array(predictions)\npredictions = np.mean(predictions, axis=)\n\neeg_ids = np.array(eeg_ids)\neeg_ids = eeg_ids[]\neeg_ids = np.swapaxes(eeg_ids, , )\n\n\n eid, prd  (eeg_ids, predictions):\n    appended_data = [eid.item()] + (prd)\n    final_sub_df.loc[(final_sub_df)] = appended_data\n</code></pre>\n<p>What am I doing wrong? Any help is appreciated. Thanks</p>",
  "messages": [
    {
      "id": "2668437",
      "postDate": "02/25/2024 17:01:09",
      "content": "<p>Hi! I am getting \"Notebook Threw Exception\" while I am doing the inference on the test data. I don't know what is causing this issue. The inference code I took it from here and changed it a bit:<br>\n<a href=\"https://www.kaggle.com/code/alejopaullier/hms-efficientnetb0-pytorch-inference\" target=\"_blank\">HMS | EfficientNetB0 PyTorch [Inference]</a></p>\n<pre><code> ():\n    model.()\n    softmax = nn.Softmax(dim=)\n    prediction_dict = {}\n    preds = []\n    eeg_ids = []\n     tqdm(test_loader, unit=, desc=)  tqdm_test_loader:\n         step, (eeg_id, X)  (tqdm_test_loader):\n            X = X.to(device)\n             torch.no_grad():\n                y_preds = model(X)\n            y_preds = softmax(y_preds)\n            preds.append(y_preds.to().numpy())\n            eeg_ids.append(eeg_id)\n\n    prediction_dict[] = np.concatenate(preds)\n    prediction_dict[] = np.array(eeg_ids)\n     prediction_dict\n\n\n\npredictions = []\neeg_ids = []\n\n f_model  fold_models:\n    test_dataset = SpectrogramDataset(test_df=test_csv)\n    test_dataloader = DataLoader(\n        test_dataset,\n        batch_size=,\n        shuffle=\n    )\n    model = torch.load(f_model)\n    model.to(device)\n    prediction_dict = inference_function(test_dataloader, model, device)\n    predictions.append(prediction_dict[])\n    eeg_ids.append(prediction_dict[])\n    torch.cuda.empty_cache()\n     model, test_dataset, test_dataloader\n    gc.collect()\n\npredictions = np.array(predictions)\npredictions = np.mean(predictions, axis=)\n\neeg_ids = np.array(eeg_ids)\neeg_ids = eeg_ids[]\neeg_ids = np.swapaxes(eeg_ids, , )\n\n\n eid, prd  (eeg_ids, predictions):\n    appended_data = [eid.item()] + (prd)\n    final_sub_df.loc[(final_sub_df)] = appended_data\n</code></pre>\n<p>What am I doing wrong? Any help is appreciated. Thanks</p>",
      "rawMarkdown": "Hi! I am getting \"Notebook Threw Exception\" while I am doing the inference on the test data. I don't know what is causing this issue. The inference code I took it from here and changed it a bit:\n[HMS | EfficientNetB0 PyTorch [Inference]](https://www.kaggle.com/code/alejopaullier/hms-efficientnetb0-pytorch-inference)\n\n```python\ndef inference_function(test_loader, model, device):\n    model.eval()\n    softmax = nn.Softmax(dim=1)\n    prediction_dict = {}\n    preds = []\n    eeg_ids = []\n    with tqdm(test_loader, unit=\"test_batch\", desc='Inference') as tqdm_test_loader:\n        for step, (eeg_id, X) in enumerate(tqdm_test_loader):\n            X = X.to(device)\n            with torch.no_grad():\n                y_preds = model(X)\n            y_preds = softmax(y_preds)\n            preds.append(y_preds.to('cpu').numpy())\n            eeg_ids.append(eeg_id)\n                \n    prediction_dict[\"predictions\"] = np.concatenate(preds)\n    prediction_dict[\"eeg_ids\"] = np.array(eeg_ids)\n    return prediction_dict\n\n\n\npredictions = []\neeg_ids = []\n\nfor f_model in fold_models:\n    test_dataset = SpectrogramDataset(test_df=test_csv)\n    test_dataloader = DataLoader(\n        test_dataset,\n        batch_size=4,\n        shuffle=False\n    )\n    model = torch.load(f_model)\n    model.to(device)\n    prediction_dict = inference_function(test_dataloader, model, device)\n    predictions.append(prediction_dict[\"predictions\"])\n    eeg_ids.append(prediction_dict[\"eeg_ids\"])\n    torch.cuda.empty_cache()\n    del model, test_dataset, test_dataloader\n    gc.collect()\n\npredictions = np.array(predictions)\npredictions = np.mean(predictions, axis=0)\n\neeg_ids = np.array(eeg_ids)\neeg_ids = eeg_ids[0]\neeg_ids = np.swapaxes(eeg_ids, 0, 1)\n\n\nfor eid, prd in zip(eeg_ids, predictions):\n    appended_data = [eid.item()] + list(prd)\n    final_sub_df.loc[len(final_sub_df)] = appended_data\n```\nWhat am I doing wrong? Any help is appreciated. Thanks",
      "votes": null
    },
    {
      "id": "2668484",
      "postDate": "02/25/2024 17:43:45",
      "content": "<p>Try change test path to train data and execute it locally. You'll see better what's wrong.</p>",
      "rawMarkdown": "Try change test path to train data and execute it locally. You'll see better what's wrong.",
      "votes": null
    },
    {
      "id": "2669939",
      "postDate": "02/26/2024 15:03:54",
      "content": "<p>Thanks for the advice. Managed to figure out what was the issue by using train path. The issue is with batch size, the last batch size was smaller, that's why.</p>",
      "rawMarkdown": "Thanks for the advice. Managed to figure out what was the issue by using train path. The issue is with batch size, the last batch size was smaller, that's why.",
      "votes": null
    },
    {
      "id": "2674241",
      "postDate": "02/29/2024 07:27:59",
      "content": "<p>Thank you!! I addressed same issue after seeing \"batch size\" of your comment.</p>",
      "rawMarkdown": "Thank you!! I addressed same issue after seeing \"batch size\" of your comment.",
      "votes": null
    },
    {
      "id": "2674332",
      "postDate": "02/29/2024 09:05:12",
      "content": "<p>hi, can you tell me more about the solution</p>",
      "rawMarkdown": "hi, can you tell me more about the solution",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2668484,
      "author_name": "sacuscreed",
      "author_url": "",
      "post_date": "02/25/2024 17:43:45",
      "content": "<p>Try change test path to train data and execute it locally. You'll see better what's wrong.</p>",
      "votes": null,
      "replies": [
        {
          "id": 2669939,
          "author_name": "pritamsinha23",
          "author_url": "",
          "post_date": "02/26/2024 15:03:54",
          "content": "<p>Thanks for the advice. Managed to figure out what was the issue by using train path. The issue is with batch size, the last batch size was smaller, that's why.</p>",
          "votes": null,
          "replies": [
            {
              "id": 2674241,
              "author_name": "seongwook93",
              "author_url": "",
              "post_date": "02/29/2024 07:27:59",
              "content": "<p>Thank you!! I addressed same issue after seeing \"batch size\" of your comment.</p>",
              "votes": null,
              "replies": [
                {
                  "id": 2674332,
                  "author_name": "chenboluo",
                  "author_url": "",
                  "post_date": "02/29/2024 09:05:12",
                  "content": "<p>hi, can you tell me more about the solution</p>",
                  "votes": null,
                  "replies": []
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2668437": "Hi! I am getting \"Notebook Threw Exception\" while I am doing the inference on the test data. I don't know what is causing this issue. The inference code I took it from here and changed it a bit:\n[HMS | EfficientNetB0 PyTorch [Inference]](https://www.kaggle.com/code/alejopaullier/hms-efficientnetb0-pytorch-inference)\n\n```python\ndef inference_function(test_loader, model, device):\n    model.eval()\n    softmax = nn.Softmax(dim=1)\n    prediction_dict = {}\n    preds = []\n    eeg_ids = []\n    with tqdm(test_loader, unit=\"test_batch\", desc='Inference') as tqdm_test_loader:\n        for step, (eeg_id, X) in enumerate(tqdm_test_loader):\n            X = X.to(device)\n            with torch.no_grad():\n                y_preds = model(X)\n            y_preds = softmax(y_preds)\n            preds.append(y_preds.to('cpu').numpy())\n            eeg_ids.append(eeg_id)\n                \n    prediction_dict[\"predictions\"] = np.concatenate(preds)\n    prediction_dict[\"eeg_ids\"] = np.array(eeg_ids)\n    return prediction_dict\n\n\n\npredictions = []\neeg_ids = []\n\nfor f_model in fold_models:\n    test_dataset = SpectrogramDataset(test_df=test_csv)\n    test_dataloader = DataLoader(\n        test_dataset,\n        batch_size=4,\n        shuffle=False\n    )\n    model = torch.load(f_model)\n    model.to(device)\n    prediction_dict = inference_function(test_dataloader, model, device)\n    predictions.append(prediction_dict[\"predictions\"])\n    eeg_ids.append(prediction_dict[\"eeg_ids\"])\n    torch.cuda.empty_cache()\n    del model, test_dataset, test_dataloader\n    gc.collect()\n\npredictions = np.array(predictions)\npredictions = np.mean(predictions, axis=0)\n\neeg_ids = np.array(eeg_ids)\neeg_ids = eeg_ids[0]\neeg_ids = np.swapaxes(eeg_ids, 0, 1)\n\n\nfor eid, prd in zip(eeg_ids, predictions):\n    appended_data = [eid.item()] + list(prd)\n    final_sub_df.loc[len(final_sub_df)] = appended_data\n```\nWhat am I doing wrong? Any help is appreciated. Thanks",
    "2668484": "Try change test path to train data and execute it locally. You'll see better what's wrong.",
    "2669939": "Thanks for the advice. Managed to figure out what was the issue by using train path. The issue is with batch size, the last batch size was smaller, that's why.",
    "2674241": "Thank you!! I addressed same issue after seeing \"batch size\" of your comment.",
    "2674332": "hi, can you tell me more about the solution"
  },
  "source": "meta"
}