Pytorch1.1版本pytorch模型转onnx的bug

Bug in pytorch 1.1:

/usr/local/lib/python3.5/dist-packages/torch/nn/functional.py:2539: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode))
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/__init__.py", line 19, in _export
    result = utils._export(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py", line 363, in _export
    _retain_param_name, do_constant_folding)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py", line 278, in _model_to_graph
    _disable_torch_constant_prop=_disable_torch_constant_prop)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py", line 188, in _optimize_graph
    graph = torch._C._jit_pass_onnx(graph, operator_export_type)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/__init__.py", line 50, in _run_symbolic_function
    return utils._run_symbolic_function(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/utils.py", line 589, in _run_symbolic_function
    return fn(g, *inputs, **attrs)
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/symbolic.py", line 130, in wrapper
    args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/symbolic.py", line 130, in
    args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]
  File "/usr/local/lib/python3.5/dist-packages/torch/onnx/symbolic.py", line 90, in _parse_arg
    raise RuntimeError("Failed to export an ONNX attribute, "

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

Solution:

降级到Pytorch 1.0版。方法示例如下:

sudo pip install torch==1.0.1 -f https://download.pytorch.org/whl/cu90/stable

直接安装新版本即可,无需执行命令卸载原来安装的版本,因为pip下载完成后会自动unistall已安装的版本,并install刚刚安装的版本,显示如下:

Installing collected packages: torch
  Found existing installation: torch 1.1.0
    Uninstalling torch-1.1.0:
      Successfully uninstalled torch-1.1.0
Successfully installed torch-1.0.1

随后安装torchvision,如果直接使用命令pip3 install torchvison安装最新版的torchvison可能与较低版本的pytorch不兼容,系统会再次默认下载安装匹配的高版本的pytorch,为防止这一问题,可安装指定版本的torchvision:

sudo pip3 install torchvision==0.2.1

你可能感兴趣的:(深度学习)