我对ML相对较新,对TensorfFlow来说非常新。我花了很多时间在TensorFlow MINST教程和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,但我不知道如何让我的情况适用于我的情况,我有一堆png&#39 ; S
谢谢!
答案 0 :(得分:5)
最近添加的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
与来自不同来源的标签数据集合并在一起的可能方式。