身体不执行时的张量流量

时间:2017-07-18 14:52:45

标签: while-loop tensorflow

我在张量流中从tfrecords文件读取FIFO队列。每个记录由图像及其注释组成,即一组特征。根据一些功能,我试图跳过一些图像,而不是将它们输入到图形中,或者不查看它们。因此,我认为最好的情况是在while循环中使用。该循环将测试指定功能的值并决定是否继续。

请查看以下代码:

render

最后,当尝试运行以下代码时,似乎主体没有执行,因此陷入无限循环。而且身体中的import tensorflow as tf import numpy as np num_epoch = 100 tfrecords_filename_seq = ["C:/Users/user/PycharmProjects/AffectiveComputing/P16_db.tfrecords"] filename_queue = tf.train.string_input_producer(tfrecords_filename_seq, num_epochs=num_epoch, shuffle=False, name='queue') reader = tf.TFRecordReader() current_image_confidence = tf.constant(0.0, dtype=tf.float32) def body(i): key, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string), 'annotation_raw': tf.FixedLenFeature([], tf.string) }) # This is how we create one example, that is, extract one example from the database. image = tf.decode_raw(features['image_raw'], tf.uint8) # The height and the weights are used to height = tf.cast(features['height'], tf.int32) width = tf.cast(features['width'], tf.int32) # The image is reshaped since when stored as a binary format, it is flattened. Therefore, we need the # height and the weight to restore the original image back. image = tf.reshape(image, [height, width, 3]) annotation = tf.cast(features['annotation_raw'], tf.string) t1 = tf.string_split([annotation], delimiter=',') t2 = tf.reshape(t1.values, [1, -1]) t3 = tf.string_to_number(t2, out_type=tf.float32) t_ = tf.slice(t3, begin=[0, 3], size=[1, 1]) # Note that t_ is holding a value of 1.0 or 0.0. So its a particular feature I'm interested in. t_ = tf.Print(t_, data=[tf.shape(t_)], message='....') z = tf.cond(t_[0][0] < 1.0, lambda: tf.add(i, 0.0), lambda: tf.add(i, 1.0)) return z cond = lambda i: tf.equal(i, tf.constant(0.0, dtype=tf.float32)) loop = tf.while_loop(cond, body, [current_image_confidence]) init_op = tf.group(tf.local_variables_initializer(), tf.global_variables_initializer()) with tf.Session() as sess: sess.run(init_op) sess.run(loop) 没有被执行。

为什么会这样?

非常感谢任何帮助!!

1 个答案:

答案 0 :(得分:0)

你的程序越来越难,因为你没有启动队列跑步者。在运行循环操作之前运行tf.start_queue_runners()