2016-11-04 133 views
15

我是TensorFlow和機器學習的新手。我試圖分類兩個對象一杯和一個pendrive(jpeg圖像)。我已經訓練併成功導出了一個model.ckpt。現在我正在嘗試恢復保存的model.ckpt進行預測。下面是該腳本:TensorFlow ValueError:無法爲Tensor u'Placeholder:0'提供形狀(64,64,3)的形狀'(?,64,64,3)'

import tensorflow as tf 
import math 
import numpy as np 
from PIL import Image 
from numpy import array 


# image parameters 
IMAGE_SIZE = 64 
IMAGE_CHANNELS = 3 
NUM_CLASSES = 2 

def main(): 
    image = np.zeros((64, 64, 3)) 
    img = Image.open('./IMG_0849.JPG') 

    img = img.resize((64, 64)) 
    image = array(img).reshape(64,64,3) 

    k = int(math.ceil(IMAGE_SIZE/2.0/2.0/2.0/2.0)) 
    # Store weights for our convolution and fully-connected layers 
    with tf.name_scope('weights'): 
     weights = { 
      # 5x5 conv, 3 input channel, 32 outputs each 
      'wc1': tf.Variable(tf.random_normal([5, 5, 1 * IMAGE_CHANNELS, 32])), 
      # 5x5 conv, 32 inputs, 64 outputs 
      'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 
      # 5x5 conv, 64 inputs, 128 outputs 
      'wc3': tf.Variable(tf.random_normal([5, 5, 64, 128])), 
      # 5x5 conv, 128 inputs, 256 outputs 
      'wc4': tf.Variable(tf.random_normal([5, 5, 128, 256])), 
      # fully connected, k * k * 256 inputs, 1024 outputs 
      'wd1': tf.Variable(tf.random_normal([k * k * 256, 1024])), 
      # 1024 inputs, 2 class labels (prediction) 
      'out': tf.Variable(tf.random_normal([1024, NUM_CLASSES])) 
     } 

    # Store biases for our convolution and fully-connected layers 
    with tf.name_scope('biases'): 
     biases = { 
      'bc1': tf.Variable(tf.random_normal([32])), 
      'bc2': tf.Variable(tf.random_normal([64])), 
      'bc3': tf.Variable(tf.random_normal([128])), 
      'bc4': tf.Variable(tf.random_normal([256])), 
      'bd1': tf.Variable(tf.random_normal([1024])), 
      'out': tf.Variable(tf.random_normal([NUM_CLASSES])) 
     } 

    saver = tf.train.Saver() 
    with tf.Session() as sess: 
     saver.restore(sess, "./model.ckpt") 
     print "...Model Loaded..." 
     x_ = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE , IMAGE_SIZE , IMAGE_CHANNELS]) 
     y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES]) 
     keep_prob = tf.placeholder(tf.float32) 

     init = tf.initialize_all_variables() 

     sess.run(init) 
     my_classification = sess.run(tf.argmax(y_, 1), feed_dict={x_:image}) 
     print 'Neural Network predicted', my_classification[0], "for your image" 


if __name__ == '__main__': 
    main() 

當我運行的預測,我得到以下錯誤上面的腳本:

ValueError: Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)' 

我在做什麼錯?如何修復numpy數組的形狀?

回答

20

image的形狀爲(64,64,3)

您輸入的佔位符_x的形狀爲(?, 64,64,3)

問題是,您正在爲佔位符提供不同形狀的值。

你必須餵它的值爲(1, 64, 64, 3) =一批1張圖片。

只需將您的image值重新設置爲大小爲1的批次。

​​

P.S:輸入佔位符接受一批圖像的事實意味着您可以並行運行一批圖像的預測。 您可以嘗試讀取多於一個圖像(N張圖像),並使用張量形狀來構建一批N圖像(N, 64,64,3)

+1

可能您的意思是'image = array(img).reshape(1,64,64 ,3)'。 –

+0

您應該使用'np.expand_dims(img,axis = 0)'來添加批次維度 – powder

+0

謝謝。 image = array(img).reshape(1,64,64,3)這工作 – Pragyan93

相關問題