Tensorflow : get prediction out of graph file (.pb file)

Viewed 1666

i am using a graph file (pb file), the aim of this Tensorflow model is to provide a prediction on certain image

i have developed a code that loaded graph file but i can't stat session . the files available are :-

  • training_model_saved_model.pb
  • variables
    • training_model_variables_variables.data-00000-of-00001
    • training_model_variables_variables.index

the output is error contains a large list of model layer.what i can do in this case , any help is appreciated

this the code i used to load / run the model

import tensorflow as tf
import sys
import os



import matplotlib.image as mpimg
import matplotlib.pyplot as plt


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

export_dir = os.path.join("./", "variables/")
filename = "imgpsh_fullsize.jpeg"
raw_image_data = mpimg.imread(filename)

g = tf.Graph()
with tf.Session(graph=g) as sess:
   model_filename ='training_model_saved_model.pb'
   with gfile.FastGFile(model_filename, 'rb') as f:

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

        image_input= tf.import_graph_def(sm.meta_graphs[0].graph_def,name='',return_elements=["input"])
        #print(image_input)
        #saver =  tf.train.Saver()
        saver = tf.train.import_meta_graph(sm.meta_graphs[0].graph_def)
        '''
        print(image_input)

        x = g.get_tensor_by_name("input:0")

        print(x)
        '''
        saver.restore(sess,model_filename)

        predictions = sess.run(feed_dict={image: raw_image_data})
        print('###################################################')
        print(predictions)

error present is

Traceback (most recent call last):
  File "model_Input-get.py", line 35, in <module>
    saver = tf.train.import_meta_graph(sm.meta_graphs[0].graph_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1691, in import_meta_graph
    meta_graph_def = meta_graph.read_meta_graph_file(meta_graph_or_file)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/meta_graph.py", line 553, in read_meta_graph_file
    if not file_io.file_exists(filename):
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/lib/io/file_io.py", line 252, in file_exists
    pywrap_tensorflow.FileExists(compat.as_bytes(filename), status)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
    (bytes_or_text,))
TypeError: Expected binary or unicode string, got node {
  name: "input"
  op: "Placeholder"
  attr {
    key: "_output_shapes"
    value {
      list {
        shape {
          dim {
            size: -1
          }
        }
      }
    }
  }
  attr {
    key: "dtype"
    value {
      type: DT_STRING
    }
  }
1 Answers

You appear to be mixing the TensorFlow Serving SavedModel format with the regular TensorFlow export/restore functionality.

This is a particularly confusing part of the TensorFlow codebase as this format wasn’t well documented when it first appeared - and there aren’t a lot of examples showing when to use this format versus the original format.

My suggestion is to either:

  1. switch to TF Serving and continue using the SavedModel format, or
  2. stick to the original export/restore model format.
Related