Repository navigation
Replies: 1 comment 1 reply
|
Hi, One thing stands out in your code: you never put the PyTorch model into evaluation mode before running inference or exporting it. Try calling model_gc = torch.load("mymodel.pt", map_location=device)
model_gc.to(device)
model_gc.eval()
pic = pic.to(device)
with torch.inference_mode():
pytorch_out = model_gc(pic)
torch.onnx.export(
model_gc,
pic,
"mymodel.onnx",
opset_version=11,
input_names=["image"],
output_names=["output"],
)Then compare the outputs using the same input tensor: with torch.inference_mode():
pytorch_out = model_gc(pic).cpu().numpy()
ort_out = session.run(["output"], {"image": pic.cpu().numpy()})[0]
print("max abs difference:", np.max(np.abs(pytorch_out - ort_out)))
print("mean abs difference:", np.mean(np.abs(pytorch_out - ort_out)))If the difference remains, I would check these next:
The first test I’d run is simply If this answer helped or pointed you in the right direction, I'd appreciate it if you could mark it as the accepted answer so it's easier for others with the same issue to find. Also, if you found my contribution useful, I'd appreciate it if you could check out my GitHub profile, follow me, and star any repositories you find interesting. GitHub: https://github.com/Advait251206 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm having trouble getting a UNET running in ONNX runtime (in python) that works fine in PyTorch. The code I have is below (basically it loads a PT model, does inference on a single image, converts to ONNX, and then does inference on the same image with that.The outputs are different as shown by differning min/max/mean. I'm not sure if it's the oNNX conversion not working, or the way I'm calling the runtime.
The model is at; https://drive.google.com/file/d/16nJfbSAzaIou7cnCamAeG8PQuCcyYpcT/view?usp=sharing
The image is at: https://drive.google.com/file/d/1aw75jZFNH98NJbTs2v9huVECGLEyMdMc/view?usp=sharing
Outputs are:
PyTorch:
ONNX
Any ideas on how to get it working gratefully recieved!
All reactions