Shortcuts

Learn the Basics || Quickstart || Tensors || Datasets & DataLoaders || Transforms || Build Model || Autograd || Optimization || Save & Load Model

Save and Load the Model

In this section we will look at how to persist model state with saving, loading and running model predictions.

import torch
import torchvision.models as models

Saving and Loading Model Weights

PyTorch models store the learned parameters in an internal state dictionary, called state_dict. These can be persisted via the torch.save method:

model = models.vgg16(pretrained=True)
torch.save(model.state_dict(), 'model_weights.pth')
/opt/conda/lib/python3.7/site-packages/torchvision/models/_utils.py:209: UserWarning:

The parameter 'pretrained' is deprecated since 0.13 and will be removed in 0.15, please use 'weights' instead.

/opt/conda/lib/python3.7/site-packages/torchvision/models/_utils.py:223: UserWarning:

Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing `weights=VGG16_Weights.IMAGENET1K_V1`. You can also use `weights=VGG16_Weights.DEFAULT` to get the most up-to-date weights.

Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to /var/lib/jenkins/.cache/torch/hub/checkpoints/vgg16-397923af.pth

  0%|          | 0.00/528M [00:00<?, ?B/s]
  1%|1         | 7.23M/528M [00:00<00:07, 75.9MB/s]
  5%|4         | 24.5M/528M [00:00<00:03, 137MB/s]
  8%|7         | 41.7M/528M [00:00<00:03, 157MB/s]
 11%|#1        | 59.0M/528M [00:00<00:02, 167MB/s]
 14%|#4        | 76.2M/528M [00:00<00:02, 172MB/s]
 18%|#7        | 93.5M/528M [00:00<00:02, 175MB/s]
 21%|##        | 111M/528M [00:00<00:02, 176MB/s]
 24%|##4       | 128M/528M [00:00<00:02, 178MB/s]
 27%|##7       | 145M/528M [00:00<00:02, 178MB/s]
 31%|###       | 162M/528M [00:01<00:02, 178MB/s]
 34%|###3      | 179M/528M [00:01<00:02, 179MB/s]
 37%|###7      | 196M/528M [00:01<00:01, 180MB/s]
 41%|####      | 214M/528M [00:01<00:01, 181MB/s]
 44%|####3     | 231M/528M [00:01<00:01, 181MB/s]
 47%|####7     | 249M/528M [00:01<00:01, 182MB/s]
 50%|#####     | 266M/528M [00:01<00:01, 182MB/s]
 54%|#####3    | 284M/528M [00:01<00:01, 182MB/s]
 57%|#####7    | 301M/528M [00:01<00:01, 182MB/s]
 60%|######    | 318M/528M [00:01<00:01, 176MB/s]
 64%|######3   | 335M/528M [00:02<00:01, 177MB/s]
 67%|######6   | 353M/528M [00:02<00:01, 178MB/s]
 70%|#######   | 370M/528M [00:02<00:00, 179MB/s]
 73%|#######3  | 387M/528M [00:02<00:00, 179MB/s]
 77%|#######6  | 404M/528M [00:02<00:00, 173MB/s]
 80%|#######9  | 421M/528M [00:02<00:00, 175MB/s]
 83%|########2 | 438M/528M [00:02<00:00, 176MB/s]
 86%|########6 | 455M/528M [00:02<00:00, 175MB/s]
 89%|########9 | 472M/528M [00:02<00:00, 176MB/s]
 93%|#########2| 489M/528M [00:02<00:00, 178MB/s]
 96%|#########5| 507M/528M [00:03<00:00, 179MB/s]
 99%|#########9| 524M/528M [00:03<00:00, 178MB/s]
100%|##########| 528M/528M [00:03<00:00, 176MB/s]

To load model weights, you need to create an instance of the same model first, and then load the parameters using load_state_dict() method.

model = models.vgg16() # we do not specify pretrained=True, i.e. do not load default weights
model.load_state_dict(torch.load('model_weights.pth'))
model.eval()
VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
)

Note

be sure to call model.eval() method before inferencing to set the dropout and batch normalization layers to evaluation mode. Failing to do this will yield inconsistent inference results.

Saving and Loading Models with Shapes

When loading model weights, we needed to instantiate the model class first, because the class defines the structure of a network. We might want to save the structure of this class together with the model, in which case we can pass model (and not model.state_dict()) to the saving function:

torch.save(model, 'model.pth')

We can then load the model like this:

model = torch.load('model.pth')

Note

This approach uses Python pickle module when serializing the model, thus it relies on the actual class definition to be available when loading the model.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources