mxl
mxl

Reputation: 269

TensorFlow:ValueError: 'images' contains no shape

I used TensorFlow function tf.image.resize_images to resize my image, but I got this Error in Code: ValueError: 'images' contains no shape. The full code is below:

# -*- coding: utf-8 -*-
import tensorflow as tf
file = ["./1.jpg"]
f = tf.train.string_input_producer(file)
reader = tf.WholeFileReader()
key, img = reader.read(f)

img = tf.image.decode_image(img)
# img.set_shape([218,178,3])
img = tf.image.resize_images(img, [64,64])

coord = tf.train.Coordinator()    
with tf.Session() as sess:
    tf.train.start_queue_runners(coord=coord)
    image = sess.run(img)

The full Error information is

Traceback (most recent call last):
  File "image_read_test.py", line 10, in <module>
    img = tf.image.resize_images(img, [64,64])
  File "C:\Python35\lib\site-packages\tensorflow\python\ops\image_ops_impl.py", line 724, in resize_images
    raise ValueError('\'images\' contains no shape.')
ValueError: 'images' contains no shape.

Then I try to fix this, but only find a way like that

# -*- coding: utf-8 -*-
    import tensorflow as tf
    file = ["./1.jpg"]
    f = tf.train.string_input_producer(file)
    reader = tf.WholeFileReader()
    key, img = reader.read(f)

    img = tf.image.decode_image(img)
    # img.set_shape([218,178])
    # img = tf.image.resize_images(img, [64,64])

    coord = tf.train.Coordinator()    
    with tf.Session() as sess:
        tf.train.start_queue_runners(coord=coord)
        image = sess.run(img)
        image = tf.image.resize_images(image, [64,64])

Only in this way the function can work well, But I don't know why? Is the function tf.image.resize_images only take the numpy array as parameter? Or I can find another way to solve this problem? NB: img.set_shape([218,78,3]) does not work for me

Upvotes: 24

Views: 15809

Answers (4)

glinka
glinka

Reputation: 383

I was feeding the raw image data directly (i.e. my input shape was (batch_size, image_height, image_width, 3) and I was not calling decode_jpeg at any point), and adding

tf.config.run_functions_eagerly(True)

to the relevant Jupyter cell happened to fix the problem for me.

Don't know the root cause in my case, but the TFRecords were generated using a different Tensorflow version than that used to load the model, which I suspect is part of the problem.

Upvotes: 1

Alireza Akhavan
Alireza Akhavan

Reputation: 679

It's important to pass expand_animations = False as an argument:

Try:

tf.image.decode_image(img, expand_animations = False) 

to make sure you have a tensor with a 3-dimensional shape. This problem is due to gif format because decode_gif returns a 4-D array [num_frames, height, width, 3] as opposed to other formats including decode_bmp, decode_jpeg, and decode_png, which return 3-D arrays [height, width, num_channels].

For more information check the related documentation

Upvotes: 46

Alex-zhai
Alex-zhai

Reputation: 311

image_string = tf.read_file(filename)
image_decoded = tf.cond(
      tf.image.is_jpeg(image_string),
      lambda: tf.image.decode_jpeg(image_string, channels=3),
      lambda: tf.image.decode_png(image_string, channels=3))

Upvotes: 8

Nagashayan
Nagashayan

Reputation: 2717

I faced this issue recently,

tf.image.decode_image() 

doesn't return tensor with shape, but my images were all in jpeg format.

So I used

 tf.image.decode_jpeg() 

it returns tensor with the shape and that solved the problem. Note there is tf.image.decode_png() too.

More info can be found here Tensorflow tf.image.decode_jpeg documentation

Upvotes: 36

Related Questions