【发布时间】: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