【问题标题】:Reading a TFRecord file where features that were used to encode is not known读取用于编码的特征未知的 TFRecord 文件
【发布时间】:2020-08-24 14:06:41
【问题描述】:

我对 TensorFlow 很陌生,这可能是一个非常初学者的问题。我已经看到使用想要使用的功能(例如-'image'、'label')的知识将自定义数据集转换为 TFRecord 文件的示例。在解析这个 TFRecord 文件时,必须事先知道特征(即“图像”、“标签”)才能使用这个数据集。

我的问题是 - 我们如何解析我们事先不知道功能的 TFRecord 文件?假设有人给了我一个 TFRecord 文件,我想用它解码所有相关的功能。

我指的一些例子是:Link 1Link 2

【问题讨论】:

  • 如果您不知道记录中包含哪些数据,您打算如何使用这些记录?您也许可以从记录文件中读取一个示例并在其中列出可用字段及其类型,以便编写代码以正确解析它,这是您想要的吗?
  • 是的,这肯定会有所帮助。至于意图部分,我可以想到一个场景,我只知道 TFRecord 数据集中的一些特征——比如“位置”和“温度”。但也有其他特征,如“湿度”、“海拔”,以及在其中编码的数据集中存在的其他相关特征,我可以在训练过程中使用。
  • 另一种情况是,我向其请求数据集的另一所大学的教授在电子邮件中提到“图像”和“位置”是存在的特征。但实际上存在的特征是“image_var”和“location_var”。但你现在无法知道,因为他可能太忙而无法回复或正在度假。

标签: tensorflow tfrecord


【解决方案1】:

这可能会有所帮助。这是一个遍历记录文件并保存有关功能的可用信息的功能。您可以将其修改为仅查看第一条记录并返回该信息,但根据具体情况,如果可选功能仅存在于某些或具有可变大小的功能中,则查看所有记录可能会很有用。

import tensorflow as tf

def list_record_features(tfrecords_path):
    # Dict of extracted feature information
    features = {}
    # Iterate records
    for rec in tf.data.TFRecordDataset([str(tfrecords_path)]):
        # Get record bytes
        example_bytes = rec.numpy()
        # Parse example protobuf message
        example = tf.train.Example()
        example.ParseFromString(example_bytes)
        # Iterate example features
        for key, value in example.features.feature.items():
            # Kind of data in the feature
            kind = value.WhichOneof('kind')
            # Size of data in the feature
            size = len(getattr(value, kind).value)
            # Check if feature was seen before
            if key in features:
                # Check if values match, use None otherwise
                kind2, size2 = features[key]
                if kind != kind2:
                    kind = None
                if size != size2:
                    size = None
            # Save feature data
            features[key] = (kind, size)
    return features

你可以这样使用它

import tensorflow as tf

tfrecords_path = 'data.tfrecord'
# Make some test records
with tf.io.TFRecordWriter(tfrecords_path) as writer:
    for i in range(10):
        example = tf.train.Example(
            features=tf.train.Features(
                feature={
                    # Fixed length
                    'id': tf.train.Feature(
                        int64_list=tf.train.Int64List(value=[i])),
                    # Variable length
                    'data': tf.train.Feature(
                        float_list=tf.train.FloatList(value=range(i))),
                }))
        writer.write(example.SerializeToString())
# Print extracted feature information
features = list_record_features(tfrecords_path)
print(*features.items(), sep='\n')
# ('id', ('int64_list', 1))
# ('data', ('float_list', None))

【讨论】:

  • 非常感谢。我检查了这个,这是有效的。我会说 TensorFlow 代码比它必须完成的简单任务要复杂得多。涉及的调用太多,初学者很难理解。
  • @SherineBrahma 很高兴它有帮助,如果您认为它解决了您的问题,请考虑接受答案。问题是您尝试以“意外方式”使用 TFRecords,因此您必须按照 example.protofeature.proto 中的规范手动导航 protobuf 数据。你通常不需要在这么低的级别上工作,因为 TF 提供了更简单的函数供常用(如tf.io.parse_single_example)。
  • 是的,我接受了这个答案。但显然 StackOverflow 认为我太逊了,不能这样做。它说我的声誉少于 15 个,因此我接受的答案不会被公开。
  • @SherineBrahma 是的,在获得 15 个代表点之前您不能投票(您快到了 :)),但您始终可以用绿色勾号接受您的问题的答案。这样其他人就可以知道问题已经解决了。
猜你喜欢
  • 2015-03-20
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
相关资源
最近更新 更多