2016-11-07 4 views
1

私はtensorflowに少し新しく、tfrecordファイルに基づいて入力パイプラインを作成しようとしています。ファイルの各エントリには、2つのイメージファイルへのパスを持つ2つの文字列と1つの浮動小数点テンソル(例のラベル)の3つのフィールドが含まれています。私は情報をもう一度書き込んで読み返すことができますが、残念ながら画像とラベルを同期させておくのに問題があります。Tensorflow - tfrecordからの読みを同期させよう

:私はコード

writer = tf.python_io.TFRecordWriter(output_tfrecord) 
... 
for index in shuffled_indexes: 
    example = tf.train.Example(
       features=tf.train.Features(
       feature={ 
       'label': tf.train.Feature(float_list=tf.train.FloatList(value=target.ravel().tolist()), 
       'image_1': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_1.encode()])), 
       'image_2': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_2.encode()])) 
       } 
     ) 
    ) 
    writer.write(example.SerializeToString()) 
writer.close() 

のこの部分を使用していますそして再び、このいずれかを(この例では、私は、各レコードのフィールド「IMAGE_2」を無視しています)、それをリードバックするためにレコードを保存するには

def read_and_decode(filename, target_shape): 
# first construct a queue containing a list of filenames. 
# this lets a user split up there dataset in multiple files to keep 
# size down 
filename_queue = tf.train.string_input_producer(filename,num_epochs=None) 

#symbolic reader to read one example at a time 
reader = tf.TFRecordReader() 
_, serialized_example = reader.read(filename_queue) 
features = tf.parse_single_example(
    serialized_example, 
    # Defaults are not specified since both keys are required. 
    features={ 
     'label': tf.FixedLenFeature(target_shape, tf.float32), 
     'image_1': tf.FixedLenFeature([], tf.string), 
     'image_2': tf.FixedLenFeature([], tf.string) 
    } 
) 

img_filename_queue = tf.train.string_input_producer([features['image_1']],shuffle=False) 
image_reader = tf.WholeFileReader() 
_, image_file = image_reader.read(img_filename_queue) 
image = tf.image.decode_jpeg(image_file, channels=3) 
with tf.control_dependencies([image]): 
    label = features['label'] 


return image,label 

それぞれのカップルの画像とラベルは、私のトレーニングセットの例です。単一のセッションでそれらを実行しようとすると、結果は同期されません。 tfrecordファイル内の2つのレコードだけを持つおもちゃの例では、イメージとラベルが交換されます:第1のラベルと第2のイメージ、そしてその逆。私のセッションコードの

例:

image,label = read_and_decode([outputfileName],result_shape) 

with tf.Session() as sess: 
    # Start the queue runners (input threads) 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(sess=sess, coord=coord) 

    for i in range(2): 
     img,trg = sess.run([image,label]) 
     ioUtils.visualizeLabel(img,trg) 

# When done, ask the threads to stop. 
coord.request_stop() 
# Wait for threads to finish. 
coord.join(threads) 

私が間違ってやって上の任意のアドバイスは?

答えて

0

[OK]を私はこの問題は、

img_filename_queue = tf.train.string_input_producer([features['image_1']],shuffle=False) 

にstring_input_producerがpieplineの残りの部分を台無しにした、それを考え出しました。 read_and_decodeを書き込む適切な方法は、

def read_and_decode_tfrecord(filename, target_shape): 
# first construct a queue containing a list of filenames. 
# this lets a user split up there dataset in multiple files to keep 
# size down 
filename_queue = tf.train.string_input_producer(filename,num_epochs=None) 

#symbolic reader to read one example at a time 
reader = tf.TFRecordReader() 
_, serialized_example = reader.read(filename_queue) 
features = tf.parse_single_example(
    serialized_example, 
    # Defaults are not specified since both keys are required. 
    features={ 
     'label': tf.FixedLenFeature(target_shape, tf.float32), 
     'image_1': tf.FixedLenFeature([], tf.string), 
     'image_2': tf.FixedLenFeature([], tf.string) 
    } 
) 

image_file = tf.read_file(image_path_1) 
image = tf.image.decode_jpeg(image_file, channels=3) 
with tf.control_dependencies([image]): 
    label = features['label'] 

return image,label 
+0

です。問題を自分で解決した場合は、覚えておいてください。 – nessuno

関連する問題