onnx / onnx-tensorflow

Tensorflow Backend for ONNX
Other
1.27k stars 296 forks source link

converting pytorch model to tensorflow #499

Open Malathi15 opened 5 years ago

Malathi15 commented 5 years ago

when i ran this command

import onnx
from onnx_tf.backend import prepare

# Load the ONNX file
model = onnx.load('output/mnist.onnx')

# Import the ONNX model to Tensorflow
tf_rep = prepare(model)

I got the following error


AttributeError                            Traceback (most recent call last)
<ipython-input-38-86790127a780> in <module>()
      1 import onnx
----> 2 from onnx_tf.backend import prepare
      3 
      4 # Load the ONNX file
      5 model = onnx.load('output/mnist.onnx')

~\Desktop\onnx-tensorflow\onnx_tf\__init__.py in <module>()
----> 1 from . import frontend
      2 from . import backend

~\Desktop\onnx-tensorflow\onnx_tf\frontend.py in <module>()
     14 
     15 from onnx_tf.common import exception
---> 16 from onnx_tf.common.handler_helper import get_all_frontend_handlers
     17 from onnx_tf.common import IS_PYTHON3
     18 from onnx_tf.handlers.frontend_handler import FrontendHandler

~\Desktop\onnx-tensorflow\onnx_tf\common\handler_helper.py in <module>()
      3 from onnx import defs
 5 from onnx_tf.handlers.backend import *  # noqa
      6 from onnx_tf.handlers.backend_handler import BackendHandler
      7 from onnx_tf.handlers.frontend import *  # noqa

~\Desktop\onnx-tensorflow\onnx_tf\handlers\backend\is_inf.py in <module>()
      6 
      7 onnx_op("IsInf")
----> 8 tf_func(tf.math.is_inf)
      9 class IsInf(BackendHandler):
     10 

AttributeError: module 'tensorflow._api.v1.math' has no attribute 'is_inf'

Can anyone help me how to solve this issue ?

Thanks in advance

pbashivan commented 5 years ago

I tried upgrading tensorflow from version 1.12.0 to 1.14.0 and it fixed the issue.

prasanthpul commented 5 years ago

Please reopen if @pbashivan's solution doesn't work.

chahna107 commented 5 years ago

@pbashivan I got the above mentioned error with Tensorflow 1.12.0. So I upgraded tensorflow to 1.14.0 as mentioned, but now I get a different error.

File "/usr/local/lib/python3.6/site-packages/onnx_tf/backend.py", line 55, in prepare
    return cls.onnx_model_to_tensorflow_rep(model, strict)
  File "/usr/local/lib/python3.6/site-packages/onnx_tf/backend.py", line 75, in onnx_model_to_tensorflow_rep
    return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict)
  File "/usr/local/lib/python3.6/site-packages/onnx_tf/backend.py", line 129, in _onnx_graph_to_tensorflow_rep
    onnx_node, tensor_dict, handlers, opset=opset, strict=strict)
  File "/usr/local/lib/python3.6/site-packages/onnx_tf/backend.py", line 224, in _onnx_node_to_tensorflow_op
    return handler.handle(node, tensor_dict=tensor_dict, strict=strict)
  File "/usr/local/lib/python3.6/site-packages/onnx_tf/handlers/handler.py", line 59, in handle
    return ver_handle(node, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/onnx_tf/handlers/backend/lstm.py", line 212, in version_7
    return cls._common(node, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/onnx_tf/handlers/backend/lstm.py", line 182, in _common
    rnn_kwargs, tf_activations, direction)
  File "/usr/local/lib/python3.6/site-packages/onnx_tf/handlers/backend/rnn_mixin.py", line 33, in rnn
    cell_kwargs["activation"] = activations[1]
IndexError: list index out of range

Can you please help? Thanks!

M-crazy commented 5 years ago

@chahna107 Hi, Have you solved the problem,I got the same error?

chahna107 commented 5 years ago

@M-crazy Unfortunately no. I haven't found any solution to this yet.

chinhuang007 commented 5 years ago

@M-crazy @chahna107 Can you provide the onnx file for debugging?

wyf94 commented 4 years ago

go to this files "onnx-tensorflow\onnx_tf\handlers\backend\is_inf.py" change "@tf_func(tf.math.is_inf)" to " @tf_func(tf.is_inf) "

zyf001 commented 4 years ago

@chinhuang007 Hi, I got the same error. And this is my onnx file, which has four lstm layers, lstm.onnx.zip

Thanks in advance

harshpaal commented 4 years ago

%%capture model_onnx = onnx.load('./efficientnet.onnx', load_external_data=False)

tf_rep = prepare(model_onnx)

Export model as .pb file

tf_rep.export_graph('./efficientnet_tf.pb')

Running above commands give me following error

TypeError Traceback (most recent call last)

in () 1 model_onnx = onnx.load('./efficientnet.onnx', load_external_data=False) 2 ----> 3 tf_rep = prepare(model_onnx) 4 5 # Export model as .pb file 8 frames /usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in prepare(cls, model, device, strict, logging_level, **kwargs) 63 common.logger.setLevel(logging_level) 64 ---> 65 return cls.onnx_model_to_tensorflow_rep(model, strict) 66 67 @classmethod /usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in onnx_model_to_tensorflow_rep(cls, model, strict) 83 else: 84 opset_import = model.opset_import ---> 85 return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict) 86 87 @classmethod /usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in _onnx_graph_to_tensorflow_rep(cls, graph_def, opset, strict) 141 onnx_node = OnnxNode(node) 142 output_ops = cls._onnx_node_to_tensorflow_op( --> 143 onnx_node, tensor_dict, handlers, opset=opset, strict=strict) 144 curr_node_output_map = dict(zip(onnx_node.outputs, output_ops)) 145 tensor_dict.update(curr_node_output_map) /usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in _onnx_node_to_tensorflow_op(cls, node, tensor_dict, handlers, opset, strict) 236 handler = handlers[node.domain].get(node.op_type, None) 237 if handler: --> 238 return handler.handle(node, tensor_dict=tensor_dict, strict=strict) 239 else: 240 exception.OP_UNIMPLEMENTED_EXCEPT(node.op_type) /usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/handler.py in handle(cls, node, **kwargs) 59 if ver_handle: 60 cls.args_check(node, **kwargs) ---> 61 return ver_handle(node, **kwargs) 62 exception.OP_UNIMPLEMENTED_EXCEPT(node.op_type, cls.SINCE_VERSION) 63 return None /usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend/pad.py in version_2(cls, node, **kwargs) 59 @classmethod 60 def version_2(cls, node, **kwargs): ---> 61 return cls._common(node, **kwargs) 62 63 @classmethod /usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend/pad.py in _common(cls, node, **kwargs) 50 return [ 51 cls.make_tensor_from_onnx_node( ---> 52 node, inputs=[x, paddings, mode, None, constant_values], **kwargs) 53 ] 54 /usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend_handler.py in make_tensor_from_onnx_node(cls, node, tf_func, inputs, attrs, name, c_first_cuda_only, c_last_only, **kwargs) 109 return cls.c_last_only(tf_func, inputs, attrs) 110 --> 111 return cls._run_tf_func(tf_func, inputs, attrs) 112 113 @classmethod /usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend_handler.py in _run_tf_func(cls, tf_func, inputs, attrs) 180 attrs = cls._process_attrs(attrs) 181 return tf_func(*inputs, --> 182 **dict([(p, attrs[p]) for p in params if p in attrs])) TypeError: pad() got multiple values for argument 'name' ------------------------------------------------------------------------------------------------------- can anyone help? Thanks in advance
ghost commented 4 years ago

%%capture model_onnx = onnx.load('./efficientnet.onnx', load_external_data=False)

tf_rep = prepare(model_onnx)

Export model as .pb file

tf_rep.export_graph('./efficientnet_tf.pb')

Running above commands give me following error

TypeError Traceback (most recent call last) in () 1 model_onnx = onnx.load('./efficientnet.onnx', load_external_data=False) 2 ----> 3 tf_rep = prepare(model_onnx) 4 5 # Export model as .pb file

8 frames /usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in prepare(cls, model, device, strict, logging_level, **kwargs) 63 common.logger.setLevel(logging_level) 64 ---> 65 return cls.onnx_model_to_tensorflow_rep(model, strict) 66 67 @classmethod

/usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in onnx_model_to_tensorflow_rep(cls, model, strict) 83 else: 84 opset_import = model.opset_import ---> 85 return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict) 86 87 @classmethod

/usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in _onnx_graph_to_tensorflow_rep(cls, graph_def, opset, strict) 141 onnx_node = OnnxNode(node) 142 output_ops = cls._onnx_node_to_tensorflow_op( --> 143 onnx_node, tensor_dict, handlers, opset=opset, strict=strict) 144 curr_node_output_map = dict(zip(onnx_node.outputs, output_ops)) 145 tensor_dict.update(curr_node_output_map)

/usr/local/lib/python3.6/dist-packages/onnx_tf/backend.py in _onnx_node_to_tensorflow_op(cls, node, tensor_dict, handlers, opset, strict) 236 handler = handlers[node.domain].get(node.op_type, None) 237 if handler: --> 238 return handler.handle(node, tensor_dict=tensor_dict, strict=strict) 239 else: 240 exception.OP_UNIMPLEMENTED_EXCEPT(node.op_type)

/usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/handler.py in handle(cls, node, kwargs) 59 if ver_handle: 60 cls.args_check(node, kwargs) ---> 61 return ver_handle(node, **kwargs) 62 exception.OP_UNIMPLEMENTED_EXCEPT(node.op_type, cls.SINCE_VERSION) 63 return None

/usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend/pad.py in version_2(cls, node, kwargs) 59 @classmethod 60 def version_2(cls, node, kwargs): ---> 61 return cls._common(node, **kwargs) 62 63 @classmethod

/usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend/pad.py in _common(cls, node, kwargs) 50 return [ 51 cls.make_tensor_from_onnx_node( ---> 52 node, inputs=[x, paddings, mode, None, constant_values], kwargs) 53 ] 54

/usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend_handler.py in make_tensor_from_onnx_node(cls, node, tf_func, inputs, attrs, name, c_first_cuda_only, c_last_only, **kwargs) 109 return cls.c_last_only(tf_func, inputs, attrs) 110 --> 111 return cls._run_tf_func(tf_func, inputs, attrs) 112 113 @classmethod

/usr/local/lib/python3.6/dist-packages/onnx_tf/handlers/backend_handler.py in _run_tf_func(cls, tf_func, inputs, attrs) 180 attrs = cls._process_attrs(attrs) 181 return tf_func(*inputs, --> 182 **dict([(p, attrs[p]) for p in params if p in attrs]))

TypeError: pad() got multiple values for argument 'name'

can anyone help? Thanks in advance

I got same isue. Have you fixed it yet?