onnx-tensorflow icon indicating copy to clipboard operation
onnx-tensorflow copied to clipboard

Can't convert from ONNX to Tensorflow

Open YYazidd opened this issue 11 months ago • 4 comments

I have a .pt pytorch model file, and i wanted to convert it to a tensorflow model, i convert the .pt to .onnx file, and now i am stuck and i can't convert it to a .pb tensorflow model file I used the CLI: ( i tried both) !onnx-tf convert -i model.onnx -o newModel.pb !onnx-tf convert -i model.onnx -o newModel

and i get this error

warnings.warn( 2023-08-05 19:53:33,648 - onnx-tf - INFO - Start converting onnx pb to tf saved model WARNING:absl:input.1 is not a valid tf.function parameter name. Sanitizing to input_1.

YYazidd avatar Aug 05 '23 18:08 YYazidd

That is a warning, not necessarily an error. What other output do you get?

gcr avatar Aug 09 '23 19:08 gcr

this is the error:

Traceback (most recent call last): File "/opt/conda/bin/onnx-tf", line 8, in sys.exit(main()) File "/opt/conda/lib/python3.10/site-packages/onnx_tf/cli.py", line 20, in main return onnx_tf.converter.main(args[1:]) File "/opt/conda/lib/python3.10/site-packages/onnx_tf/converter.py", line 21, in main convert(**{k: v for k, v in vars(args).items() if v is not None}) File "/opt/conda/lib/python3.10/site-packages/onnx_tf/converter.py", line 147, in convert tf_rep.export_graph(outdir) File "/opt/conda/lib/python3.10/site-packages/onnx_tf/backend_rep.py", line 143, in export_graph signatures=self.tf_module.call.get_concrete_function( File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1258, in get_concrete_function concrete = self.get_concrete_function_garbage_collected(*args, **kwargs) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 1238, in get_concrete_function_garbage_collected self.initialize(args, kwargs, add_initializers_to=initializers) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 763, in initialize self.variable_creation_fn # pylint: disable=protected-access File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 171, in get_concrete_function_internal_garbage_collected concrete_function, _ = self.maybe_define_concrete_function(args, kwargs) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 166, in maybe_define_concrete_function return self.maybe_define_function(args, kwargs) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 396, in maybe_define_function concrete_function = self.create_concrete_function( File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 300, in create_concrete_function func_graph_module.func_graph_from_py_func( File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/func_graph.py", line 1214, in func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py", line 667, in wrapped_fn out = weak_wrapped_fn().wrapped(*args, **kwds) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py", line 484, in bound_method_wrapper return wrapped_fn(*args, **kwargs) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/func_graph.py", line 1200, in autograph_handler raise e.ag_error_metadata.to_exception(e) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/func_graph.py", line 1189, in autograph_handler return autograph.converted_call( File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 439, in converted_call result = converted_f(*effective_args, **kwargs) File "/tmp/autograph_generated_filebkizkbzj.py", line 30, in tf____call ag.for_stmt(ag.ld(self).graph_def.node, None, loop_body, get_state, set_state, (), {'iterate_names': 'node'}) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 451, in for_stmt for_fn(iter, extra_test, body, get_state, set_state, symbol_names, opts) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 502, in py_for_stmt body(target) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 468, in protected_body original_body(protected_iter) File "/tmp/autograph_generated_filebkizkbzj.py", line 23, in loop_body output_ops = ag.converted_call(ag.ld(self).backend.onnx_node_to_tensorflow_op, (ag.ld(onnx_node), ag.ld(tensor_dict), ag.ld(self).handlers), dict(opset=ag_.ld(self).opset, strict=ag__.ld(self).strict), fscope) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 439, in converted_call result = converted_f(*effective_args, **kwargs) File "/tmp/autograph_generated_file0fw9zfos.py", line 62, in tf___onnx_node_to_tensorflow_op ag.if_stmt(ag__.ld(handlers), if_body_1, else_body_1, get_state_1, set_state_1, ('do_return', 'retval_'), 2) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 1266, in if_stmt py_if_stmt(cond, body, orelse) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 1319, in py_if_stmt return body() if cond else orelse() File "/tmp/autograph_generated_file0fw9zfos.py", line 56, in if_body_1 ag.if_stmt(ag.ld(handler), if_body, else_body, get_state, set_state, ('do_return', 'retval_'), 2) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 1266, in if_stmt py_if_stmt(cond, body, orelse) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 1319, in py_if_stmt return body() if cond else orelse() File "/tmp/autograph_generated_file0fw9zfos.py", line 48, in if_body retval = ag.converted_call(ag_.ld(handler).handle, (ag__.ld(node),), dict(tensor_dict=ag__.ld(tensor_dict), strict=ag__.ld(strict)), fscope) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 439, in converted_call result = converted_f(*effective_args, **kwargs) File "/tmp/autograph_generated_fileo80mftje.py", line 41, in tf__handle ag.if_stmt(ag__.ld(ver_handle), if_body, else_body, get_state, set_state, ('do_return', 'retval_'), 2) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 1266, in if_stmt py_if_stmt(cond, body, orelse) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/operators/control_flow.py", line 1319, in py_if_stmt return body() if cond else orelse() File "/tmp/autograph_generated_fileo80mftje.py", line 30, in if_body ag.converted_call(ag.ld(cls).args_check, (ag__.ld(node),), dict(**ag__.ld(kwargs)), fscope) File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py", line 439, in converted_call result = converted_f(*effective_args, **kwargs) File "/tmp/autograph_generated_filekqynpcje.py", line 12, in tf__args_check x = ag.ld(kwargs)['tensor_dict'][ag__.ld(node).inputs[0]] tensorflow.python.autograph.pyct.error_utils.MultilineMessageKeyError: in user code:

File "/opt/conda/lib/python3.10/site-packages/onnx_tf/backend_tf_module.py", line 99, in __call__  *
    output_ops = self.backend._onnx_node_to_tensorflow_op(onnx_node,
File "/opt/conda/lib/python3.10/site-packages/onnx_tf/backend.py", line 347, in _onnx_node_to_tensorflow_op  *
    return handler.handle(node, tensor_dict=tensor_dict, strict=strict)
File "/opt/conda/lib/python3.10/site-packages/onnx_tf/handlers/handler.py", line 58, in handle  *
    cls.args_check(node, **kwargs)
File "/opt/conda/lib/python3.10/site-packages/onnx_tf/handlers/backend/resize.py", line 66, in args_check  *
    x = kwargs["tensor_dict"][node.inputs[0]]

KeyError: 'input.1'

YYazidd avatar Aug 09 '23 20:08 YYazidd

I have the same problem

justlike-prog avatar Aug 11 '23 10:08 justlike-prog

@YYazidd , if you used torch.onnx.export() to export .onnx model, can you try to set the input/output node names with only alpha-num, that is without . or any other special characters?

something like;

i_names=['i1', 'i2']
o_names=['o1']
torch.onnx.export(model, inputs, '/path/to/onnx/model/file',
                  input_names=i_names,
                  output_names=o_names)

change i_names and o_names array items to number of inputs/outputs of your model.

seanshpark avatar Aug 16 '23 23:08 seanshpark