Tensorflow中的形状误差

时间:2016-05-03 03:01:41

标签: image numpy tensorflow

所以我有一些带有3个颜色通道的32x32图像,所以我将它们展平并制作成形3072.我加载了图像并将它们重新塑造为(1,3072)numpy矩阵但是当网络运行时它将给出以下错误:

Traceback (most recent call last):
  File "/Users/Me/project/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 428, in _do_run
target_list)
tensorflow.python.pywrap_tensorflow.StatusNotOK: Invalid argument: Incompatible shapes: [300] vs. [100]       
[[Node: Equal_1 = Equal[T=DT_INT64,   _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax_2, ArgMax_3)]]

这是加载图片的代码:

name = QtGui.QFileDialog.getOpenFileNames(self, 'Open File')
fname = [str(each) for each in name]
flist = []
dlist = []
for n, val in enumerate(name):
    flist.append(val)
    img = Image.open(flist[n])
    img.load()
    data = np.asarray(img, dtype = "int32")
    print(data.shape)
    data.shape = (1, 3072)
    quack = np.asmatrix(data)
    print(quack)
    dlist.append(quack)
print(dlist)
for n in range(len(dlist)):
    if n==0:
        self.inlist = dlist[n]
    if n>0:
        self.inlist = np.vstack((self.inlist, dlist[n]))

我正在分批进行100次。 该错误似乎来自以下几行:

for i in range(2000):
    if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict={
        x:self.inlist, y_: self.outListm, keep_prob: 1.0})
    print ("step %d, training accuracy %g"%(i, train_accuracy))
    self.progress.setValue(i/199.99)
    train_step.run(feed_dict={x: self.inlist, y_: self.outListm, keep_prob: 0.5})        

这是我从Tensorflow网站获得的网络代码并稍作修改。

W_conv1 = weight_variable([1, 2, 1, 32])
b_conv1 = bias_variable([32])

x_image = tf.reshape(x, [-1,32,32,1])

h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

W_conv2 = weight_variable([1, 2, 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([8 * 8 * 64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*64])

keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

W_fc2 = weight_variable([1024, 512])
b_fc2 = bias_variable([512])

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, "float"))
sess.run(tf.initialize_all_variables())
for i in range(2000):
    if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict={x: self.inlist, y_: self.outListm, keep_prob: 1.0})
    print ("step %d, training accuracy %g"%(i, train_accuracy))
    self.progress.setValue(i/199.99)
    train_step.run(feed_dict={x: self.inlist, y_: self.outListm, keep_prob: 0.5})

0 个答案:

没有答案