polarisZhao / PFLD-pytorch

PFLD pytorch Implementation
798 stars 197 forks source link

使用onnx模型时得到的结果不相同/不一致 Result is different between using pytorch-model and onnx-model #58

Open zuozhen opened 3 years ago

zuozhen commented 3 years ago

python 3.7 + torch 1.8.1 ---中文--- 使用相同的输入,对pytorch模型 和 转换后的onnx模型进行测试,结果两个输出不一致,差别很大。 请问是什么原因,或者有人遇到相同问题吗? 谢谢。

---English--- I convert the pytorch model to onnx model using 'pytorch2onnx.py', then test the output of these two models. But the ouput of these two model on same input img is different. Very strange, can anyone help? Thanks.

zuozhen commented 3 years ago

code is as following: `from models.pfld import PFLDInference import torch

img = get_test_img() # here is input checkpoint = torch.load('test_model.pth.tar') pfld_backbone = PFLDInference() pfld_backbone.load_state_dict(checkpoint['pfldbackbone']) , landmarks1 = pfld_backbone(img)

import onnxruntime as rt sess = rt.InferenceSession('test_model.onnx') input_name = sess.get_inputs()[0].name img_n = img.cpu().detach().numpy() # convert tensor to numpy array onnx_output = sess.run(None, {input_name:img_n}) landmarks2 = onnx_output[1]`

the landmarks is quite different with landmarks2...

arch-raven commented 2 years ago

Hi @zuozhen, can you please share your requirements.txt? I am getting the following error. Thanks!

RuntimeError: ./checkpoint/snapshot/checkpoint.pth.tar is a zip archive (did you mean to use torch.jit.load()?)
arch-raven commented 2 years ago

Using python=3.7 along with the following package versions worked for me.

numpy==1.17.2
torch==1.7.0
opencv_python==4.1.0.25
tensorboardX==1.8
torchvision==0.8.0
zuozhen commented 2 years ago

Thanks for your reply. I forget how or whether I fixed it and now I do not take such research anymore though...