Tensorflow:计算 TFRecord 文件中示例的数量——不使用已弃用的 `tf.python_io.tf_record_iterator`

标签 tensorflow tfrecord

请在标记重复之前阅读帖子 :

我一直在寻找一种有效的方法来计算 TFRecord 图像文件中的示例数量。由于 TFRecord 文件不保存有关文件本身的任何元数据,因此用户必须遍历文件才能计算此信息。

StackOverflow 上有几个不同的问题可以回答这个问题。 问题是他们似乎都使用了弃用的 tf.python_io.tf_record_iterator 命令,所以这不是一个稳定的解决方案。 以下是现有帖子的示例:

Obtaining total number of records from .tfrecords file in Tensorflow

Number of examples in each tfrecord

Number of examples in each tfrecord

所以我想知道是否有一种方法可以使用新的 Dataset API 来计算记录数。

最佳答案

有一个 reduce Dataset 下列出的方法类(class)。他们给出了使用以下方法计算记录的示例:

# generate the dataset (batch size and repeat must be 1, maybe avoid dataset manipulation like map and shard)
ds = tf.data.Dataset.range(5) 
# count the examples by reduce
cnt = ds.reduce(np.int64(0), lambda x, _: x + 1)

## produces 5

不知道这个方法是否比@krishnab 的 for 循环快。

关于Tensorflow:计算 TFRecord 文件中示例的数量——不使用已弃用的 `tf.python_io.tf_record_iterator`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55598516/

相关文章:

python - 运行时错误: as_numpy_iterator() is not supported while tracing functions

python - 导入错误 : No module named 'tensorflow.python' with tensorflow-gpu

python - tf.data.dataset.shuffle 的机制是什么?

tensorflow - tf.keras 替换预训练 resnet50 中的下层

pandas - vectorize_sequences 的标准实现

tensorflow - 混洗 tfrecords 文件

python - 如何在 Tensorflow 中高效加入 TFRecords 中的数据

python - 属性错误: 'Tensor' object has no attribute 'numpy' in Tensorflow 2. 1

Tensorflow - 数据集 API 中的字符串处理

Python Tensorflow 创建具有多个数组特征的 tfrecord