我使用 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)
完整的错误信息为
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])对我无效。
decode_image()
中避免使用形状参数? - Swarooptf.image.decode_jpeg()
已经起作用了。难道tf.image.decode_image()
不应该自动推断出jpeg
格式吗? - Karan Shah