Save and Load the Model — PyTorch Tutorials 2.0.1+cu117 documentation
在本节中,我们将了解如何通过保存、加载和运行模型预测来持久化模型状态。
import torch
import torchvision.models as models
PyTorch模型将学习到的参数存储在一个名为state_dict的内部状态字典中。这些可以通过torch.save 方法持久化
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/jenkins/.cache/torch/hub/checkpoints/vgg16-397923af.pth
0%| | 0.00/528M [00:00, ?B/s]
5%|4 | 23.9M/528M [00:00<00:02, 250MB/s]
10%|9 | 50.7M/528M [00:00<00:01, 268MB/s]
16%|#6 | 85.5M/528M [00:00<00:01, 313MB/s]
22%|##2 | 118M/528M [00:00<00:01, 322MB/s]
28%|##8 | 148M/528M [00:00<00:01, 304MB/s]
34%|###3 | 178M/528M [00:00<00:01, 292MB/s]
39%|###8 | 206M/528M [00:00<00:01, 285MB/s]
44%|####4 | 233M/528M [00:00<00:01, 267MB/s]
49%|####8 | 258M/528M [00:00<00:01, 267MB/s]
54%|#####3 | 284M/528M [00:01<00:00, 269MB/s]
59%|#####8 | 310M/528M [00:01<00:00, 269MB/s]
64%|######3 | 336M/528M [00:01<00:00, 270MB/s]
69%|######8 | 362M/528M [00:01<00:00, 269MB/s]
74%|#######3 | 388M/528M [00:01<00:00, 270MB/s]
78%|#######8 | 414M/528M [00:01<00:00, 270MB/s]
83%|########3 | 440M/528M [00:01<00:00, 270MB/s]
88%|########8 | 465M/528M [00:01<00:00, 270MB/s]
93%|#########3| 491M/528M [00:01<00:00, 270MB/s]
98%|#########8| 517M/528M [00:01<00:00, 271MB/s]
100%|##########| 528M/528M [00:02<00:00, 276MB/s]
要加载模型权重,需要首先创建同一模型的实例,然后使用load_state_dict() 方法加载参数。
model = models.vgg16() # we do not specify ``weights``, i.e. create untrained model
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)
)
)
注意
一定要在推理之前调用model.eval() 方法,将dropout层 和normalization 层设置成evaluation模式。如果不这样做,将产生不一致的推理结果。
当加载模型权重时,我们需要首先实例化模型类,因为类定义了网络的结构。我们可能希望将该类的结构与模型一起保存,在这种情况下,我们可以使用model(而不是model.state_dict())方法保存:
torch.save(model, 'model.pth')
然后我们可以像这样加载模型:
model = torch.load('model.pth')
注意
这种方法在序列化模型时使用Python pickle模块,因此它依赖于 加载模型时可用的实际类定义。
Saving and Loading a General Checkpoint in PyTorch