ext_portrait_segmentation icon indicating copy to clipboard operation
ext_portrait_segmentation copied to clipboard

Converting .pth model to onnx

Open richard-salaschavez opened this issue 4 years ago • 7 comments

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.

richard-salaschavez avatar Jul 24 '20 19:07 richard-salaschavez

Have solve it?

xidiancpy avatar Oct 09 '20 07:10 xidiancpy

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)

xidiancpy avatar Oct 10 '20 05:10 xidiancpy

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?

aguthrie19 avatar Nov 07 '20 12:11 aguthrie19

add upon the avg_pool2d

发自我的iPhone

在 2020年11月7日,下午8:40,AlexanderGuthrie [email protected] 写道:

 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.

xidiancpy avatar Nov 07 '20 12:11 xidiancpy

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.

aguthrie19 avatar Nov 07 '20 14:11 aguthrie19

sorry

发自我的iPhone

在 2020年11月7日,下午10:38,AlexanderGuthrie [email protected] 写道:

 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'}}) Any advice to resolve this error and complete this onnx export?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

xidiancpy avatar Nov 09 '20 08:11 xidiancpy

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

aguthrie19 avatar Nov 17 '20 20:11 aguthrie19