0

I have train my data with cnn model and shuffle images. The first convolution layer is defined:

 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)

if I remove the line" tf.summary.image('conv1_img',conv1)", program can run successfully. When I add this line ,the error:

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

happens,why?

1 Answer 1

1

The summary of you define with tf.summary.image is automatically added to a collection of summaries.

You surely run the op summaries = tf.summary.merge_all(key='summaries') to collect all the summaries added to the collection named summaries (the default sumamry collection).

Then, once you run this op within a session with sess.run(summaries), every summary previously defined is executed.

The histogram summaries depend on the model parameters values only, thus they don't need any external data to be computed.

Instead, the tf.summary.image('conv1_img',conv1) draws the output of the conv1 op that needs a placeholder (image) to be computed.

Thus, you should execute the summary op feeding the graph with the image placeholder:

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

A suggestion:

Let the placeholder be a placehoder. With the statement

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

you're

  1. defining image as a placeholder
  2. overwriting the python image variable to a tensorflow operation.

Thus, when you run use the feed_dict parameter to inject into the computational graph a value for the image placeholder, you're in reality, overriding a tensorflow op (and thus you have to feed an already reshaped value to make it work).

Thus, it's better to let the placeholder be a placeholder:

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

and then use it to correctly feed the graph:

sess.run(<your ops>, feed_dict:{image_placeholder: <your image>})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your detailed explanation, it's really helpful. I have solved my problem by "sess.run(summaries, feed_dict{image: <your image here>})."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.