Sam Hammamy
Sam Hammamy

Reputation: 11017

Error while feeding images to TensorFlow graph

I am attempting to load some images into a TensorFlow graph which are RGB, however I would like the graph to transform them to grayscale before processing.

x = tf.placeholder(tf.float32, shape=[None, 32, 32, 1], name='x')

gray = tf.image.rgb_to_grayscale(x, name='grayscale')

However I am getting the error

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

I exported the notebook with the error and uploaded it to Github as an md file for completness and brevity.

I realise the error is because the x_batch is in RGB shape.

However I thought TensorFlow would do the conversion automatically.

Since tf.image.rgb_to_grayscale wraps the inputs, shouldn't TF do the grayscaling as part of the session?

Or have I missunderstood how that works?

Upvotes: 0

Views: 931

Answers (2)

Khan
Khan

Reputation: 1498

The error is because you wan to load an RGB image for RGB2grayscale conversion but in placeholder

x = tf.placeholder(tf.float32, shape=[None, 32, 32, 1], name='x')

x is defined to have a single dimension, change 1 to 3, error will be removed.

Upvotes: 0

Olivier Moindrot
Olivier Moindrot

Reputation: 28208

The function tf.image.rgb_to_graycale expects an input tensor with its last dimension having size 3. For instance a batch of images of shape (250, 32, 32, 3) in your case, or it could be a single image of shape (32, 32, 3).

If you want to feed RGB images and immediately process them to grayscale, you can do:

images = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])
gray_images = tf.image.rgb_to_grayscale(images)  # has shape (None, 32, 32, 1)

Upvotes: 3

Related Questions