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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With