使用 tf.data.Dataset 从 .tfrecord 文件读取

Reading from .tfrecord files using tf.data.Dataset

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

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.

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

我无法测试您的代码,因为我没有 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

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

我仍在学习 TensorFlow 和 tfrecordfile 的用法,所以我不是这些东西的专家,但我发现这个 guide 对我的情况很有用,可能对你也有用。

这个问题有点老了,但它帮助我读取和加载标记图像(用 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]