将 TFRecords 和 tf.Examples 转换为常用数据类型

Converting TFRecords and tf.Examples to commonly used data types

我正在学习创建 TensorFlow Extended 管道并发现它们非常有用。 但是,我还没有弄清楚如何调试和测试通过这些管道的(表格)数据。我知道 TensorFlow 使用 TFRecords/tf.Examples,它们是 protobufs.

这些能够通过使用 TFRecordDataset 和 tf.Example 的 parseFromString 人类可读。 尽管如此,这种格式还是难以阅读。

如何实际测试数据?我觉得我需要一个 pandas 数据框。由于我们有 100 多个列和不同的用例,我很难每次都定义所有列,我想这样做。我能以某种方式使用我的架构吗?谢谢!

编辑:我会接受@TheEngineer 的回答,因为它给了我关于如何实现我想要的东西的关键提示。尽管如此,我还是想分享 我的解决方案

免责声明:我使用此代码只是为了测试并查看我的管道中发生了什么。在生产中使用此代码时要小心。可能有更好更安全的方法。

import sys 
import numpy as np
import tensorflow_data_validation as tfdv 

# Our default values for missing values within the tfrecord. We'll restore them later
STR_NA_VALUE = "NA"
INT_NA_VALUE = -sys.maxsize - 1
FLOAT_NA_VALUE = float("nan")

# Create a dict containing FixedLenFeatures using our schema
def load_schema_as_feature_dict(schema_path):
    schema = tfdv.load_schema_text(schema_path)

    def convert_feature(feature):
        if feature.type == 1:
            return tf.io.FixedLenFeature((), tf.string, STR_NA_VALUE)
        if feature.type == 2:
            return tf.io.FixedLenFeature((), tf.int64, INT_NA_VALUE)
        if feature.type == 3:
            return tf.io.FixedLenFeature((), tf.float32, FLOAT_NA_VALUE)
        raise ValueError("Non-implemented type {}".format(feature.type))

    return dict((feature.name, convert_feature(feature)) for feature in schema.feature)  

def as_pandas_frame(tfrecord_path, schema_path):
    feature_dict = load_schema_as_feature_dict(schema_path)
    dataset = tf.data.TFRecordDataset(tfrecord_path, compression_type="GZIP")
    parsed_dataset = dataset.map(lambda serialized_example: tf.io.parse_single_example(serialized_example, feature_dict))
    df = pd.DataFrame(list(parsed_dataset.as_numpy_iterator()))
    
    # Restore NA values from default_values we had to set
    for key, value in {np.object: str.encode(STR_NA_VALUE), np.int64: INT_NA_VALUE, np.float: FLOAT_NA_VALUE}.items():
        type_columns = df.select_dtypes(include=[key]).columns
        df[type_columns] = df[type_columns].replace({value:None})
    
    return df

现在,您只需使用存储的 tfrecords 和 schema.pbtxt 文件调用此函数:

df = as_pandas_frame("path/to/your/tfrecord.gz", "path/to/your/schema.pbtxt")

我不确定你所说的 TFRecordDataset 很难读是什么意思。但她是我如何使用我的 TFRecord 数据的一个例子。 Feature_description 包含 TFRecord 中每个样本所拥有的特征(及其数据类型)一旦您以这种方式加载记录,您就可以用它做各种事情,包括批处理、扩充、管道中的混洗或访问单个文件, 将它们转换为 numpy 等

import tensorflow as tf
import numpy as np
from PIL import Image

filenames = []
for i in range(128):
    name = "./../result/validation-%.5d-of-%.5d" % (i, 128)
    filenames.append(name)

def read_tfrecord(serialized_example):
    feature_description = {
            'image/height': tf.io.FixedLenFeature((), tf.int64),
            'image/width': tf.io.FixedLenFeature((), tf.int64),
            'image/colorspace': tf.io.FixedLenFeature((), tf.string),
            'image/channels': tf.io.FixedLenFeature((), tf.int64),
            'image/class/label': tf.io.FixedLenFeature((), tf.int64),
            'image/encoded': tf.io.FixedLenFeature((), tf.string),
    }

    parsed_features = tf.io.parse_single_example(serialized_example, feature_description)

    parsed_features['image/encoded'] = tf.io.decode_jpeg(
            parsed_features['image/encoded'], channels=3)

    return parsed_features



data = tf.data.TFRecordDataset(filenames)


parsed_dataset = data.shuffle(128).map(read_tfrecord).batch(128)


for sample in parsed_dataset.take(1):
        numpyed = sample['image/encoded'].numpy()
        img = Image.fromarray(numpyed, 'RGB')
        img.show()
        tf.print(sample['image/class/label'])