question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save as .pb and single image inference

See original GitHub issue

I am saving the model in a protobuf format like this in the evaluate.py :

tf.train.write_graph(_session.graph_def, save_dir, "FCN8_bilinear_session_Full.pb", False)
tf.train.write_graph(_session.graph_def, save_dir, "FCN8_bilinear_session_Full.pbtxt", True)

After that I want to do a single image inference through that .pb file using this code :

import tensorflow as tf
import os
import numpy as np
from tensorflow.python.platform import gfile
from PIL import Image

# Read the image & get statstics
img=Image.open('/path-to-image/demoImage.png')
img.show()
width, height = img.size
print(width)
print(height)

#Plot the image
#image.show()

with tf.Graph().as_default() as graph:

        with tf.Session() as sess:

                # Load the graph in graph_def
                print("load graph")

                # We load the protobuf file from the disk and parse it to retrive the unserialized graph_drf
                with gfile.FastGFile("/path-to-FCN-model/FCN8.pb",'rb') as f:

                                #Set default graph as current graph
                                graph_def = tf.GraphDef()
                                graph_def.ParseFromString(f.read())
                                #sess.graph.as_default() #new line

                                # Import a graph_def into the current default Graph
                                tf.import_graph_def(graph_def, name='')

                                # Print the name of operations in the session
                                #for op in sess.graph.get_operations():

                                    #print "Operation Name :",op.name            # Operation name
                                    #print "Tensor Stats :",str(op.values())     # Tensor name

                                # INFERENCE Here
                                l_input = graph.get_tensor_by_name('Placeholder:0')
                                l_output = graph.get_tensor_by_name('save/Assign_38:0')

                                print "l_input", l_input
                                print "l_output", l_output
                                print
                                print

                                # Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.                              
                                result = sess.run(l_output, feed_dict={l_input : img}) #   <= error here
                                print(results)

                                print("Inference done")


                                # Info
                                # First Tensor name : Placeholder:0
                                # Last tensor name  : save/Assign_38:0"

But I get the following error in sess.run :

2017-08-16 15:54:11.567779: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for model
2017-08-16 15:54:11.571391: W tensorflow/core/framework/op_kernel.cc:1158] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for model
Traceback (most recent call last):
  File "InferencePb2.py", line 51, in <module>
    result = sess.run(l_output, feed_dict={l_input : img}) #   <= error here
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for model
	 [[Node: save/RestoreV2_38 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](save/Const, save/RestoreV2_38/tensor_names, save/RestoreV2_38/shape_and_slices)]]

Caused by op u'save/RestoreV2_38', defined at:
  File "InferencePb2.py", line 33, in <module>
    tf.import_graph_def(graph_def, name='')
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 311, in import_graph_def
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

NotFoundError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to find any matching files for model
	 [[Node: save/RestoreV2_38 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](save/Const, save/RestoreV2_38/tensor_names, save/RestoreV2_38/shape_and_slices)]]

Is there any idea why I get this error? Even if I change the output tensor in order to inference in a part of the graph I still get different errors. Any suggestion is appriciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13

github_iconTop GitHub Comments

14reactions
kmonachopouloscommented, Aug 23, 2017

I managed to fix the error, below is the working script to inference a single image on Fully Convolutional Networks (for whoever is interesting in an alternative segmentation algorithm from SEGNET) . This model use billinear interpolation for scaling rather than un-pooling layer. Anyway, you must set the correct input and output tensor name and pass the network from TF optimizer to set Dropout probabilities to 1. Afterwards, the inference works correctly, extracting the segmented image.

import tensorflow as tf # Default graph is initialized when the library is imported
import os
from tensorflow.python.platform import gfile
from PIL import Image
import numpy as np
import scipy
from scipy import misc
import matplotlib.pyplot as plt
import cv2

with tf.Graph().as_default() as graph: # Set default graph as graph

           with tf.Session() as sess:
                # Load the graph in graph_def
                print("load graph")

                # We load the protobuf file from the disk and parse it to retrive the unserialized graph_drf
                with gfile.FastGFile("/path-to-protobuf/FCN8_Freezed.pb",'rb') as f:

                                print("Load Image...")
                                # Read the image & get statstics
                                image = scipy.misc.imread('/Path-To-Image/uu_000010.png')
                                image = image.astype(float)
                                Input_image_shape=image.shape
                                height,width,channels = Input_image_shape

                                print("Plot image...")
                                #scipy.misc.imshow(image)

                                # Set FCN graph to the default graph
                                graph_def = tf.GraphDef()
                                graph_def.ParseFromString(f.read())
                                sess.graph.as_default()

                                # Import a graph_def into the current default Graph (In this case, the weights are (typically) embedded in the graph)

                                tf.import_graph_def(
                                graph_def,
                                input_map=None,
                                return_elements=None,
                                name="",
                                op_dict=None,
                                producer_op_list=None
                                )

                                # Print the name of operations in the session
                                for op in graph.get_operations():
                                        print "Operation Name :",op.name         # Operation name
                                        print "Tensor Stats :",str(op.values())     # Tensor name

                                # INFERENCE Here
                                l_input = graph.get_tensor_by_name('Inputs/fifo_queue_Dequeue:0') # Input Tensor
                                l_output = graph.get_tensor_by_name('upscore32/conv2d_transpose:0') # Output Tensor

                                print "Shape of input : ", tf.shape(l_input)
                                #initialize_all_variables
                                tf.global_variables_initializer()

                                # Run Kitty model on single image
                                Session_out = sess.run( l_output, feed_dict = {l_input : image} )

3reactions
andrewginnscommented, Jun 16, 2018

@kmonachopoulos Thanks your solution helped me understand how to do inference for my model using a .pb

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save, Load and Inference From TensorFlow Frozen Graph
# This will only save the graph but the variables will not be saved. # You have to freeze your model first.
Read more >
Single Image Inference in Tensorflow [Python] - Stack Overflow
What I am trying to do now is to make a simple inference using that .pb file and extract and save output image....
Read more >
How to convert trained Keras model to a single TensorFlow ...
You are going to learn step by step how to freeze and convert your trained Keras model into a single TensorFlow .pb file....
Read more >
Using the SavedModel format | TensorFlow Core
You can save and load a model in the SavedModel format using the following APIs: Low-level tf.saved_model API. This document describes how to...
Read more >
How to Save and Load Your Keras Deep Learning Model
Save Your Neural Network Model to JSON. JSON is a simple file format for describing data hierarchically. Keras provides the ability to describe ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found