tensorflow-onnx icon indicating copy to clipboard operation
tensorflow-onnx copied to clipboard

Convert error: Initialized table b'' for node LookupTableFindV2 not found

Open Agoniii opened this issue 3 years ago • 3 comments

Describe the bug I converted tensorflow model to ONNX format, but I got this error ValueError: make_sure failure: Initialized table b'' for node TempHashTable_Lookup/LookupTableFindV2 not found. Same error: https://github.com/onnx/tensorflow-onnx/issues/1485#issue-869633065

Urgency If there are particular important use cases blocked by this or strict project-related timelines, please share more information and dates. If there are no hard deadlines, please specify none. None

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 20.04
  • Tensorflow Version: 1.15.5
  • Python version: 3.8.10

To Reproduce Here is my code.

import tensorflow as tf
import tf2onnx

with tf.Session() as sess:
    #init = tf.compat.v1.lookup.KeyValueTensorInitializer(['a', 'b'], [1, 2])
    keys_tensor = tf.constant([1, 2], name="input_keys")
    vals_tensor = tf.constant(["a", "b"], name="input_vals")
    init = tf.contrib.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor)
    table = tf.contrib.lookup.HashTable(init, default_value="z", name="TempHashTable")
    result = table.lookup(tf.constant([1, 5]))
    table.init.run()
    result = tf.identity(result, name="output")

    print(sess.run(result))

    # write pb model
    frozen_graph_def = tf.graph_util.convert_variables_to_constants(
       sess,
       sess.graph_def,
       ["output"])
    sess.close()
    tf.train.write_graph(frozen_graph_def, '', "model.pb", as_text=False)

    # write onnx model
    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=["input_keys:0", "input_vals:0"], output_names=["output:0"], opset=13)
    model_proto = onnx_graph.make_model("test")
    with open("model.onnx", "wb") as f:
        f.write(model_proto.SerializeToString())

Screenshots If applicable, add screenshots to help explain your problem. The error is

Failed to convert node 'TempHashTable_Lookup/LookupTableFindV2' (fct=<bound method LookupTableFind.version_8 of <class 'tf2onnx.custom_opsets.onnx_ml.LookupTableFind'>>)
'OP=LookupTableFindV2\nName=TempHashTable_Lookup/LookupTableFindV2\nInputs:\n\tTempHashTable:0=HashTableV2, [], 7\n\tConst_1:0=Const, [2], 6\n\tConst:0=Const, [], 8\nOutpus:\n\tTempHashTable_Lookup/LookupTableFindV2:0=[2], 8'
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/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.8/dist-packages/tf2onnx/custom_opsets/onnx_ml.py", line 35, in version_8
    utils.make_sure(shared_name in initialized_tables, "Initialized table %s for node %s not found.",
  File "/usr/local/lib/python3.8/dist-packages/tf2onnx/utils.py", line 264, in make_sure
    raise ValueError("make_sure failure: " + error_msg % args)
ValueError: make_sure failure: Initialized table b'' for node TempHashTable_Lookup/LookupTableFindV2 not found.
Traceback (most recent call last):
  File "hashtable_test.py", line 26, in <module>
    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=["input_keys:0", "input_vals:0"], output_names=["output:0"], opset=13)
  File "/usr/local/lib/python3.8/dist-packages/tf2onnx/tfonnx.py", line 438, in process_tf_graph
    g = process_graphs(main_g, subgraphs, custom_op_handlers, inputs_as_nchw, continue_on_error, custom_rewriter,
  File "/usr/local/lib/python3.8/dist-packages/tf2onnx/tfonnx.py", line 490, in process_graphs
    g = process_parsed_graph(main_g, custom_op_handlers, inputs_as_nchw, continue_on_error, custom_rewriter,
  File "/usr/local/lib/python3.8/dist-packages/tf2onnx/tfonnx.py", line 600, in process_parsed_graph
    raise exceptions[0]
  File "/usr/local/lib/python3.8/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.8/dist-packages/tf2onnx/custom_opsets/onnx_ml.py", line 35, in version_8
    utils.make_sure(shared_name in initialized_tables, "Initialized table %s for node %s not found.",
  File "/usr/local/lib/python3.8/dist-packages/tf2onnx/utils.py", line 264, in make_sure
    raise ValueError("make_sure failure: " + error_msg % args)
ValueError: make_sure failure: Initialized table b'' for node TempHashTable_Lookup/LookupTableFindV2 not found.

Additional context Add any other context about the problem here. If the issue is about a particular model, please share the model details as well to facilitate debugging.

Agoniii avatar May 12 '22 03:05 Agoniii

There are several limitations right now for conversion:

  1. Please set a shared_name when you initialized tf.contrib.lookup.HashTable(). Then construct an initialized_tables and pass it into process_tf_graph() method. This is required by LookupTableFindV2 op.

  2. Right now, we can only support key's dtype is String and value's dtype is INT64. probably you need to change keys and values as below and set a new default value:

    keys_tensor = tf.constant(["a", "b"], name="input_keys",) vals_tensor = tf.constant([1, 2], dtype=tf.int64, name="input_vals")

fatcat-z avatar May 18 '22 09:05 fatcat-z

Thanks @fatcat-z
I modified my code, and it can convert to onnx.

import tensorflow as tf
import tf2onnx

with tf.Session() as sess:
    #init = tf.compat.v1.lookup.KeyValueTensorInitializer(['a', 'b'], [1, 2])
    keys_tensor = tf.constant(["a", "b"], name="input_keys")
    vals_tensor = tf.constant([1, 2], dtype=tf.int64, name="input_vals")
    init = tf.contrib.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor)
    table = tf.contrib.lookup.HashTable(init, default_value=-1, name="TempHashTable", shared_name="shared_hash_table")
    result = table.lookup(tf.constant(["a"]))
    table.init.run()
    result = tf.identity(result, name="output")

    print(sess.run(result))

    initialized_tables = {}
    #shared_name = table.get_attr_value("shared_name")
    #print("shared_name", shared_name)
    initialized_tables[b"shared_hash_table"] = (["a", "b"], [1, 2])

    # write onnx model
    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=["input_keys:0", "input_vals:0"], output_names=["output:0"], opset=13, initialized_tables=initialized_tables)
    model_proto = onnx_graph.make_model("test")
    with open("model.onnx", "wb") as f:
        f.write(model_proto.SerializeToString())

But the model is error. image

Agoniii avatar May 30 '22 04:05 Agoniii

Thanks @fatcat-z I modified my code, and it can convert to onnx.

import tensorflow as tf
import tf2onnx

with tf.Session() as sess:
    #init = tf.compat.v1.lookup.KeyValueTensorInitializer(['a', 'b'], [1, 2])
    keys_tensor = tf.constant(["a", "b"], name="input_keys")
    vals_tensor = tf.constant([1, 2], dtype=tf.int64, name="input_vals")
    init = tf.contrib.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor)
    table = tf.contrib.lookup.HashTable(init, default_value=-1, name="TempHashTable", shared_name="shared_hash_table")
    result = table.lookup(tf.constant(["a"]))
    table.init.run()
    result = tf.identity(result, name="output")

    print(sess.run(result))

    initialized_tables = {}
    #shared_name = table.get_attr_value("shared_name")
    #print("shared_name", shared_name)
    initialized_tables[b"shared_hash_table"] = (["a", "b"], [1, 2])

    # write onnx model
    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=["input_keys:0", "input_vals:0"], output_names=["output:0"], opset=13, initialized_tables=initialized_tables)
    model_proto = onnx_graph.make_model("test")
    with open("model.onnx", "wb") as f:
        f.write(model_proto.SerializeToString())

But the model is error. image

Probably you can update the way to call this function. You can find 2 examples in our tests for how to use it correctly:

test_hashtable_lookup

test_hashtable_lookup_const

fatcat-z avatar Aug 10 '22 16:08 fatcat-z

It's been over 2 months, so closing this. Feel free to open a new one if the issue still exists.

fatcat-z avatar Oct 11 '22 05:10 fatcat-z

qq related to this: currently we only support dtype is String and value's dtype is INT64, can we support other key dtype? (say, INT64)

hgong-snap avatar Dec 13 '22 00:12 hgong-snap

qq related to this: currently we only support dtype is String and value's dtype is INT64, can we support other key dtype? (say, INT64)

So far, there is no plan for this.

fatcat-z avatar Dec 13 '22 09:12 fatcat-z