Get input (filenames) from tensorflow dataset iterators

*爱你&永不变心* 提交于 2019-12-10 11:26:27

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/54752287/get-input-filenames-from-tensorflow-dataset-iterators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!