{
  "id": 263551,
  "title": "Load pretrained model into FastAI",
  "url": "/competitions/rsna-miccai-brain-tumor-radiogenomic-classification/discussion/263551",
  "author_name": "",
  "post_date": "2021-08-09T17:05:11.697270100Z",
  "votes": 2,
  "comment_count": 2,
  "views": 0,
  "content": "<p>I want to use a <code>cnn_learner</code> from FastAI, but since internet access is disabled I can't just pass in <code>models.resnet50</code> for the <code>arch</code> parameter since this function downloads from the internet.</p>\n<p>How can I easily load an architecture/model into FastAI?</p>\n<p>I've tried loading a <code>.pth</code> file but not sure how to put it as the <code>arch</code> parameter?</p>",
  "messages": [
    {
      "id": "1462039",
      "postDate": "08/09/2021 17:05:11",
      "content": "<p>I want to use a <code>cnn_learner</code> from FastAI, but since internet access is disabled I can't just pass in <code>models.resnet50</code> for the <code>arch</code> parameter since this function downloads from the internet.</p>\n<p>How can I easily load an architecture/model into FastAI?</p>\n<p>I've tried loading a <code>.pth</code> file but not sure how to put it as the <code>arch</code> parameter?</p>",
      "rawMarkdown": "I want to use a `cnn_learner` from FastAI, but since internet access is disabled I can't just pass in `models.resnet50` for the `arch` parameter since this function downloads from the internet.\n\nHow can I easily load an architecture/model into FastAI?\n\nI've tried loading a `.pth` file but not sure how to put it as the `arch` parameter?",
      "votes": null
    },
    {
      "id": "1462086",
      "postDate": "08/09/2021 17:25:34",
      "content": "<p>I actually think the answer here is save the <code>cnn_learner</code> itself using <code>learner.save()</code>, then upload the file as a dataset and the use <code>learner.load()</code></p>",
      "rawMarkdown": "I actually think the answer here is save the `cnn_learner` itself using `learner.save()`, then upload the file as a dataset and the use `learner.load()`",
      "votes": null
    },
    {
      "id": "1462172",
      "postDate": "08/09/2021 17:59:53",
      "content": "<p>yes.. you can treat model that is wrapped in <code>can_learner</code> as standard Pytorch one.</p>\n<p><code>torch.save(learn.model.state_dict(), PATH)</code></p>\n<p>and then if you want to use <code>fastai</code> again for inference just create <code>can_learner</code> and upload the saved weights`.</p>\n<p><code>learn.model.load_state_dict(torch.load(PATH))</code></p>",
      "rawMarkdown": "yes.. you can treat model that is wrapped in `can_learner` as standard Pytorch one.\n\n`torch.save(learn.model.state_dict(), PATH)`\n\nand then if you want to use `fastai` again for inference just create `can_learner` and upload the saved weights`.\n\n`learn.model.load_state_dict(torch.load(PATH))`",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1462086,
      "author_name": "d223chen",
      "author_url": "",
      "post_date": "08/09/2021 17:25:34",
      "content": "<p>I actually think the answer here is save the <code>cnn_learner</code> itself using <code>learner.save()</code>, then upload the file as a dataset and the use <code>learner.load()</code></p>",
      "votes": null,
      "replies": [
        {
          "id": 1462172,
          "author_name": "drhabib",
          "author_url": "",
          "post_date": "08/09/2021 17:59:53",
          "content": "<p>yes.. you can treat model that is wrapped in <code>can_learner</code> as standard Pytorch one.</p>\n<p><code>torch.save(learn.model.state_dict(), PATH)</code></p>\n<p>and then if you want to use <code>fastai</code> again for inference just create <code>can_learner</code> and upload the saved weights`.</p>\n<p><code>learn.model.load_state_dict(torch.load(PATH))</code></p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1462039": "I want to use a `cnn_learner` from FastAI, but since internet access is disabled I can't just pass in `models.resnet50` for the `arch` parameter since this function downloads from the internet.\n\nHow can I easily load an architecture/model into FastAI?\n\nI've tried loading a `.pth` file but not sure how to put it as the `arch` parameter?",
    "1462086": "I actually think the answer here is save the `cnn_learner` itself using `learner.save()`, then upload the file as a dataset and the use `learner.load()`",
    "1462172": "yes.. you can treat model that is wrapped in `can_learner` as standard Pytorch one.\n\n`torch.save(learn.model.state_dict(), PATH)`\n\nand then if you want to use `fastai` again for inference just create `can_learner` and upload the saved weights`.\n\n`learn.model.load_state_dict(torch.load(PATH))`"
  },
  "source": "meta"
}