python - tf.image.decode_jpeg() 返回零矩阵

标签 python image tensorflow

我正在使用一个脚本将图像加载到 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/

相关文章:

python - 检查 numpy 数组行是否小于下一个

python - 如何将带有多个字符的定界符的 .text 解析为 pandas df?

html - background-size 与元素有什么关系?

tensorflow - TensorFlow AttributeError : Tensor.op is meaningless when eager execution is enabled

python - 如何在 Keras python 中输入 LSTM 模型?

python - 使用 sqlalchemy.engine.url.URL 为 mssql+pyodbc 构建连接 URL

python - 绕过软件向导并使用默认参数安装

php - 如何更改指定 div 中存在的所有图像的尺寸

javascript - 调整图像大小以完美适合容器

python-3.x - SciKit-Learn 的分解模块中未安装 LatentDirichletAllocation