{
  "id": 223571,
  "title": "Cropping model's weights with PyTorch in 5 lines of code",
  "url": "/competitions/ranzcr-clip-catheter-line-classification/discussion/223571",
  "author_name": "Vadim Timakin",
  "post_date": "2021-03-04T13:05:22.574000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Model's weights can contain more than just the state of the model itself.</p>\n<p>For example, to able to start training from the any moment, you should save the full checkpoint including optimizer's state, scheduler's state and etc. Here's an example of my code:</p>\n<pre><code>def savemodel(cfg, model, epoch, trainloss, valloss, metric, optimizer, stopflag, name, scheduler):\n    \"\"\"Saves PyTorch model.\"\"\"\n    torch.save({\n        'model': model.state_dict(),\n        'epoch': epoch,\n        'trainloss': trainloss,\n        'valloss': valloss,\n        'metric': metric,\n        'optimizer': optimizer.state_dict(),\n        'stopflag': stopflag,\n        'scheduler': scheduler.state_dict(),\n    }, os.path.join(cfg.path, name))\n</code></pre>\n<p>However, for the inference you need model's state only. So you can greatly reduce the file size with weights. In this way, you will reduce the loading time of the weights on Kaggle, and reduce the inference time by reducing the time allotted for loading the checkpoint.</p>\n<p>If you are using the same checkpoint system as me during the training, you can use the following code as well for cropping your weights:</p>\n<pre><code>import torch\n\nPATHTOLOAD = \"\"\nPATHTOSAVE = \"\"\n\nmodel = torch.load(PATHTOLOAD, map_location=\"cuda:0\")[\"model\"]\ntorch.save({\"model\": model}, PATHTOSAVE)\n</code></pre>\n<p>In my case, I was able to reduce the size of the file from 1.1 GB to 274.2 MB.</p>",
  "messages": [
    {
      "id": 1226337,
      "postDate": "2021-03-04T13:05:22.573Z",
      "content": "<p>Model's weights can contain more than just the state of the model itself.</p>\n<p>For example, to able to start training from the any moment, you should save the full checkpoint including optimizer's state, scheduler's state and etc. Here's an example of my code:</p>\n<pre><code>def savemodel(cfg, model, epoch, trainloss, valloss, metric, optimizer, stopflag, name, scheduler):\n    \"\"\"Saves PyTorch model.\"\"\"\n    torch.save({\n        'model': model.state_dict(),\n        'epoch': epoch,\n        'trainloss': trainloss,\n        'valloss': valloss,\n        'metric': metric,\n        'optimizer': optimizer.state_dict(),\n        'stopflag': stopflag,\n        'scheduler': scheduler.state_dict(),\n    }, os.path.join(cfg.path, name))\n</code></pre>\n<p>However, for the inference you need model's state only. So you can greatly reduce the file size with weights. In this way, you will reduce the loading time of the weights on Kaggle, and reduce the inference time by reducing the time allotted for loading the checkpoint.</p>\n<p>If you are using the same checkpoint system as me during the training, you can use the following code as well for cropping your weights:</p>\n<pre><code>import torch\n\nPATHTOLOAD = \"\"\nPATHTOSAVE = \"\"\n\nmodel = torch.load(PATHTOLOAD, map_location=\"cuda:0\")[\"model\"]\ntorch.save({\"model\": model}, PATHTOSAVE)\n</code></pre>\n<p>In my case, I was able to reduce the size of the file from 1.1 GB to 274.2 MB.</p>",
      "rawMarkdown": "Model's weights can contain more than just the state of the model itself.\n\nFor example, to able to start training from the any moment, you should save the full checkpoint including optimizer's state, scheduler's state and etc. Here's an example of my code:\n```\ndef savemodel(cfg, model, epoch, trainloss, valloss, metric, optimizer, stopflag, name, scheduler):\n    \"\"\"Saves PyTorch model.\"\"\"\n    torch.save({\n        'model': model.state_dict(),\n        'epoch': epoch,\n        'trainloss': trainloss,\n        'valloss': valloss,\n        'metric': metric,\n        'optimizer': optimizer.state_dict(),\n        'stopflag': stopflag,\n        'scheduler': scheduler.state_dict(),\n    }, os.path.join(cfg.path, name))\n```\n\nHowever, for the inference you need model's state only. So you can greatly reduce the file size with weights. In this way, you will reduce the loading time of the weights on Kaggle, and reduce the inference time by reducing the time allotted for loading the checkpoint.\n\nIf you are using the same checkpoint system as me during the training, you can use the following code as well for cropping your weights:\n\n```\nimport torch\n\nPATHTOLOAD = \"\"\nPATHTOSAVE = \"\"\n\nmodel = torch.load(PATHTOLOAD, map_location=\"cuda:0\")[\"model\"]\ntorch.save({\"model\": model}, PATHTOSAVE)\n```\n\nIn my case, I was able to reduce the size of the file from 1.1 GB to 274.2 MB.",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1226337": "Model's weights can contain more than just the state of the model itself.\n\nFor example, to able to start training from the any moment, you should save the full checkpoint including optimizer's state, scheduler's state and etc. Here's an example of my code:\n```\ndef savemodel(cfg, model, epoch, trainloss, valloss, metric, optimizer, stopflag, name, scheduler):\n    \"\"\"Saves PyTorch model.\"\"\"\n    torch.save({\n        'model': model.state_dict(),\n        'epoch': epoch,\n        'trainloss': trainloss,\n        'valloss': valloss,\n        'metric': metric,\n        'optimizer': optimizer.state_dict(),\n        'stopflag': stopflag,\n        'scheduler': scheduler.state_dict(),\n    }, os.path.join(cfg.path, name))\n```\n\nHowever, for the inference you need model's state only. So you can greatly reduce the file size with weights. In this way, you will reduce the loading time of the weights on Kaggle, and reduce the inference time by reducing the time allotted for loading the checkpoint.\n\nIf you are using the same checkpoint system as me during the training, you can use the following code as well for cropping your weights:\n\n```\nimport torch\n\nPATHTOLOAD = \"\"\nPATHTOSAVE = \"\"\n\nmodel = torch.load(PATHTOLOAD, map_location=\"cuda:0\")[\"model\"]\ntorch.save({\"model\": model}, PATHTOSAVE)\n```\n\nIn my case, I was able to reduce the size of the file from 1.1 GB to 274.2 MB."
  }
}