2017-01-10 54 views
0

我想讀一組圖像和標籤批量訓練,但我不斷收到錯誤:Tensorflow令人困惑的錯誤讀取圖像與標籤

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

+1

此錯誤在哪些行發生?也應該是'sess.run([ibatch,lbatch])'。 – bodokaiser

+0

@bodokaiser第16行,「sess.run(ibatch,lbatch)」。您的修復工作,謝謝! –

+1

@bodokaiser另外,你可以發佈,作爲一個完整的答覆? –

回答

0

如果你想評價你需要在一個陣列fetches傳遞他們session.run(fetches)一個session多張量。

因此,將sess.run(ibatch, lbatch)更改爲sess.run([ibatch, lbatch])請參閱docs瞭解更多信息。