How can I use the tensorflow .pb file?

I have a Tensorflow file AlexNet.pb I am trying to load it then classify an image that I have. I have searched for hours but I still cant find a way to load it then classify an image. Is it so obvious and that I am just so stupid because no one seems to have a simple example of loading and running the .pb file.

Please I have spent so many hours do not just downvote but just send me the link if you are aware of a tutorial somewhere !

1 answer

  • answered 2018-07-12 08:24 nessuno

    It depends on how the protobuf file has been created.

    If the .pb file is the result of:

        # Create a builder to export the model
        builder = tf.saved_model.builder.SavedModelBuilder("export")
        # Tag the model in order to be capable of restoring it specifying the tag set
        builder.add_meta_graph_and_variables(sess, ["tag"])

    You have to know how that model has been tagged and use the tf.saved_model.loader.load method to load the saved graph in the current, empty, graph.

    If the model instead has been frozen you have to load the binary file in memory manually:

    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
    graph = tf.get_default_graph()
    tf.import_graph_def(graph_def, name="prefix")

    In both cases, you have to know the name of the input tensor and the name of the node you want to execute:

    If, for example, your input tensor is a placeholder named batch_ and the node you want to execute is the node named dense/BiasAdd:0 you have to

        batch = graph.get_tensor_by_name('batch:0')
        prediction = restored_graph.get_tensor_by_name('dense/BiasAdd:0')
        values =, feed_dict={
            batch: your_input_batch,