onnx / onnx-tensorflow

Tensorflow Backend for ONNX
Other
1.29k stars 297 forks source link

Runtime Error with KeyError: 'org.pytorch.aten' #604

Open P-Light opened 4 years ago

P-Light commented 4 years ago

Hello, I tried to convert model from onnx to tensorflow by CLI. Tensorflow successfully started up but there is runtime error without any details except this one:

  File "/home/user/.local/bin/onnx-tf", line 11, in <module>
    load_entry_point('onnx-tf', 'console_scripts', 'onnx-tf')()
  File "/home/user/onnx-tensorflow/onnx_tf/cli.py", line 20, in main
    return onnx_tf.converter.main(args[1:])
  File "/home/user/onnx-tensorflow/onnx_tf/converter.py", line 20, in main
    convert(**{k: v for k, v in vars(args).items() if v is not None})
  File "/home/user/onnx-tensorflow/onnx_tf/converter.py", line 133, in convert
    tf_rep = backend.prepare(onnx_model, **kwargs)
  File "/home/user/onnx-tensorflow/onnx_tf/backend.py", line 65, in prepare
    return cls.onnx_model_to_tensorflow_rep(model, strict)
  File "/home/user/onnx-tensorflow/onnx_tf/backend.py", line 85, in onnx_model_to_tensorflow_rep
    return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict)
  File "/home/user/onnx-tensorflow/onnx_tf/backend.py", line 143, in _onnx_graph_to_tensorflow_rep
    onnx_node, tensor_dict, handlers, opset=opset, strict=strict)
  File "/home/user/onnx-tensorflow/onnx_tf/backend.py", line 243, in _onnx_node_to_tensorflow_op
    handler = handlers[node.domain].get(node.op_type, None)
KeyError: 'org.pytorch.aten'

onnx==1.6.0 onnx-tf==1.5.0 tensorflow==2.1.0

theotheo commented 4 years ago

What the model did you try to convert?

P-Light commented 4 years ago

Custom CNN. Backbone is resnet34.

ShahrearBinAmin commented 4 years ago

@P-Light have you found any solution? I'm also facing same issue. My model is CRNN backbone is Resnet. My package's version is also same as you.

P-Light commented 4 years ago

@ShahrearBinAmin unfortunately I didn't.

ShahrearBinAmin commented 4 years ago

@theotheo I'm also facing same issue. I've converted my pytroch model to onnx. It was exported my torch model with the following parameters

onnx.export(self.net_,
                          x,
                          "saved_models/anpr.onnx",
                          opset_version=11,
                          operator_export_type=onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK,
                          input_names=["input"],
                          output_names=["output"])

If I pass operator_export_type=onnx.OperatorExportTypes.ONNX I get adaptive_max_pool2d not supported

chinhuang007 commented 4 years ago

@P-Light can you share your onnx model file for debug?

ShahrearBinAmin commented 4 years ago

@chinhuang007 I'm also facing same error. You can check my onnx model file here, I've also tried to load the model in onnx-runtime but got error

Fatal error: adaptive_max_pool2d is not a registered function/op

created an issue in onnx-runtime repo here they replied

Please report this to PyTorch as the exported onnx model is not valid because onnx doesn’t support ‘adaptive_max_pool2D’ in any opset version.

but adaptive_max_pool2d is supported from opset version 9, it's mapped to equivalent MaxPool of onnx.

adaptive_max_pool2d = _adaptive_pool('adaptive_max_pool2d', "MaxPool", _pair, max_pool2d_with_indices)

adaptive_max_pool2D in Symbolic_opeset9 Thanks

chinhuang007 commented 4 years ago

With some debugging, I found this node with type "adaptive_max_pool2d" and domain "org.pytorch.aten" causing the error. We currently support ONNX default domain only. So this model would not convert properly to tensorflow. The reason is that we don't understand what adaptive_max_pool2d means in org.pytorch.aten domain.

Your statement about adaptive_max_pool2d mapped to MaxPool is based on the code in Pytorch, not ONNX standard. I would recommend to find out whether the Pytorch to ONNX converter can change the node type to "MaxPool" and domain to "" which is the default for ONNX. Once that is fixed, the ONNX model should work in ONNX runtime and ONNX-TF.

P-Light commented 4 years ago

@P-Light can you share your onnx model file for debug?

Sure, download here.

chinhuang007 commented 4 years ago

@P-Light I found the similar issue with your model, a node with type="adaptive_avg_pool2d" and domain="org.pytorch.aten". So currently this model would not convert to tensorflow because we don't understand what adaptive_max_pool2d means in org.pytorch.aten domain.