tensorflow-onnx
tensorflow-onnx copied to clipboard
Problem converting Tensorflow Hub model to Onnx
Describe the bug
Conversion of TensorFlow model (TF-Hub) to ONNX failed Urgency
a project deployment deadline 7th April 2023
System information
- OS Platform and Distribution (e.g., Linux Ubuntu 18.04*): Google Colab Environment Ubuntu 20.04 LTS
- TensorFlow Version: 2.11.1
- Python version: 3.9.16
- ONNX version (if applicable, e.g. 1.11*): 1.13.1
To Reproduce
Link to the model Code instructions to reproduce the behaviour on Google colab environment:
import tensorflow_hub as hub
import urllib.request
module_url = "https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3?tf-hub-format=compressed"
# Download the module
urllib.request.urlretrieve(module_url, 'tfmodel.tar')
!mkdir TfModel
#Extract the module
!tar xvf tfmodel.tar -C TfModel/
import onnx
import tensorflow as tf
import tf2onnx
model = tf.saved_model.load('TfModel')
# Get the concrete function of the model
concrete_func = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
# Set the input and output tensors
input_tensor_names = [t.name for t in concrete_func.inputs]
output_tensor_names = [t.name for t in concrete_func.outputs]
onnx_model = onnx.load(tf2onnx.tfonnx.process_tf_graph(concrete_func.graph, input_names=input_tensor_names, output_names=output_tensor_names))
# Save the ONNX model
onnx.save(onnx_model, 'model.onnx')
Screenshots
Error Output:
ERROR:tf2onnx.tfonnx:Failed to convert node 'EncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup' (fct=<bound method DynamicStitch.version_13 of <class 'tf2onnx.onnx_opset.tensor.DynamicStitch'>>)
'OP=ParallelDynamicStitch\nName=EncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup\nInputs:\n\tCompress__5730:0=Compress, [-1], 6\n\tCompress__5738:0=Compress, [-1], 6\n\tCompress__5746:0=Compress, [-1], 6\n\tCompress__5754:0=Compress, [-1], 6\n\tCompress__5762:0=Compress, [-1], 6\n\tCompress__5770:0=Compress, [-1], 6\n\tCompress__5778:0=Compress, [-1], 6\n\tCompress__5786:0=Compress, [-1], 6\n\tCompress__5794:0=Compress, [-1], 6\n\tCompress__5802:0=Compress, [-1], 6\n\tCompress__5810:0=Compress, [-1], 6\n\tCompress__5818:0=Compress, [-1], 6\n\tCompress__5826:0=Compress, [-1], 6\n\tCompress__5834:0=Compress, [-1], 6\n\tCompress__5842:0=Compress, [-1], 6\n\tCompress__5850:0=Compress, [-1], 6\n\tCompress__5858:0=Compress, [-1], 6\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_1:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_2:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_3:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_4:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_5:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_6:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_7:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_8:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_9:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_10:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_11:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_12:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_13:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_14:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_15:0=Identity, None, 1\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Identity_16:0=Identity, None, 1\nOutpus:\n\tEncoderTransformer/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup:0=None, 1'
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/tf2onnx/tfonnx.py", line 292, in tensorflow_onnx_mapping
func(g, node, **kwargs, initialized_tables=initialized_tables, dequantize=dequantize)
File "/usr/local/lib/python3.9/dist-packages/tf2onnx/onnx_opset/tensor.py", line 3114, in version_13
cls.any_version(13, ctx, node, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/tf2onnx/onnx_opset/tensor.py", line 3079, in any_version
utils.make_sure(all(s is not None for s in data_shapes), "DynamicStitch requires data tensors of known rank")
File "/usr/local/lib/python3.9/dist-packages/tf2onnx/utils.py", line 264, in make_sure
raise ValueError("make_sure failure: " + error_msg % args)
ValueError: make_sure failure: DynamicStitch requires data tensors of known rank
ERROR:tf2onnx.tfonnx:Unsupported ops: Counter({'ReadVariableOp': 1598, 'ResourceGather': 17, 'SentencepieceOp': 1, 'SentencepieceTokenizeOp': 1})
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[<ipython-input-37-142bebc55e2f>](https://ff1on6ph466-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20230323-060210-RC02_518914852#) in <cell line: 12>()
10 output_tensor_names = [t.name for t in concrete_func.outputs]
11
---> 12 onnx_model = onnx.load(tf2onnx.tfonnx.process_tf_graph(concrete_func.graph, input_names=input_tensor_names, output_names=output_tensor_names))
13
14
6 frames
[/usr/local/lib/python3.9/dist-packages/tf2onnx/utils.py](https://ff1on6ph466-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20230323-060210-RC02_518914852#) in make_sure(bool_val, error_msg, *args)
262 def make_sure(bool_val, error_msg, *args):
263 if not bool_val:
--> 264 raise ValueError("make_sure failure: " + error_msg % args)
265
266 def combine_seeds(seed, seed2):
ValueError: make_sure failure: DynamicStitch requires data tensors of known rank
Additional context
import tensorflow as tf
import tensorflow_hub as hub
import cv2
from matplotlib import pyplot as plt
import numpy as np
model = hub.load("https://tfhub.dev/google/movenet/singlepose/lightning/4")
# Get the concrete function of the model
concrete_func = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
# Set the input and output tensors
input_tensor_names = [t.name for t in concrete_func.inputs]
output_tensor_names = [t.name for t in concrete_func.outputs]
onnx_model = onnx.load(tf2onnx.tfonnx.process_tf_graph(concrete_func.graph, input_names=input_tensor_names, output_names=output_tensor_names))
# Save the ONNX model
onnx.save(onnx_model, 'model.onnx')
There are also problems with this network conversion!
TypeError: object of type ‘NoneType’ has no len()