{
  "id": 167660,
  "title": "beginner issue with saving and loading pytorch model",
  "url": "/competitions/prostate-cancer-grade-assessment/discussion/167660",
  "author_name": "Aristide Pottier",
  "post_date": "2020-07-17T12:26:22.769000",
  "votes": 0,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Hello,\nI am new with pytorch and I would like to get some clarification.\nI create a notebook to train my model ( which take 2-3 hours ) and then I saved it.\n<code>\ntorch.save(model.state_dict(), os.path.join(f'{kernel_type}_final_fold{fold}.pth'))</code>\nIn my new notebook I import it and load it.\n<code>state_dict = torch.load(path)\n    model = enetv2(enet_type, out_dim=out_dim)\n    model.load_state_dict(state_dict)\n    model = model.to(device)\n</code>\nIn order to load the model I instantiate an object of the exact same class, I load the state but then when I want to use this model to predict I have a dimension error.\nI trained my model with batch of image of [640,640,3]\nI also give such image to my model to do the prediction but I receive this error message\n<code>\nRuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[8, 640, 641, 4] to have 3 channels, but got 640 channels instead\n</code></p>",
  "messages": [
    {
      "id": 933922,
      "postDate": "2020-07-18T06:03:47.717Z",
      "content": "<p>Looks like your input tensor size from data generator is not adjusted to batch_size, channel, w , h format. You need to transpose your input tensor to 3 x 640 x 640. </p>",
      "rawMarkdown": "Looks like your input tensor size from data generator is not adjusted to batch_size, channel, w , h format. You need to transpose your input tensor to 3 x 640 x 640. ",
      "votes": 1,
      "replies": [
        {
          "id": 933932,
          "postDate": "2020-07-18T06:22:48.393Z",
          "content": "<p>thank you I will give it a try</p>",
          "rawMarkdown": "thank you I will give it a try"
        },
        {
          "id": 933947,
          "postDate": "2020-07-18T06:41:15.447Z",
          "content": "<p>very nice I manage to solve this problem thanks to your help.</p>",
          "rawMarkdown": "very nice I manage to solve this problem thanks to your help.",
          "votes": 1
        }
      ]
    },
    {
      "id": 933012,
      "postDate": "2020-07-17T12:26:22.770Z",
      "content": "<p>Hello,\nI am new with pytorch and I would like to get some clarification.\nI create a notebook to train my model ( which take 2-3 hours ) and then I saved it.\n<code>\ntorch.save(model.state_dict(), os.path.join(f'{kernel_type}_final_fold{fold}.pth'))</code>\nIn my new notebook I import it and load it.\n<code>state_dict = torch.load(path)\n    model = enetv2(enet_type, out_dim=out_dim)\n    model.load_state_dict(state_dict)\n    model = model.to(device)\n</code>\nIn order to load the model I instantiate an object of the exact same class, I load the state but then when I want to use this model to predict I have a dimension error.\nI trained my model with batch of image of [640,640,3]\nI also give such image to my model to do the prediction but I receive this error message\n<code>\nRuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[8, 640, 641, 4] to have 3 channels, but got 640 channels instead\n</code></p>",
      "rawMarkdown": "Hello,\nI am new with pytorch and I would like to get some clarification.\nI create a notebook to train my model ( which take 2-3 hours ) and then I saved it.\n`\ntorch.save(model.state_dict(), os.path.join(f'{kernel_type}_final_fold{fold}.pth'))`\nIn my new notebook I import it and load it.\n`    state_dict = torch.load(path)\n    model = enetv2(enet_type, out_dim=out_dim)\n    model.load_state_dict(state_dict)\n    model = model.to(device)\n`\nIn order to load the model I instantiate an object of the exact same class, I load the state but then when I want to use this model to predict I have a dimension error.\nI trained my model with batch of image of [640,640,3]\nI also give such image to my model to do the prediction but I receive this error message\n```\nRuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[8, 640, 641, 4] to have 3 channels, but got 640 channels instead\n```\n"
    }
  ],
  "comments": [
    {
      "id": 933922,
      "author_name": "Sedat",
      "author_url": "",
      "post_date": "2020-07-18T06:03:47.717000",
      "content": "<p>Looks like your input tensor size from data generator is not adjusted to batch_size, channel, w , h format. You need to transpose your input tensor to 3 x 640 x 640. </p>",
      "votes": 1,
      "replies": [
        {
          "id": 933932,
          "author_name": "Aristide Pottier",
          "author_url": "",
          "post_date": "2020-07-18T06:22:48.393000",
          "content": "<p>thank you I will give it a try</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 933947,
          "author_name": "Aristide Pottier",
          "author_url": "",
          "post_date": "2020-07-18T06:41:15.447000",
          "content": "<p>very nice I manage to solve this problem thanks to your help.</p>",
          "votes": 1,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "933922": "Looks like your input tensor size from data generator is not adjusted to batch_size, channel, w , h format. You need to transpose your input tensor to 3 x 640 x 640. ",
    "933012": "Hello,\nI am new with pytorch and I would like to get some clarification.\nI create a notebook to train my model ( which take 2-3 hours ) and then I saved it.\n`\ntorch.save(model.state_dict(), os.path.join(f'{kernel_type}_final_fold{fold}.pth'))`\nIn my new notebook I import it and load it.\n`    state_dict = torch.load(path)\n    model = enetv2(enet_type, out_dim=out_dim)\n    model.load_state_dict(state_dict)\n    model = model.to(device)\n`\nIn order to load the model I instantiate an object of the exact same class, I load the state but then when I want to use this model to predict I have a dimension error.\nI trained my model with batch of image of [640,640,3]\nI also give such image to my model to do the prediction but I receive this error message\n```\nRuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[8, 640, 641, 4] to have 3 channels, but got 640 channels instead\n```\n"
  }
}