I'm relatively new to ML and very much new to TensorfFlow. I've spent quite a bit of time on the TensorFlow MINST tutorial as well as https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/how_tos/reading_data to try and figure out how to read my own data, but I'm getting a bit confused.
I have a bunch of images (.png) in a directory /images/0_Non/. I'm trying to make these into a TensorFlow Data set so then I can basically run the stuff from the MINST tutorial on it as a first pass.
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)
I'm having a bit of trouble understanding what's going on here. So it seems like image
is a tensor and image_tensor
is an numpy array?
How do I get my images into a data set? I also tried following along the Iris example which is for a CSV which brought me to here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/base.py, but wasn't sure how to get this to work for my case where I have a bunch of png's.
Thanks!
The recently added tf.data
API makes it easier to do this:
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
Note that for training you will need to get labels from somewhere. The Dataset.zip()
transformation is a possible way to combine together image_dataset
with a dataset of labels from a different source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With