gpt4 book ai didi

python - 从 tensorflow 数据集迭代器获取输入(文件名)

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:12 25 4
gpt4 key购买 nike

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

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中包含这些信息,但是找不到。

最佳答案

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

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)

关于python - 从 tensorflow 数据集迭代器获取输入(文件名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54752287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com