Convert .pb file into frozen .pb file

1,188 views
Skip to first unread message

nwo...@ebay.com

unread,
Aug 13, 2018, 1:48:42 PM8/13/18
to Discuss
Hello,

I have been struggling with this conversion. I tried the following:

import tensorflow as tf
import sys
from tensorflow.python.platform import gfile

from tensorflow.core.protobuf import saved_model_pb2
from tensorflow.python.util import compat

with tf.Session() as sess:
    model_filename
='saved_model.pb' # binary .pb file
   
with gfile.FastGFile(model_filename, 'rb') as f:

        data
= compat.as_bytes(f.read()) # reads binary
        sm
= saved_model_pb2.SavedModel()
       
print(sm)
        sm
.ParseFromString(data) # parses through the file
       
print(sm)
       
if 1 != len(sm.meta_graphs):
           
print('More than one graph found. Not sure which to write')
            sys
.exit(1)

        g_in
= tf.import_graph_def(sm.meta_graphs[0].graph_def)
        output_graph
= "frozen_grapha.pb"

       
# This not working fully  
        output_graph_def
= tf.graph_util.convert_variables_to_constants(
            sess
, # The session is used to retrieve the weights
            tf
.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes
           
['softmax']# The output node names are used to select the usefull nodes
       
)

       
# Finally we serialize and dump the output graph to the filesystem
       
with tf.gfile.GFile(output_graph, "wb") as f:
            f
.write(output_graph_def.SerializeToString())
       
print("%d ops in the final graph." % len(output_graph_def.node))
       
print(g_in)
LOGDIR
='.'
train_writer
= tf.summary.FileWriter(LOGDIR)
train_writer
.add_graph(sess.graph)


Without the output graph definition, it creates a event.out file that is readable by tensorboard. 


Could someone tell me what is wrong?

Best,
Nyla 


Andrew Selle

unread,
Aug 13, 2018, 1:52:00 PM8/13/18
to Discuss
you can use sess.graph_def instead of tf.get_default_graph().as_graph_def(). I don't see anything wrong. What exactly is going wrong?

nwo...@ebay.com

unread,
Aug 13, 2018, 4:41:42 PM8/13/18
to Discuss
Hello!


The code was not running within Jupyter notebooks, but kinda works from my computer when I replaced ['softmax'] with output_nodes = [n.name for n in tf.get_default_graph().as_graph_def().node], but now I have another bug: tensorflow.python.framework.errors_impl.FailedPreconditionError:Attempting to use uninitialized value . I found someone that fixed this, because they were constructing the Saver before I made my session. Is that what is going on within the code? It does not appear evident to me.   
Could there be something wrong with the .pb file I have?

Thank you!

On Monday, August 13, 2018 at 10:48:42 AM UTC-7, nwo...@ebay.com wrote:

Andrew Selle

unread,
Aug 13, 2018, 4:48:46 PM8/13/18
to nwo...@ebay.com, Discuss
You are using only the meta graph out of the saved model. This means you aren't loading  the checkpoint.

Look at how tflite does it

And if you have further problems create a github bug with complete information and post it here. This discussion should be moved there.

-A



nwo...@ebay.com

unread,
Aug 14, 2018, 4:04:02 PM8/14/18
to Discuss
Hey! 


I would really appreciate some help. I tried initializing the variables, but couldn't do it.

Thank you
Reply all
Reply to author
Forward
0 new messages