2016-11-07 1 views
1

저는 tensorflow에 조금 익숙하며 tfrecord 파일을 기반으로 입력 파이프 라인을 만들려고합니다. 파일의 각 항목에는 3 개의 필드가 있습니다. 2 개의 이미지 파일에 대한 경로가있는 2 개의 문자열과 1 개의 플로트 텐서 (예제의 경우 레이블)가 있습니다. 나는 정보를 다시 쓰고 읽을 수 있지만 불행히도 이미지와 레이블을 동기화하는 데 문제가 있습니다.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 파일에 단지 두 개의 레코드가있는 장난감 예제에서 이미지와 레이블이 교환됩니다. 두 번째 이미지의 첫 번째 레이블과 두 번째 이미지의 첫 번째 레이블. 내 세션 코드의

예 :

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

좋아, 문제는 string_input_producer가 piepline의 나머지 엉망했다

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

했다. 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

관련 문제