1.) 如何从该文件中提取图像以将其保存为 numpy-array?
你要找的是这个:
record_iterator = tf.python_io.tf_record_iterator(path=filename)
for string_record in record_iterator:
example = tf.train.Example()
example.ParseFromString(string_record)
print(example)
# Exit after 1 iteration as this is purely demonstrative.
break
2.) 我如何获得这些信息?
这里是官方documentation。我强烈建议您阅读文档,因为它逐步介绍了如何提取您正在寻找的值。
基本上,您必须将example 转换为字典。因此,如果我想找出 tfrecord 文件中包含哪些类型的信息,我会这样做(在第一个问题中所述代码的上下文中):dict(example.features.feature).keys()
3.) 如何将它们保存为 numpy-array?
我会建立在上面提到的 for 循环之上。因此,对于每个循环,它都会提取您感兴趣的值并将它们附加到 numpy 数组中。如果需要,您可以从这些数组中创建一个 pandas 数据框并将其保存为 csv 文件。
但是……
您似乎有多个 tfrecord 文件...tf.data.TFRecordDataset(filename) 返回一个用于训练模型的数据集。
因此,如果有多个 tfrecord,您将需要一个双 for 循环。外循环将遍历每个文件。对于该特定文件,内部循环将遍历所有 tf.examples。
编辑:
转换为 np.array()
import tensorflow as tf
from PIL import Image
import io
for string_record in record_iterator:
example = tf.train.Example()
example.ParseFromString(string_record)
print(example)
# Get the values in a dictionary
example_bytes = dict(example.features.feature)['image_raw'].bytes_list.value[0]
image_array = np.array(Image.open(io.BytesIO(example_bytes)))
print(image_array)
break
以上代码的来源:
PIL的官方文档
编辑 2:
import tensorflow as tf
from PIL import Image
import io
import numpy as np
# Load image
cat_in_snow = tf.keras.utils.get_file(path, 'https://storage.googleapis.com/download.tensorflow.org/example_images/320px-Felis_catus-cat_on_snow.jpg')
#------------------------------------------------------Convert to tfrecords
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def image_example(image_string):
feature = {
'image_raw': _bytes_feature(image_string),
}
return tf.train.Example(features=tf.train.Features(feature=feature))
with tf.python_io.TFRecordWriter('images.tfrecords') as writer:
image_string = open(cat_in_snow, 'rb').read()
tf_example = image_example(image_string)
writer.write(tf_example.SerializeToString())
#------------------------------------------------------
#------------------------------------------------------Begin Operation
record_iterator = tf.python_io.tf_record_iterator(path to tfrecord file)
for string_record in record_iterator:
example = tf.train.Example()
example.ParseFromString(string_record)
print(example)
# OPTION 1: convert bytes to arrays using PIL and IO
example_bytes = dict(example.features.feature)['image_raw'].bytes_list.value[0]
PIL_array = np.array(Image.open(io.BytesIO(example_bytes)))
# OPTION 2: convert bytes to arrays using Tensorflow
with tf.Session() as sess:
TF_array = sess.run(tf.image.decode_jpeg(example_bytes, channels=3))
break
#------------------------------------------------------
#------------------------------------------------------Compare results
(PIL_array.flatten() != TF_array.flatten()).sum()
PIL_array == TF_array
PIL_img = Image.fromarray(PIL_array, 'RGB')
PIL_img.save('PIL_IMAGE.jpg')
TF_img = Image.fromarray(TF_array, 'RGB')
TF_img.save('TF_IMAGE.jpg')
#------------------------------------------------------