I have quantized and saved the model using this code -
model = Net()
model.load_state_dict(torch.load('model.pth', map_location=torch.device('cpu')))
model.qconfig = torch.quantization.default_qconfig
model.eval()
torch.quantization.prepare(model, inplace=True)
evaluate(model)
torch.quantization.convert(model, inplace=True)
model.eval()
x = evaluate(model)
torch.save(model.state_dict(), 'model_q.pth')
and loading the model like this -
model2 = Net()
torch.qconfig = torch.quantization.default_qconfig
model2.eval()
torch.quantization.prepare(model2, inplace=True)
torch.quantization.convert(model2, inplace=True)
model2.eval()
model2.load_state_dict(torch.load('model_q.pth'))
xQ = evaluate(model2)
Now x and xQ are different. I checked the parameters of both ‘model’ and ‘model2’. Parameters are same.
for i in range(len(list(model.parameters()))):
print(np.equal(list(model.parameters())[i].detach().numpy(), list(model2.parameters())[i].detach().numpy()))
All parameters are equal.
Is there anything incorrect with my method of loading or saving? or some bug in PyTorch?