tensorflow for poets: "The name 'import/input' refers to an Operation not in the graph."

Viewed 13912

I was following the codelabs tensorflow for poets and the training worked fine but when I runned the script to evaluate a image:

python -m scripts.label_image \
    --graph=tf_files/retrained_graph.pb  \
    --image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg

I got the following error:

The name 'import/input' refers to an Operation not in the graph.

I looked around and it has something to do with chosing the input and output layer, the script label_image.py has 'input' and 'output' set as default. The architecture I'm using is 'inception_v3'.

11 Answers

I changed ~/scripts/label_image.py line 77 and it works:

from

input_layer = "input"

to

input_layer = "Mul"

As @Mimii and @Celio mentioned: change ~/scripts/label_image.py, at the line input_layer = "input" to input_layer = "Mul" AND change the input dimensions: input_height = 299 and input_width= 299

Use this

curl -LO https://github.com/tensorflow/tensorflow/raw/master/tensorflow/examples/label_image/label_image.py
python label_image.py \
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--input_layer=Placeholder \
--output_layer=final_result \
--image=$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg

You have to make some changes in label_image.py in the scripts folder

input_height = 299 Change input_height to 299 from 224
input_width = 299 Change input_width to 299 from 224
input_mean = 128
input_std = 128
input_layer = "Mul" Change input_layer to Mul from input
output_layer = "final_result"

Output:

Evaluation time (1-image): 1.901s

daisy (score=0.98584)
sunflowers (score=0.01136)
dandelion (score=0.00210)
tulips (score=0.00066)
roses (score=0.00004)

For more info, refer this page

You should add --output_layer=final_result:0 as parameter.

Final call is : python -m scripts.label_image \
--graph=tf_files/retrained_graph.pb  \
--output_layer=final_result:0 \
--image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg

Sorry for late answer. I run python script below with a retrained model. Can you try this one?

Requirements: labels.txt and output.pb(retrained model) should be at same directory with my python scipt. Save code below as test.py And call it as: python test.py xxx.jpg

import sys
import tensorflow as tf


image_path = sys.argv[1]


image_data = tf.gfile.FastGFile(image_path, 'rb').read()


label_lines = [line.rstrip() for line
                   in tf.gfile.GFile("./labels.txt")]


with tf.gfile.FastGFile("./output.pb", 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:



    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

    predictions = sess.run(softmax_tensor, \
             {'DecodeJpeg/contents:0': image_data})


    top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

    for node_id in top_k:
        human_string = label_lines[node_id]
        score = predictions[0][node_id]
        print('%s (score = %.5f)' % (human_string, score))

Inside the "retrain.py" code you'll see an argument called '--final_tensor_name'. If you don't pass that argument it will keep 'final_result' or 'Mul' (depending on the version your using) as the default.

The only way to view the input and output names without the actual training output files is to view the graph in TensorBoard of the 'frozen_graph.pb' or in your case the 'retrained_graph.pb' file.

This is a nice way of outputting the required files to view it in TensorBoard. https://gist.github.com/jubjamie/2eec49ca1e4f58c5310d72918d991ef6

Once you run that code and have the output going to your chosen directory, you can start up TensorBoard and view it in Chrome. Viewing the graph helps me alot since I'm a noob in this area.

Setting input layer to Mul works for me. However, it seems to be ignoring my input size settings and doesn't do any magic to resize the image to 299x299 which I guess Mul is expecting. I did this:

set INPUT_WIDTH=194 
set INPUT_HEIGHT=141 
set INPUT_LAYER=Mul 
python -m scripts.label_image --image=%IMAGE% --input_height=%INPUT_HEIGHT% \
--input_width=%INPUT_WIDTH% --graph=%GRAPH% \
--input_layer=%INPUT_LAYER% --output_layer=final_result

and got this:

ValueError: Cannot feed value of shape (1, 141, 194, 3) 
for Tensor 'import/Mul:0', which has shape '(1, 299, 299, 3)'

And ohhh, looking at the code, input_width and input_height are what to normalize to, not to normalize from. So it's all good. Also I needed to add my labels.

Related