Python 将目录中的图像加载为Tensorflow数据集
我对ML比较陌生,对TensorfFlow也很陌生。我花了相当多的时间在TensorFlow MINST教程上,并试图找出如何读取自己的数据,但我有点困惑 我在/images/0\u Non/目录中有一堆图像(.png)。我正试图把它们做成一个TensorFlow数据集,这样我就可以第一次运行MINST教程中的内容Python 将目录中的图像加载为Tensorflow数据集,python,tensorflow,Python,Tensorflow,我对ML比较陌生,对TensorfFlow也很陌生。我花了相当多的时间在TensorFlow MINST教程上,并试图找出如何读取自己的数据,但我有点困惑 我在/images/0\u Non/目录中有一堆图像(.png)。我正试图把它们做成一个TensorFlow数据集,这样我就可以第一次运行MINST教程中的内容 import tensorflow as tf # Make a queue of file names including all the JPEG images files i
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\u张量
是numpy数组
如何将图像放入数据集中?我还尝试了下面的Iris示例,这是一个CSV示例,它将我带到了这里:,但我不确定如何让它在我有一堆png的情况下工作
谢谢 最近添加的功能使此操作更容易:
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\u Dataset
与来自不同来源的标签数据集组合在一起的一种可能方式。您可以使用type(image)查找类型。您的数据集格式/组织与MNIST示例有何不同?你能重复使用MNIST示例加载数据的代码吗?嗯,MNIST示例看起来数据是.tar.gz格式的?如果我只是将png的目录设置为.tar.gz格式,这会起作用吗?