错误原因:使用了原始定义的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")