Dimension Error while feeding data in an AlexNet implemented using TensofFlow
<p>I am trying to feed my own data using TensorFlow in an AlexNet. I'm using (227 x 227) rgb images while training and the <code>BATCH_SIZE</code> is 50. The following is a part of the code. I'm always getting an error in the line <code>train_accuracy = accuracy.eval( ... )</code></p>

<pre><code>x = tf.placeholder(tf.float32, shape=[None, 227, 227, 3])
x_image = tf.reshape(x, [1, 227, 227, 3])
y_ = tf.placeholder(tf.float32, shape=[None, 5])


train_image_batch, train_label_batch = tf.train.batch([train_image, train_label], batch_size=BATCH_SIZE)                                              
test_image_batch, test_label_batch = tf.train.batch([test_image, test_label], batch_size=BATCH_SIZE) 


print train_label_batch.get_shape()
print y_.get_shape()
print "input pipeline ready"




cross_entropy = tf.reduce_mean(-tf.reduce_sum(train_y * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(train_y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))




init = tf.initialize_all_variables()

with tf.Session() as sess:

    sess.run(init)

    # initialize the queue threads to start to shovel data
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)


    for i in range(2):
        train_batch_image = sess.run(train_image_batch)
        train_batch_label = sess.run(train_label_batch)

    #if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={x: train_batch_image, y_: train_batch_label, keep_prob: 1.0})
    print("step %d, training accuracy %g"%(i, train_accuracy))
    train_step.run(feed_dict={x: train_batch_image, y_: train_batch_label, keep_prob: 0.5})

    test_batch_image = sess.run(train_image_batch)
    test_batch_label = sess.run(train_label_batch)

    print("test accuracy %g"%accuracy.eval(feed_dict={x: test_batch_image, y_: test_batch_label, keep_prob: 1.0}))



    coord.request_stop()
    coord.join(threads)
    sess.close()
</code></pre>

<p>The current Error is:</p>

<blockquote>
  <p>Traceback (most recent call last):
    File "tf_alexnet.py", line 294, in 
      train_accuracy = accuracy.eval(feed_dict={x: train_batch_image, y_: train_batch_label, keep_prob: 1.0})
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 556, in eval
      return _eval_using_default_session(self, feed_dict, self.graph, session)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3649, in _eval_using_default_session
      return session.run(tensors, feed_dict)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 382, in run
      run_metadata_ptr)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 655, in _run
      feed_dict_string, options, run_metadata)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 723, in _do_run
      target_list, options, run_metadata)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 743, in _do_call
      raise type(e)(node_def, op, message)
  tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 7729350 values, but the requested shape has 154587
       [[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0, Reshape/shape)]]
  Caused by op u'Reshape', defined at:
    File "tf_alexnet.py", line 79, in 
      x_image = tf.reshape(x, [1, 227, 227, 3])
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1750, in reshape
      name=name)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
      op_def=op_def)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2310, in create_op
      original_op=self._default_original_op, op_def=op_def)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1232, in <strong>init</strong>
      self._traceback = _extract_stack()</p>
</blockquote>

<p>The problem is you set batch_size to 50, but trying to reshape x to the form as if you had batch size equal to one. To fix that problem, change 1 in the shape to -1, it would preserve total size of the input.</p>

python machine-learning neural-network tensorflow conv-neural-network