2017-09-15 21 views
2

我有輸入到一個tensorflow convnet作爲秩4張量(32,32,3,73257)(73257來自IMGS的數目),這是numpy的陣列,但我的x輸入的佔位符變量是2-d,(None,3072)。 3072來自img height x img width x channels。我的問題是,如何重塑或使用圖像,以便它們與佔位符兼容?如何使用numpy的數組作爲輸入到Tensorflow CNN無錯配的尺寸

P.S.這些圖像是從SVHN裁剪32×32的數據集

images = np.array(features, dtype=np.float32) 
... 
x = tf.placeholder(tf.float32, shape=[None, 3072]) 
... 
for _ in range(1000): 
    batch = next_batch(50, images, labels) 
    train_step.run(feed_dict={x: batch[0], y_: batch[1]}) 
... 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for i in range(20000): 
    batch = next_batch(50, images, labels) 
    if i % 100 == 0: 
     train_accuracy = accuracy.eval(feed_dict={ 
      x: batch[0], y_: batch[1], keep_prob: 1.0}) 
     print('step %d, training accuracy %g' % (i, train_accuracy)) 
    train_step.run(feed_dict={x: images, y_: labels, keep_prob: 0.5}) 

回答

2

假設你有73257幅圖像32由32像素保持3個頻帶(例如RGB)。你可以做一個

input = tf.transpose(input, [3, 0, 1, 2]) 

使最後維度第一。張量應該看起來像(73257,32,32,3)。

然後做

input = tf.reshape(input, [-1, 3072]) 

減少尺寸。張量應該看起來像(73257,3072)。

+0

明白了!感謝您的幫助 – Meka

+0

請您接受我的回答嗎?謝謝。 – pgross

+0

是的,我的壞!再次感謝 – Meka