TFRecords:将张量列表写入单个示例

时间:2017-06-09 18:48:14

标签: tensorflow

我使用卷积神经网络从图像中提取特征。所讨论的网络具有三个输出(三个输出张量),其大小不同。我想将提取的特征存储在TFRecords中,每个图像都有一个示例:

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

如何使用TFRecords实现此结构?

1 个答案:

答案 0 :(得分:4)

编辑:优雅的方式是使用tf.SequenceExample。

  1. 使用tf.SequenceExample()格式

    转换数据
    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. 写入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. 解析示例

    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']
    
  4. 注意:此处的功能保存在byte_list中,您也可以将其保存在float_list中 另一种方法是通过将示例存储为:

    来使用tf.parse_single_example()
    image_id: 1  
    features/fc8_1: output1.1  
    features/fc8_2: output1.2  
    features/fc8_3: output1.3