TensorFlow ValueError:无法为形状为((?,64,64,3)'的Tensor 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和机器学习的新手.我正在尝试将两个对象归类为杯子和笔式驱动器(jpeg图像).我已经成功训练并导出了model.ckpt.现在,我正在尝试恢复保存的model.ckpt以进行预测.这是脚本:
I am new to TensorFlow and machine learning. I am trying to classify two objects a cup and a pendrive (jpeg images). I have trained and exported a model.ckpt successfully. Now I am trying to restore the saved model.ckpt for prediction. Here is the script:
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()
运行上述脚本进行预测时,出现以下错误:
When I run the above script for prediction I get the following error:
ValueError: Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)'
我做错了什么?以及如何修复numpy数组的形状?
What am I doing wrong? And how do I fix the shape of numpy array?
推荐答案
image
的形状为(64,64,3)
.
您输入的占位符_x
的形状为(?, 64,64,3)
.
Your input placeholder _x
have a shape of (?, 64,64,3)
.
问题是您要为占位符提供不同形状的值.
The problem is that you're feeding the placeholder with a value of a different shape.
您必须使用(1, 64, 64, 3)
=一批1张图像的值来填充它.
You have to feed it with a value of (1, 64, 64, 3)
= a batch of 1 image.
只需将您的image
值重塑为大小为1的批处理即可.
Just reshape your image
value to a batch with size one.
image = array(img).reshape(1, 64,64,3)
P.S:输入占位符接受一批图像的事实,这意味着您可以并行运行一批图像的谓词.
您可以尝试使用形状为(N, 64,64,3)
P.S: the fact that the input placeholder accepts a batch of images, means that you can run predicions for a batch of images in parallel.
You can try to read more than 1 image (N images) and than build a batch of N image, using a tensor with shape (N, 64,64,3)
这篇关于TensorFlow ValueError:无法为形状为((?,64,64,3)'的Tensor u'Placeholder:0'输入形状(64,64,3)的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!