2017-01-09 56 views
0

我处理以下错误:如何在Python中为Tensorflow预测设置图像形状?

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

占位符设置为:x = tf.placeholder(tf.float32, (None, 32, 32, 3))

而且图像(运行print(img1.shape)时),具有输出:(32, 32, 3)

我怎样才能更新运行时要对齐的图像:print(sess.run(correct_prediction, feed_dict={x: img1}))

+0

重塑IMG(1,32,32,3) –

+0

谢谢!有关如何做到这一点的任何提示? –

+0

img.reshape((1,32,32,3)) –

回答

1

程序中的占位符x代表批次 32x32(推测)RGB图像,其预测将在一个单一的步骤计算。如果要计算单个图像—上的预测,即形状为(32, 32, 3) —的阵列,则必须重新构造它以具有其他主要维度。有很多方法可以做到这一点,但np.newaxis是一个很好的方式做到这一点:

img1 = ...        # Array of shape (32, 32, 3) 
img1_as_batch = img1[np.newaxis, ...] # Array of shape (1, 32, 32, 3) 

print(sess.run(correct_prediction, feed_dict={x: img1_as_batch})) 
相关问题