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?
EDIT: Elegant way is to use tf.SequenceExample.
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
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()
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