Python Can';t将Pytorch转换为ONNX

Python Can';t将Pytorch转换为ONNX,python,pytorch,onnx,Python,Pytorch,Onnx,尝试用ONNX转换pytorch模型会出现此错误。我搜索过github,这个错误在1.1.0版本中出现过,但显然已经纠正了。现在我在火炬1.4.0上。(Python3.6.9)我看到了这个错误 File "/usr/local/lib/python3.6/dist-packages/torch/onnx/init.py", line 148, in export strip_doc_string, dynamic_axes, keep_initializers_as_inputs) File "

尝试用ONNX转换pytorch模型会出现此错误。我搜索过github,这个错误在1.1.0版本中出现过,但显然已经纠正了。现在我在火炬1.4.0上。(Python3.6.9)我看到了这个错误

File "/usr/local/lib/python3.6/dist-packages/torch/onnx/init.py", line 148, in export
strip_doc_string, dynamic_axes, keep_initializers_as_inputs)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 66, in export
dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 416, in _export
fixed_batch_size=fixed_batch_size)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 296, in _model_to_graph
fixed_batch_size=fixed_batch_size, params_dict=params_dict)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 135, in _optimize_graph
graph = torch._C._jit_pass_onnx(graph, operator_export_type)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/init.py", line 179, in _run_symbolic_function
return utils._run_symbolic_function(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 657, in _run_symbolic_function
return op_fn(g, *inputs, **attrs)
File "/usr/local/lib/python3.6/dist-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 "/usr/local/lib/python3.6/dist-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 "/usr/local/lib/python3.6/dist-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
如何修复它?我还尝试了最新的夜间构建,同样的错误也出现了

我的代码:

from model import BiSeNet
import torch.onnx
import torch

net = BiSeNet(19)
net.cuda()
net.load_state_dict(torch.load('/content/drive/My Drive/Collab/fp/res/cp/79999_iter.pth'))
net.eval()

dummy = torch.rand(1,3,512,512).cuda()
torch.onnx.export(net, dummy, "Model.onnx", input_names=["image"], output_names=["output"])
在引发运行时错误之前,我向symbolic_helper.py添加了
print(v.node())
,以查看导致错误的原因

这是输出:
%595:Long()=onnx::Gather[axis=0](%592,%594)#/content/drive/My drive/Collab/fp/model.py:111:0

model.py中111行是:
avg=F.avg\u pool2d(feat32,feat32.size()[2:])

这表明onnx无法识别Pytork中的tensor.size方法,需要将其修改为常数。

您可以:

print(feat32.size()[2:])
并替换:

F.avg_pool2d(feat32, feat32.size()[2:]) 
与:


我一直在为同样的问题挣扎

F.在我的情况下,不支持adaptive_avg_pool2d。您必须尝试其他操作

我希望这有帮助

感谢更改的实例

x = F.avg_pool2d(x, x.shape[2:])


这样,
avg_pool2d
的输入形状是常量
[k,k]
,而不是前面提到的
torch.Size([k,k])

在使用

torch.onnx.export(型号、x、onnx文件路径)

我通过指定
opset\u版本来修复它,如下所示:

torch.onnx.export(型号,x,onnx文件路径,操作集版本=11)

x = F.avg_pool2d(x, x.shape[2:])
x_shape = [int(s) for s in x.shape[2:]]
x = F.avg_pool2d(x, x_shape)