I'm a new guy in tensorflow,and I'm trying an experiment with my own .tfrecords files.Now I get something wrong in my code and I don't know what happend.Does anybody tell me how can I solve this problem
我是tensorflow的新成员,我正在尝试用我自己的。tfrecords文件做一个实验。现在我的代码出现了问题,我不知道发生了什么。有人告诉我怎么解决这个问题吗?
from color_1 import read_and_decode, get_batch, get_test_batch
import cv2
import os
import time
import numpy as np
import tensorflow as tf
batch_size=50
n_input=56*56*3
n_classes=10
def weight_variable(shape):
initial = tf.truncated_normal(shape=shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
x = tf.placeholder(tf.float32, [None,56,56,3])
y = tf.placeholder(tf.float32, [None,n_classes])
x_image = tf.reshape(x, [-1, 56, 56, 3])
W_conv1 = weight_variable([5, 5, 3, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([14*14*64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 14*14*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = -tf.reduce_sum(y * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
def run():
image, label = read_and_decode('train.tfrecords')
batch_image, batch_label = get_batch(image, label, batch_size, crop_size=56)
test_image, test_label = read_and_decode('val.tfrecords')
test_images, test_labels = get_test_batch(test_image, test_label, batch_size, crop_size=56)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(20000):
image_batch, label_batch = sess.run([batch_image, batch_label])
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: image_batch, y:label_batch,keep_prob:1.0})
print("step %d, training accuracy %g" % (i, train_accuracy))
train_step.run(feed_dict={x:image_batch, y:label_batch,keep_prob:0.5})
print("test accuracy %g" % accuracy.eval(feed_dict={
x:test_images, y:test_labels, keep_prob:1.0}))
coord.request_stop()
coord.join(threads)
if __name__=='__main__':
run()
And the problem is just like this:
问题是这样的:
Traceback (most recent call last):
File "/home/vrview/tensorflow/example/char/tfrecords/LeNet.py", line 130, in <module>
run()
File "/home/vrview/tensorflow/example/char/tfrecords/LeNet.py", line 120, in run
train_accuracy = accuracy.eval(feed_dict={x: image_batch, y:label_batch,keep_prob:1.0})
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 581, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3797, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 944, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (50,) for Tensor u'Placeholder_1:0', which has shape '(?, 10)'
train_accuracy = accuracy.eval(feed_dict={x: image_batch, y:label_batch,keep_prob:1.0})#bug is here
train_accuracy =准确性。eval(feed_dict={x: image_batch, y:label_batch,keep_prob:1.0})#bug在这里。
I don't know how should I do. Please tell me if you know. Thank you very much!
我不知道该怎么做。如果你知道,请告诉我。非常感谢!
1 个解决方案
#1
1
Looks like label_batch on line 120 is has the value of the labels and not the 1-hot encodings. For example, it probably looks like a 1 dimensional array like this [1,3,4,0,6...] when instead it needs to be a 2 dimensional array of 1-hot encodings like this [ [0,1,0,0,0,0,0,0,0,0] , [0,0,0,1,0,0,0,0,0,0] .... ].
在第120行上看起来label_batch具有标签的值,而不是1个热编码。例如,它可能看起来像这样的一维数组[1,3,4,0,6…[[endnoteref: 1]] [[endnoteref: 4]] [[endnoteref: 3]] [[endnoteref: 4]] [[endnoteref: 4]] [[endnoteref: 4]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]]]。
You can use the tf.one_hot function to convert your label_batch into the needed form. To do so change
你可以使用tf。one_hot函数将您的label_batch转换为所需的表单。这样做改变
y = tf.placeholder(tf.float32, [None,n_classes])
to
来
y = tf.placeholder(tf.float32, [None])
y_one_hot = tf.one_hot( y , 10 )
and you'll have to change references to y to instead use y_one_hot
你需要改变对y的引用来代替y_one_hot。
cross_entropy = -tf.reduce_sum(y_one_hot * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Cheers!
干杯!
#1
1
Looks like label_batch on line 120 is has the value of the labels and not the 1-hot encodings. For example, it probably looks like a 1 dimensional array like this [1,3,4,0,6...] when instead it needs to be a 2 dimensional array of 1-hot encodings like this [ [0,1,0,0,0,0,0,0,0,0] , [0,0,0,1,0,0,0,0,0,0] .... ].
在第120行上看起来label_batch具有标签的值,而不是1个热编码。例如,它可能看起来像这样的一维数组[1,3,4,0,6…[[endnoteref: 1]] [[endnoteref: 4]] [[endnoteref: 3]] [[endnoteref: 4]] [[endnoteref: 4]] [[endnoteref: 4]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]] [[endnoteref: 11]]]。
You can use the tf.one_hot function to convert your label_batch into the needed form. To do so change
你可以使用tf。one_hot函数将您的label_batch转换为所需的表单。这样做改变
y = tf.placeholder(tf.float32, [None,n_classes])
to
来
y = tf.placeholder(tf.float32, [None])
y_one_hot = tf.one_hot( y , 10 )
and you'll have to change references to y to instead use y_one_hot
你需要改变对y的引用来代替y_one_hot。
cross_entropy = -tf.reduce_sum(y_one_hot * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Cheers!
干杯!