我正在尝试使用AlexNet中的TensorFlow提供自己的数据。我在训练时使用(227 x 227)rgb图像,而BATCH_SIZE
是50.以下是代码的一部分。我总是在第train_accuracy = accuracy.eval( ... )
行
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()
目前的错误是:
Traceback (most recent call last):
File "tf_alexnet.py", line 294, in <module>
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 <module>
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 __init__
self._traceback = _extract_stack()
答案 0 :(得分:1)
问题是您将batch_size设置为50,但尝试将x重新整形为表单,就像批量大小等于1一样。要解决该问题,请将形状中的1更改为-1,这样可以保留输入的总大小。