Tensorflow图像读取和显示

mtt*_*ttk 32 python tensorflow

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

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

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

代码如下:

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)
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

我在我的CPU上运行它,如果它增加了任何东西.

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

...
with tf.Session() as sess:
    tf.train.start_queue_runners(sess=sess)
...
Run Code Online (Sandbox Code Playgroud)

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

Ham*_* MP 42

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

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)
Run Code Online (Sandbox Code Playgroud)

或者如果你有一个图像目录,你可以通过这个Github源文件添加它们

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

  • `Image.fromarray(np.asarray(image)).show()`来自哪里? (8认同)

Ros*_*chi 14

根据文档,您可以解码JPEG/PNG图像.

它应该是这样的:

import tensorflow as tf

filenames = ['/image_dir/img.jpg']
filename_queue = tf.train.string_input_producer(filenames)

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

images = tf.image.decode_jpeg(value, channels=3)
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到更多信息


Sal*_*ali 10

在评论中与您交谈后,我相信您可以使用numpy/scipy来做到这一点.想法是读取numpy3d数组中的图像并将其提供给变量.

from scipy import misc
import tensorflow as tf

img = misc.imread('01.png')
print img.shape    # (32, 32, 3)

img_tf = tf.Variable(img)
print img_tf.get_shape().as_list()  # [32, 32, 3]
Run Code Online (Sandbox Code Playgroud)

然后你可以运行你的图表:

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
im = sess.run(img_tf)
Run Code Online (Sandbox Code Playgroud)

并验证它是否相同:

import matplotlib.pyplot as plt
fig = plt.figure()
fig.add_subplot(1,2,1)
plt.imshow(im)
fig.add_subplot(1,2,2)
plt.imshow(img)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

PS你提到:Since it's supposed to parallelize reading, it seems useful to know..我可以说,在数据分析中很少读取数据是瓶颈.你的大部分时间都花在训练你的模型上.

  • 同意数据读取不是瓶颈,但是我知道应该有一种方法可以通过纯TensorFlow进行,所以我想知道我缺少了什么。除此之外,感谢您的回答,它有所帮助:) (2认同)