Tensorflow:加载未知的 TFRecord 数据集

Tensorflow: Load unknown TFRecord dataset

我得到了一个 TFRecord 数据文件 filename = train-00000-of-00001,其中包含未知大小的图像,可能还包含其他信息。我知道我可以使用 dataset = tf.data.TFRecordDataset(filename) 打开数据集。

如何从此文件中提取图像并将其保存为 numpy 数组?

我也不知道TFRecord文件中是否保存了其他信息,例如标签或分辨率。我怎样才能得到这些信息?如何将它们保存为 numpy 数组?

我通常只使用 numpy 数组,对 TFRecord 数据文件不熟悉。

1.) 如何从此文件中提取图像并将其保存为 numpy 数组?

你要找的是这个:

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 数组?

我会在上面提到的 for 循环的基础上构建。因此,对于每个循环,它都会提取您感兴趣的值并将它们附加到 numpy 数组中。如果需要,您可以从这些数组创建一个 pandas 数据框并将其保存为 csv 文件。

但是...

您似乎有多个 tfrecord 文件...tf.data.TFRecordDataset(filename) returns 一个用于训练模型的数据集。

所以在多个 tfrecords 的情况下,您将需要一个双 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

以上代码的来源:

  • Base code
  • 正在转换bytes to PIL.JpegImagePlugin.JpegImageFile
  • PIL.JpegImagePlugin.JpegImageFile to np.array
  • 转换

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')
#------------------------------------------------------
  • 请记住,tfrecords 只是一种存储信息的方式,供张量流模型以高效方式读取。

  • 我使用 PIL 和 IO 从本质上将字节转换为图像。 IO 获取字节并将它们转换为 ,然后 PIL.Image 可以读取

  • 是的,有一种纯粹的tensorflow方法可以做到:tf.image.decode_jpeg
  • 是的,当你比较两个数组时,这两种方法是有区别的
  • 你应该选择哪一个?如果您担心 Tensorflow's github : "The TensorFlow-chosen default for jpeg decoding is IFAST, sacrificing image quality for speed". Credit for this information belongs to this
  • 中所述的准确性,则 Tensorflow 不是您的选择