【问题标题】:Tensorflow: You must feed a value for placeholder tensor 'input_image' with dtype floatTensorflow:您必须使用 dtype float 为占位符张量“input_image”提供一个值
【发布时间】:2017-08-04 02:44:11
【问题描述】:

我知道这是一个常见错误,但我无法理解这个问题。这是我的代码:

def convert_image(url):

    checkpoint_file = './vgg_16.ckpt'

    input_tensor = tf.placeholder(tf.float32, shape=(None,224,224,3), name='input_image')
    scaled_input_tensor = tf.scalar_mul((1.0/255), input_tensor)
    scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5)
    scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0)

    #Load the model
    sess = tf.Session()
    arg_scope = vgg_arg_scope()
    with slim.arg_scope(arg_scope):
        logits, end_points = vgg_16(scaled_input_tensor, is_training=False)
    saver = tf.train.Saver()
    saver.restore(sess, checkpoint_file)

    response = requests.get(url)
    img = Image.open(BytesIO(response.content))
    im = np.array(img, dtype='float32')
    im = im.reshape(-1,224,224,3)

    features = sess.run(end_points['vgg_16/fc7'], feed_dict={input_tensor: im})
    sess.close()
    return np.squeeze(features)

如您所见,我使用 VGG_16 预训练模型来提取 fc7 特征。大约 50% 的代码只是简单地从 URL 中获取图像并将其转换为 224x224x3;另外 50% 的 tensorflow 工作是为了实际获得特征表示。

问题是,我第一次运行此代码时它运行良好。但是,第二次,我收到上述错误。当然,“im”是一个 float32,即使我遇到了这个错误。所以我认为这个问题与我第二次运行这个函数时出现的问题有关。如果我不得不猜测,它与“保护程序”的工作方式有关,但我无法弄清楚究竟是什么。

有什么想法吗?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    错误很可能是由于您重新定义了 input_tensor,而不是在 VGG 模型中使用了输入占位符。您可以在将输入图像im 输入网络之前对其应用转换。

    此外,您为每张图片加载模型。 相反,只加载一次模型,然后在循环中遍历图像列表。 像这样的:

    def convert_images(url_list):
       # Load the TF model
       #.....
       # Session, etc.
    
       # Now, go over the list of images one by one
       for url in url_list:
          image = ... # get image
          features = session.run(...) # extract features
    

    【讨论】:

    • 我同意这是更好的编码实践。我实际上会继续重构整个代码,这样我就不必多次加载模型。不过,我真希望我知道我写的代码到底为什么出错了。
    • 我认为,实际的问题是:您重新定义输入(input_tensor),而不是使用模型中已经可用的输入占位符。因此,您应该查看模型中的 input 并将图像提供给它。
    猜你喜欢
    • 2017-11-02
    • 2017-06-10
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 2018-12-29
    • 2019-01-23
    • 2018-12-27
    相关资源
    最近更新 更多