Output Mismatch between pytorch model and converted onnx model for blazeface


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():
net = BlazeFace()
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")

#comparing onnx output to pytorch output
torch_model = BlazeFace()
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)

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.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


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