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

标签 python numpy tensorflow deep-learning

我是 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()

当我运行上述脚本进行预测时,出现以下错误:

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

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

最佳答案

image 的形状为 (64,64,3)

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

问题是您为占位符提供了不同形状的值。

您必须为其提供值 (1,64,64,3) = 一批 1 张图像。

只需将您的 image 值 reshape 为大小为 1 的批处理。

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

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

的张量构建一批 N 个图像

关于python - TensorFlow ValueError : Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder : 0', which has shape ' (? , 64, 64, 3)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52024267/

相关文章:

python - 重复 numpy 数组

Python:定义最小外接矩形

java - TensorFlow Lite 在机器学习模型处理数值数据方面有用吗?

Tensorflow lstm 用于情感分析而不是学习。更新

python - 正确的 Keras/TensorFlow 激活函数用于简单的数字输入/输出

python - Selenium - 从 highcharts 获取数据

python - 对列的部分函数进行 Pandas 矢量化

python - 访问剪贴板 Windows (7) 64 位

python - 将具有相同值的元组组合到列表中

python - 在各种场景中迭代循环的最快方法