Hub: "Table not initialized" when loading model in Java

Created on 16 Jun 2019  路  4Comments  路  Source: tensorflow/hub

Hi all. I am trying to save USE model and restore it in Java.
I use the following code to save the model:

import tensorflow as tf
import tensorflow_hub as hub

module_url = "https://tfhub.dev/google/universal-sentence-encoder/2"
embed = hub.Module(module_url)

sts_input = tf.placeholder(tf.string, name="input")
sts_encode = tf.nn.l2_normalize(embed([sts_input]), axis=1, name="output")

with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    session.run(tf.tables_initializer())
    vector = session.run([sts_encode], feed_dict={sts_input: 'Hello World'})
    print(vector)
    tf.saved_model.simple_save(
        session,
        '/path/to/model/',
        inputs={'input': sts_input},
        outputs={'output': sts_encode},
    )

Then I try to load this model in Java:

SavedModelBundle bundle = SavedModelBundle.load("/path/to/model/", "serve");
Session sess = bundle.session();
Tensor<String> inputTensor = Tensor.create("Hello World".getBytes("UTF-8"), String.class);
Tensor result = sess.runner().feed("input", inputTensor).fetch("output").run().get(0);
float[] output = new float[512];
result.copyTo(output);
System.out.println(Arrays.toString(output));

But I get the following exception:

2019-06-16 03:07:28.033142: I tensorflow/cc/saved_model/loader.cc:182] Restoring SavedModel bundle.
2019-06-16 03:07:28.728843: I tensorflow/cc/saved_model/loader.cc:285] SavedModel load for tags { serve }; Status: success. Took 1175121 microseconds.
2019-06-16 03:07:29.340644: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at lookup_table_op.cc:809 : Failed precondition: Table not initialized.
Exception in thread "main" java.lang.IllegalStateException: Table not initialized.
     [[{{node module_apply_default/string_to_index_Lookup/hash_table_Lookup}}]]
    at org.tensorflow.Session.run(Native Method)
    at org.tensorflow.Session.access$100(Session.java:48)
    at org.tensorflow.Session$Runner.runHelper(Session.java:314)
    at org.tensorflow.Session$Runner.run(Session.java:264)
    at Main.main(Main.java:16)

Am I using a wrong way to save/load the model?

support

Most helpful comment

Sorry, forgot to mention here. My friend helped me to find a solution of this problem. I added 'legacy_init_op=tf.tables_initializer()' to the simple_save and it solved my problem. Thanks for your reply!

All 4 comments

When using the Estimator API, table_not_initialized error should not occur.However if you are consuming the output of tf.Transform in some other way, then you may need to call聽tf.tables_initializer聽and run that op yourself.
Can you please take a look at this issue and let me know if this helps. Thanks!

Sorry, forgot to mention here. My friend helped me to find a solution of this problem. I added 'legacy_init_op=tf.tables_initializer()' to the simple_save and it solved my problem. Thanks for your reply!

Sorry, forgot to mention here. My friend helped me to find a solution of this problem. I added 'legacy_init_op=tf.tables_initializer()' to the simple_save and it solved my problem. Thanks for your reply!

Greate help to me. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iliaschalkidis picture iliaschalkidis  路  5Comments

truas picture truas  路  4Comments

artemmavrin picture artemmavrin  路  3Comments

lugq1990 picture lugq1990  路  4Comments

PradyumnaGupta picture PradyumnaGupta  路  3Comments