{
  "id": 452202,
  "title": "Save/load models for inference",
  "url": "/competitions/UBC-OCEAN/discussion/452202",
  "author_name": "Andreu Arderiu",
  "post_date": "2023-11-01T09:29:29.898000",
  "votes": 1,
  "comment_count": 1,
  "views": 0,
  "content": "<p>I am new to Kaggle and while starting to get the hands dirty in this competition, I am wondering what is the best way to load/save trained models for inference.</p>\n<p>Currently I use different notebooks for training and for inference. Is there a way to save the model checkpoints from the training notebook directly in the space of the inference notebook? What are best practices here? Thank you very much!! :)</p>",
  "messages": [
    {
      "id": 2508531,
      "postDate": "2023-11-01T19:21:16.603Z",
      "content": "<p>Yes, you can save your trained model or checkpoint in one <a href=\"https://www.kaggle.com/code/jirkaborovec/cancer-subtype-baseline-with-lightning-timm\" target=\"_blank\">notebook</a>:</p>\n<pre><code>model = ...\ntrainer = lightning.Trainer(...)\ntrainer.fit(model, ...)\ntrainer.save_checkpoint()\n</code></pre>\n<p>then add this train notebook in the <a href=\"https://www.kaggle.com/code/jirkaborovec/cancer-subtype-lit-torch-inference-thumbnails\" target=\"_blank\">inference notebook</a> as input data and load:</p>\n<pre><code>PATH_CKPT = \nckpt = torch.load(PATH_CKPT, map_location=torch.device())\nmodel = LitCancerSubtype(...)\nmodel.load_state_dict(ckpt[])\n</code></pre>",
      "rawMarkdown": "Yes, you can save your trained model or checkpoint in one [notebook](https://www.kaggle.com/code/jirkaborovec/cancer-subtype-baseline-with-lightning-timm):\n```py\nmodel = ...\ntrainer = lightning.Trainer(...)\ntrainer.fit(model, ...)\ntrainer.save_checkpoint(\"image_classification_model.pt\")\n```\nthen add this train notebook in the [inference notebook](https://www.kaggle.com/code/jirkaborovec/cancer-subtype-lit-torch-inference-thumbnails) as input data and load:\n```py\nPATH_CKPT = \"/kaggle/input/cancer-subtype-baseline-with-lightning-torch/image_classification_model.pt\"\nckpt = torch.load(PATH_CKPT, map_location=torch.device('cpu'))\nmodel = LitCancerSubtype(...)\nmodel.load_state_dict(ckpt['state_dict'])\n```",
      "votes": 1
    },
    {
      "id": 2507842,
      "postDate": "2023-11-01T09:29:29.897Z",
      "content": "<p>I am new to Kaggle and while starting to get the hands dirty in this competition, I am wondering what is the best way to load/save trained models for inference.</p>\n<p>Currently I use different notebooks for training and for inference. Is there a way to save the model checkpoints from the training notebook directly in the space of the inference notebook? What are best practices here? Thank you very much!! :)</p>",
      "rawMarkdown": "I am new to Kaggle and while starting to get the hands dirty in this competition, I am wondering what is the best way to load/save trained models for inference.\n\nCurrently I use different notebooks for training and for inference. Is there a way to save the model checkpoints from the training notebook directly in the space of the inference notebook? What are best practices here? Thank you very much!! :)",
      "votes": 1
    }
  ],
  "comments": [
    {
      "id": 2508531,
      "author_name": "Jirka",
      "author_url": "",
      "post_date": "2023-11-01T19:21:16.603000",
      "content": "<p>Yes, you can save your trained model or checkpoint in one <a href=\"https://www.kaggle.com/code/jirkaborovec/cancer-subtype-baseline-with-lightning-timm\" target=\"_blank\">notebook</a>:</p>\n<pre><code>model = ...\ntrainer = lightning.Trainer(...)\ntrainer.fit(model, ...)\ntrainer.save_checkpoint()\n</code></pre>\n<p>then add this train notebook in the <a href=\"https://www.kaggle.com/code/jirkaborovec/cancer-subtype-lit-torch-inference-thumbnails\" target=\"_blank\">inference notebook</a> as input data and load:</p>\n<pre><code>PATH_CKPT = \nckpt = torch.load(PATH_CKPT, map_location=torch.device())\nmodel = LitCancerSubtype(...)\nmodel.load_state_dict(ckpt[])\n</code></pre>",
      "votes": 1,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2508531": "Yes, you can save your trained model or checkpoint in one [notebook](https://www.kaggle.com/code/jirkaborovec/cancer-subtype-baseline-with-lightning-timm):\n```py\nmodel = ...\ntrainer = lightning.Trainer(...)\ntrainer.fit(model, ...)\ntrainer.save_checkpoint(\"image_classification_model.pt\")\n```\nthen add this train notebook in the [inference notebook](https://www.kaggle.com/code/jirkaborovec/cancer-subtype-lit-torch-inference-thumbnails) as input data and load:\n```py\nPATH_CKPT = \"/kaggle/input/cancer-subtype-baseline-with-lightning-torch/image_classification_model.pt\"\nckpt = torch.load(PATH_CKPT, map_location=torch.device('cpu'))\nmodel = LitCancerSubtype(...)\nmodel.load_state_dict(ckpt['state_dict'])\n```",
    "2507842": "I am new to Kaggle and while starting to get the hands dirty in this competition, I am wondering what is the best way to load/save trained models for inference.\n\nCurrently I use different notebooks for training and for inference. Is there a way to save the model checkpoints from the training notebook directly in the space of the inference notebook? What are best practices here? Thank you very much!! :)"
  }
}