Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting .pth model to onnx #28

Open
richard-salaschavez opened this issue Jul 24, 2020 · 7 comments
Open

Converting .pth model to onnx #28

richard-salaschavez opened this issue Jul 24, 2020 · 7 comments

Comments

@richard-salaschavez
Copy link

I am trying to convert the pth model to onnx and I get the following error:

Traceback (most recent call last):
File "", line 1, in
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/init.py", line 26, in _export
result = utils._export(*args, **kwargs)
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/utils.py", line 488, in _export
fixed_batch_size=fixed_batch_size)
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/utils.py", line 351, in _model_to_graph
fixed_batch_size=fixed_batch_size, params_dict=params_dict)
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/utils.py", line 154, in _optimize_graph
graph = torch._C._jit_pass_onnx(graph, operator_export_type)
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/init.py", line 199, in _run_symbolic_function
return utils._run_symbolic_function(*args, **kwargs)
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/utils.py", line 740, in _run_symbolic_function
return op_fn(g, *inputs, **attrs)
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py", line 128, in wrapper
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py", line 128, in
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]
File "/home/rsalasch/miniconda3/envs/tf1_4/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py", line 81, in _parse_arg
"', since it's not constant, please try to make "

RuntimeError: Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible

I think the error is caused by the AveragePool2D

(6)
(d2): S2block(
(down_res): AvgPool2d(kernel_size=4, stride=4, padding=0)
(7)
(d2): S2block(
(down_res): AvgPool2d(kernel_size=2, stride=2, padding=0)

Please let me know if you have converted the model to onnx format before or if this error seems familiar.

@xidiancpy
Copy link

Have solve it?

@xidiancpy
Copy link

add this code:
size_array = [int(s) for s in x.size()[2:]]
out = torch.nn.functional.avg_pool2d(x, size_array).view(batch, -1)

@aguthrie19
Copy link

add this code:
size_array = [int(s) for s in x.size()[2:]]
out = torch.nn.functional.avg_pool2d(x, size_array).view(batch, -1)
@richard-salaschavez I ran into the same problem.
@xidiancpy Where to add this code?

@xidiancpy
Copy link

xidiancpy commented Nov 7, 2020 via email

@aguthrie19
Copy link

aguthrie19 commented Nov 7, 2020

add upon the avg_pool2d 发自我的iPhone

在 2020年11月7日,下午8:40,AlexanderGuthrie @.***> 写道:  add this code: size_array = [int(s) for s in x.size()[2:]] out = torch.nn.functional.avg_pool2d(x, size_array).view(batch, -1) @richard-salaschavez I ran into the same problem. @xidiancpy Where to add this code? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

谢谢 thank you but I think I need a different solution. I added print(desc,value,value.node(),value.node().kind()) to symbolic_helper.py so that I can debug. I am getting this error:

v 426 defined in (%426 : Float(1:37632, 12:3136, 56:56, 56:1, requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=12, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%413, %414) # /usr/local/lib/python3.8/dist-packages/torch/nn/modules/conv.py:419:0 )
%426 : Float(1:37632, 12:3136, 56:56, 56:1, requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=12, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%413, %414) # /usr/local/lib/python3.8/dist-packages/torch/nn/modules/conv.py:419:0
 onnx::Conv

is 447 defined in (%447 : int[] = prim::ListConstruct(%441, %446) )
%447 : int[] = prim::ListConstruct(%441, %446)
 prim::ListConstruct
Traceback (most recent call last):
...
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/symbolic_helper.py", line 81, in _parse_arg
    raise RuntimeError("Failed to export an ONNX attribute '" + v.node().kind() +
RuntimeError: Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible

Also I am new to exporting .pth to .onnx and this is my exporting code:

import torch
import torch.onnx
from models.SINet import SINet

config = [[[3, 1], [5, 1]], [[3, 1], [3, 1]],[[3, 1], [5, 1]], [[3, 1], [3, 1]], [[5, 1], [3, 2]], [[5, 2], [3, 4]],[[3, 1], [3, 1]], [[5, 1], [5, 1]], [[3, 2], [3, 4]], [[3, 1], [5, 2]]]
model = SINet(classes=2, p=2, q=8, config=config,chnn=1)
state_dict = torch.load('result/SINet/SINet.pth', map_location=torch.device('cpu'))
model.load_state_dict(state_dict)
dummy_input = torch.FloatTensor(1, 3, 224, 224)
torch.onnx.export(model,dummy_input,"SINet.onnx",export_params=True,input_names=['input'],output_names=['output'],dynamic_axes={'input' : {0: 'batch_size'},'output' : {0 : 'batch_size'}})

Is this a similar problem but with nn.Conv2d()? If so, how do I resolve? Any advice to complete this onnx export appreciated.

@xidiancpy
Copy link

xidiancpy commented Nov 9, 2020 via email

@aguthrie19
Copy link

@richard-salaschavez @xidiancpy
I successfully converted the SINet pytorch model to a .onnx model using the SINet_ONNX.ipynb notebook by @anilsathyan7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants