对自己之前的一段代码的.pth文件里面储存的内容有些疑惑,因为有时候.pth文件储存的是网络结构,而有些储存的是数据,例如:
from collections import OrderedDict
import torch
import torch.nn as nn
class MDNet(nn.Module):
def init(self, model_path=None, K=1):
super(MDNet, self).init()
self.avgpool=nn.AdaptiveAvgPool2d(1)
self.layers=nn.Sequential(OrderedDict([
(‘conv1’, nn.Sequential(nn.Conv2d(3, 96, kernel_size=7, stride=2),
nn.ReLU(inplace=True),
nn.LocalResponseNorm(2),
nn.MaxPool2d(kernel_size=3, stride=2))),
(‘conv2’, nn.Sequential(nn.Conv2d(96, 256, kernel_size=5, stride=2),
nn.ReLU(inplace=True),
nn.LocalResponseNorm(2),
nn.MaxPool2d(kernel_size=3, stride=2))),
(‘conv3’, nn.Sequential(nn.Conv2d(256, 512, kernel_size=3, stride=1),
nn.ReLU(inplace=True))),
# (‘fc4’, nn.Sequential(nn.Linear(500, 512),
# nn.ReLU(inplace=True))),
# (‘fc5’, nn.Sequential(nn.Dropout(0.5),
# nn.Linear(500, 512),
# nn.ReLU(inplace=T

.pth文件通常用于保存PyTorch模型的参数,包括权重和偏置,而不是模型架构。模型架构存在于代码中。torch.load加载.pth文件用于恢复模型状态,继续训练或推理。错误的保存方式可能导致.pth文件包含模型结构,正确的保存应使用torch.save(obj=self.state_dict())或torch.save(obj=model)。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



