21

我使用 TensorFlow 函数 tf.image.resize_images 来调整图像大小,但在代码中出现此错误:ValueError: 'images' contains no shape。完整代码如下:

# -*- 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.

然后我尝试解决这个问题,但只能找到这样的方法

# -*- 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]) 对我不起作用

4

3 回答 3

39

expand_animations = False作为参数传递很重要:

尝试:

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

以确保您有一个具有 3 维形状的张量。这个问题是由于 gif 格式,因为 decode_gif 返回一个 4-D 数组 [num_frames, height, width, 3] 而其他格式包括 decode_bmp、decode_jpeg 和 decode_png,它们返回 3-D 数组 [height, width, num_channels] .

有关更多信息,请查看相关文档

于 2020-01-28T08:05:13.607 回答
34

我最近遇到了这个问题,

tf.image.decode_image() 

不返回带形状的张量,但我的图像都是 jpeg 格式。

所以我用

 tf.image.decode_jpeg() 

它返回具有形状的张量并解决了问题。注意也有tf.image.decode_png()

更多信息可以在这里找到Tensorflow tf.image.decode_jpeg 文档

于 2018-03-04T23:47:52.440 回答
8
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))
于 2018-04-14T03:33:32.980 回答