Output Mismatch between pytorch model and converted onnx model for blazeface

Hi

pytorch version = 1.6.0+cpu
onnxruntime version =1.7.0
environment =ubuntu

I am trying to export a pretrained pytorch model for “blazeface” face detector in onnx.
Pytorch model definition and weights file taken from : GitHub - hollance/BlazeFace-PyTorch: The BlazeFace face detector model implemented in PyTorch
I exported the pytorch model to onnx using torch.onnx.export. And then used onnxruntime to compare the outputs from pytorch model and onnx model. But the outputs dont match

Code used:

import io
import sys

import numpy as np
import onnxruntime
import torch.onnx
import onnx

from blazeface import BlazeFace

def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()

def main():
print(torch.version)
#print(onnx.version)
print(onnxruntime.version)
net = BlazeFace()
net.load_weights(“blazeface.pth”)
torch.onnx.export(net, torch.randn(1, 3, 128, 128, device=‘cpu’), “blazeface.onnx”,
input_names=(“image”,), output_names=(“preds”, “confs”), opset_version=9, verbose=True
)

onnx_model = onnx.load("blazeface.onnx")
onnx.checker.check_model(onnx_model)

#comparing onnx output to pytorch output
torch_model = BlazeFace()
torch_model.eval()
x = torch.ones(1, 3, 128, 128, requires_grad=True)
torch_out = torch_model(x)
ort_session = onnxruntime.InferenceSession("blazeface.onnx")
# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
ort_outs = ort_session.run(None, ort_inputs)
# compare ONNX Runtime and PyTorch results
np.testing.assert_allclose(to_numpy(torch_out[0]), ort_outs[0], rtol=1e-03, atol=1e-05)

print("Exported model has been tested with ONNXRuntime, and the result looks good!")

if name == “main”:
sys.exit(main() or 0)

Output:
AssertionError:
Not equal to tolerance rtol=0.001, atol=1e-05

Mismatched elements: 14335 / 14336 (100%)
Max absolute difference: 99.00309
Max relative difference: 799.63086
x: array([[[ 0.335632, 0.003543, 0.032026, …, 0.004705, -0.224979,
-0.469438],
[ 0.180302, 0.292883, -0.117604, …, -0.218451, 0.165066,…
y: array([[[-9.223551e-01, 3.763577e-01, 2.190542e+01, …,
1.208612e+00, 5.133753e+00, -6.580073e+00],
[-7.786160e-01, 1.016221e+00, 2.847925e+01, …,…

Process finished with exit code 1

Hello,

Can you please share with us how you solved the problem? I am having a similar issue but don’t know how to fix it. Thanks!

1 Like