Unable To Use Trained Tensorflow Model
I am new to Deep Learning and Tensorflow. I retrained a pretrained tensorflow inceptionv3 model as saved_model.pb to recognize different type of images but when I tried to use the
Solution 1:
I am assuming that you saved your trained model using tf.saved_model.Builder
provided by TensorFlow, in which case you could possibly do something like:
Load model
export_path = './path/to/saved_model.pb'# We start a session using a temporary fresh Graphwith tf.Session(graph=tf.Graph()) as sess:
'''
You can provide 'tags' when saving a model,
in my case I provided, 'serve' tag
'''
tf.saved_model.loader.load(sess, ['serve'], export_path)
graph = tf.get_default_graph()
# print your graph's ops, if neededprint(graph.get_operations())
'''
In my case, I named my input and output tensors as
input:0 and output:0 respectively
'''
y_pred = sess.run('output:0', feed_dict={'input:0': X_test})
To give some more context here, this is how I saved my model which can be loaded as above.
Save model
x = tf.get_default_graph().get_tensor_by_name('input:0')
y = tf.get_default_graph().get_tensor_by_name('output:0')
export_path = './models/'
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
signature = tf.saved_model.predict_signature_def(
inputs={'input': x}, outputs={'output': y}
)
# using custom tag instead of: tags=[tf.saved_model.tag_constants.SERVING]
builder.add_meta_graph_and_variables(sess=obj.sess,
tags=['serve'],
signature_def_map={'predict': signature})
builder.save()
This will save your protobuf ('saved_model.pb') in the said folder ('models' here) which can then be loaded as stated above.
Solution 2:
Please use the frozen_inference_graph.pb to load the model, than to use the saved_model.pb
Model_output
- saved_model
- saved_model.pb
- checkpoint- frozen_inference_graph.pb # Main model - model.ckpt.data-00000-of-00001- model.ckpt.index- model.ckpt.meta- pipeline.config
Solution 3:
Have you passed as_text=False when saving a model? Please have a look at: TF save/restore graph fails at tf.GraphDef.ParseFromString()
Post a Comment for "Unable To Use Trained Tensorflow Model"