2017-07-18 59 views
0

我有一個FIFO隊列從tensorflow中讀取tfrecords文件。每條記錄都由一個圖像及其註釋組成,即一組特徵。根據某些功能,我試圖跳過一些圖像,而不是將它們送入圖表,或者不查看它們。因此,我認爲最好的情況是在while循環中使用。該循環將測試指定功能的值並決定是否繼續。Tensorflow雖然身體不執行

請看看下面的代碼:

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) 

最後,嘗試運行下面的代碼時,似乎身體不執行,因此停留在一個無限循環。並且體內的tf.Print(...)未被執行。

爲什麼會出現這種情況?

任何幫助非常感謝!

回答

0

你的程序越來越堅持,因爲你沒有啓動隊列跑步者。在運行循環操作之前運行tf.start_queue_runners()

+0

因爲通過在主體部分添加以下代碼行,所以不會出現這種情況:'i = tf.Print(i,data = [i],message ='....')'它會被執行。另一方面,如果我嘗試以下行:'t_ = tf.Print(t_,data = [t_],message ='----')'不執行。 –

+0

t_取決於出隊的數據,但我沒有,這就是爲什麼評估我不必阻塞,直到隊列跑步者開始 –