2017-01-16 46 views
0

我已经用cnn模型和随机播放图像训练我的数据。第一卷积层被定义:tf.summary.image获取错误InvalidArgumentError

with tf.name_scope("conv1") as scope: 
    image = tf.placeholder(tf.float32, [FLAGS.batch_size, 32, 32, 1]) 
    image = tf.reshape(image, [FLAGS.batch_size, 32, 32, 1]) 
    print(image) 

    w_conv1 = weight_variable([7, 7, 1, 50]) 
    tf.summary.histogram('w_conv1', w_conv1) 
    conv = tf.nn.conv2d(image, w_conv1, [1, 1, 1, 1], padding='SAME') 
    b_conv1 = bias_variable([50]) 
    tf.summary.histogram('b_conv1', b_conv1) 
    conv1 = tf.nn.bias_add(conv, b_conv1) 
    tf.summary.image('conv1_img',conv1)# **this line get the error** 
    print('conv1:') 
    print(conv1) 

如果删除行 “tf.summary.image( 'conv1_img',CONV1)”,程序可以成功地运行。当我加入这一行,错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a v alue for placeholder tensor 'conv1/Placeholder' with dtype float and shape [30,32,32,1]

发生,为什么呢?

回答

0

您用tf.summary.image定义的摘要会自动添加到摘要集合中。

您肯定会运行op summaries = tf.summary.merge_all(key='summaries')以收集添加到名为summaries(默认的sumamry集合)的集合中的所有摘要。

然后,一旦您在与sess.run(summaries)的会话中运行此操作,将执行之前定义的每个汇总。

直方图汇总仅取决于模型参数值,因此它们不需要计算任何外部数据。

而是,tf.summary.image('conv1_img',conv1)绘制需要计算占位符(image)的conv1 op的输出。

因此,你应该执行摘要运算与图像占位符供给图形:

sess.run(summaries, feed_dict{image: <your image here>})

建议:

让占位符成为placehoder。用语句

image = tf.placeholder(tf.float32, [FLAGS.batch_size, 32, 32, 1]) 
image = tf.reshape(image, [FLAGS.batch_size, 32, 32, 1]) 

  1. 定义image作为占位符
  2. 覆盖蟒蛇image变量为tensorflow操作。

因此,当您运行使用feed_dict参数注入的计算图表为图像占位符的值,你在现实中,覆盖一个tensorflow OP(因此你必须已经重塑价值喂让它起作用)。

因此,最好是让占位符是一个占位符:

image_placeholder = tf.placeholder(tf.float32, [FLAGS.batch_size, 32, 32, 1]) 
image = tf.reshape(image_placeholder, [FLAGS.batch_size, 32, 32, 1]) 

,然后用它来正确进图:

sess.run(<your ops>, feed_dict:{image_placeholder: <your image>}) 
+0

感谢您详细的解释,这是真的很有帮助。我已经通过“sess.run(摘要,feed_dict {image:<您的图片在这里}}”解决了我的问题。“ – judyzha