unable to use Trained Tensorflow model

11,903

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 Graph
with 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 needed
    print(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()

Share:
11,903
Torab Shaikh
Author by

Torab Shaikh

Updated on July 16, 2022

Comments

  • Torab Shaikh
    Torab Shaikh almost 2 years

    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 fie with below code.

    with tf.Session() as sess:
        with tf.gfile.FastGFile("tensorflow/trained/saved_model.pb",'rb') as  f:
            graph_def = tf.GraphDef()
            tf.Graph.as_graph_def()
            graph_def.ParseFromString(f.read())
            g_in=tf.import_graph_def(graph_def)
            LOGDIR='/log'
            train_writer=tf.summary.FileWriter(LOGDIR)
            train_writer.add_graph(sess.graph)
    

    it gives me this error -

     File "testing.py", line 7, in <module>
    graph_def.ParseFromString(f.read())
    google.protobuf.message.DecodeError: Error parsing message
    

    I tried many solution I can find for this problem and modules in tensorflow/python/tools which uses the graph_def.ParseFromString(f.read()) function are giving me same error. Please tell me how to solve this or tell me the way in which I can avoid ParseFromString(f.read()) function. Any help would be appreciated. Thank you!

  • Ella Jameson
    Ella Jameson almost 4 years
    This was actually my problem! Thank you!
  • subbu
    subbu about 2 years
    I am getting the same error. Still unable to do, could you help me? stackoverflow.com/questions/71439124/…