pytorch 动态量化加载权重出错unexpected keys _packed_params

错误原因:使用了原始定义的model,加载了量化后保存的checkpoint 文件。

解决方式:用quantized.dynamic对应的量化层替代原网络的量化成就行了(这里我只量化了fc,和lstm层)。

相关实验代码:

仅仅只是量化对应的权重层,这里只量化lstm,fc层。

import numpy as np
import time
import cv2
import torch
import torch.nn as nn
import torch.nn.quantized.dynamic as nnqd
class mymodel(nn.Module):
    def __init__(self):
        super(mymodel, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, stride=2,kernel_size=(2,2))
        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64,stride=2,kernel_size=(2,2))
        self.conv3 = nn.Conv2d(in_channels=64, out_channels=3, stride=2,kernel_size=(2,2))
        self.lstm = nn.LSTM(3, 3,bidirectional=True)
        self.fc = nn.Linear(in_features=864,out_features=1000)
        #define quantized.dynamic layer,如果你在量化后保存了对应的pth,就要用下面注释的两行代替上面的fc,lstm,不然当你用量化前的model.load("量化后的pth")就会报错unexpected keys
        # self.lstm = nnqd.LSTM(3, 3,bidirectional=True)#(seq_len, batch, input_size)
        # self.fc = nnqd.Linear(in_features=864, out_features=1000, dtype=torch.qint8)
    def forward(self,inputs):
        b,c,h,w = inputs.size()
        assert c==3,"ok"
        x = self.conv1(inputs)
        x = self.conv2(x)
        x = self.conv3(x)#torch.Size([3, 3, 12, 12])
        x = x.view(x.size(0),x.size(0),-1)#(3,3,144)
        x = x.permute(2, 0, 1)#(144,3,3)
        x,_ = self.lstm(x)#input:(seq_len,batch,feature),output:(144,3,6)
        x = x.permute(1,2,0)
        x= x.reshape(x.size(0),-1)
        x = self.fc(x)
        return x

if  __name__ == "__main__":
    x = torch.rand((3,3,96,96))
    model = mymodel()
    print(model)
    tq_model =tq.quantize_dynamic(model,{nn.LSTM,nn.Linear},dtype=torch.qint8)
    print(tq_model)
    #y_checkpoint = torch.load("1.pth")['state_dict']
    #tq_checkpoint = torch.load("1_tq.pth")['state_dict']
    # torch.save({"state_dict":model.state_dict()},"1.pth")
    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值