我正在尝试通过 tensorflow 将图像调整为固定大小。但我正在查看如下奇怪的结果。 原始图像-->调整大小的图像。我写的简单代码在这里。只有注释了有问题的行 (resize_images),原始图像才能正确显示。我在 python 3.5 的 virtualenv 和 tensorflow 1.1 的 pycharm 上从 PIP 安装在 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')
Run Code Online (Sandbox Code Playgroud)
/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
Run Code Online (Sandbox Code Playgroud)
小智 6
发生这种情况是因为:
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)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1249 次 |
| 最近记录: |