【问题标题】:tensorflow for poets: "The name 'import/input' refers to an Operation not in the graph."用于诗人的 tensorflow:“名称‘导入/输入’指的是不在图中的操作。”
【发布时间】:2018-03-01 17:39:43
【问题描述】:

我正在关注针对诗人的 codelabs tensorflow,训练效果很好,但是当我运行脚本来评估图像时:

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

我收到以下错误:

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

我环顾四周,它与选择输入和输出层有关,脚本 label_image.py 将“输入”和“输出”设置为默认值。我使用的架构是“inception_v3”。

【问题讨论】:

    标签: python machine-learning tensorflow


    【解决方案1】:

    我更改了 ~/scripts/label_image.py 第 77 行,它起作用了:

    来自

    input_layer = "input"
    

    到

    input_layer = "Mul"
    

    【讨论】:

    • 我工作了,但我不得不将“input_height = 224”和“input_width = 224”都更改为 299。你是怎么想出来的?使用“Mul”
    • 也为我工作......但不知道它实际上做了什么。
    • retrain.py的第870行有一个resized_input_layer_name=Mul,就是inception_v3的。这就是为什么我们需要将宽度和高度更改为 299。这就是重新训练的设置。
    • 我也把output_layer="InceptionV3/Predictions/Reshape_1"线上的79改成了output_layer = "final_result"
    【解决方案2】:

    使用 --input_layer 名称作为占位符。它将起作用,因为 retrain.py 脚本已将 input_layer 的默认值设置为“Placeholder”。

        python label_image.py 
              --graph=retrained_graph.pb 
              --labels=retrained_labels.txt 
              --output_layer=final_result 
              --image=testimage654165.jpg 
              --input_layer=Placeholder
    

    【讨论】:

    • 为我工作。谢谢。
    【解决方案3】:

    并非每个人都会遇到此错误。我猜你是否使用了除了 MobileNet 之外的任何其他架构,这个错误就会出现。 在您的 label_image.py 文件中 将值更改为:

    input_height = 299
    input_width = 299
    input_layer = "Mul"
    

    这应该可以解决。

    【讨论】:

      【解决方案4】:

      正如@Mimii 和@Celio 提到的:将~/scripts/label_image.py,在input_layer = "input" 行更改为input_layer = "Mul" AND 更改输入尺寸:input_height = 299 和input_width= 299

      【讨论】:

        【解决方案5】:

        使用这个

        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
        

        【讨论】:

          【解决方案6】:

          您必须在 scripts 文件夹中的 label_image.py 中进行一些更改

          input_height = 299 将 input_height 从 224 更改为 299
          input_width = 299 将 input_width 更改为 299 从 224
          input_mean = 128
          input_std = 128
          input_layer = "Mul" 将 input_layer 更改为 Mul 来自 输入
          output_layer = "final_result"

          输出:

          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)

          更多信息,请参考this page

          【讨论】:

            【解决方案7】:

            您应该添加--output_layer=final_result:0 作为参数。

            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
            

            【讨论】:

            • 您好,感谢您的帮助!将 output_layer 作为参数传递并没有帮助,在尝试将“import/input_layer”用作图中的操作时,我仍然遇到相同的错误。我打印了所有的操作,'import/input_layer' 甚至不存在,最接近的是:,
            【解决方案8】:

            抱歉回复晚了。 我使用重新训练的模型在下面运行 python 脚本。你可以试试这个吗?

            要求: labels.txt 和 output.pb(retrained model) 应该与我的 python scipt 位于同一目录。 将下面的代码另存为 test.py 并将其称为: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))
            

            【讨论】:

              【解决方案9】:

              在“retrain.py”代码中,您会看到一个名为“--final_tensor_name”的参数。如果您不传递该参数,它将保留“final_result”或“Mul”(取决于您使用的版本)作为默认值。

              在没有实际训练输出文件的情况下查看输入和输出名称的唯一方法是查看 TensorBoard 中“frozen_graph.pb”或在您的情况下为“retrained_graph.pb”文件的图表。

              这是输出所需文件以在 TensorBoard 中查看的好方法。 https://gist.github.com/jubjamie/2eec49ca1e4f58c5310d72918d991ef6

              运行该代码并将输出转到您选择的目录后,您可以启动 TensorBoard 并在 Chrome 中查看它。查看图表对我有很大帮助,因为我是这方面的菜鸟。

              【讨论】:

                【解决方案10】:

                或者您可以通过带有选项的命令行运行而不更改代码:

                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
                

                【讨论】:

                  【解决方案11】:

                  将输入层设置为 Mul 对我有用。但是,它似乎忽略了我的输入大小设置,并且没有任何魔法将图像调整为 299x299,我猜这是 Mul 所期望的。我这样做了:

                  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
                  

                  得到了这个:

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

                  还有哦,看代码,input_width 和 input_height 是规范化to,而不是规范化from。所以一切都很好。我还需要添加我的标签。

                  【讨论】:

                    猜你喜欢
                    • 2018-08-28
                    • 2020-05-14
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-03-18
                    • 1970-01-01
                    • 2021-06-06
                    • 1970-01-01
                    相关资源
                    最近更新 更多