rwightman/gen-efficientnet-pytorch

Cannot export an ONNX file with PyTorch 1.7.0

kentanabe opened this issue · 3 comments

I downloaded models via the PyTorch Hub API and exported ONNX file well on a few weeks ago.
But now, I cannot do that.

The geffnet 3edcd4d, which was commited a week ago, uses SiLU activation with PyTorch 1.7.0, but recent ONNX opset 12 hasn't supported SiLU yet.
Therefore torch.onnx.export() of PyTorch 1.7.0 results in following error.

import torch

model = torch.hub.load(
    'rwightman/gen-efficientnet-pytorch', 'tf_efficientnet_b0',
    pretrained=True, exportable=True, verbose=False)
model.eval()
dummy_input = torch.randn(1,3,224,224)
model(dummy_input)
traced_model = torch.jit.trace(model, dummy_input)
torch_out = torch.onnx.export(
    model, dummy_input, 'tf_efficientnet_b0.onnx',
    export_params=True, verbose=False,opset_version=12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/__init__.py", line 230, in export
    custom_opsets, enable_onnx_checker, use_external_data_format)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 91, in export
    use_external_data_format=use_external_data_format)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 639, in _export
    dynamic_axes=dynamic_axes)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 421, in _model_to_graph
    dynamic_axes=dynamic_axes, input_names=input_names)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 203, in _optimize_graph
    graph = torch._C._jit_pass_onnx(graph, operator_export_type)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/__init__.py", line 263, in _run_symbolic_function
    return utils._run_symbolic_function(*args, **kwargs)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 930, in _run_symbolic_function
    symbolic_fn = _find_symbolic_in_registry(domain, op_name, opset_version, operator_export_type)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 888, in _find_symbolic_in_registry
    return sym_registry.get_registered_op(op_name, domain, opset_version)
  File "/home/k-tanabe/.local/lib/python3.6/site-packages/torch/onnx/symbolic_registry.py", line 111, in get_registered_op
    raise RuntimeError(msg)
RuntimeError: Exporting the operator silu to ONNX opset version 12 is not supported. Please open a bug to request ONNX export support for the missing operator

You have updated also geffnet/version.py from 1.0.0 to 1.0.1 in 3edcd4d.
Could you tag v1.0.0 on the previous revision e84f554, which supports ONNX file export with PyTorch 1.7.0.
Then I can down load v1.0.0 with a following command.

model = torch.hub.load(
    'rwightman/gen-efficientnet-pytorch:v1.0.0', 'tf_efficientnet_b0',
    pretrained=True, exportable=True, verbose=False)

Damn, I thought the native SiLU would reduce the export issues, didn't realize they hadn't supported it.

Quick fix, hard code this to False https://github.com/rwightman/gen-efficientnet-pytorch/blob/master/geffnet/activations/__init__.py#L7

Thanks for your quick response, @rwightman

I hope that Swish or SiLU will be supported in the future ONNX opset soon and PyTorch will support the recent opset immediately.

Btw, I'll clone codes from this repo and modify the file, instead of downloading via PyTorch hub.

Thanks @rwightman !

I’ve confirmed exporting tf_efficientnet_b0 via Torch Hub, a4ac4dd, just now.