onnx / onnx-tensorflow

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

TypeError: Failed to convert object of type <class 'list'> to Tensor. #688

Closed buqing2009 closed 4 years ago

buqing2009 commented 4 years ago

Describe the bug

I convert my pytorch model to onnx, and the precision of result has been tested. Now i convert the onnx to tensorflow using onnx-tf, it return error code:

TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [1, None, 1, 1]. Consider casting elements to a supported type.

To Reproduce The details as below:

Traceback (most recent call last):
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py", line 543, in make_tensor_proto
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py", line 543, in <listcomp>
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/util/compat.py", line 87, in as_bytes
    (bytes_or_text,))
TypeError: Expected binary or unicode string, got 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "export_onnx2tensorflow.py", line 11, in <module>
    tf_rep = prepare(model)
  File "/home/buqing/projects/ONNX2Tensorflow/onnx-tensorflow/onnx_tf/backend.py", line 66, in prepare
    return cls.onnx_model_to_tensorflow_rep(model, strict)
  File "/home/buqing/projects/ONNX2Tensorflow/onnx-tensorflow/onnx_tf/backend.py", line 86, in onnx_model_to_tensorflow_rep
    return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict)
  File "/home/buqing/projects/ONNX2Tensorflow/onnx-tensorflow/onnx_tf/backend.py", line 147, in _onnx_graph_to_tensorflow_rep
    strict=strict)
  File "/home/buqing/projects/ONNX2Tensorflow/onnx-tensorflow/onnx_tf/backend.py", line 252, in _onnx_node_to_tensorflow_op
    return handler.handle(node, tensor_dict=tensor_dict, strict=strict)
  File "/home/buqing/projects/ONNX2Tensorflow/onnx-tensorflow/onnx_tf/handlers/handler.py", line 60, in handle
    return ver_handle(node, **kwargs)
  File "/home/buqing/projects/ONNX2Tensorflow/onnx-tensorflow/onnx_tf/handlers/backend/batch_normalization.py", line 69, in version_9
    return cls._common(node, **kwargs)
  File "/home/buqing/projects/ONNX2Tensorflow/onnx-tensorflow/onnx_tf/handlers/backend/batch_normalization.py", line 34, in _common
    scale = tf.reshape(tensor_dict[node.inputs[1]], params_shape_broadcast)
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py", line 193, in reshape
    result = gen_array_ops.reshape(tensor, shape, name)
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 8087, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 473, in _apply_op_helper
    raise err
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 470, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1341, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 321, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 262, in constant
    allow_broadcast=True)
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 300, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "/home/buqing/anaconda3/envs/Tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py", line 547, in make_tensor_proto
    "supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [1, None, 1, 1]. Consider casting elements to a supported type.

But there is no list type in my onnx model, so how did this error occur?

ONNX model file

downlowd model

Python, ONNX, ONNX-TF, Tensorflow version

This section can be obtained by running get_version.py from util folder.

Additional context

Add any other context about the problem here.

sayakpaul commented 4 years ago

Encountering the same problem from today but this was not the case a week back. Here's the Colab Notebook for reproducing the issue.

buqing2009 commented 4 years ago

@winnietsang recompiled 6c1213e, and now fixed! Thank you!

sayakpaul commented 4 years ago

@buqing2009 could you elaborate on how you fixed it up?

I installed onnx-tf like the following:

!git clone https://github.com/onnx/onnx-tensorflow 
!pip install onnx
%cd onnx-tensorflow
!git checkout 6c1213ed5628111d03bdb58f61fe941357ec18cf # To reflect the fix
!pip install -e .

The ! is there because I am running from a Colab Notebook. Here's my Colab Notebook that can reproduce this issue.

Any pointers?

Cc: @winnietsang

winnietsang commented 4 years ago

@sayakpaul please checkout my PR #687 before convert your model.

  1. git clone https://github.com/onnx/onnx-tensorflow.git
  2. cd onnx-tensorflow
  3. git fetch origin +refs/pull/687/merge:
  4. git checkout -qf FETCH_HEAD
  5. pip install -e .
sayakpaul commented 4 years ago

@winnietsang tried this, thank you. It now results into TypeError: can't multiply sequence by non-int of type 'Tensor'.

Here's the Colab Notebook to reproduce this issue.

winnietsang commented 4 years ago

@sayakpaul PR #687 should fix the TypeError you show above.

amandazw commented 3 years ago

2020-09-29 14:37:06,027 - onnx-tf - INFO - Start converting onnx pb to tf pb: WARNING:tensorflow:From c:\users\88000024\onnx-tensorflow\onnx_tf\backend.py:124: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

2020-09-29 14:37:07.160249: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2020-09-29 14:37:07.170141: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found 2020-09-29 14:37:07.174680: E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit: UNKNOWN ERROR (303) 2020-09-29 14:37:07.188364: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: NS88000024 2020-09-29 14:37:07.195368: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: NS88000024 WARNING:tensorflow:From c:\users\88000024\onnx-tensorflow\onnx_tf\handlers\backend\reshape.py:26: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.where in 2.0, which has the same broadcast rule as np.where WARNING:tensorflow:From c:\users\88000024\onnx-tensorflow\onnx_tf\handlers\backend\reshape.py:31: sparse_to_dense (from tensorflow.python.ops.sparse_ops) is deprecated and will be removed in a future version. Instructions for updating: Create a tf.sparse.SparseTensor and use tf.sparse.to_dense instead. WARNING:tensorflow:From c:\users\88000024\onnx-tensorflow\onnx_tf\handlers\backend\gemm.py:14: flatten (from tensorflow.python.layers.core) is deprecated and will be removed in a future version. Instructions for updating: Use keras.layers.flatten instead. WARNING:tensorflow:From D:\setup\Anaconda3\envs\test_py361\lib\site-packages\tensorflow_core\python\layers\core.py:332: Layer.apply (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version. Instructions for updating: Please use layer.__call__ method instead. WARNING:tensorflow:From c:\users\88000024\onnx-tensorflow\onnx_tf\handlers\backend\dilated_pooling.py:640: The name tf.nn.max_pool_v2 is deprecated. Please use tf.nn.max_pool instead.

Traceback (most recent call last): File "D:\setup\Anaconda3\envs\test_py361\lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 541, in make_tensor_proto str_values = [compat.as_bytes(x) for x in proto_values] File "D:\setup\Anaconda3\envs\test_py361\lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 541, in str_values = [compat.as_bytes(x) for x in proto_values] File "D:\setup\Anaconda3\envs\test_py361\lib\site-packages\tensorflow_core\python\util\compat.py", line 71, in as_bytes (bytes_or_text,)) TypeError: Expected binary or unicode string, got 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:\setup\Anaconda3\envs\test_py361\Scripts\onnx-tf-script.py", line 33, in sys.exit(load_entry_point('onnx-tf', 'console_scripts', 'onnx-tf')()) File "c:\users\88000024\onnx-tensorflow\onnx_tf\cli.py", line 20, in main return onnx_tf.converter.main(args[1:]) File "c:\users\88000024\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 "c:\users\88000024\onnx-tensorflow\onnx_tf\converter.py", line 136, in convert tf_rep = backend.prepare(onnx_model, kwargs) File "c:\users\88000024\onnx-tensorflow\onnx_tf\backend.py", line 66, in prepare return cls.onnx_model_to_tensorflow_rep(model, strict) File "c:\users\88000024\onnx-tensorflow\onnx_tf\backend.py", line 86, in onnx_model_to_tensorflow_rep return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict) File "c:\users\88000024\onnx-tensorflow\onnx_tf\backend.py", line 147, in _onnx_graph_to_tensorflow_rep strict=strict) File "c:\users\88000024\onnx-tensorflow\onnx_tf\backend.py", line 253, in _onnx_node_to_tensorflow_op return handler.handle(node, tensor_dict=tensor_dict, strict=strict) File "c:\users\88000024\onnx-tensorflow\onnx_tf\handlers\handler.py", line 60, in handle return ver_handle(node, **kwargs) File "c:\users\88000024\onnx-tensorflow\onnx_tf\handlers\backend\slice.py", line 47, in version_1 tf.constant(full_sizes) File "D:\setup\Anaconda3\envs\test_py361\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 161, in constant_v1 allow_broadcast=False) File "D:\setup\Anaconda3\envs\test_py361\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 265, in _constant_impl allow_broadcast=allow_broadcast)) File "D:\setup\Anaconda3\envs\test_py361\lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 545, in make_tensor_proto "supported type." % (type(values), values)) TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [1, None, 48]. Consider casting elements to a supported type.

same problem. how to fix it?please here is my test model: https://github.com/ouyanghuiyu/chineseocr_lite/blob/onnx/models/crnn_lite_lstm.onnx

chinhuang007 commented 3 years ago

@amandazw the dynamic shape for the early version of slice handler is fixed in the PR, https://github.com/onnx/onnx-tensorflow/pull/776. Feel free to check it out.