TensorFlow:ValueError:'images'不包含任何形状

mxl*_*mxl 9 tensorflow tensorflow-gpu

我使用TensorFlow函数tf.image.resize_images来调整我的图像大小,但我在代码中得到了这个错误:ValueError:'images'不包含任何形状.完整代码如下:

# -*- 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)
Run Code Online (Sandbox Code Playgroud)

完整的错误信息是

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.
Run Code Online (Sandbox Code Playgroud)

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

# -*- 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])
Run Code Online (Sandbox Code Playgroud)

只有这样功能才能正常运行,但我不知道为什么?函数tf.image.resize_images是否只将numpy数组作为参数?或者我可以找到解决这个问题的另一种方法?注意:img.set_shape([218,78,3])对我不起作用

小智 30

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

尝试:

tf.image.decode_image(img, expand_animations = False) 
Run Code Online (Sandbox Code Playgroud)

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

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


Nag*_*yan 16

我最近遇到过这个问题,

tf.image.decode_image() 
Run Code Online (Sandbox Code Playgroud)

没有返回形状的张量,但我的图像都是jpeg格式.

所以我用过

 tf.image.decode_jpeg() 
Run Code Online (Sandbox Code Playgroud)

它返回形状的张量,解决了问题.请注意,还有tf.image.decode_png().

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

  • @alireza Akhavan 在下面回答了您的问题;这是因为该方法还可以读取 gif 图像。有点烦人和神秘,因为在文档中它说它返回一个形状。 (3认同)

小智 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))
Run Code Online (Sandbox Code Playgroud)