2017-09-01 59 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.

相关问题