0

I'm trying to read a set of images and labels for batch training, but I keep getting the error:

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.

Here is my very reduced code that reproduces the error:

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)

I only used one image in this example. What is the cause of this error in this simple example?

A related question that didn't help: Tensorflow read images with labels

Community
  • 1
  • 1

1 Answers1

0

If you want evaluate multiple tensors in a session you need to pass them in an array fetches to session.run(fetches).

So change sess.run(ibatch, lbatch) to sess.run([ibatch, lbatch]) see docs for more.

bodokaiser
  • 15,122
  • 22
  • 97
  • 140