从tensorflow数据集迭代器获取输入(文件名) [英] Get input (filenames) from tensorflow dataset iterators

查看:230
本文介绍了从tensorflow数据集迭代器获取输入(文件名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tensorflow数据集来训练模型.数据集将使用文件名列表在会话期间读取它们,我想将文件名与图像一起获取. 更详细地说,我有这样的东西:

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

我认为此信息可以包含在iterator中,但找不到.

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

推荐答案

您只需要将文件名和图像数据保留在数据集中:

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)

这篇关于从tensorflow数据集迭代器获取输入(文件名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆