[英]Tensorflow serving error: "Data loss: Can't parse saved_model.pb as binary proto"
I used google collaboratory to train a simple mnist example model to get myself familiar with tensorflow serving but my tensorflow model server is not able to read my protobuf file.我使用 google collaboratory 训练了一个简单的 mnist 示例 model 让自己熟悉 tensorflow 服务,但是我的 tensorflow model 服务器无法读取我的 protobuf 文件。 It's really strange.
这真的很奇怪。
I tried to load a different protobuf model which I downloaded from github and it worked which means my tensorflow server is working.我尝试加载一个不同的 protobuf model,它是我从 github 下载的,它工作正常,这意味着我的 tensorflow 服务器正在工作。 After that I used a keras model, exported with the
tf.saved_model.simple_save()
function and it worked.之后我使用了 keras model,用
tf.saved_model.simple_save()
function 导出并且它工作了。 Finally, I tried to import my own exported protobuf model which couldn't be read by my tensorflow server back into my python code and everything worked fine.最后,我尝试将我自己导出的 protobuf model 导入我的 tensorflow 服务器无法读取的 python 代码,一切正常。
I hope someone can help me.我希望有一个人可以帮助我。
Full error message: Loading servable: {name: mnist_test version: 1} failed: Data loss: Can't parse /home/models/mnist_test/1/saved_model.pb as binary proto
完整错误消息:
Loading servable: {name: mnist_test version: 1} failed: Data loss: Can't parse /home/models/mnist_test/1/saved_model.pb as binary proto
My export code:我的导出代码:
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()
I also got the same error(Tensorflow serving error: "Data loss: Can't parse saved_model.pb as binary proto").我也遇到了同样的错误(Tensorflow 服务错误:“数据丢失:无法将 saved_model.pb 解析为二进制原型”)。 Then, I solved the problem by converting GraphDef(*.pb) format to SavedModel format using the following code.
然后,我通过使用以下代码将 GraphDef(*.pb) 格式转换为 SavedModel 格式解决了这个问题。 Hope it helps you.
希望对你有帮助。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.