{
  "id": 347574,
  "title": "VIT implementation throwing an error",
  "url": "/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/347574",
  "author_name": "Collide_Conquer_19",
  "post_date": "2022-08-24T17:21:36.686000",
  "votes": 0,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Hello all,<br>\nI'm trying to implement VIT from scratch and the model is built well as per my observation when I try to train the model it's throwing an error called \"RUNTIME 0 Error\"  and I have solved this issue, but the issue here is it's showing an error of \"Expected 4-dimensional input, got 3-dimensional input\". I'm not able to trace the error from my code. any help and suggestions are useful please give me.</p>\n<p>The link for the notebook is <a href=\"https://www.kaggle.com/code/robber19/vit-try/notebook\" target=\"_blank\">Notebook</a>. </p>\n<p>Thanks,<br>\nConquer_19</p>",
  "messages": [
    {
      "id": 1912394,
      "postDate": "2022-08-24T17:42:13.967Z",
      "content": "<p>You need to add a channel dimension to your input data by changing <code>image=np.array(image)</code> to  <code>image=np.expand_dims(np.array(image),0)</code> in your Dataset <code>__getitem__</code>, for example. </p>",
      "rawMarkdown": "You need to add a channel dimension to your input data by changing `image=np.array(image)` to  `image=np.expand_dims(np.array(image),0)` in your Dataset `__getitem__`, for example. ",
      "replies": [
        {
          "id": 1912410,
          "postDate": "2022-08-24T17:48:23.347Z",
          "content": "<p>lemme see if this works out!</p>",
          "rawMarkdown": "lemme see if this works out!"
        },
        {
          "id": 1912451,
          "postDate": "2022-08-24T18:18:04.483Z",
          "content": "<p><a href=\"https://www.kaggle.com/brandenkmurray\" target=\"_blank\">@brandenkmurray</a> bow it's showing an error of \"RuntimeError: shape '[0, 16384, 48]' is invalid for input of size 4194304<br>\n\"</p>",
          "rawMarkdown": "@brandenkmurray bow it's showing an error of \"RuntimeError: shape '[0, 16384, 48]' is invalid for input of size 4194304\n\""
        },
        {
          "id": 1912460,
          "postDate": "2022-08-24T18:28:29.813Z",
          "content": "<p>I don't get that error. I just:</p>\n<ol>\n<li>changed <code>image=np.array(image)</code> to <code>image=np.expand_dims(np.array(image),0)</code> in your Dataset <code>__getitem__</code></li>\n<li>added <code>channels=1</code> to <code>model = ViT(...)</code></li>\n<li>convert <code>x</code> to <code>float</code> i.e. <code>y_hat=model(x.float())</code></li>\n</ol>\n<p>And the model works fine. After that, you still have dimensionality issues with <code>y</code> and <code>y_hat</code> you need to address, but the <code>model</code> part doesn't error.</p>",
          "rawMarkdown": "I don't get that error. I just:\n1. changed `image=np.array(image)` to `image=np.expand_dims(np.array(image),0)` in your Dataset `__getitem__`\n2. added `channels=1` to `model = ViT(...)`\n3. convert `x` to `float` i.e. `y_hat=model(x.float())`\n\nAnd the model works fine. After that, you still have dimensionality issues with `y` and `y_hat` you need to address, but the `model` part doesn't error."
        }
      ]
    },
    {
      "id": 1912375,
      "postDate": "2022-08-24T17:21:36.687Z",
      "content": "<p>Hello all,<br>\nI'm trying to implement VIT from scratch and the model is built well as per my observation when I try to train the model it's throwing an error called \"RUNTIME 0 Error\"  and I have solved this issue, but the issue here is it's showing an error of \"Expected 4-dimensional input, got 3-dimensional input\". I'm not able to trace the error from my code. any help and suggestions are useful please give me.</p>\n<p>The link for the notebook is <a href=\"https://www.kaggle.com/code/robber19/vit-try/notebook\" target=\"_blank\">Notebook</a>. </p>\n<p>Thanks,<br>\nConquer_19</p>",
      "rawMarkdown": "Hello all,\nI'm trying to implement VIT from scratch and the model is built well as per my observation when I try to train the model it's throwing an error called \"RUNTIME 0 Error\"  and I have solved this issue, but the issue here is it's showing an error of \"Expected 4-dimensional input, got 3-dimensional input\". I'm not able to trace the error from my code. any help and suggestions are useful please give me.\n\nThe link for the notebook is [Notebook](https://www.kaggle.com/code/robber19/vit-try/notebook). \n\nThanks,\nConquer_19"
    }
  ],
  "comments": [
    {
      "id": 1912394,
      "author_name": "Branden Murray",
      "author_url": "",
      "post_date": "2022-08-24T17:42:13.967000",
      "content": "<p>You need to add a channel dimension to your input data by changing <code>image=np.array(image)</code> to  <code>image=np.expand_dims(np.array(image),0)</code> in your Dataset <code>__getitem__</code>, for example. </p>",
      "votes": 0,
      "replies": [
        {
          "id": 1912410,
          "author_name": "Collide_Conquer_19",
          "author_url": "",
          "post_date": "2022-08-24T17:48:23.347000",
          "content": "<p>lemme see if this works out!</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1912451,
          "author_name": "Collide_Conquer_19",
          "author_url": "",
          "post_date": "2022-08-24T18:18:04.483000",
          "content": "<p><a href=\"https://www.kaggle.com/brandenkmurray\" target=\"_blank\">@brandenkmurray</a> bow it's showing an error of \"RuntimeError: shape '[0, 16384, 48]' is invalid for input of size 4194304<br>\n\"</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1912460,
          "author_name": "Branden Murray",
          "author_url": "",
          "post_date": "2022-08-24T18:28:29.813000",
          "content": "<p>I don't get that error. I just:</p>\n<ol>\n<li>changed <code>image=np.array(image)</code> to <code>image=np.expand_dims(np.array(image),0)</code> in your Dataset <code>__getitem__</code></li>\n<li>added <code>channels=1</code> to <code>model = ViT(...)</code></li>\n<li>convert <code>x</code> to <code>float</code> i.e. <code>y_hat=model(x.float())</code></li>\n</ol>\n<p>And the model works fine. After that, you still have dimensionality issues with <code>y</code> and <code>y_hat</code> you need to address, but the <code>model</code> part doesn't error.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1912394": "You need to add a channel dimension to your input data by changing `image=np.array(image)` to  `image=np.expand_dims(np.array(image),0)` in your Dataset `__getitem__`, for example. ",
    "1912375": "Hello all,\nI'm trying to implement VIT from scratch and the model is built well as per my observation when I try to train the model it's throwing an error called \"RUNTIME 0 Error\"  and I have solved this issue, but the issue here is it's showing an error of \"Expected 4-dimensional input, got 3-dimensional input\". I'm not able to trace the error from my code. any help and suggestions are useful please give me.\n\nThe link for the notebook is [Notebook](https://www.kaggle.com/code/robber19/vit-try/notebook). \n\nThanks,\nConquer_19"
  }
}