onnx / keras-onnx

Convert tf.keras/Keras models to ONNX
Apache License 2.0
381 stars 109 forks source link

Incorrect Tensor Type Conversion Issue #690

Open msquigle opened 3 years ago

msquigle commented 3 years ago

Issue: When trying to convert the attached model using the following script

import tensorflow as tf import onnx import keras2onnx keras_model = tf.keras.models.load_model(mobilenet_ssd.h5, compile=False) model_proto = keras2onnx.convert_keras(keras_model, target_opset=11) with open(outfile, "wb") as f: f.write(model_proto.SerializeToString())

the following error is received site-packages\keras2onnx_parser_tf.py", line 47, in infer_variable_type raise ValueError( ValueError: Unable to find out a correct type for tensor type = 20 of conv2d_6/Conv2D/ReadVariableOp/resource:0

The model is a standard mobilenet ssd and it seems that this type of error should not arise. mobilenet_ssd.zip

jiafatom commented 3 years ago

Can you try tensorflow 2.2.0 and keras2onnx from master branch?

msquigle commented 3 years ago

Using tensorflow 2.2.0 and the latest pypi release of keras2onnx, I get the error site-packages\tensorflow\python\keras\utils\generic_utils.py", line 321, in class_and_config_for_serialized_keras_object raise ValueError('Unknown ' + printable_module_name + ': ' + class_name) ValueError: Unknown layer: Functional

jagadishcoder commented 3 years ago

Issue: when I run the code below, onnx_model_name = 'classify.onnx'

model = load_model('path_to_folder/saved_models/final') onnx_model = keras2onnx.convert_keras(model, model.name) keras2onnx.save_model(onnx_model, os.path.join('saved_models', onnx_model_name))

Even I ran into the same error: Traceback (most recent call last): File "/snap/pycharm-professional/230/plugins/python/helpers/pydev/pydevd.py", line 1477, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/snap/pycharm-professional/230/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "correct_image_classification/test.py", line 38, in onnx_model = keras2onnx.convert_keras(model, model.name) File "correct_image_classification/venv/lib/python3.8/site-packages/keras2onnx/main.py", line 99, in convert_keras parse_graph(topology, tf_graph, target_opset, output_names, output_dict) File "correct_image_classification/venv/lib/python3.8/site-packages/keras2onnx/parser.py", line 905, in parse_graph return _parse_graph_core_v2( File "correct_image_classification/venv/lib/python3.8/site-packages/keras2onnx/parser.py", line 784, in _parse_graph_core_v2 _on_parsing_tf_nodes(graph, layer_info.nodelist, varset, topology.debug_mode) File "correct_image_classification/venv/lib/python3.8/site-packages/keras2onnx/parser.py", line 328, in _on_parsing_tf_nodes var_type = infer_variabletype(i, varset.target_opset) File "correct_image_classification/venv/lib/python3.8/site-packages/keras2onnx/_parser_tf.py", line 43, in infer_variable_type raise ValueError( ValueError: Unable to find out a correct type for tensor type = 20 of random_zoom/stateful_uniform/StatefulUniform/resource:0

I am using, tensorflow==2.3.0 tensorflow image classification problem

Madankumar90 commented 3 years ago

using tensorflow 2.3.1 and installing keras2onnx from source i was able to convert mobilenet keras model to onnx. But when i am running in open CV readnetfromonnx it is crashing. Did not happen with prior conversions. Earlier i was facing this issue ValueError: Unable to find out a correct type for tensor type = 20 of conv2d_6/Conv2D/ReadVariableOp/resource:0 but after installing keras2onnx it converted but not able to run the onnx. Any leads @jiafatom Thanks. Here's the attachment of my converted onnx. onnx.zip

Madankumar90 commented 3 years ago

Using tensorflow 2.2.0 and the latest pypi release of keras2onnx, I get the error site-packages\tensorflow\python\keras\utils\generic_utils.py", line 321, in class_and_config_for_serialized_keras_object raise ValueError('Unknown ' + printable_module_name + ': ' + class_name) ValueError: Unknown layer: Functional

@msquigle because you would have trained with new tf version and when u load model with old tf model it will throw this unknown layer error. Run with tf 2.3.1 version it should load the model. And install keras2onnx from source. For me these steps converted my keras model to onnx but not able to run onnx model now.

jiafatom commented 3 years ago

Seems that your model is converted. Now the issue can be in (1) the onnx model (2) in open CV readnetfromonnx To verify (1), can you try running your onnx model via onnxruntime? We have many tests in the repo, so you can find those examples.