TensorFlow:ValueError: 'images' 不包含任何形状 [英] TensorFlow:ValueError: 'images' contains no shape
问题描述
我使用 TensorFlow 函数 tf.image.resize_images 来调整我的图像大小,但我在代码中遇到了这个错误:ValueError: 'images' 不包含任何形状.完整代码如下:
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)
完整的错误信息是
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])
只有这样函数才能很好的工作,但是不知道为什么?函数 tf.image.resize_images 是否只将 numpy 数组作为参数?或者我可以找到另一种方法来解决这个问题?注意:img.set_shape([218,78,3]) 对我不起作用
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()
不返回带形状的张量,但我的图像都是 jpeg 格式.
doesn't return tensor with shape, but my images were all in jpeg format.
所以我用了
tf.image.decode_jpeg()
它返回具有形状的张量并解决了问题.请注意,还有 tf.image.decode_png().
it returns tensor with the shape and that solved the problem. Note there is tf.image.decode_png() too.
可以在此处找到更多信息Tensorflow tf.image.decode_jpeg 文档
More info can be found here Tensorflow tf.image.decode_jpeg documentation
这篇关于TensorFlow:ValueError: 'images' 不包含任何形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!