简体   繁体   English

Tensorflow 服务错误:“数据丢失:无法将 saved_model.pb 解析为二进制原型”

[英]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.

相关问题 如何正确创建saved_model.pb? - How to correctly create saved_model.pb? 将 tf1.x saved_model.pb 重新保存到新的 tf2.0 saved_model.pb - resave tf1.x saved_model.pb into new tf2.0 saved_model.pb keras.models.save_model 中的 saved_model.pb 是否与 tensorflow freeze_graph output.pb 文件相同? - Is saved_model.pb from keras.models.save_model the same with tensorflow freeze_graph output .pb file? 将 saved_model.pb 转换为 model.tflite - Converting saved_model.pb to model.tflite 如何从 tf.Session 保存到 saved_model.pb? - How to save to saved_model.pb from tf.Session? 在ML引擎上进行训练后,获取save_model.pb的路径 - Get the path of saved_model.pb after training on ML engine 如何使用来自 Google AutoML Vision Classification 的 TensorFlow Frozen GraphDef (single saved_model.pb) 进行推理和迁移学习 - How to do Inference and Transfer Learning with TensorFlow Frozen GraphDef (single saved_model.pb) from Google AutoML Vision Classification frozen_inference_graph.pb 和saved_model.pb 有什么区别? - What is difference frozen_inference_graph.pb and saved_model.pb? OSError:SavedModel 文件不存在于:/content\model\2016/{saved_model.pbtxt|saved_model.pb} - OSError: SavedModel file does not exist at: /content\model\2016/{saved_model.pbtxt|saved_model.pb} SavedModel 文件不存在于:model.h5/{saved_model.pbtxt|saved_model.pb} - SavedModel file does not exist at: model.h5/{saved_model.pbtxt|saved_model.pb}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM