'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. 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. After that I used a keras model, exported with the tf.saved_model.simple_save()
function and it worked. 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.
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
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()
Solution 1:[1]
I also got the same error(Tensorflow serving error: "Data loss: Can't parse saved_model.pb as binary proto"). Then, I solved the problem by converting GraphDef(*.pb) format to SavedModel format using the following code. 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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |