2016-11-07 3 views
1

Ich bin ein wenig neu in Tensorflow und ich versuche, eine Eingabe-Pipeline basierend auf tfrecord-Datei zu erstellen. Jeder Eintrag in der Datei enthält drei Felder: 2 Zeichenfolgen mit Pfaden zu 2 Bilddateien und 1 Float-Tensor (die Bezeichnungen für das Beispiel). Ich bin in der Lage zu schreiben und die Informationen wieder zu lesen, leider habe ich ein Problem damit, Bild und Etiketten synchronisiert zu halten.Tensorflow - Messwerte aus tfrecord synchronisieren

die Datensätze speichern ich dieses Stück Code bin mit

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() 

Und es wieder, dieses zu lesen (für dieses Beispiel Feld ‚image_2‘ in jedem Datensatz Ich ignoriere):

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 

Jedes Paar Bild und Label sind ein Beispiel aus meinem Trainingssatz. Wenn ich versuche, sie in einer einzigen Sitzung auszuführen, bekomme ich kein synchronisiertes Ergebnis, z. In einem Spielzeugbeispiel mit nur zwei Datensätzen in der tfrecord-Datei werden das Bild und das Etikett ausgetauscht: erstes Etikett mit zweitem Bild und umgekehrt.

Beispiel meiner Session-Code:

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) 

Alle Ratschläge, was ich falsch mache?

Antwort

0

Ok dachte ich es herausstellte, war das Problem in

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

die string_input_producer wurde mit dem Rest des piepline vermasselt. Der richtige Weg zum Schreiben von read_and_decode ist

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

Denken Sie daran, Ihre Antwort als akzeptiert zu markieren, wenn Sie das Problem selbst gelöst haben. – nessuno

Verwandte Themen