I have an tensorflow .pb file which I would like to load into python DNN, restore the graph and get the predictions. I am doing this to test out whether the .pb file created can make the predictions similar to the normal Saver.save() model.
My basic problem is am getting a very different value of predictions when I make them on Android using the above mentioned .pb file
My .pb file creation code:
frozen_graph = tf.graph_util.convert_variables_to_constants( session, session.graph_def, ['outputLayer/Softmax'] ) with open('frozen_model.pb', 'wb') as f: f.write(frozen_graph.SerializeToString())
So I have two major concerns:
Restoring Models The first thing to do when restoring a TensorFlow model is to load the graph structure from the ". meta" file into the current graph. The current graph could be explored using the following command tf. get_default_graph() .
The only thing you should do is use this code: model = tf. keras. models. load_model('./_models/vgg50_finetune') And you can both train model or use it for prediction.
The . pb format is the protocol buffer (protobuf) format, and in Tensorflow, this format is used to hold models. Protobufs are a general way to store data by Google that is much nicer to transport, as it compacts the data more efficiently and enforces a structure to the data.
The following code will read the model and print out the names of the nodes in the graph.
import tensorflow as tf from tensorflow.python.platform import gfile GRAPH_PB_PATH = './frozen_model.pb' with tf.Session() as sess: print("load graph") with gfile.FastGFile(GRAPH_PB_PATH,'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) sess.graph.as_default() tf.import_graph_def(graph_def, name='') graph_nodes=[n for n in graph_def.node] names = [] for t in graph_nodes: names.append(t.name) print(names)
You are freezing the graph properly that is why you are getting different results basically weights are not getting stored in your model. You can use the freeze_graph.py (link) for getting a correctly stored graph.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With