Does timm ViT models support PyTorch to ONNX conversion? #779
Unanswered
manideep2510
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Don't know much about ONNX myself, but in case it's useful, I know that tracer warning isn't your culprit. If you use an input with a valid size, that |
Beta Was this translation helpful? Give feedback.
0 replies
-
I was able to get a good result with (following this tutorial): import onnxruntime
import torch
import timm
import numpy as np
model = timm.create_model('vit_base_patch16_224', pretrained=True)
model.eval()
inp = torch.randn(2, 3, 224, 224)
with torch.no_grad():
torch_out = model(inp)
torch.onnx.export(model, inp, 'test.onnx', export_params=True, input_names = ['input'], output_names = ['output'])
ort_session = onnxruntime.InferenceSession('test.onnx')
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
# Compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(inp)}
ort_outs = ort_session.run(None, ort_inputs)
# Compare ONNX Runtime and PyTorch results
if np.allclose(to_numpy(torch_out), ort_outs[0], rtol=1e-03, atol=1e-05):
print("Exported model has been tested with ONNXRuntime, and the result looks good!")
else:
print("FAILED due to mismatch between outputs") |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello
Can we convert PyTorch ViT models provided in
timm
to ONNX.When I tried to convert
vit_base_patch16_224
model to ONNX, I am getting the below warning and the output of PyTorch and ONNX models are different for the same input.Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions