23

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'.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Celio
  • 231
  • 1
  • 2
  • 4

11 Answers11

15

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

from

input_layer = "input"

to

input_layer = "Mul"
MCheng
  • 1,072
  • 9
  • 9
  • 6
    I worked, but I had to change "input_height = 224" and "input_width = 224" both to 299. How did you figured that out? Using "Mul" – Celio Oct 18 '17 at 17:34
  • Worked for me as well... no idea what it actually does though. – Davidjb Oct 27 '17 at 02:30
  • 1
    There is a resized_input_layer_name=Mul in line 870 of retrain.py, that is for inception_v3. That is why we need to change the width and height into 299. That is the retrained setting. – Fenix Lam Dec 20 '17 at 10:02
  • I also changed `output_layer="InceptionV3/Predictions/Reshape_1"` on line `79` to `output_layer = "final_result"` – wcyn Jan 29 '18 at 09:05
8

Use --input_layer name as Placeholder. It will work because the retrain.py script has set default value of input_layer as "Placeholder".

    python label_image.py 
          --graph=retrained_graph.pb 
          --labels=retrained_labels.txt 
          --output_layer=final_result 
          --image=testimage654165.jpg 
          --input_layer=Placeholder
The YooGle
  • 81
  • 2
  • 2
7

Not everyone is getting this error. I'm guessing if you used any other architecture apart from MobileNet this error turns up. In your label_image.py file change the values to:

input_height = 299
input_width = 299
input_layer = "Mul"

This should solve it.

Umang Mistry
  • 374
  • 2
  • 7
  • 14
2

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

Enayat
  • 3,904
  • 1
  • 33
  • 47
2

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
HimalayanCoder
  • 9,630
  • 6
  • 59
  • 60
2

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

CrookedNoob
  • 100
  • 1
  • 11
0

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
Hakan A.
  • 21
  • 1
  • Hey, thanks for the help! Passing the output_layer as a parameter did not help, I still get the same error while trying to use 'import/input_layer' as a operation in the graph. I printed out all the operations and 'import/input_layer' is not even there, the closest thing is: , – Celio Sep 22 '17 at 16:21
0

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))
Hakan A.
  • 21
  • 1
0

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.

MichaelGee
  • 51
  • 7
0

Or you can run by command lines with the options without changing codes:

python -m scripts.label_image2 --graph=tf_files/retrained_graph.pb -- 
folder_images=../updated_images/testing -- 
labels=tf_files/retrained_labels.txt --input_layer=Mul -- 
input_height=299 --input_width=299
beahacker
  • 1,660
  • 14
  • 10
0

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.

Lars Ericson
  • 1,952
  • 4
  • 32
  • 45