tensorflow for poets: “The name 'import/input&

2020-02-24 12:32发布

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条回答
萌系小妹纸
2楼-- · 2020-02-24 12:49

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

from

input_layer = "input"

to

input_layer = "Mul"
查看更多
The star\"
3楼-- · 2020-02-24 12:56

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
查看更多
叛逆
4楼-- · 2020-02-24 12:59

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))
查看更多
冷血范
5楼-- · 2020-02-24 12:59

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.

查看更多
Evening l夕情丶
6楼-- · 2020-02-24 13:02

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.

查看更多
Fickle 薄情
7楼-- · 2020-02-24 13:08

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

查看更多
登录 后发表回答