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(weights='IMAGENET1K_V1')
torch.save(model.state_dict(), 'model_weights.pth')
Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to /var/lib/ci-user/.cache/torch/hub/checkpoints/vgg16-397923af.pth

  0%|          | 0.00/528M [00:00<?, ?B/s]
  4%|3         | 20.5M/528M [00:00<00:02, 214MB/s]
  8%|7         | 41.4M/528M [00:00<00:02, 216MB/s]
 12%|#1        | 62.2M/528M [00:00<00:02, 217MB/s]
 16%|#5        | 83.1M/528M [00:00<00:02, 218MB/s]
 20%|#9        | 104M/528M [00:00<00:02, 219MB/s]
 24%|##3       | 125M/528M [00:00<00:01, 219MB/s]
 28%|##7       | 146M/528M [00:00<00:01, 219MB/s]
 32%|###1      | 167M/528M [00:00<00:01, 220MB/s]
 36%|###5      | 188M/528M [00:00<00:01, 220MB/s]
 40%|###9      | 210M/528M [00:01<00:01, 220MB/s]
 44%|####3     | 231M/528M [00:01<00:01, 220MB/s]
 48%|####7     | 252M/528M [00:01<00:01, 220MB/s]
 52%|#####1    | 273M/528M [00:01<00:01, 219MB/s]
 56%|#####5    | 294M/528M [00:01<00:01, 219MB/s]
 60%|#####9    | 315M/528M [00:01<00:01, 219MB/s]
 64%|######3   | 336M/528M [00:01<00:00, 220MB/s]
 68%|######7   | 357M/528M [00:01<00:00, 220MB/s]
 72%|#######1  | 378M/528M [00:01<00:00, 220MB/s]
 76%|#######5  | 399M/528M [00:01<00:00, 220MB/s]
 80%|#######9  | 420M/528M [00:02<00:00, 220MB/s]
 84%|########3 | 441M/528M [00:02<00:00, 220MB/s]
 88%|########7 | 462M/528M [00:02<00:00, 220MB/s]
 92%|#########1| 483M/528M [00:02<00:00, 220MB/s]
 96%|#########5| 504M/528M [00:02<00:00, 220MB/s]
100%|#########9| 525M/528M [00:02<00:00, 219MB/s]
100%|##########| 528M/528M [00:02<00:00, 219MB/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.

In the code below, we set weights_only=True to limit the functions executed during unpickling to only those necessary for loading weights. Using weights_only=True is considered a best practice when loading weights.

model = models.vgg16() # we do not specify ``weights``, i.e. create untrained model
model.load_state_dict(torch.load('model_weights.pth', weights_only=True))
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 as demonstrated below.

As described in Saving and loading torch.nn.Modules, saving state_dict is considered the best practice. However, below we use weights_only=False because this involves loading the model, which is a legacy use case for torch.save.

model = torch.load('model.pth', weights_only=False),

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