新版本Pytorch中load_lua失效的解决办法及参数获取

在PyTorch新版本中,由于torch.utils.serialization被移除,导致load_lua无法使用。解决方法包括使用torchfile库(但可能丢失网络结构信息)或者将Lua Torch模型转换为PyTorch模型。转换过程涉及创建特定环境安装PyTorch 0.4.1,然后使用转换工具将.t7模型转换为.pth,最后根据.pth文件的键值赋值给新模型的相应层。
Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

由于 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())        

您可能感兴趣的与本文相关的镜像

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值