作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
我尝试在 d3.js 中进行链式转换。为此,我在数组中定义了一组转换,并(尝试)创建一个函数以使用 .each("end", function()) 递归调用它们,以在前一个为完成,但我还没有结果。
我是一名优秀的程序员,十分优秀!