Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Wrong wire type in tag' error when trying to convert .pb to ONNX #1038

Closed
dfvr1994 opened this issue Aug 4, 2020 · 52 comments
Closed

'Wrong wire type in tag' error when trying to convert .pb to ONNX #1038

dfvr1994 opened this issue Aug 4, 2020 · 52 comments
Labels
pending on user response Waiting for more information or validation from user

Comments

@dfvr1994
Copy link

dfvr1994 commented Aug 4, 2020

Describe the bug
I'm using the following command to convert the frozen pb model to ONNX with no success

python -m tf2onnx.convert --graphdef saved_model.pb --output frozen.onnx --fold_const --opset 10 --inputs image_tensor:0 --outputs num_detections:0,detection_boxes:0,detection_scores:0,detection_classes:0

System information

  • Windows 10
  • Tensorflow Version: 2.3.0
  • Python version: 3.8

Model attached

saved_model.zip

I do not have the chance to obtain the model in the saved model format. I created it using Google's AutoML for object detection, which lets me output the model in .tflite (including dict.txt and json metadata), .pb (alone), and tensorflow.js (3 bins, dict.txt and model.json) formats.

Error

2020-08-04 01:15:11.683354: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-08-04 01:15:14.659143: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-08-04 01:15:14.663804: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2020-08-04 01:15:14.677143: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-T1GI7LL
2020-08-04 01:15:14.681924: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-T1GI7LL
2020-08-04 01:15:14.700290: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x229959341f0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-04 01:15:14.707935: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\convert.py", line 171, in <module>
    main()
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\convert.py", line 125, in main
    graph_def, inputs, outputs = tf_loader.from_graphdef(args.graphdef, args.inputs, args.outputs)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\tf_loader.py", line 147, in from_graphdef
    graph_def.ParseFromString(f.read())
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\message.py", line 199, in ParseFromString
    return self.MergeFromString(serialized)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\python_message.py", line 1134, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\python_message.py", line 1201, in InternalParse
    pos = field_decoder(buffer, new_pos, end, self, field_dict)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\decoder.py", line 738, in DecodeField
    if value._InternalParse(buffer, pos, new_pos) != new_pos:
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\python_message.py", line 1201, in InternalParse
    pos = field_decoder(buffer, new_pos, end, self, field_dict)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\decoder.py", line 717, in DecodeRepeatedField
    if value.add()._InternalParse(buffer, pos, new_pos) != new_pos:
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\python_message.py", line 1201, in InternalParse
    pos = field_decoder(buffer, new_pos, end, self, field_dict)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\decoder.py", line 872, in DecodeMap
    if submsg._InternalParse(buffer, pos, new_pos) != new_pos:
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\python_message.py", line 1187, in InternalParse
    (data, new_pos) = decoder._DecodeUnknownField(
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\decoder.py", line 973, in _DecodeUnknownField
    (data, pos) = _DecodeUnknownFieldSet(buffer, pos)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\decoder.py", line 952, in _DecodeUnknownFieldSet
    (data, pos) = _DecodeUnknownField(buffer, pos, wire_type)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\google\protobuf\internal\decoder.py", line 977, in _DecodeUnknownField
    raise _DecodeError('Wrong wire type in tag.')
google.protobuf.message.DecodeError: Wrong wire type in tag.
@guschmue
Copy link
Collaborator

guschmue commented Aug 4, 2020

This looks like a saved-model format. So you'd do you can:

python -m tf2onnx.convert --saved-model THE_DIR_THE_PB_IS_IN --output saved-model.onnx --opset 10

That loads but we throw some error while processing a loop:

  File "c:\src\tensorflow-onnx\tf2onnx\rewriter\rnn.py", line 50, in rewrite_generic_loop
    return LoopRewriter(g).run()
  File "c:\src\tensorflow-onnx\tf2onnx\rewriter\loop_rewriter.py", line 35, in run
    return self.run_internal()
  File "c:\src\tensorflow-onnx\tf2onnx\rewriter\loop_rewriter_base.py", line 207, in run_internal
    self._check_in_read_only_mode(context)
  File "c:\src\tensorflow-onnx\tf2onnx\rewriter\loop_rewriter_base.py", line 230, in _check_in_read_only_mode
    self._parse_loop_variables(context)
  File "c:\src\tensorflow-onnx\tf2onnx\rewriter\loop_rewriter_base.py", line 244, in _parse_loop_variables
    loop_var = self._get_loop_var_from_switch(s)
  File "c:\src\tensorflow-onnx\tf2onnx\rewriter\loop_rewriter_base.py", line 366, in _get_loop_var_from_switch
    enter_node = [n for n in merge_node.inputs if n.type == 'Enter'][0]
  File "c:\src\tensorflow-onnx\tf2onnx\rewriter\loop_rewriter_base.py", line 366, in <listcomp>
    enter_node = [n for n in merge_node.inputs if n.type == 'Enter'][0]
AttributeError: 'NoneType' object has no attribute 'type'

We'll take a look.

@TomWildenhain-Microsoft TomWildenhain-Microsoft added the bug An unexpected problem or unintended behavior label Aug 4, 2020
@dfvr1994
Copy link
Author

dfvr1994 commented Aug 5, 2020

Hi @guschmue ,

I couldn't reproduce your error.

I tried this code python -m tf2onnx.convert --saved-model "path of folder containing the .pb model" --output saved-model.onnx --opset 10 under on 2 separate environments:

Python 3.8.5, tensorflow Version: 2.3.0 - Error: 'ValueError: Failed to import metagraph, check error log for more info..'

2020-08-04 23:50:08.385766: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-08-04 23:50:08.395851: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-08-04 23:50:11,519 - WARNING - '--tag' not specified for saved_model. Using --tag serve
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
2020-08-04 23:50:17,707 - INFO - Saver not created because there are no variables in the graph to restore
2020-08-04 23:50:19.483501: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-08-04 23:50:19.488592: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2020-08-04 23:50:19.498929: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-T1GI7LL
2020-08-04 23:50:19.503647: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-T1GI7LL
2020-08-04 23:50:19.517505: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1ce5aab9990 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-04 23:50:19.523375: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-08-04 23:50:25,948 - INFO - Signatures found in model: [serving_default].
2020-08-04 23:50:25,949 - WARNING - '--signature_def' not specified, using first signature: serving_default
2020-08-04 23:50:27.435117: I tensorflow/core/grappler/devices.cc:69] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2020-08-04 23:50:27.439747: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2020-08-04 23:50:28.413639: E tensorflow/core/grappler/grappler_item_builder.cc:669] Init node index_to_string/table_init/LookupTableImportV2 doesn't exist in graph
Traceback (most recent call last):
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\convert.py", line 171, in <module>
    main()
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\convert.py", line 131, in main
    graph_def, inputs, outputs = tf_loader.from_saved_model(
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\tf_loader.py", line 288, in from_saved_model
    _from_saved_model_v2(model_path, input_names, output_names, tag, signatures, concrete_function)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\tf_loader.py", line 277, in _from_saved_model_v2
    frozen_graph = from_function(concrete_func, inputs, outputs)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\tf_loader.py", line 100, in from_function
    frozen_func = convert_variables_to_constants_v2(func, lower_control_flow=False)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 1066, in convert_variables_to_constants_v2
    converter_data = _FunctionConverterData(
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 798, in __init__
    graph_def = _run_inline_graph_optimization(func, lower_control_flow,
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 970, in _run_inline_graph_optimization
    return tf_optimizer.OptimizeGraph(config, meta_graph)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\grappler\tf_optimizer.py", line 55, in OptimizeGraph
    out_graph = tf_opt.TF_OptimizeGraph(cluster.tf_cluster,
ValueError: Failed to import metagraph, check error log for more info..

2nd scenario: Python 3.7.7, tensorflow version: 1.13.2 - Error: 'KeyError: 'FusedBatchNormV3'

C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
Traceback (most recent call last):
  File "C:\Anaconda_pyhton\envs\myenv\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Anaconda_pyhton\envs\myenv\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tf2onnx\convert.py", line 161, in <module>
    main()
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tf2onnx\convert.py", line 123, in main
    args.saved_model, args.inputs, args.outputs, args.signature_def)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tf2onnx\loader.py", line 104, in from_saved_model
    meta_graph_def = tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], model_path)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 269, in load
    return loader.load(sess, tags, import_scope, **saver_kwargs)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 420, in load
    **saver_kwargs)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 350, in load_graph
    meta_graph_def, import_scope=import_scope, **saver_kwargs)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\training\saver.py", line 1457, in _import_meta_graph_with_return_elements
    **kwargs))
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\meta_graph.py", line 806, in import_scoped_meta_graph_with_return_elements
    return_elements=return_elements)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\importer.py", line 399, in import_graph_def
    _RemoveDefaultAttrs(op_dict, producer_op_list, graph_def)
  File "C:\Anaconda_pyhton\envs\myenv\lib\site-packages\tensorflow\python\framework\importer.py", line 159, in _RemoveDefaultAttrs
    op_def = op_dict[node.op]
KeyError: 'FusedBatchNormV3'

I guess Google Vision output the model using TF last version (2.3 to this day)

@dfvr1994
Copy link
Author

dfvr1994 commented Aug 5, 2020

Netron displays:

MODEL:
image

INPUT:
image

OUTPUT:
image

@guschmue
Copy link
Collaborator

guschmue commented Aug 5, 2020

FusedBatchNormV3 came in after tf-1.13 and it can't load this model. tf-1.14 or tf-1.15 should work.
I was able to load your model on tf-2.3 with:

mkdir saved-model
cp your_pb into saved-model
python -m tf2onnx.convert --saved-model saved-model ...

but hit this issue
enter_node = [n for n in merge_node.inputs if n.type == 'Enter'][0]
which I think is a tf2onnx issue.

@dfvr1994
Copy link
Author

dfvr1994 commented Aug 5, 2020

I'm getting a different error, am I missing a library?

Init node index_to_string/table_init/LookupTableImportV2 doesn't exist in graph

c:\test>python -m tf2onnx.convert --saved-model saved-model --output saved-model.onnx --opset 10
2020-08-05 09:34:13.927585: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-08-05 09:34:13.933056: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-08-05 09:34:16,710 - WARNING - '--tag' not specified for saved_model. Using --tag serve
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
2020-08-05 09:34:22,879 - INFO - Saver not created because there are no variables in the graph to restore
2020-08-05 09:34:24.713262: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-08-05 09:34:24.718259: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2020-08-05 09:34:24.728378: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-T1GI7LL
2020-08-05 09:34:24.733007: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-T1GI7LL
2020-08-05 09:34:24.748327: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x248f4c3da00 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-05 09:34:24.755013: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-08-05 09:34:31,265 - INFO - Signatures found in model: [serving_default].
2020-08-05 09:34:31,265 - WARNING - '--signature_def' not specified, using first signature: serving_default
2020-08-05 09:34:32.810442: I tensorflow/core/grappler/devices.cc:69] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2020-08-05 09:34:32.815771: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2020-08-05 09:34:33.954703: E tensorflow/core/grappler/grappler_item_builder.cc:669] Init node index_to_string/table_init/LookupTableImportV2 doesn't exist in graph
Traceback (most recent call last):
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\convert.py", line 171, in <module>
    main()
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\convert.py", line 131, in main
    graph_def, inputs, outputs = tf_loader.from_saved_model(
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\tf_loader.py", line 288, in from_saved_model
    _from_saved_model_v2(model_path, input_names, output_names, tag, signatures, concrete_function)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\tf_loader.py", line 277, in _from_saved_model_v2
    frozen_graph = from_function(concrete_func, inputs, outputs)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tf2onnx\tf_loader.py", line 100, in from_function
    frozen_func = convert_variables_to_constants_v2(func, lower_control_flow=False)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 1066, in convert_variables_to_constants_v2
    converter_data = _FunctionConverterData(
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 798, in __init__
    graph_def = _run_inline_graph_optimization(func, lower_control_flow,
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 970, in _run_inline_graph_optimization
    return tf_optimizer.OptimizeGraph(config, meta_graph)
  File "C:\Users\VIZCHINO PC 2.0\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\grappler\tf_optimizer.py", line 55, in OptimizeGraph
    out_graph = tf_opt.TF_OptimizeGraph(cluster.tf_cluster,
ValueError: Failed to import metagraph, check error log for more info.

folder structure:
image

@guschmue
Copy link
Collaborator

guschmue commented Aug 5, 2020

I had a cpu build installed. If I use a gpu I see the same error that you see. Odd

@dfvr1994
Copy link
Author

dfvr1994 commented Aug 5, 2020

Do you mean you a GPU build installed? I do not have a GPU...

@guschmue
Copy link
Collaborator

guschmue commented Aug 5, 2020

pip install tensorflow has gpu support and shows your error. pip install tensorflow-cpu does not and you'll get the error I see. grappler seems to be impacted by this. We turn on some logging for grappler to see what it is complaining about.

@dfvr1994
Copy link
Author

dfvr1994 commented Aug 5, 2020

I see...how can we solve this? Any work-around?

@TomWildenhain-Microsoft TomWildenhain-Microsoft removed the bug An unexpected problem or unintended behavior label Aug 6, 2020
@dfvr1994
Copy link
Author

dfvr1994 commented Aug 8, 2020

Hi, any update on this?

@csl-8bit
Copy link

I am facing completely same problem too.
Same environment, same situation. Any update on this?

@TomWildenhain-Microsoft
Copy link
Contributor

@csl-8bit looking at this now. Which of the above errors are you currently getting?

@TomWildenhain-Microsoft
Copy link
Contributor

@csl-8bit can you upload a model you are trying to convert? I'm unable to open the saved model at the top of this issue.

@csl-8bit
Copy link

csl-8bit commented Nov 11, 2020

I trained the object detection model using Google AutoML.
saved_model.zip

System information:
Google Colab - no gpu
tensorflow 2.3.0
python 3.6.9
onnx 1.8.0
tf2onnx 1.7.2

I tried this code !python -m tf2onnx.convert --opset 10 --fold_const --saved-model saved_model --output converted_model.onnx and got the following error.

2020-11-11 19:45:19,065 - WARNING - '--tag' not specified for saved_model. Using --tag serve
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
2020-11-11 19:45:19,883 - INFO - Saver not created because there are no variables in the graph to restore
2020-11-11 19:45:19.991123: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1
2020-11-11 19:45:20.000308: E tensorflow/stream_executor/cuda/cuda_driver.cc:314] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2020-11-11 19:45:20.000369: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (3aa02ba2880a): /proc/driver/nvidia/version does not exist
2020-11-11 19:45:20.006240: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 2300000000 Hz
2020-11-11 19:45:20.006539: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x9adc1c0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-11 19:45:20.006580: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-11-11 19:45:22,408 - INFO - Signatures found in model: [serving_default].
2020-11-11 19:45:22,408 - WARNING - '--signature_def' not specified, using first signature: serving_default
2020-11-11 19:45:22.485563: I tensorflow/core/grappler/devices.cc:69] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2020-11-11 19:45:22.485809: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2020-11-11 19:45:22.541521: E tensorflow/core/grappler/grappler_item_builder.cc:669] Init node index_to_string/table_init/LookupTableImportV2 doesn't exist in graph
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/root/.local/lib/python3.6/site-packages/tf2onnx/convert.py", line 188, in <module>
    main()
  File "/root/.local/lib/python3.6/site-packages/tf2onnx/convert.py", line 137, in main
    args.signature_def, args.concrete_function, args.large_model)
  File "/root/.local/lib/python3.6/site-packages/tf2onnx/tf_loader.py", line 333, in from_saved_model
    _from_saved_model_v2(model_path, input_names, output_names, tag, signatures, concrete_function, large_model)
  File "/root/.local/lib/python3.6/site-packages/tf2onnx/tf_loader.py", line 320, in _from_saved_model_v2
    raise e
  File "/root/.local/lib/python3.6/site-packages/tf2onnx/tf_loader.py", line 316, in _from_saved_model_v2
    frozen_graph = from_function(concrete_func, inputs, outputs, large_model)
  File "/root/.local/lib/python3.6/site-packages/tf2onnx/tf_loader.py", line 127, in from_function
    frozen_func = convert_variables_to_constants_v2(func, lower_control_flow=False, aggressive_inlining=True)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/convert_to_constants.py", line 1069, in convert_variables_to_constants_v2
    aggressive_inlining=aggressive_inlining)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/convert_to_constants.py", line 799, in __init__
    aggressive_inlining)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/convert_to_constants.py", line 970, in _run_inline_graph_optimization
    return tf_optimizer.OptimizeGraph(config, meta_graph)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/grappler/tf_optimizer.py", line 58, in OptimizeGraph
    graph_id, strip_default_attributes)
ValueError: Failed to import metagraph, check error log for more info.

@TomWildenhain-Microsoft
Copy link
Contributor

Interesting. This definitely is in saved model format but it has no meta graph. Also it uses the DecodeJpeg op which we don't support yet.

@TomWildenhain-Microsoft
Copy link
Contributor

@csl-8bit Can you upload the tflite version as well?

@guschmue
Copy link
Collaborator

I think the name is saved_model but its actually a graphdef ... use --input instead of --saved-model.

@TomWildenhain-Microsoft
Copy link
Contributor

I thought that too at first but it is actually a saved model according to Netron.

@guschmue
Copy link
Collaborator

yes, I get a decode error. wired.

@csl-8bit
Copy link

csl-8bit commented Nov 12, 2020

I have uploaded both tflite and tfjs version
automl tflite model.zip
automl tfjsmodel.zip
image

@dfvr1994
Copy link
Author

dfvr1994 commented Dec 8, 2020

Interesting. This definitely is in saved model format but it has no meta graph. Also it uses the DecodeJpeg op which we don't support yet.

Hi @TomWildenhain-Microsoft , any upgrade on DecodeJpeg op yet?

@TomWildenhain-Microsoft
Copy link
Contributor

Yes, while DecodeJpeg isn't supported, the tflite version of the model accepts a tensor of pixel values directly and looks easy to convert. We plan on adding support for converting tflite models next semester. When we do, your model should be able to be converted.

@guschmue
Copy link
Collaborator

guschmue commented Dec 9, 2020

The way you can work around it is to pass in an explicit input that goes behind the DecodeJpeg.

@TomWildenhain-Microsoft
Copy link
Contributor

@guschmue Do we have the code that does that for saved models merged?

@guschmue
Copy link
Collaborator

guschmue commented Dec 9, 2020

I can send a PR today.

@dfvr1994
Copy link
Author

dfvr1994 commented Dec 9, 2020

@guschmue thanks, can you please post here when the PR processes successfully? I really want to be able to use the workaround you mentioned.

@theumang100
Copy link

I can send a PR today.

hey bro, have you got anything?!!

@guschmue
Copy link
Collaborator

I had spoken to early - to override of inputs creates some issue. Thinking how to get around this.

@TomWildenhain-Microsoft
Copy link
Contributor

You should now be able to convert the tflite model using the --tflite arg. You will need opset 13 and the latest ORT nightly build.
https://test.pypi.org/project/ort-nightly/

@dfvr1994
Copy link
Author

dfvr1994 commented Feb 3, 2021

Thanks @TomWildenhain-Microsoft . I'm getting an error trying to run this:

python -m tf2onnx.convert --opset 13 --tflite --fold_const --saved-model saved_model --output converted_model.onnx

convert.py: error: unrecognized arguments: --tflite

Should I use another github branch?

For the saved-model argument should I use the folder containing the saved_model.pb file or should I use the file path? I guess I would have to use the model.tflite file path, am I right? Could you please post the code to be used?

Thanks

@TomWildenhain-Microsoft
Copy link
Contributor

Use --tflite instead of --saved-model.

python -m tf2onnx.convert --opset 13 --tflite "path/to/model.tflite" --output "converted.onnx"

This feature is beta so let me know if it works.

@dfvr1994
Copy link
Author

dfvr1994 commented Feb 3, 2021

I'm still getting the error:

convert.py: error: unrecognized arguments: --tflite path/to/model.tflite

What can I do?

@theumang100
Copy link

theumang100 commented Feb 3, 2021

I'm still getting the error:

convert.py: error: unrecognized arguments: --tflite path/to/model.tflite

What can I do?

Trying of another tensorflow version might be useful to you. It's work in my case i've used tensorflow 2.5.0.

@dfvr1994
Copy link
Author

dfvr1994 commented Feb 3, 2021

@theumang100 how did you install it? I'm trying this:

pip install tensorflow==2.5.0 ERROR: Could not find a version that satisfies the requirement tensorflow==2.5.0 ERROR: No matching distribution found for tensorflow==2.5.0

@theumang100
Copy link

@theumang100 how did you install it? I'm trying this:

pip install tensorflow==2.5.0 ERROR: Could not find a version that satisfies the requirement tensorflow==2.5.0 ERROR: No matching distribution found for tensorflow==2.5.0

try pip install tf-nightly==2.5.0.dev20210203 or pip install tensorflow==2.4.1

@guschmue
Copy link
Collaborator

guschmue commented Feb 3, 2021

You wan the latest version of tf2onnx I think:
pip install -U tf2onnx

@dfvr1994
Copy link
Author

dfvr1994 commented Feb 3, 2021

Thanks, uninstalling tensorflow and running pip install tf-nightly==2.5.0.dev20210203 and pip install -U tf2onnx did the trick.

Does anyone know how to use this model on live video to recognize objects? I would like to send the object's coordinates to a microcontroller using bluetooth or serial port.

@TomWildenhain-Microsoft
Copy link
Contributor

To use the model on video, you should be able to:

  1. Capture a frame of the video
  2. Resize the frame to the dimensions the network expects
  3. Convert the frame to a tensor
  4. Put frame into ORT
  5. The first output of the model is a list of rectangles. The second is the classes of the objects. The third is the confidence. The fourth is the number of objects detected.

@TomWildenhain-Microsoft
Copy link
Contributor

tf2onnx now also supports "graph cutting". If you specify inputs that are nodes in the middle of the graph, tf2onnx will promote them to inputs and remove the nodes before them.

@dfvr1994
Copy link
Author

@TomWildenhain-Microsoft can you please provide an example?

@TomWildenhain-Microsoft
Copy link
Contributor

python -m tf2onnx.convert --input path/to/saved/model/dir --inputs IntermediateNodeOutput:0,OtherNode:0 --outputs SomeOutput:0 --output model.onnx

@TomWildenhain-Microsoft
Copy link
Contributor

You will need to install the latest tf2onnx from master.
pip uninstall tf2onnx
pip install git+https://github.com/onnx/tensorflow-onnx

@TomWildenhain-Microsoft
Copy link
Contributor

@dfvr1994 did this work for you?

@TomWildenhain-Microsoft TomWildenhain-Microsoft added the pending on user response Waiting for more information or validation from user label Feb 26, 2021
@dfvr1994
Copy link
Author

@TomWildenhain-Microsoft , can I specify the output opset? Matlab is not able to read opset 13

@TomWildenhain-Microsoft
Copy link
Contributor

Sure, use --opset #. For lower opsets, the model may need to be dequantized with --dequantize.

@dfvr1994
Copy link
Author

dfvr1994 commented Mar 4, 2021

@TomWildenhain-Microsoft thank you so much, dequatizing the model and setting opset to 9 seems to work.

@dfvr1994
Copy link
Author

dfvr1994 commented Mar 4, 2021

Quick question, Matlab doesn't seem to like certain layers. At the moment of converting the model can I set which layers to exclude or be replaced with others?

@tnaduc
Copy link

tnaduc commented Mar 26, 2021

Hello, does anyone figure out how to convert saved model (.pb - AutoML version) to onnx yet? I still run into this issue

ValueError: Failed to import metagraph, check error log for more info.

@TomWildenhain-Microsoft
Copy link
Contributor

@tnaduc are you able to get a tflite version of the model?

@tnaduc
Copy link

tnaduc commented Apr 12, 2021

@TomWildenhain-Microsoft No, I haven't tried tflite, I only tried the savedmodel (.pb file).

@TomWildenhain-Microsoft
Copy link
Contributor

@tnaduc If you have access to a tflite version of the model, I would recommend trying it. Let me know if it doesn't work.

@tnaduc
Copy link

tnaduc commented Apr 13, 2021

@TomWildenhain-Microsoft thanks Tom, I will follow your recommendation and let you know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending on user response Waiting for more information or validation from user
Projects
None yet
Development

No branches or pull requests

6 participants