Reputation: 213
I just want to convert python model to tensorflow.js model, but after saving as .pb, I run "tensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model --signature_name=serving_default --saved_model_tags=serve ./saved_model ./web_model", error appeared.
2019-03-20 23:07:05.970985: I tensorflow/core/grappler/devices.cc:53] Number of eligible GPUs (core count >= 8): 0 (Note: TensorFlow was not compiled with CUDA support) 2019-03-20 23:07:05.978764: I tensorflow/core/grappler/clusters/single_machine.cc:359] Starting new session 2019-03-20 23:07:05.985340: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2019-03-20 23:07:06.072370: E tensorflow/core/grappler/grappler_item_builder.cc:636] Init node Variable/Assign doesn't exist in graph Traceback (most recent call last): File "d:\anaconda3\lib\site-packages\tensorflow\python\grappler\tf_optimizer.py", line 43, in OptimizeGraph verbose, graph_id, status) SystemError: returned NULL without setting an error
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "d:\anaconda3\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "d:\anaconda3\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "D:\Anaconda3\Scripts\tensorflowjs_converter.exe__main__.py", line 9, in File "d:\anaconda3\lib\site-packages\tensorflowjs\converters\converter.py", line 358, in main strip_debug_ops=FLAGS.strip_debug_ops) File "d:\anaconda3\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 271, in convert_tf_saved_model concrete_func) File "d:\anaconda3\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 140, in convert_variables_to_constants_v2 graph_def = _run_inline_graph_optimization(func) File "d:\anaconda3\lib\site-packages\tensorflow\python\framework\convert_to_constants.py", line 59, in _run_inline_graph_optimization return tf_optimizer.OptimizeGraph(config, meta_graph) File "d:\anaconda3\lib\site-packages\tensorflow\python\grappler\tf_optimizer.py", line 43, in OptimizeGraph verbose, graph_id, status) File "d:\anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 548, in exit c_api.TF_GetCode(self.status.status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Failed to import metagraph, check error log for more info.
This is my code. And the version of tensorflow is 1.14.0(preview as I failed to install tf 2.0)
# coding=utf-8#
import tensorflow as tf
import numpy as np
x_data = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
y_data = [[0.0], [1.0], [1.0], [0.0]]
x_test = [[0.0, 1.0], [1.0, 1.0]]
xs = tf.placeholder(tf.float32, [None, 2])
ys = tf.placeholder(tf.float32, [None, 1])
W1 = tf.Variable(tf.random_normal([2, 10]))
B1 = tf.Variable(tf.zeros([1, 10]) + 0.1)
out1 = tf.nn.relu(tf.matmul(xs, W1) + B1)
W2 = tf.Variable(tf.random_normal([10, 1]))
B2 = tf.Variable(tf.zeros([1, 1]) + 0.1)
prediction = tf.add(tf.matmul(out1, W2), B2, name="model")
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(40):
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
re = sess.run(prediction, feed_dict={xs: x_test})
print(re)
for x in re:
if x[0] > 0.5:
print(1)
else:
print(0)
tf.saved_model.simple_save(sess, "./saved_model", inputs={"x": xs, }, outputs={"model": prediction, })
Upvotes: 0
Views: 2928
Reputation: 4823
just add
tf.enable_resource_variables()
before initializing x_data
and use this command for conversion
tensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model ./saved_model ./web_model
Upvotes: 0
Reputation: 213
In the end, I give up it, as latest version has removed loadFrozenModel, and the support is little. I try to use keras model and it works. However, I still hope that somebody tell me why my tf model fails to convert to tfjs model.
Upvotes: 1