我创建了一个深度学习模型,该模型使用文本文件在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服务文档中看到,我们可以将资产传递给服务。但是不要那样做。没有可用的明确示例。
谁能让我知道如何通过这个?
答案 0 :(得分:0)