【问题标题】:tf.image.resize_images() results in random image (full of noise). What's going on?tf.image.resize_images() 产生随机图像(充满噪声)。这是怎么回事?
【发布时间】:2017-10-06 08:47:15
【问题描述】:

我正在尝试通过 tensorflow 将图像大小调整为固定大小。 但我正在查看如下奇怪的结果。 original image --> resized image。 我写的简单代码在这里。只有在有问题的行 (resize_images) 被注释时,原始图像才会正确显示。我在 Ubuntu 16.04 中的 PIP 安装的 python 3.5 和 tensorflow 1.1 的 virtualenv 中的 pycharm 上运行它。

import tensorflow as tf
from PIL import Image

filenames = ['/home/cideep/Work/tensorflow/datasets/VOC-2012/VOC-2012-train/JPEGImages/2007_000032.jpg']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False, num_epochs=1)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value)

# PROBLEM HERE!
resized_image = tf.image.resize_images(image, [200, 200])

init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess = tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)

img = sess.run(resized_image)
print('image shape', img.shape)
img = Image.fromarray(img, "RGB")
img.show('image')

消息如下。

/home/cideep/Work/tensorflow/tfenv/bin/python /home/cideep/Work/tensorflow/mycodes/test_preproc.py
2017-05-08 19:59:54.029800: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-08 19:59:54.029818: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-08 19:59:54.029822: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-05-08 19:59:54.029825: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-08 19:59:54.029827: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-05-08 19:59:54.168591: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-05-08 19:59:54.168997: I tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 with properties: 
name: TITAN X (Pascal)
major: 6 minor: 1 memoryClockRate (GHz) 1.531
pciBusID 0000:01:00.0
Total memory: 11.90GiB
Free memory: 11.43GiB
2017-05-08 19:59:54.169007: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 
2017-05-08 19:59:54.169023: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y 
2017-05-08 19:59:54.169032: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: TITAN X (Pascal), pci bus id: 0000:01:00.0)
image shape (200, 200, 3)

Process finished with exit code 0

【问题讨论】:

  • 在 TF github 上创建一个 bug 并给他们你的图像

标签: tensorflow


【解决方案1】:

发生这种情况是因为:

  • tf.image.decode_jpeg 返回一个 tf.uint8 张量
  • tf.image.resize_image_with_crop_or_pad 不对像素值进行任何数值运算,它只是裁剪或填充以改变输入张量的形状,但返回相同类型的张量。在这种情况下,它是一个 tf.uint8 张量。
  • tf.image.resize_images 返回一个 tf.float32 张量
  • plt.imshow 需要一个 uint8 数组或一个值介于 0 和 1 之间的浮点数组。 由于调整后的张量是一个浮点张量,但像素值不在 0 和 1 之间,它可能会混淆 plt.imgshow。像下面这样的东西应该可以解决问题:

    img = tf.image.decode_jpeg(tf.read_file(path), channels=3)
    
    img = tf.cast(tf.image.resize_images(img, [200, 200]), tf.uint8)
    
    sess.run(img)
    

【讨论】:

    【解决方案2】:

    tf.image.resize_images 期望 4-D 输入张量,因为它期望对一批 3-D(高度、宽度、通道 - 例如红-蓝-绿颜色)图像进行操作。 tf.image.decode_jpeg 返回具有 [height, width, channels] 的 3-D 形状的张量。 tf.image.resize_image_with_crop_or_pad 适当地处理 4-D 或 3-D 输入张量,这就是它起作用的原因。

    【讨论】:

    • 也感谢saeta^^
    【解决方案3】:

    我刚刚遇到了与 tensorflow 类似的问题。尝试了其他不同的调整大小命令,效果最好的一个是

    resize_image_with_crop_or_pad(
        image,
        target_height,
        target_width
    )
    

    希望对您有所帮助。

    (另见https://www.tensorflow.org/api_docs/python/tf/image/resize_image_with_crop_or_pad

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多