【问题标题】:Tensorflow confusing error reading images with labelsTensorflow混淆错误读取带有标签的图像
【发布时间】:2017-01-12 03:42:23
【问题描述】:

我正在尝试读取一组图像和标签以进行批量训练,但我不断收到错误消息:

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.

这是重现错误的非常简化的代码:

import tensorflow as tf
import numpy as np

image = tf.image.decode_jpeg('C:\\Users\\Alex\\Documents\\Programing\\Python\\cs_dataset\\square.jpeg', channels = 1)
image.set_shape([15, 15, 1])
label = np.array([0, 1])
tf.convert_to_tensor(label)

ibatch, lbatch = tf.train.batch([image, label], batch_size=1)
init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    init_op.run()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    sess.run(ibatch, lbatch)
    coord.request_stop()
    coord.join(threads)

在这个例子中我只使用了一张图片。 在这个简单的例子中,这个错误的原因是什么?

一个没有帮助的相关问题: Tensorflow read images with labels

【问题讨论】:

  • 这个错误发生在哪几行?也应该是sess.run([ibatch, lbatch])
  • @bodokaiser 第 16 行,“sess.run(ibatch, lbatch)”。您的修复成功了,谢谢!
  • @bodokaiser 另外,您可以将其发布为完整回复吗?

标签: tensorflow


【解决方案1】:

如果您想评估 session 中的多个张量,您需要将它们以数组 fetches 的形式传递给 session.run(fetches)

所以将sess.run(ibatch, lbatch) 更改为sess.run([ibatch, lbatch]) 更多信息请参见docs

【讨论】:

    猜你喜欢
    • 2016-03-24
    • 2018-11-06
    • 2017-06-11
    • 2013-10-14
    • 2019-01-13
    • 2020-12-08
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多