EfficientNet-PyTorch
EfficientNet-PyTorch copied to clipboard
ONNX can't export SwishImplementation
Hi, Thanks for the PyTorch implementation. It is really good. However, I've ran into some issues. While exporting using torch.onnx, I am getting an error. The code is as follows:
model = EfficientNet.from_name(model_name='efficientnet-b0')
torch.onnx.export(model, torch.rand(10,3,240,240), "EfficientNet-B0.onnx")
The error I am getting is:
Traceback (most recent call last):
File "/home/bishshoy/pycharm/EfficientNet-PyTorch/main.py", line 17, in <module>
main()
File "/home/bishshoy/pycharm/EfficientNet-PyTorch/main.py", line 7, in main
torch.onnx.export(model, torch.rand(10,3,240,240), "EfficientNet-B7.onnx")
File "/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/__init__.py", line 27, in export
return utils.export(*args, **kwargs)
File "/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/utils.py", line 104, in export
operator_export_type=operator_export_type)
File "/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/utils.py", line 287, in _export
proto, export_map = graph._export_onnx(params, _onnx_opset_version, defer_weight_export, operator_export_type)
RuntimeError: ONNX export failed: Couldn't export Python operator SwishImplementation
Defined at:
/home/bishshoy/pycharm/EfficientNet-PyTorch/efficientnet_pytorch/utils.py(52): forward
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py(477): _slow_forward
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py(487): __call__
/home/bishshoy/pycharm/EfficientNet-PyTorch/efficientnet_pytorch/model.py(175): extract_features
/home/bishshoy/pycharm/EfficientNet-PyTorch/efficientnet_pytorch/model.py(193): forward
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py(477): _slow_forward
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py(487): __call__
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/jit/__init__.py(252): forward
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py(489): __call__
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/jit/__init__.py(197): get_trace_graph
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/utils.py(192): _trace_and_get_graph_from_model
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/utils.py(224): _model_to_graph
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/utils.py(281): _export
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/utils.py(104): export
/home/bishshoy/miniconda3/lib/python3.7/site-packages/torch/onnx/__init__.py(27): export
/home/bishshoy/pycharm/EfficientNet-PyTorch/main.py(7): main
/home/bishshoy/pycharm/EfficientNet-PyTorch/main.py(17): <module>
Graph we tried to export:
graph(%input.1 : Float(10, 3, 240, 240)
%1 : Float(32, 3, 3, 3)
%2 : Float(32)
%3 : Float(32)
%4 : Float(32)
%5 : Float(32)
%6 : Long()
...
...
...
Hi, thanks for the issue. The latest update to the repo (pip version 0.5.1) includes two versions of the Swish function, one for training and another for exporting. To switch to the export-friendly version, use .set_swish(memory_efficient=False) . For example:
model = EfficientNet.from_name(model_name='efficientnet-b0')
model.set_swish(memory_efficient=False)
torch.onnx.export(model, torch.rand(10,3,240,240), "EfficientNet-B0.onnx")
Let me know if this does or does not work for you.
On second thought, I'll leave this open until you confirm it is working.
Yes it is working now. Thanks.
I also meet this problem when I convert .pth to pt. I think it is the same problems.
So, just a reminder to update the ONNX export example in the Readme https://github.com/zhanghang1989/EfficientNet-PyTorch/blob/master/README.md
Also please update Colab
Hi, thanks for the issue. The latest update to the repo (pip version
0.5.1) includes two versions of the Swish function, one for training and another for exporting. To switch to the export-friendly version, use.set_swish(memory_efficient=False). For example:model = EfficientNet.from_name(model_name='efficientnet-b0') model.set_swish(memory_efficient=False) torch.onnx.export(model, torch.rand(10,3,240,240), "EfficientNet-B0.onnx")Let me know if this does or does not work for you.
Thanks for your nice work. I want to ask, by setting model.set_swish(memory_efficient=False), obviously the convertion is slower. Does it hurt the performance of the converted model for inference?
then i get AttributeError: 'UnetPlusPlus' object has no attribute 'set_swish', how should I do