Converting TFRecords back into JPEG Images
我有一个包含数百个TFRecords的文件。每个TFRecord文件包含1,024条记录。每个记录都包含以下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
这应该起作用:
1 2 3 4 5 6 7 8 9 10 | 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的工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # 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())) |