Tensorflow 图像读取 &展示 [英] Tensorflow image reading & display

查看:42
本文介绍了Tensorflow 图像读取 &展示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆类似于 Cifar10 格式的图像(二进制文件,每个图像size = 96*96*3 字节),一个接一个(STL-10 数据集).我打开的文件有 138MB.

我试着阅读 &检查包含图像的张量的内容以确保读取正确,但是我有两个问题 -

  1. FixedLengthRecordReader 是否加载整个文件,但一次只提供一个输入?由于读取第一个 size 字节应该相对较快.但是,代码运行大约需要两分钟.
  2. 如何以可显示的格式获取实际图像内容,或如何在内部显示它们以验证图像是否被正确读取?我做了sess.run(uint8image),但结果是空的.

代码如下:

 将 tensorflow 导入为 tfdef read_stl10(filename_queue):类 STL10Record(对象):经过结果 = STL10Record()结果.高度 = 96结果.宽度 = 96结果.深度 = 3image_bytes = result.height * result.width * result.depthrecord_bytes = image_bytesreader = tf.FixedLengthRecordReader(record_bytes=record_bytes)result.key, value = reader.read(filename_queue)打印值record_bytes = tf.decode_raw(value, tf.uint8)depth_major = tf.reshape(tf.slice(record_bytes, [0], [image_bytes]),[结果.深度,结果.高度,结果.宽度])result.uint8image = tf.transpose(depth_major, [1, 2, 0])返回结果# 可能是一个黑客,因为我应该提供一个字符串张量filename_queue = tf.train.string_input_producer(['./data/train_X'])图像 = read_stl10(filename_queue)打印 image.uint8image使用 tf.Session() 作为 sess:结果 = sess.run(image.uint8image)打印结果,类型(结果)

输出:

Tensor("ReaderRead:1", shape=TensorShape([]), dtype=string)Tensor("transpose:0", shape=TensorShape([Dimension(96), Dimension(96), Dimension(3)]), dtype=uint8)I tensorflow/core/common_runtime/local_device.cc:25] 本地设备内部操作并行线程:4I tensorflow/core/common_runtime/local_session.cc:45] 本地会话互操作并行线程:4[最后打印的空行]进程完成,退出代码 137

我正在我的 CPU 上运行它,如果有的话.

感谢 Rosa,我找到了纯 TensorFlow 解决方案.显然,在使用string_input_producer 时,为了查看结果,您需要初始化队列运行器.唯一需要添加到上面代码的是下面的第二行:

<代码>...使用 tf.Session() 作为 sess:tf.train.start_queue_runners(sess=sess)...

之后,result中的图片可以用matplotlib.pyplot.imshow(result)显示.我希望这可以帮助别人.如果您有任何其他问题,请随时问我或查看 Rosa 回答中的链接.

解决方案

只是为了给出一个完整的答案:

filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) # 要读取的文件列表阅读器 = tf.WholeFileReader()键,值 = reader.read(filename_queue)my_img = tf.image.decode_png(value) # 根据您的文件使用 png 或 jpg 解码器.init_op = tf.global_variables_initializer()使用 tf.Session() 作为 sess:sess.run(init_op)# 开始填充文件名队列.坐标 = tf.train.Coordinator()线程 = tf.train.start_queue_runners(coord=coord)for i in range(1): #length of your filename listimage = my_img.eval() #here is your image Tensor :)打印(图像.形状)Image.fromarray(np.asarray(image)).show()coord.request_stop()coord.join(线程)

或者,如果您有一个图像目录,您可以通过这个 Github 源文件>

@mttk 和@salvador-dali:我希望这是你所需要的

I've got a bunch of images in a format similar to Cifar10 (binary file, size = 96*96*3 bytes per image), one image after another (STL-10 dataset). The file I'm opening has 138MB.

I tried to read & check the contents of the Tensors containing the images to be sure that the reading is done right, however I have two questions -

  1. Does the FixedLengthRecordReader load the whole file, however just provide inputs one at a time? Since reading the first size bytes should be relatively fast. However, the code takes about two minutes to run.
  2. How to get the actual image contents in a displayable format, or display them internally to validate that the images are read well? I did sess.run(uint8image), however the result is empty.

The code is below:

import tensorflow as tf
def read_stl10(filename_queue):
  class STL10Record(object):
    pass
  result = STL10Record()

  result.height = 96
  result.width = 96
  result.depth = 3
  image_bytes = result.height * result.width * result.depth
  record_bytes = image_bytes

  reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  result.key, value = reader.read(filename_queue)
  print value
  record_bytes = tf.decode_raw(value, tf.uint8)

  depth_major = tf.reshape(tf.slice(record_bytes, [0], [image_bytes]),
                       [result.depth, result.height, result.width])
  result.uint8image = tf.transpose(depth_major, [1, 2, 0])
  return result
# probably a hack since I should've provided a string tensor

filename_queue = tf.train.string_input_producer(['./data/train_X'])
image = read_stl10(filename_queue)

print image.uint8image
with tf.Session() as sess:
  result = sess.run(image.uint8image)
  print result, type(result)

Output:

Tensor("ReaderRead:1", shape=TensorShape([]), dtype=string)
Tensor("transpose:0", shape=TensorShape([Dimension(96), Dimension(96), Dimension(3)]), dtype=uint8)
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
[empty line for last print]
Process finished with exit code 137

I'm running this on my CPU, if that adds anything.

EDIT: I found the pure TensorFlow solution thanks to Rosa. Apparently, when using the string_input_producer, in order to see the results, you need to initialize the queue runners. The only required thing to add to the code above is the second line from below:

...
with tf.Session() as sess:
    tf.train.start_queue_runners(sess=sess)
...

Afterwards, the image in the result can be displayed with matplotlib.pyplot.imshow(result). I hope this helps someone. If you have any further questions, feel free to ask me or check the link in Rosa's answer.

解决方案

Just to give a complete answer:

filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) #  list of files to read

reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)

my_img = tf.image.decode_png(value) # use png or jpg decoder based on your files.

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
  sess.run(init_op)

  # Start populating the filename queue.

  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  for i in range(1): #length of your filename list
    image = my_img.eval() #here is your image Tensor :) 

  print(image.shape)
  Image.fromarray(np.asarray(image)).show()

  coord.request_stop()
  coord.join(threads)

Or if you have a directory of images you can add them all via this Github source file

@mttk and @salvador-dali: I hope it is what you need

这篇关于Tensorflow 图像读取 &amp;展示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆