Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow:ValueError: 'images' contains no shape

Tags:

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

like image 612
mxl Avatar asked Jul 06 '17 07:07

mxl


2 Answers

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

like image 21
Nagashayan Avatar answered Sep 20 '22 17:09

Nagashayan


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

like image 199
Alireza Akhavan Avatar answered Sep 19 '22 17:09

Alireza Akhavan