0

实现的我想在AlexNet使用TensorFlow养活我自己的数据。我在训练期间使用(227 x 227)rgb图像,而BATCH_SIZE为50.以下是代码的一部分。我总是在该行收到错误train_accuracy = accuracy.eval(...)尺寸误差而在AlexNet喂养数据使用TensofFlow

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() 

回答

1

问题是你的batch_size设置为50,而是试图重塑X的形式,如果你批量大小等于1。要解决该问题,请将形状中的1更改为-1,它将保留输入的总大小。

+0

return tf.reshape(tf.nn.bias_add(conv,biases),conv.get_shape()。as_list()) 文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python /ops/gen_array_ops.py“,行1750,在重塑 我现在得到这个错误。 –

+0

我在卷积图层中遇到了一些其他问题,我将其整理出来。谢谢。 –