问题
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