【问题标题】:Single Image Inference in Tensorflow [Python]TensorFlow 中的单图像推理 [Python]
【发布时间】:2017-08-15 17:04:18
【问题描述】:

我已经将预训练的 .ckpt 文件转换为 .pb 文件,冻结模型并保存权重。我现在要做的是使用该 .pb 文件进行简单推断,然后提取并保存输出图像。该模型是从这里下载的(用于语义分割的全卷积网络):https://github.com/MarvinTeichmann/KittiSeg。到目前为止,我已经设法加载图像,设置默认 tf 图并导入模型定义的图,读取输入和输出张量并运行会话(此处出错)。

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})
                                print(results)

                                print("Inference done")

                                # Info
                                # First Tensor name : Placeholder:0
                                # Last tensor name  : save/Assign_38:0"

错误是否来自图像格式(例如,我应该将 .png 转换为另一种格式吗?)。这是另一个根本性错误吗?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    我设法修复了错误,下面是在全卷积网络上推断单个图像的工作脚本(适用于对 SEGNET 的替代分割算法感兴趣的人)。该模型使用双线性插值进行缩放,而不是使用非池化层。无论如何,由于模型可以 .chkpt 格式下载,因此您必须先冻结模型并将其保存为 .pb 文件。稍后,您必须从 TF 优化器传递网络以将 Dropout 概率设置为 1。然后,在此脚本中设置正确的输入和输出张量名称,推理正常工作,提取分割图像。

    import tensorflow as tf # Default graph is initialized when the library is imported
    import os
    from tensorflow.python.platform import gfile
    from PIL import Image
    import numpy as np
    import scipy
    from scipy import misc
    import matplotlib.pyplot as plt
    import cv2
    
    with tf.Graph().as_default() as graph: # Set default graph 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-protobuf/FCN8_Freezed.pb",'rb') as f:
    
                                    print("Load Image...")
                                    # Read the image & get statstics
                                    image = scipy.misc.imread('/Path-To-Image/uu_000010.png')
                                    image = image.astype(float)
                                    Input_image_shape=image.shape
                                    height,width,channels = Input_image_shape
    
                                    print("Plot image...")
                                    #scipy.misc.imshow(image)
    
                                    # Set FCN graph to the default graph
                                    graph_def = tf.GraphDef()
                                    graph_def.ParseFromString(f.read())
                                    sess.graph.as_default()
    
                                    # Import a graph_def into the current default Graph (In this case, the weights are (typically) embedded in the graph)
    
                                    tf.import_graph_def(
                                    graph_def,
                                    input_map=None,
                                    return_elements=None,
                                    name="",
                                    op_dict=None,
                                    producer_op_list=None
                                    )
    
                                    # Print the name of operations in the session
                                    for op in 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('Inputs/fifo_queue_Dequeue:0') # Input Tensor
                                    l_output = graph.get_tensor_by_name('upscore32/conv2d_transpose:0') # Output Tensor
    
                                    print "Shape of input : ", tf.shape(l_input)
                                    #initialize_all_variables
                                    tf.global_variables_initializer()
    
                                    # Run Kitty model on single image
                                    Session_out = sess.run( l_output, feed_dict = {l_input : image} 
    

    【讨论】:

      【解决方案2】:

      您是否已经看过demo.py。在141 行显示了他们如何修改图形的输入:

      # Create placeholder for input
      image_pl = tf.placeholder(tf.float32)
      image = tf.expand_dims(image_pl, 0)
      
      # build Tensorflow graph using the model from logdir
      prediction = core.build_inference_graph(hypes, modules,
                                              image=image)
      

      164 行,图像是如何打开的:

      image = scp.misc.imread(input_image)
      

      直接馈送到 image_pl。唯一的一点是 core.build_inference_graph 是一个 TensorVision 调用。

      请注意,提供确切的错误消息作为输入也会很有趣。

      【讨论】:

      • 我想从 protobuf (.pb) 文件中加载模型和权重,因为我想测量原始模型和量化模型的精度差异!如果我设法推断出一张图像并测量两个输出的差异(量化 - 未量化),那么我可以推断出数据库的所有照片并得到一般错误。使用 .pb 文件的原因是 TF 为此提供了量化。如果有更简单的近似值,请告诉我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多