Open hardik-ajmani opened 4 years ago
@hardik-ajmani I'm in nearly the same boat as you, trying to convert the recognizer to ONNX using both keras-onnx and tf2onnx.
My system info:
However, I was able to make your suggested changes to _meshgrid()
in the recognition model:
#x_linspace = tf.linspace(-1., 1., width) (removed these lines and used **np**)
#y_linspace = tf.linspace(-1., 1., height)
x_linspace = np.linspace(-1., 1., width, dtype='float16')
y_linspace = np.linspace(-1., 1., height, dtype='float16')
And then successfully instantiate the model:
import keras_ocr
recognizer = keras_ocr.recognition.Recognizer()
However, when trying to convert to ONNX with this code:
import keras2onnx
import keras_ocr
import onnxruntime as rt
recognizer = keras_ocr.recognition.Recognizer()
onnx_model = keras2onnx.convert_keras(recognizer.model, recognizer.model.name)
content = onnx_model.SerializeToString()
sess = rt.InferenceSession(content)
I get a dtype error when trying to start an onnx runtime session:
Traceback (most recent call last):
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 468, in _apply_op_helper
preferred_dtype=default_dtype)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1290, in convert_to_tensor
(dtype.name, value.dtype.name, value))
ValueError: Tensor conversion requested dtype float32 for Tensor with dtype float16: <tf.Tensor 'lambda_1/Reshape_5:0' shape=(None, 3, None) dtype=float16>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "recon_convert.py", line 15, in <module>
main()
File "recon_convert.py", line 7, in main
recognizer = keras_ocr.recognition.Recognizer()
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\keras_ocr\recognition.py", line 325, in __init__
alphabet=alphabet, **build_params)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\keras_ocr\recognition.py", line 254, in build_model
output_shape=stn_input_output_shape)([x, localization_net(x)])
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 773, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\keras\layers\core.py", line 846, in call
result = self.function(inputs, **kwargs)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\keras_ocr\recognition.py", line 95, in _transform
transformed_grid = tf.matmul(locnet_y, indices_grid)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\util\dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\ops\math_ops.py", line 2760, in matmul
a, b, adj_x=adjoint_a, adj_y=adjoint_b, name=name)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py", line 1526, in batch_mat_mul_v2
"BatchMatMulV2", x=x, y=y, adj_x=adj_x, adj_y=adj_y, name=name)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 504, in _apply_op_helper
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'BatchMatMulV2' Op has type float16 that does not match type float32 of argument 'x'.
If I go back into recognition.py
and set the dtypes to float32:
x_linspace = np.linspace(-1., 1., width, dtype='float32')
y_linspace = np.linspace(-1., 1., height, dtype='float32')
I'm still able to instantiate the recognizer and convert with keras2onnx. However, I get an invalid graph error:
Traceback (most recent call last):
File "recon_convert.py", line 15, in <module>
main()
File "recon_convert.py", line 11, in main
sess = rt.InferenceSession(content)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\onnxruntime\capi\session.py", line 158, in __init__
self._load_model(providers)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\onnxruntime\capi\session.py", line 177, in _load_model
self._sess.load_model(providers)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : This is an invalid model. Error in Node:lambda_1/AddN_add : Node (lambda_1/AddN_add) has input size 4 not in range [min=2, max=2].
I tried using the onnx model checker to debug, but it naturally crashes Python:
import onnx
onnx.checker.check_model(onnx_model)
But retrying using the onnx shape inference method gets past the error:
from onnx import shape_inference
...
inferred_model = shape_inference.infer_shapes(onnx_model)
content = inferred_model.SerializeToString()
sess = rt.InferenceSession(content)
But only to raise a new error:
Traceback (most recent call last):
File "recon_convert.py", line 21, in <module>
main()
File "recon_convert.py", line 17, in main
sess = rt.InferenceSession(content)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\onnxruntime\capi\session.py", line 158, in __init__
self._load_model(providers)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\onnxruntime\capi\session.py", line 177, in _load_model
self._sess.load_model(providers)
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Node:permute/Identity:0_cropping Output:permute/Identity:0_cropping0 [ShapeInferenceError] Can't merge shape info. Both source and target dimension have values but they differ. Source=31 Target=0 Dimension=2
Incidentally, this is the same error I get when using the tf2onnx python CLI (python -m tf2onnx.convert --saved-model saved_recognizer_model --opset 11 --output recognizer.onnx
) to convert the model to an onnx file, which is then loaded into the onnx runtime:
import onnx
import onnxruntime as rt
from onnx import helper, shape_inference
onnx_model_path = 'ocr/recognizer.onnx'
onnx_model = onnx.load(onnx_model_path)
inferred_model = shape_inference.infer_shapes(onnx_model)
content = inferred_model.SerializeToString()
sess = rt.InferenceSession(content)
And it's this same ShapeInferenceError
, but for a different node:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\onnxruntime\capi\session.py", line 158, in __init__
self._load_model(providers)
File "C:\Users\scottmcallister\anaconda3\envs\xact_model\lib\site-packages\onnxruntime\capi\session.py", line 177, in _load_model
self._sess.load_model(providers)
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Node:StatefulPartitionedCall/model_2/lambda/strided_slice Output:StatefulPartitionedCall/model_2/lambda/strided_slice:0 [ShapeInferenceError] Can't merge shape info. Both source and target dimension have values but they differ. Source=31 Target=0 Dimension=2
Any thoughts or have you made progress since creating this issue?
Hey @csmcallister ! Glad to have you on the same page.
I was using Google Colab till now, I think the aforementioned issue (initially raised by me) would have been due to that. Would try running it locally now.
Not much, only progress from my side is, that the same conversion would have to be done for text-detector model as well. As all the detected text in an image is fed into the recognition model.
Although the detection model converts without any issue, while parsing it throws the following issue
ERROR: builtin_op_importers.cpp:2501 In function importResize:
[8] Assertion failed: scales.is_weights() && "Resize scales must be an initializer!"
Please let me know if you get any updates on the conversion of either of the model. Cheers!
@csmcallister greetings!
A quick update: Using tf2onnx I was able to eliminate linspace
error, without modifying the code.
I converted the model into .pb file and then, used the following code to convert the model.
!python -m tf2onnx.convert --opset 11 --input /content/keras-ocr/keras_ocr/model.pb --inputs input_1:0 --outputs decode/PadV2:0 --output model_tf.onnx
But it throws the following output -
2020-07-27 03:50:57.666890: E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2020-07-27 03:50:58,966 - INFO - Using tensorflow=1.15.2, onnx=1.7.0, tf2onnx=1.6.2/8d5253
2020-07-27 03:50:58,967 - INFO - Using opset <onnx, 11>
2020-07-27 03:51:03,337 - ERROR - Tensorflow op [decode/CTCGreedyDecoder: CTCGreedyDecoder] is not supported
2020-07-27 03:51:03,337 - ERROR - Tensorflow op [decode/SparseToDense: SparseToDense] is not supported
2020-07-27 03:51:03,351 - ERROR - Unsupported ops: Counter({'CTCGreedyDecoder': 1, 'SparseToDense': 1})
2020-07-27 03:51:03,408 - INFO - Optimizing ONNX model
2020-07-27 03:51:08,136 - INFO - After optimization: Cast -31 (96->65), Concat -6 (20->14), Const -235 (372->137), Expand -6 (11->5), Identity -109 (114->5), Less -2 (9->7), Reshape +1 (16->17), Shape -9 (22->13), Slice -5 (21->16), Squeeze -9 (21->12), Sub -4 (12->8), Transpose -26 (45->19), Unsqueeze -33 (48->15)
2020-07-27 03:51:08,183 - INFO -
2020-07-27 03:51:08,183 - INFO - Successfully converted TensorFlow model /content/keras-ocr/keras_ocr/model.pb to ONNX
2020-07-27 03:51:08,225 - INFO - ONNX model is saved at model_tf.onnx
Please let me know if you have got any updates on the same or any suggestions in the workflow?
PS - You may view my notebook here https://colab.research.google.com/drive/1u2rzxD-wSqK2hGlHWPjJDi5TMtC4ysjN?usp=sharing
Glad to have found this thread. If converting to TensorRT compatible graph is the end goal then this article might be useful.
I am looking into converting the models (both the detection and the recognition) to .tflite
format.
Furthermore, I think if the issues are stemming from the ONNX conversion step it likely could be an issue of op versions. So, it might be better suited for the ONNX repo.
Hi,
I'm attempting to do something similar with my project. Have you had any successes on your end?
Hi guys With the help of @sayakpaul I succesfully converted both detection and recognition models to TFLite Format.
I am able to convert to onnx model using tf2onnx.convert.
import tensorflow as tf import keras_ocr
d=keras_ocr.detection.Detector(weights='clovaai_general') tf.saved_model.save(obj=d.model, export_dir='Data')
!pip install -U tf2onnx
!python -m tf2onnx.convert --saved-model 'Data' --output 'Data/craft_mlt_25k.onnx' --opset 11
The generated ONNX model is working good with onnxruntime
Hi, has anyone been able to run the ONNX model on Triton? I have loaded the model, but how do we configure the input and output? It apparently takes a 64 dimension input and gives a 97 dimension output
Here's what Triton is showing for the config
{
"name": "easyocr",
"versions": ["1"],
"platform": "onnxruntime_onnx",
"inputs": [
{
"name": "input1",
"datatype": "FP32",
"shape": [1, 1, 64, -1]
}
],
"outputs": [
{
"name": "output",
"datatype": "FP32",
"shape": [1, -1, 97]
}
]
}
Hey there,
Keras-OCR model gives an appreciable accuracy in most of the use cases, kudos for that!
But, we are trying to use pre-trained Keras-OCR on Jetson Nano employing NVIDIA Deepstream. And, the initial steps require the conversion of the model into ONNX representation. Ref - https://github.com/onnx/keras-onnx/tree/master/keras2onnx
Initially, the conversion was successful. But, while converting the model further into TensorRt gave two errors -
tf.linspace
But, now the model doesn't compile correctly on running this command:
r = recognition.Recognizer()
and throws these lines of error (complete traceback):
WARNING:tensorflow:From /tensorflow-
Request your help/guidance on this or any other alternative for converting the model for Jetson Nano. Thank you :)