I am trying to export a previously trained model (pb) to be used for serving
using the following snippet
from tensorflow_serving.session_bundle import exporter
def create_graph():
"""Creates a graph from saved GraphDef file and returns a saver."""
# Creates graph from saved graph_def.pb.
with tf.gfile.FastGFile(modelFullPath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def export():
print 'Exporting trained model to', export_path
saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(input_tensor=x, scores_tensor=y)
model_exporter.init(sess.graph.as_graph_def(),default_graph_signature=signature)
model_exporter.export(export_path, tf.constant(FLAGS.export_version), sess)
print 'Done exporting!'
However the manifest_pb2 in exporter.py is not Found.
Am I missing something fundamental in this approach?
manifest_pb2.py is generated from manifest.proto when the session_bundle is built using bazel. On Mac I installed TensorFlow using pip3 and the file is located at /usr/local/lib/python3.5/site-packages/tensorflow/contrib/session_bundle/manifest_pb2.py.
Follow the steps at https://www.tensorflow.org/versions/r0.10/get_started/os_setup.html for your platform and it should fix the problem.
Related
Hi I was making tflite for custom albert model with pb file in tf1.15 but raised error of
raise IOError("Cannot parse file %s: %s." % (path_to_pb, str(e)))
OSError: Cannot parse file b'/home/choss/test2/freeze2/saved_model.pb': Error parsing message.
Code below is How I made .pb file
meta_path = 'model.ckpt-400.meta' # Your .meta file
output_node_names = ['loss/Softmax']
with tf.Session() as sess:
# Restore the graph
saver = tf.train.import_meta_graph(meta_path)
# Load weights
ckpt ='/home/choss/test2/freeze2/model.ckpt-400'
print(ckpt)
saver.restore(sess, ckpt)
output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]
# Freeze the graph
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
sess,
sess.graph_def,
output_node_names)
# Save the frozen graph
with open('saved_model.pb', 'wb') as f:
f.write(frozen_graph_def.SerializeToString())
And I tried to make tflite file with code below
saved_model_dir = "/home/choss/test2/freeze2"
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
I used f.graph_util.convert_variables_to_constants because of freeze_graph because
freeze_graph.freeze_graph('./graph.pbtxt', saver, False, 'model.ckpt-400', 'loss/ArgMax', "", "", 'frozen.pb', True, "")
gave me an error message
File "/home/pgb/anaconda3/envs/myenv/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 2154, in __getitem__
return self._inputs[i]
IndexError: list index out of range
Is it because I did not use freeze_graph?
If so is there any other way aside from freeze_graph?
Instead of freezing the graph by yourself, I recommend exporting as a TF saved model and using the saved model converter with the recent TF version. You can decouple the TensorFlow versions for training and converting. For example, training can be done in the TF 1.15 and the saved model can be exported from it. And then, it is possible to bring the saved model to the TFLite converter API in TensorFlow 2.4.1 version or beyonds.
I have downloaded checkpoints along with model for Mobilenet v3. After extraction of rar file, I get two folders and two other files. Directory looks like following
Main Folder
ema (folder)
checkpoint
model-x.data-00000-of-00001
model-x.index
model-x.meta
pristine (folder)
model.ckpt-y.data-00000-of-00001
model.ckpt-y.index
model.ckpt-y.meta
.pb
.tflite
I have tried many codes among which few are below.
import tensorflow as tf
from tensorflow.python.platform import gfile
model_path = "./weights/v3-large-minimalistic_224_1.0_uint8/model.ckpt-3868848"
detection_graph = tf.Graph()
with tf.Session(graph=detection_graph) as sess:
# Load the graph with the trained states
loader = tf.train.import_meta_graph(model_path+'.meta')
loader.restore(sess, model_path)
The above code results in following error
Node {{node batch_processing/distort_image/switch_case/indexed_case}} of type Case has '_lower_using_switch_merge' attr set but it does not support lowering.
I tried following code:
import tensorflow as tf
import sys
sys.path.insert(0, 'models/research/slim')
from nets.mobilenet import mobilenet_v3
tf.reset_default_graph()
file_input = tf.placeholder(tf.string, ())
image = tf.image.decode_jpeg(tf.read_file('test.jpg'))
images = tf.expand_dims(image, 0)
images = tf.cast(images, tf.float32) / 128. - 1
images.set_shape((None, None, None, 3))
images = tf.image.resize_images(images, (224, 224))
model = mobilenet_v3.wrapped_partial(mobilenet_v3.mobilenet,
new_defaults={'scope': 'MobilenetEdgeTPU'},
conv_defs=mobilenet_v3.V3_LARGE_MINIMALISTIC,
depth_multiplier=1.0)
with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope(is_training=False)):
logits, endpoints = model(images)
ema = tf.train.ExponentialMovingAverage(0.999)
vars = ema.variables_to_restore()
print(vars)
with tf.Session() as sess:
tf.train.Saver(vars).restore(sess, './weights/v3-large-minimalistic_224_1.0_uint8/saved_model.pb')
tf.train.Saver().save(sess, './weights/v3-large-minimalistic_224_1.0_uint8/pristine/model.ckpt')
The above code generates following error:
Unable to open table file ./weights/v3-large-minimalistic_224_1.0_uint8/saved_model.pb: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
[[node save/RestoreV2 (defined at <ipython-input-11-1531bbfd84bb>:29) ]]
How can I load Mobilenet v3 model along with the checkpoints and use it for my data?
try this
with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope(is_training=False)):
logits, endpoints = mobilenet_v3.large_minimalistic(images)
instead of
model = mobilenet_v3.wrapped_partial(mobilenet_v3.mobilenet,
new_defaults={'scope': 'MobilenetEdgeTPU'},
conv_defs=mobilenet_v3.V3_LARGE_MINIMALISTIC,
depth_multiplier=1.0)
with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope(is_training=False)):
logits, endpoints = model(images)
I am trying to load the pre-trained ResNet model in the below link
https://drive.google.com/open?id=1xkVK92XLZOgYlpaRpG_-WP0Elzg4ewpw
But it gives RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
What could be the possible issue?
import tensorflow as tf
import tensorflow.contrib.slim as slim
# Let's load a previously saved meta graph in the default graph
# This function returns a Saver
saver = tf.train.import_meta_graph('model.ckpt-0.meta')
# We can now access the default graph where all our metadata has been loaded
graph = tf.get_default_graph()
with tf.Session(graph=tf.Graph()) as sess:
saver.restore(sess, 'model.ckpt-0.data-00000-of-00001')
print('Worked')
you must have a model(Rough house),and load parameter(Bed, furniture).now you need a Rough house(operations,such as:tf.Variable(),tf.add(),tf.nn.softmax_cross_entropy_with_logits()).
with tf.Session() as sess:
# tf.saved_model.loader.load(sess, [tag_constants.TRAINING], export_dir)
saver = tf.train.import_meta_graph('C://Users//hardi//tutorial//resnet//model.ckpt.meta')
# new_saver = saver.restore(sess, tf.train.latest_checkpoint('C://Users//hardi//tutorial//resnet//'))
saver.restore(sess, 'model.ckpt')
graph = tf.get_default_graph()
print('success')
The error was to bring the saver instance in the loop and use 'model.ckpt' instead of 'model.ckpt-0.data-00000-of-00001' as V2 checkpoint
found solution here https://github.com/tensorflow/models/issues/2676
I am trying to export a saved model using the code below. It exports the saved_model.pb, but for any input that I give it, I get the same result. I think there is something wrong with how I am doing the output. I am very new to TensorFlow, so I apologize if this is a simple mistake.
with tf.gfile.FastGFile("retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def export_model(saved_model_dir, final_tensor_name):
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
with sess.graph.as_default() as graph:
tf.saved_model.simple_save(
sess,
saved_model_dir,
inputs={'image': tf.placeholder(tf.string)},
outputs={'prediction': graph.get_tensor_by_name(final_tensor_name + ":0")}
)
I am also using the retrain.py from the following tutorial: https://github.com/BartyzalRadek/Multi-label-Inception-net so it could be something from there.
How I have structured my code is that I have included this in a separate file so I don’t need to retrain every time I want to try to save.
I'm trying to load an already trained model taken from https://github.com/tensorflow/models/tree/master/official/resnet, but when I try to load the .pb I get an error on ParseFromString method:
import tensorflow as tf
from tensorflow.python.platform import gfile
GRAPH_PB_PATH = '../resnet_v2_fp32_savedmodel_NHWC/1538687283/saved_model.pb'
with tf.gfile.FastGFile(GRAPH_PB_PATH, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
g_in = tf.import_graph_def(graph_def, name="")
sess = tf.Session(graph=g_in)
DecodeError: Error parsing message
What I wrong?
I was having a similar problem, instead of using gfile I use the tf.saved_model.loader.load function like in this post https://stackoverflow.com/a/46547595/4637693:
sess = tf.Session(graph=tf.Graph())
model = tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], './model')
graph_def = model.graph_def