[英]Converting TFRecords back into JPEG Images
I have a file that contains hundreds of TFRecords.我有一个包含数百个 TFRecord 的文件。 Each TFRecord file contains 1,024 records.每个 TFRecord 文件包含 1,024 条记录。 Each record contains this information:每条记录都包含以下信息:
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
I have each of these TFRecords stored in a directory path /Data/train.我将这些 TFRecord 中的每一个都存储在目录路径 /Data/train 中。 Is there a less complex way in python to convert these images within the TFRecord back to JPEG format and save them to another directory /data/image. python 中是否有一种不太复杂的方法将 TFRecord 中的这些图像转换回 JPEG 格式并将它们保存到另一个目录 /data/image.conf ? Ive looked at the TensorFlow docs which seem painful and also this script which converts the TFRecord to an array but I was running into issues.我看过看起来很痛苦的 TensorFlow 文档,还有这个将 TFRecord 转换为数组的脚本,但我遇到了问题。 Any help, corrections, or feedback would be very appreciated!任何帮助、更正或反馈将不胜感激! Thank you.谢谢你。
The data I'm working with is the MARCO image data:我正在使用的数据是 MARCO 图像数据:
https://marco.ccr.buffalo.edu/download https://marco.ccr.buffalo.edu/download
This should work:这应该有效:
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
# ...
I got this to work in viewing a single TFRecord.我让它在查看单个 TFRecord 时起作用。 Still working on writing a loop to get through multiple TFRecords:仍在编写一个循环来处理多个 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()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.