Save as .pb and single image inference
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:1
- Comments:13
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
@kmonachopoulos Thanks your solution helped me understand how to do inference for my model using a .pb