Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get input (filenames) from tensorflow dataset iterators

I am using tensorflow datasets to train a model. A list of filenames is taken by the dataset to read them during the session, and I would like to get the filename together with the image. In more detail, I have something like this:

filenames = tf.constant(["/var/data/image1.jpg", "/var/data/image2.jpg", ...])
labels = tf.constant([0, 37, ...])
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset.shuffle()

def _parse_function(filename, label):
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_jpeg(image_string)
  image_resized = tf.image.resize_images(image_decoded, [28, 28])
  return image_resized, label

dataset = dataset.map(_parse_function)
iterator = dataset.make_one_shot_iterator()
X, Y = iterator.get_next()

sess = tf.Session()
sess.run(iterator.initializer)
while True:
  sess.run(X) #Here I want the element from filenames being used for X

I thought that this information could be included in the iterator, but I could not find it.

like image 920
alvgom Avatar asked Oct 15 '25 03:10

alvgom


1 Answers

You just need to keep the filename along with the image data in the dataset:

filenames = tf.constant(["/var/data/image1.jpg", "/var/data/image2.jpg", ...])
labels = tf.constant([0, 37, ...])
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset.shuffle()

def _parse_function(filename, label):
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_jpeg(image_string)
  image_resized = tf.image.resize_images(image_decoded, [28, 28])
  return filename, image_resized, label

dataset = dataset.map(_parse_function)
iterator = dataset.make_one_shot_iterator()
F, X, Y = iterator.get_next()

sess = tf.Session()
sess.run(iterator.initializer)
while True:
  sess.run(F, X)
like image 140
jdehesa Avatar answered Oct 17 '25 18:10

jdehesa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!