将 TFRecords 转换回 JPEG 图像

Converting TFRecords back into JPEG Images

我有一个包含数百个 TFRecords 的文件。每个 TFRecord 文件包含 1,024 条记录。每条记录包含以下信息:

The Example proto contains the following fields:

image/height: integer, image height in pixels
image/width: integer, image width in pixels
image/colorspace: string, specifying the colorspace, always 'RGB'
image/channels: integer, specifying the number of channels, always 3
image/class/label: integer, specifying the index in a normalized classification layer
image/class/raw: integer, specifying the index in the raw (original) classification layer
image/class/source: integer, specifying the index of the source (creator of the image)
image/class/text: string, specifying the human-readable version of the normalized label
image/format: string, specifying the format, always 'JPEG'
image/filename: string containing the basename of the image file
image/id: integer, specifying the unique id for the image
image/encoded: string, containing JPEG encoded image in RGB colorspace

我将这些 TFRecords 中的每一个都存储在目录路径 /Data/train 中。 python 中是否有更简单的方法将 TFRecord 中的这些图像转换回 JPEG 格式并将它们保存到另一个目录 /data/image。我查看了看起来很痛苦的 TensorFlow 文档以及将 TFRecord 转换为数组的 ,但我 运行 遇到了问题。任何帮助、更正或反馈将不胜感激!谢谢。

我正在处理的数据是 MARCO 图像数据:

https://marco.ccr.buffalo.edu/download

这应该有效:

record_iterator = tf.python_io.tf_record_iterator(path_to_tfrecords_file)

    for string_record in record_iterator:
        example = tf.train.Example()
        example.ParseFromString(string_record)

        image = example.features.feature["encoded"].bytes_list.value[0]

        # save image to file
        # ...

我在查看单个 TFRecord 时使用了它。仍在编写循环以通过多个 TFRecords:

# Read and print data:
sess = tf.InteractiveSession()

# Read TFRecord file
reader = tf.TFRecordReader()
filename_queue = 
tf.train.string_input_producer(['marcoTrainData00001.tfrecord'])
_, serialized_example = reader.read(filename_queue)

# Define features
read_features = {
    'image/height': tf.FixedLenFeature([], dtype=tf.int64),
    'image/width': tf.FixedLenFeature([], dtype=tf.int64),
    'image/colorspace': tf.FixedLenFeature([], dtype=tf.string),
    'image/class/label': tf.FixedLenFeature([], dtype=tf.int64),
    'image/class/raw': tf.FixedLenFeature([], dtype=tf.int64),
    'image/class/source': tf.FixedLenFeature([], dtype=tf.int64),
    'image/class/text': tf.FixedLenFeature([], dtype=tf.string),
    'image/format': tf.FixedLenFeature([], dtype=tf.string),
    'image/filename': tf.FixedLenFeature([], dtype=tf.string),
    'image/id': tf.FixedLenFeature([], dtype=tf.int64),
    'image/encoded': tf.FixedLenFeature([], dtype=tf.string)
}

# Extract features from serialized data
read_data = tf.parse_single_example(serialized=serialized_example,
                                features=read_features)

# Many tf.train functions use tf.train.QueueRunner,
# so we need to start it before we read
tf.train.start_queue_runners(sess)

# Print features
for name, tensor in read_data.items():
    print('{}: {}'.format(name, tensor.eval()))