由于 torch.utils.serialization被从新版Pytorch中移除了,可选的方法有两种
方法一:
简单方法,能读入数据,但丢失网络结构信息
import torchfile 替代 from torch.utils.serialization import load_lua
同时torchfile.load 替换 load_lua
但是在运行Pytorch0.4.1代码时加载models,这种替换会出现读入格式的错误,例如https://github.com/sunshineatnoon/PytorchWCT的代码。
运行self.conv1.weight = torch.nn.Parameter(vgg1.get(0).weight.float())
会报错TypeError: ‘NoneType’ object is not callable
方法二:
解决办法是将Lua Torch model 转换为 PyTorch model,同时替换网络中读取参数的命令

将Lua Torch model 转换为 PyTorch model,需要使用Pytorch0.4.1。可以通过设置安装环境,避免pytorch版本冲突的问题。
1.环境配准方法如下
设置安装环境,可以通过两个Python空间安装不同版本的pytorch
创建一个名为python36的环境,指定Python版本是3.6(不用管是3.6.x,conda会为我们自动寻找3.6.x中的最新版本)
conda create --name python36 python=3.6安装好后,使用activate激活某个环境
source activate python36
2.配置和环境后,安装Pytorch0.4.1,方法https://www.pytorchtutorial.com/pytorch-installation-commands/,仅安装pytorch即可,不用安装torchvision
3.随后即可使用Pytorch0.4.1转换数据
转换.t7向.pth数据转换代码见https://github.com/clcarwin/convert_torch_to_pytorch
4.将.t7转换为.pth后,如下代码可以读入.pth的网络参数,并赋给new_state_dict。
import torch
from collections import OrderedDict
vgg1 = torch.load('./models/vgg_normalised_conv1_1.pth')
new_state_dict = OrderedDict()
for name, v in vgg1.items():
print(name)
new_state_dict[name] = v
name即为.pth的索引,name的值为
0.weight
0.bias
2.weight
2.bias
vgg1['0.weight']#名为'0.weight'的层参数
5.将部分网络参数赋值给新网络
首先建立新的网络,模型(层)必须与读入的.pth一致,随后将Lua网络层的参数读取替换。
vgg1.get(0).weight.float()替换为vgg1[‘0.weight’].float()
例子如下
class encoder1(nn.Module):
def __init__(self,vgg1):
super(encoder1,self).__init__()
# dissemble vgg2 and decoder2 layer by layer
# then resemble a new encoder-decoder network
# 224 x 224
self.conv1 = nn.Conv2d(3,3,1,1,0)
# self.conv1.weight = torch.nn.Parameter(vgg1.get(0).weight.float())
# self.conv1.bias = torch.nn.Parameter(vgg1.get(0).bias.float())
#替换层参数获取方法
self.conv1.weight = torch.nn.Parameter(vgg1['0.weight'].float())
self.conv1.bias = torch.nn.Parameter(vgg1['0.bias'].float())
在PyTorch新版本中,由于torch.utils.serialization被移除,导致load_lua无法使用。解决方法包括使用torchfile库(但可能丢失网络结构信息)或者将Lua Torch模型转换为PyTorch模型。转换过程涉及创建特定环境安装PyTorch 0.4.1,然后使用转换工具将.t7模型转换为.pth,最后根据.pth文件的键值赋值给新模型的相应层。
1700

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



