2017-07-18 8 views
0

テンソルフローのtfrecordsファイルからのFIFOキューの読み取りがあります。各レコードは、イメージとその注釈、つまり一連のフィーチャで構成されています。私はいくつかの画像をスキップしてグラフに表示したり、表示したりしないようにしていました。したがって、最善のケースのシナリオは、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_はデキューされたデータに依存しますが、そうではありません。キューのランナーが開始するまでブロックする必要はありません –

関連する問題