简体   繁体   中英

How to set image shape in Python for Tensorflow prediction?

I'm dealing with the following error:

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

The placeholder is set to: x = tf.placeholder(tf.float32, (None, 32, 32, 3))

And the image (when running print(img1.shape) ) has the output: (32, 32, 3)

How can I update the image to be aligned when running: print(sess.run(correct_prediction, feed_dict={x: img1}))

The placeholder x in your program represents a batch of 32x32 (presumably) RGB images, for which predictions will be computed in a single step. If you want to compute a prediction on a single image—ie an array of shape (32, 32, 3) —you must reshape it to have an additional leading dimension. There are many ways to do this, but np.newaxis is one nice way to do it:

 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}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM