在Tensorflow数据集中将目录中的图像加载为数据集

15

我对机器学习和TensorFlow都比较新手。我花了很多时间在TensorFlow MINST教程上,以及https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/how_tos/reading_data,试图找出如何读取自己的数据,但是我有点困惑。

我有一堆图片(.png)在目录/images/0_Non/中。我正在尝试将它们转换成TensorFlow数据集,然后我可以基本上运行MINST教程中的内容作为第一步。

import tensorflow as tf

# Make a queue of file names including all the JPEG images files in the relative image directory.
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("../images/0_Non/*.png"))

image_reader = tf.WholeFileReader()

# Read a whole file from the queue, the first returned value in the tuple is the filename which we are ignoring.
_, image_file = image_reader.read(filename_queue)

image = tf.image.decode_png(image_file)

# Start a new session to show example output.
with tf.Session() as sess:
    # Required to get the filename matching to run.
    tf.initialize_all_variables().run()

    # Coordinate the loading of image files.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    # Get an image tensor and print its value.
    image_tensor = sess.run([image])
    print(image_tensor)

    # Finish off the filename queue coordinator.
    coord.request_stop()
    coord.join(threads)

我有一点难以理解这里发生了什么。所以似乎image是一个张量,而image_tensor是一个numpy数组?

我该如何将我的图像放入数据集中?我还尝试了跟随Iris示例(用于CSV),这让我来到了这里:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/base.py,但不确定如何使其适用于我有大量PNG的情况。

谢谢!


你可以使用type(image)来查找类型。你的数据集格式/组织与MNIST示例有何不同?你能重复使用MNIST示例加载数据的相同代码吗? - Yao Zhang
嗯,MNIST示例看起来数据是以.tar.gz格式提供的?如果我将我的PNG目录制作成.tar.gz格式,这样行得通吗? - Vincent
1个回答

7
最近新增的tf.data API使得这个过程更加容易:
import tensorflow as tf

# Make a Dataset of file names including all the PNG images files in
# the relative image directory.
filename_dataset = tf.data.Dataset.list_files("../images/0_Non/*.png")

# Make a Dataset of image tensors by reading and decoding the files.
image_dataset = filename_dataset.map(lambda x: tf.decode_png(tf.read_file(x)))

# NOTE: You can add additional transformations, like 
# `image_dataset.batch(BATCH_SIZE)` or `image_dataset.repeat(NUM_EPOCHS)`
# in here.

iterator = image_dataset.make_one_shot_iterator()
next_image = iterator.get_next()

# Start a new session to show example output.
with tf.Session() as sess:

  try:

    while True:
      # Get an image tensor and print its value.
      image_array = sess.run([next_image])
      print(image_tensor)

  except tf.errors.OutOfRangeError:
    # We have reached the end of `image_dataset`.
    pass

注意,对于训练,您需要从某个地方获取标签。 Dataset.zip() 转换是一种可能的方式,它将 image_dataset 与来自不同源的标签数据集结合在一起。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接