Pytorch的模型加载、保存、打印

Pytorch的模型加载、保存、打印

一、加载预训练模型

.pt .pth .pkl:它们并不是在格式上有区别,只是后缀不同而已(仅此而已)

torch.load
resnet18.load_state_dict()

# 此处调用torchvision中已有的model
import torch
import torchvision.models as models # 预训练模型都在这里面

resnet18 = models.resnet18(pretrained=True) # 加载预训练模型和参数
resnet18 = models.resnet18(pretrained=False) # 模型没有预训练,导入模型结构
resnet18.load_state_dict(torch.load('resnet18.pth')) # 加载预先下载好的预训练参数到resnet18
alexnet = models.alexnet() # ❓是默认会预训练么(查)

二、保存模型

1、只保存模型权重参数,不保存模型结构

1.1 只保存模型权重参数

# dir = 'xxxx/resnet18.pth'
import torch
import torchvision.models as models
resnet18 = models.resnet18(pretrained=True)

# 方式一:只保存模型权重参数
# 注意:resnet18可以替换为任意模型
torch.save(resnet18.state_dict(), 'xxxx/resnet18.pth') 	 # 只保存模型权重参数,不保存模型结构
resnet18.load_state_dict(torch.load('xxxx/resnet18.pth')) # 这里根据模型结构,调用存储的模型参数

1.2 保存模型权重、优化器权重、epoch信息

dir = 'mymodel.pth'
state = {'net':model.state_dict(), 'optimizer':optimizer.state_dict(), 'epoch':epoch}
torch.save(state, dir) # 权重参数包括了模型权重、优化器权重、epoch
checkpoint = torch.load(dir) # checkpoint 把之前save的state加载进来
model.load_state_dict(checkpoint['net'])
optimizer.load_state_dict(checkpoint['optimizer'])
start_epoch = checkpoint['epoch'] + 1

2、保存整个模型,包括模型结构和权重参数

dir = 'xxxx/resnet18.pth'
import torch
import torchvision.models as models
resnet18 = models.resnet18(pretrained=True)

torch.save(resnet18, dir) 		# 保存整个model的状态
resnet18_load = torch.load(dir) 	# 这里已经不需要重构模型结构了,直接load就可以

三、model.state_dict() 测试、模型打印测试

已知的有两种打印方式

#encoding:utf-8
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import numpy as mp
import matplotlib.pyplot as plt
import torch.nn.functional as F
from torchsummary import summary
 
#define model
class TheModelClass(nn.Module):
    def __init__(self):
        super(TheModelClass,self).__init__()
        self.conv1=nn.Conv2d(3,6,5)
        self.pool=nn.MaxPool2d(2,2)
        self.conv2=nn.Conv2d(6,16,5)
        self.fc1=nn.Linear(16*5*5,120)
        self.fc2=nn.Linear(120,84)
        self.fc3=nn.Linear(84,10)
 
    def forward(self,x):
        x=self.pool(F.relu(self.conv1(x)))
        x=self.pool(F.relu(self.conv2(x)))
        x=x.view(-1,16*5*5)
        x=F.relu(self.fc1(x))
        x=F.relu(self.fc2(x))
        x=self.fc3(x)
        return x
 
def main():
    # Initialize model
    model = TheModelClass()
 
    #Initialize optimizer
    optimizer=optim.SGD(model.parameters(),lr=0.001,momentum=0.9)
    
    # 打印方式一:直接打印网络
    print('model:')
    print(model)
    #print model's state_dict
    print('############################################################')
    print('Model.state_dict:')
    for param_tensor in model.state_dict():
        #打印 key value字典
        print(param_tensor,'\t',model.state_dict()[param_tensor].size())
 
    #print optimizer's state_dict
    print('############################################################')
    print('Optimizer,s state_dict:')
    for var_name in optimizer.state_dict():
        print(var_name,'\t',optimizer.state_dict()[var_name])


    # 打印方式二:模型导入设备后打印
    print('############################################################')
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    t = model.to(device)
    summary(model=t, input_size= (3, 32, 32))
 
if __name__=='__main__':
    main()

输出结果

'''
model:   # 这种直接print(model)的方式打印不全
TheModelClass(
  (conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
  (fc1): Linear(in_features=400, out_features=120, bias=True)
  (fc2): Linear(in_features=120, out_features=84, bias=True)
  (fc3): Linear(in_features=84, out_features=10, bias=True)
)

############################################################
Model.state_dict:
conv1.weight 	 torch.Size([6, 3, 5, 5])
conv1.bias 	 torch.Size([6])
conv2.weight 	 torch.Size([16, 6, 5, 5])
conv2.bias 	 torch.Size([16])
fc1.weight 	 torch.Size([120, 400])
fc1.bias 	 torch.Size([120])
fc2.weight 	 torch.Size([84, 120])
fc2.bias 	 torch.Size([84])
fc3.weight 	 torch.Size([10, 84])
fc3.bias 	 torch.Size([10])

############################################################
Optimizer,s state_dict:
state 	 {}
param_groups 	 [{'lr': 0.001, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'nesterov': False, 'params': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}]

############################################################
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1            [-1, 6, 28, 28]             456
         MaxPool2d-2            [-1, 6, 14, 14]               0
            Conv2d-3           [-1, 16, 10, 10]           2,416
         MaxPool2d-4             [-1, 16, 5, 5]               0
            Linear-5                  [-1, 120]          48,120
            Linear-6                   [-1, 84]          10,164
            Linear-7                   [-1, 10]             850
================================================================
Total params: 62,006
Trainable params: 62,006
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 0.06
Params size (MB): 0.24
Estimated Total Size (MB): 0.31
----------------------------------------------------------------
'''

你可能感兴趣的:(pytorch)