我使用google协作来训练一个简单的mnist示例模型,让我熟悉tensorflow服务,但是我的tensorflow模型服务器无法读取我的protobuf文件。真的很奇怪。
我尝试加载从github下载的不同的protobuf模型,这意味着我的tensorflow服务器正在工作。在此之后,我使用了一个keras模型,并使用tf.saved_model.simple_save()函数导出了它。最后,我尝试将我自己导出的protobuf模型导入我的tensorflow服务器无法读取到我的python代码中,一切都很好。
我希望有人能帮我。
完全错误消息:Loading servable: {name: mnist_test version: 1} failed: Data loss: Can't parse /home/models/mnist_test/1/saved_model.pb as binary proto
我的出口代码:
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
model_inputs = tf.saved_model.utils.build_tensor_info(X_placeholder)
model_outputs = tf.saved_model.utils.build_tensor_info(output)
signature_definition = tf.saved_model.signature_def_utils.build_signature_def(
inputs={'inputs': model_inputs},
outputs={'outputs': model_outputs},
method_name= tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_definition})
builder.save()发布于 2022-05-05 11:29:03
我也得到了同样的错误(Tensorflow服务错误:“数据丢失:不能将saved_model.pb解析为二进制proto")。然后,通过使用以下代码将GraphDef(*.pb)格式转换为SavedModel格式解决了这个问题。希望它能帮到你。
import tensorflow as tf
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants
version_str = tf.__version__
if int(version_str.split('.')[0]) == 2:
# tensorflow 2.x
del tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# else tensorflow 1.x
export_dir = '/path/to/saved_model_dir'
graph_pb = '/path/to/graph_def.pb'
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.gfile.GFile(graph_pb, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
sigs = {}
with tf.Session(graph=tf.Graph()) as sess:
tf.import_graph_def(graph_def, name="")
g = tf.get_default_graph()
ids = g.get_tensor_by_name("ids:0")
values = g.get_tensor_by_name("values:0")
predictions = g.get_tensor_by_name("predictions:0")
sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
tf.saved_model.signature_def_utils.predict_signature_def(
{"ids": ids, "values": values}, {"predictions": predictions})
builder.add_meta_graph_and_variables(sess, [tag_constants.SERVING],
signature_def_map=sigs)
builder.save()https://stackoverflow.com/questions/53817924
复制相似问题