Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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!

like image 834
Torab Shaikh Avatar asked Mar 05 '18 19:03

Torab Shaikh


3 Answers

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.

like image 59
anujonthemove Avatar answered Oct 24 '22 02:10

anujonthemove


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
like image 29
Naga kiran Avatar answered Oct 24 '22 02:10

Naga kiran


Have you passed as_text=False when saving a model? Please have a look at: TF save/restore graph fails at tf.GraphDef.ParseFromString()

like image 4
Agnieszka Walter Avatar answered Oct 24 '22 00:10

Agnieszka Walter