TensorFlow ValueError: Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)' TensorFlow 值错误:无法为形状为 (64, 64, 3) 的张量 u'Placeholder:0' 提供值,该张量的形状为 '(?, 64, 64, 3)'。
TensorFlow ValueError: Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)' TensorFlow 值错误:无法为形状为 (64, 64, 3) 的张量 u'Placeholder:0' 提供值,该张量的形状为 '(?, 64, 64, 3)'。
我对TensorFlow和机器学习都不太了解。我试图对两个对象进行分类,一个是杯子,一个是U盘(jpeg图像)。我已经成功训练并导出了model.ckpt模型。现在我正在尝试恢复保存的model.ckpt以进行预测。以下是脚本:
import tensorflow as tf import math import numpy as np from PIL import Image from numpy import array # 图像参数 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)) # 为我们的卷积和全连接层存储权重 with tf.name_scope('weights'): weights = { # 5x5卷积,3个输入通道,每个通道32个输出 'wc1': tf.Variable(tf.random_normal([5, 5, 1 * IMAGE_CHANNELS, 32])), # 5x5卷积,32个输入,64个输出 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), # 5x5卷积,64个输入,128个输出 'wc3': tf.Variable(tf.random_normal([5, 5, 64, 128])), # 5x5卷积,128个输入,256个输出 'wc4': tf.Variable(tf.random_normal([5, 5, 128, 256])), # 全连接,k * k * 256个输入,1024个输出 'wd1': tf.Variable(tf.random_normal([k * k * 256, 1024])), # 1024个输入,2个类别标签(预测) 'out': tf.Variable(tf.random_normal([1024, NUM_CLASSES])) } # 为我们的卷积和全连接层存储偏差 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 "...模型已加载..." 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 '神经网络预测你的图像为', my_classification[0] 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数组的形状?