【发布时间】:2019-07-08 11:36:40
【问题描述】:
我创建了一个深度学习模型,它使用文本文件在 Tensorflow 中初始化词汇表,如下所示 -
class MyModel(object):
def __init__(self):
table_init = tf.lookup.TextFileInitializer('/home/abhilash/resmap.txt', tf.int64, 0, tf.int64, 1, delimiter=" ")
table = tf.lookup.StaticVocabularyTable(table_init, num_oov_buckets)
resmap.txt 文件有这样的条目 -
2345 1
3456 2
1234 3
我使用以下代码将此 TF 模型转换为 Tensorflow 服务 -
from model import MyModel
with tf.Session() as sess:
tf.app.flags.DEFINE_string('f', '', 'kernel')
tf.app.flags.DEFINE_integer('model_version', 1, 'version number of the model.')
tf.app.flags.DEFINE_string('save_dir', '/home/abhilash', 'Saving directory.')
FLAGS = tf.app.flags.FLAGS
export_path = os.path.join(tf.compat.as_bytes(FLAGS.save_dir), tf.compat.as_bytes(str(FLAGS.model_version)))
print('Exporting trained model to', export_path)
# Creating Model object and initializing all the global variables in TF Graph.
model = MyModel()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(tf.tables_initializer())
tf.train.Saver().restore(sess, os.path.join('/home/abhilash', 'model1'))
print("Model restored.")
# SavedModel Builder Object
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
# Converting Tensor to TensorInfo Objects so that they can be used in SignatureDefs
tensor_info_input1 = tf.saved_model.utils.build_tensor_info(model.input1)
tensor_info_input2 = tf.saved_model.utils.build_tensor_info(model.input2)
tensor_info_prob = tf.saved_model.utils.build_tensor_info(model.logits_all)
# SignatureDef
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={'input1':tensor_info_input1,
'input2':tensor_info_input2},
outputs={'probs': tensor_info_prob},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tf.saved_model.tag_constants.SERVING],
signature_def_map={'predict_prob': prediction_signature},
main_op=tf.tables_initializer(),
strip_default_attrs=False,
)
# Export the model
builder.save()
print('Done exporting TF Model to SavedModel format!')
model = MyModel() 实例化 TF Graph 结构。
使用上述代码模型成功转换为SavedModel格式。服务后给出正确的结果。
但是当我在其他机器上提供这个 servable 时,它给了我这样的错误 -
Traceback (most recent call last):
File "/home/abhilashawasthi/anaconda3/envs/mlflow-95a14f155def99fdbaccbe70ebfbcf3065700c56/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1356, in _do_call
return fn(*args)
File "/home/abhilashawasthi/anaconda3/envs/mlflow-95a14f155def99fdbaccbe70ebfbcf3065700c56/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1341, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/home/abhilashawasthi/anaconda3/envs/mlflow-95a14f155def99fdbaccbe70ebfbcf3065700c56/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1429, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.NotFoundError: /home/abhilash/resmap.txt; No such file or directory
[[{{node text_file_init/InitializeTableFromTextFileV2}}]]
也就是说,它会尝试在同一位置找到该文本文件。
那么如何传递这个文本文件以便它与SavedModel 捆绑在一起呢?
我在 TF Serving 文档中看到我们可以将资产传递给服务。但不要这样做。没有可用的明确示例。
谁能告诉我如何通过这个?
【问题讨论】:
标签: python tensorflow tensorflow-serving