【问题标题】:Reading from .tfrecord files using tf.data.Dataset使用 tf.data.Dataset 从 .tfrecord 文件中读取
【发布时间】:2021-04-02 15:18:59
【问题描述】:

我想用tf.data.Dataset api 读取this code 生成的数据集。 repo 显示它是这样写的:

def image_to_tfexample(image_data, image_format, height, width, class_id):
  return tf.train.Example(features=tf.train.Features(feature={
      'image/encoded': bytes_feature(image_data),
      'image/format': bytes_feature(image_format),
      'image/class/label': int64_feature(class_id),
      'image/height': int64_feature(height),
      'image/width': int64_feature(width),
  }))

(encoded byte-string, b'png', 32, 32, label) 作为参数。

因此,要读取 .tfrecord 文件,数据格式必须是:

example_fmt = {
    'image/encoded': tf.FixedLenFeature((), tf.string, ""),
    'image/format': tf.FixedLenFeature((), tf.string, ""),
    'image/class/label': tf.FixedLenFeature((), tf.int64, -1),
    'image/height': tf.FixedLenFeature((), tf.int64, -1),
    'image/width': tf.FixedLenFeature((), tf.int64, -1)
}
parsed = tf.parse_single_example(example, example_fmt)
image = tf.decode_raw(parsed['image/encoded'], out_type=tf.uint8)

但它不起作用。读取并生成迭代器后,数据集为空,引发OutOfRangeError: End of sequence

可以在here 找到一个用于复制的简短 python 脚本。我正在努力为这个问题找到确切的文档或示例。

【问题讨论】:

  • 查看更多代码可能会有所帮助。您是否正在调用数据集的 map 函数来解析每个示例?你是在使用 GZIP 解压读取 TFRecord 文件吗?
  • 我目前只是想让数据集正常工作。我链接的代码是我目前正在使用的所有代码。在上面的 repo 中创建 tfrecord 的代码没有使用 GZIP 进行序列化,我还需要它来正确反序列化吗?是的,我正在调用数据集的 map 函数以使用我上面发布的示例格式解析每个示例,但到目前为止无济于事。我现在真的只是在寻找一种方法来正确解析示例......

标签: python tensorflow tensorflow-datasets


【解决方案1】:

我无法测试您的代码,因为我没有 train.tfrecords 文件。这段代码会创建一个空数据集吗?

dataset = tf.data.TFRecordDataset('train.tfrecords')
dataset = dataset.map(parse_fn)
itr = dataset.make_one_shot_iterator()

with tf.Session() as sess:
    while True:
        try:
            print(sess.run(itr.get_next()))
        except tf.errors.OutOfRangeError:
            break

如果这给你一个错误,请告诉我是哪一行产生的。

【讨论】:

  • 这让我找到了解决方案! Dataset.list_files().interleave() 仅应用搜索过滤器,而不是直接打开文件。你的代码直接查询文件,发现文件名错误。默认情况下,数据集的下载和转换脚本将其命名为 train.tfrecord(无 S)。因此我的搜索模式不包括该文件,因此我没有收到任何输入。
【解决方案2】:

这个问题有点老了,但它帮助我阅读和加载标记图像(用 VoTT 标记)以训练 YOLOv4/v3。也许这段代码是另一个可能对某人有所帮助的“示例”:

def load_single_boxed_tfrecord(record):
"""
Loads a single tfrecord with its boundary boxes and corresponding labels, from a single tfrecord.

Args:
    record: as tfrecord (Tensor), as yielded from tf.data.TFRecordDataset
Returns:
    (Tensor of image), (Tensor of labels), (Tensor of: x_top_left, x_lower_right, y_top_left, y_lower_right)

"""
feature = {'image/encoded': tf.io.FixedLenFeature([], tf.string),
           'image/object/class/label': tf.io.VarLenFeature(tf.int64),
           'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
           'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
           'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
           'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
           'image/filename': tf.io.FixedLenFeature([], tf.string),
           'image/width': tf.io.FixedLenFeature([], tf.int64),
           'image/height': tf.io.FixedLenFeature([], tf.int64)
           }

tf_file = tf.io.parse_single_example(record, feature)

tf_img = tf.image.decode_image(tf_file["image/encoded"], channels=COLOR_CHANNELS)
tf_img = tf.image.convert_image_dtype(tf_img, tf.float32)

label = tf.sparse.to_dense(tf_file['image/object/class/label'], default_value=0)
# normalized values:
x1norm = tf.sparse.to_dense(tf_file['image/object/bbox/xmin'], default_value=0)
x2norm = tf.sparse.to_dense(tf_file['image/object/bbox/xmax'], default_value=0)
y1norm = tf.sparse.to_dense(tf_file['image/object/bbox/ymin'], default_value=0)
y2norm = tf.sparse.to_dense(tf_file['image/object/bbox/ymax'], default_value=0)

return tf_img, label, [x1norm, x2norm, y1norm, y2norm]

【讨论】:

    【解决方案3】:

    我仍在学习 TensorFlow 和 tfrecordfile 的使用,所以我不是这些东西的大师,但我发现这个 guide 对我来说很有用,可能对你也有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2018-06-26
      • 2018-03-31
      相关资源
      最近更新 更多