TensorFlow ValueError:无法为Tensor u'Placeholder提供形状值(64,64,3):0',其形状为'(?,64,64,3)'

Pra*_*n93 37 python numpy deep-learning tensorflow

我是TensorFlow和机器学习的新手.我试图将两个物体分类为杯子和pendrive(jpeg图像).我成功地训练并导出了一个model.ckpt.现在我正在尝试恢复已保存的model.ckpt以进行预测.这是脚本:

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()
Run Code Online (Sandbox Code Playgroud)

当我运行上面的脚本进行预测时,我收到以下错误:

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

我究竟做错了什么?我如何修复numpy数组的形状?

nes*_*uno 34

image有一个形状(64,64,3).

您的输入占位符_x的形状为(?, 64,64,3).

问题是你正在为占位符提供不同形状的值.

您必须使用值(1, 64, 64, 3)=一批1图像来提供它.

只需将您的image值重塑为大小为1的批处理.

image = array(img).reshape(1, 64,64,3)
Run Code Online (Sandbox Code Playgroud)

PS:输入占位符接受一批图像这一事实意味着您可以并行运行一批图像的预测.您可以尝试读取多个图像(N个图像),然后使用形状的张量构建一批N图像(N, 64,64,3)

  • 您应该使用`np.expand_dims(img,axis = 0)`来添加批量维度 (5认同)
  • 可能您的意思是`image = array(img).reshape(1,64,64,3)`。 (2认同)
  • 谢谢你。image = array(img).reshape(1, 64, 64, 3) 这有效 (2认同)

roc*_*yne 5

粉末的评论可能没有被发现,就像我错过了很多次一样.因此,为了让它更加明显,我将重申他的观点.

有时候使用image = array(img).reshape(a,b,c,d)会重塑形状,但是从经验来看,每次我尝试在操作中使用新维度时,我的内核都会崩溃.最安全的是

np.expand_dims(img,axis = 0)

它每次都很完美.我无法解释原因.此链接有一个很好的解释和有关其用法的示例.