TFRecords: Write list of tensors to single Example

匿名 (未验证) 提交于 2019-12-03 02:34:02

问题:

I'm extracting features from images using a convolutional neural network. The network in question has three outputs (three output tensors), which differ in size. I want to store the extracted features in TFRecords, one Example for each image:

Example:     image_id: 1     features/fc8: [output1.1, output1.2, output1.3] Example:     image_id: 2     features/fc8: [output2.1, output2.2, output2.3] .... 

How can I achieve this structure using TFRecords?

回答1:

EDIT: Elegant way is to use tf.SequenceExample.

  1. Convert the data using tf.SequenceExample() format

    def make_example(features, image_id):    ex = tf.train.SequenceExample()      ex.context.feature['image_id'].int64_list.value.append(image_id)   fl_features = ex.feature_lists.feature_list['features/fc8']    for feature in features:     fl_features.feature.add().bytes_list.value.append(frame.tostring())   return ex    
  2. Writing to TFRecord

    def _convert_to_tfrecord(output_file, feature_batch, ids_batch):    writer = tf.python_io.TFRecordWriter(output_file)    for features, id in zip(feature_batch, ids_batch):     ex = make_example(features, id)     writer.write(ex.SerializeToString())   writer.close() 
  3. Parsing example

    def parse_example_proto(example_serialized):   context_features = {     'image_id': tf.FixedLenFeature([], dtype=tf.int64)}  sequence_features = {     'features/fc8': tf.FixedLenSequenceFeature([], dtype=tf.string)}   context_parsed, sequence_parsed = tf.parse_single_sequence_example(     serialized=example_serialized,     context_features=context_features,     sequence_features=sequence_features)  return context_parsed['image_id'], sequence_features['features/fc8'] 

Note: The features here are saved in byte_list, you can also save it in float_list.
Another way, is to use tf.parse_single_example() by storing the examples as:

image_id: 1   features/fc8_1: output1.1   features/fc8_2: output1.2   features/fc8_3: output1.3   


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!