标准TensorFlow格式 TFRecords

时间:2022-07-23
本文章向大家介绍标准TensorFlow格式 TFRecords,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

TFRecords可以允许你讲任意的数据转换为TensorFlow所支持的格式, 这种方法可以使TensorFlow的数据集更容易与网络应用架构相匹配。这种建议的方法就是使用TFRecords文件,TFRecords文件包含了[tf.train.Example 协议内存块(protocol buffer)](协议内存块包含了字段[Features]。你可以写一段代码获取你的数据, 将数据填入到Example协议内存块(protocol buffer),将协议内存块序列化为一个字符串, 并且通过[tf.python_io.TFRecordWriter class]写入到TFRecords文件。

TFRecords文件格式在图像识别中有很好的使用,其可以将二进制数据和标签数据(训练的类别标签)数据存储在同一个文件中,它可以在模型进行训练之前通过预处理步骤将图像转换为TFRecords格式,此格式最大的优点实践每幅输入图像和与之关联的标签放在同一个文件中.TFRecords文件是一种二进制文件,其不对数据进行压缩,所以可以被快速加载到内存中.格式不支持随机访问,因此它适合于大量的数据流,但不适用于快速分片或其他非连续存取。

TFRecordWriter

tf.python_io.TFRecordWriter.write(record)

Write a string record to the file. 将字符记录写到文件中,注意传入的参数是string类型的字符串.

Args:

record: str


TFRecordReader

class tf.TFRecordReader

A Reader that outputs the records from a TFRecords file. 从TFrecords文件中读取记录 See ReaderBase for supported methods.


tf.TFRecordReader.__init__(name=None)

Create a TFRecordReader. 创建一个TFRecordReader

  • name: A name for the operation (optional).

tf.TFRecordReader.num_records_produced(name=None)

Returns the number of records this reader has produced.

This is the same as the number of Read executions that have succeeded. 返回这个阅读器生成的记录的数量。这与已成功执行读取操作的数量相同。

  • name: A name for the operation (optional).

Returns:

An int64 Tensor. 一个int64位张量.


tf.TFRecordReader.num_work_units_completed(name=None)

Returns the number of work units this reader has finished processing. 返回该阅读器完成处理的工作单元的数量。

Args:

  • name: A name for the operation (optional).

Returns:

An int64 Tensor.


tf.TFRecordReader.read(queue, name=None)

Returns the next record (key, value pair) produced by a reader. 返回一个阅读器生成的下一个记录(键值对)。Will dequeue a work unit from queue if necessary (e.g. when the Reader needs to start reading from a new file since it has finished with the previous file). 如果有必要,将从队列中对一个工作单元进行排序(例如,当读者需要从一个新文件开始阅读时,因为它已经完成了前面的文件)。

Args:

  • queue: A Queue or a mutable string Tensor representing a handle to a Queue, with string work items. 文件名队列句柄
  • name: A name for the operation (optional).

Returns:

A tuple of Tensors (key, value).

  • key: A string scalar Tensor.
  • value: A string scalar Tensor. 返回键值对,其中值表示读取的文件

tf.TFRecordReader.reset(name=None)

Restore a reader to its initial clean state. 恢复一个文件阅读器使其置空

Args:

  • name: A name for the operation (optional).

Returns:

The created Operation.


tf.TFRecordReader.restore_state(state, name=None)

Restore a reader to a previously saved state. 恢复阅读器至先前保存的状态. Not all Readers support being restored, so this can produce an Unimplemented error. 并不是所有的阅读器都可以实现恢复的操作,所以这有可能导致一个未实现的错误.

Args:

  • state: A string Tensor. 一个字符串张量 Result of a SerializeState of a Reader with matching type. 一个具有匹配类型的阅读器的串行化的结果。
  • name: A name for the operation (optional).

Returns:

The created Operation.


tf.TFRecordReader.serialize_state(name=None)

Produce a string tensor that encodes the state of a reader. 产生一个字符串张量,它可以对一个阅读器的状态进行编码。

Not all Readers support being serialized, so this can produce an Unimplemented error. 不是所有的阅读器都支持编码,所以这会导致一个未实现的错误.

Args:

  • name: A name for the operation (optional).

Returns:

A string Tensor.


tf.TFRecordReader.supports_serialize

Whether the Reader implementation can serialize its state. 阅读器是否可以实现对当前状态进行编码.