2017-09-01 56 views
1

我正在創建tfrecords文件並從tfrecords讀取數據。 tfrecords有兩個功能,車輛和長度。從TFRecords讀取時丟失數據

創建tfrecords:

writer = tf.python_io.TFRecordWriter(filename + '_Squence.tfrecords') 
example = tf.train.Example(features=tf.train.Features(
    feature={ 
     'vehicleid': tf.train.Feature(int64_list=tf.train.Int64List(value=[vehicleid])), 
     'length': tf.train.Feature(int64_list=tf.train.Int64List(value=[length])) 

    })) 
writer.write(example.SerializeToString()) 
writer.close() 

閱讀tfrecords:

filepath = filename + "_Squence.tfrecords" 
filename_queue = tf.train.string_input_producer([filepath]) 
reader = tf.TFRecordReader() 
_, serialized_example = reader.read(filename_queue) # return filename and file 
features = tf.parse_single_example(serialized_example, features={ 
    'vehicleid': tf.FixedLenFeature([], tf.int64), 
    'length': tf.FixedLenFeature([], tf.int64) 
    }) 

vehicleid = tf.cast(features["vehicleid"], tf.int64) 
length = tf.cast(features["length"], tf.int64) 
return vehicleid, length 

但是,當我調試的代碼,我會失去一些數據。 例如,如果我發送此兩個例子

[vehicleid = A,frameid = B], [vehicleid = C,frameid = d]

成tfrecords文件,當我讀出的數據,我會得到這樣的數據

[vehicleid = a,frameid = d]。

我丟失了一些數據。

有人請幫我解決這個問題嗎?非常感謝你。

回答

1

tf.train.string_input_producer([filepath])返回一個隊列。每次使用reader.read(filename_queue)加入時,它都會返回隊列的最後一個元素。如果第二次執行reader.read,它將返回第二個元素。

如果你想達到一個批次的元素,你可以使用tf.train.batch與隊列作爲輸入輸出tf.train.batch.

相關問題