前言

训练完一个模型后需要进行保存或是需要加载别人(自己)训练好的模型

实践

方式一

保存

torch.save(model, path)
model:需要保存的模型
path:模型保存的路径

import torch
from torch import nn


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64*4*4, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model(x)
        return x


net = Net()
# 方式1 保存模型的结构和参数
torch.save(net, "../test_model_save.pth")

在这里插入图片描述

加载

torch.load(path)
path:加载路径

import torch
from torch import nn

# 这里需要提前将你load的模型结构定义一下,否则直接加载会报错
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64*4*4, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model(x)
        return x


net = torch.load("../test_model_save.pth")
print(net)

在这里插入图片描述

方式二

torch.save(net.state_dict(), path)

保存

import torch
from torch import nn


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64*4*4, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model(x)
        return x


net = Net()
# 方式2 将参数转化为了字典的形式进行保存
torch.save(net.state_dict(), "../test_model_save.pth")

在这里插入图片描述

加载

这种方式保存的是模型的参数,所以直接加载出来的内容是一个tensor数组
在这里插入图片描述应采用如下这种方式

import torch
from torch import nn


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64*4*4, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model(x)
        return x

net = Net()

net.load_state_dict(torch.load("../test_model_save.pth"))
print(net)

在这里插入图片描述

总结

模型很大的情况下采用第二种方式,不是很大可采用第一种方式,官方推荐第二种方式

更多推荐