gpt4 book ai didi

python - 如何在 Tensorflow 中逐个读取文件?

转载 作者:行者123 更新时间:2023-11-28 21:42:55 24 4
gpt4 key购买 nike

Tensorflow 中有读取文件的函数,但这些函数接受文件名队列。

这意味着,我有义务在读取文件时从文件本身准确地推断出标签。

不幸的是,我在内存中有一个元组列表,其中每个元组由文件名和标签组成。 IE。标签不在文件中,而是在内存中。

是否有可能以某种方式或以某种其他方式创建两个同步队列以从不同来源获取数据和标签?

更新

我写过这样的东西,但是失败了

data = [[os.path.join(corpus_dir, filename), label] for (filename, label) in data]

def read_my_file():

records = tf.train.input_producer(data)
record = records.dequeue()
filename = record[0]
filenames = tf.FIFOQueue(1, tf.string)
filenames.enqueue(filename)
label = record[1]
reader = tf.WholeFileReader()
key, raw = reader.read(filenames)
image = tf.image.decode_png(raw)
return image, label


image, label = read_my_file()

init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

for i in range(10):
image1, label1 = sess.run(image, label)
print(label1)

这里的 data 是 Python 内存中的元组列表,filenames 是我组织的一个队列,用于提供文件读取器。

看起来很糟糕而且不起作用:

...test05.py", line 37, in <module>
image1, label1 = sess.run(image, label)
File "C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 769, in run
run_metadata_ptr)
File "C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line 915, in _run
if feed_dict:
File "C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 525, in __bool__
raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

如你所见,我在任何地方都使用条件反射。

最佳答案

由于您使用的是 tf.WholeFileReader ,您可以通过将其替换为更简单的 tf.read_file() 来避免同步多个队列的问题。 op,如下:

def read_my_file():
records = tf.train.input_producer(data)
filename, label = records.dequeue()
raw = tf.read_file(filename)
image = tf.image.decode_png(raw)
return image, label

关于python - 如何在 Tensorflow 中逐个读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42987099/

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