我正在使用一个脚本将图像加载到 TensorFlow,这显然适合每个人,但当我尝试它时,我最终得到黑色图像(零矩阵)。我用几个图像文件尝试过,它总是为零,当我故意拼错图像位置字符串时,它会报告错误(正如它应该的那样)。返回的图像张量的大小是正确的(256,256,3)
。这是脚本,有人看到错误吗?
file_names = ['/home/marko/Data/train_27.jpg']
filename_queue = tf.train.string_input_producer(file_names)
image_reader = tf.WholeFileReader()
title, image_file = image_reader.read(filename_queue)
image = tf.image.decode_jpeg(image_file,channels=3)
with tf.Session() as sess:
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
image_tensor = sess.run(image)
print(image_tensor)
print(image_tensor.shape)
coord.request_stop()
coord.join(threads)
最佳答案
您的代码是正确的。 我对 Kaggle competition 中的图像遇到了同样的问题
Tensorflow 似乎错误地检测了这些文件的色彩空间,或者图像中编码的色彩空间信息不正确。
Tensorflow 似乎不允许在解码图像时强制执行色彩空间。因此,最简单的方法可能是“修复”图像。
我使用了 ImageMagic 工具包中的“转换”实用程序:
ls train-jpg/ | \
parallel convert -colorspace sRGB train-jpg/{} fixed-train-jpg/{}
关于python - tf.image.decode_jpeg() 返回零矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806965/