최혁두
최혁두

Reputation: 11

tf.image.resize_images() results in random image (full of noise). What's going on?

I am trying to resize images into a fixed size via tensorflow. But I am looking at the weird result as follows. original image --> resized image. The simple code I wrote is here. Only if the problematic line (resize_images) is commented, the original image is shown properly. I ran it on pycharm in virtualenv of python 3.5 and tensorflow 1.1 from PIP install in Ubuntu 16.04.

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')

The messages were given as follows.

/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

Upvotes: 1

Views: 1647

Answers (3)

Clark Wan
Clark Wan

Reputation: 69

this happens because:

  • tf.image.decode_jpeg returns a tf.uint8 tensor
  • tf.image.resize_image_with_crop_or_pad doesn't do any numerical operations on the pixel values, it just crops or pads to change the shape of the input tensor but returns a tensor of the same type. In this case, it's a tf.uint8 tensor.
  • tf.image.resize_images returns a tf.float32 tensor
  • plt.imshow expects either a uint8 array or a float array with values between 0 and 1. Since the resized tensor is a float tensor but pixel values are not between 0 and 1, it's probably confusing plt.imgshow. Something like the following should fix things up:

    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)
    

Upvotes: 7

saeta
saeta

Reputation: 639

tf.image.resize_images expects a 4-D input tensor, as it expects to operate on a batch of 3-D (height, width, channels - e.g. red-blue-green colors) images. tf.image.decode_jpeg returns tensors with a 3-D shape of [height, width, channels]. tf.image.resize_image_with_crop_or_pad appropriately handles 4-D or 3-D input tensors, which is why it works.

Upvotes: 0

user7981577
user7981577

Reputation: 11

I just had a similar problem with tensorflow. Tried different other resize commands, the one working best was

resize_image_with_crop_or_pad(
    image,
    target_height,
    target_width
)

Hope that's helpful.

(see also https://www.tensorflow.org/api_docs/python/tf/image/resize_image_with_crop_or_pad)

Upvotes: 1

Related Questions