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

2024-01-06

我对 ML 比较陌生,对 TensorfFlow 也非常陌生。我花了很多时间学习 TensorFlow MINST 教程以及https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/how_tos/reading_data https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/how_tos/reading_data尝试弄清楚如何读取我自己的数据,但我有点困惑。

我在目录 /images/0_Non/ 中有一堆图像 (.png)。我正在尝试将它们放入 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 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/base.py,但不确定如何让它适用于我有一堆 png 的情况。

Thanks!


最近添加的tf.data API https://www.tensorflow.org/programmers_guide/datasets使执行此操作变得更容易:

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具有来自不同来源的标签数据集。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将目录中的图像作为 Tensorflow 数据集加载 的相关文章

随机推荐