TFRecord —— tensorflow 下的统一数据存储格式

tensorflow 提供了统一的数据存储格式,即 TFRecord(record 表示记录),以提高程序的可扩展性,当数据来源十分复杂时,仍能有效记录输入数据中的信息。

1. tfrecord 使用流程

比如对于 mnist 训练数据集,我们要将其 label 和像素内容以 TFRecord 的形式写入到本地。

所需 api:

  • tf.python_io.TFRecordWriter(path):writer
  • tf.train.Example() ⇒ 构建一条 record
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data(train_dir='./MNIST_DATA', dtype=tf.uint8, one_hot=True)
labels = mnist.train.labels
images = mnist.train.images

num_images, num_pixels = images.shape
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,需要将Pascal VOC标注格式数据转换成TensorFlow支持的格式,可以使用一些开源工具来完成这个转换,例如`tfrecord-generator`。接着,可以使用TensorFlow的API来加载和训练数据集。 以下是一个简单的示例代码,用于加载和训练Pascal VOC数据集: ```python import tensorflow as tf # 加载tfrecord数据集 def load_dataset(tfrecord_path): dataset = tf.data.TFRecordDataset(tfrecord_path) # 解析tfrecord文件中的数据 feature_description = { 'image': tf.io.FixedLenFeature([], tf.string), 'label': tf.io.FixedLenFeature([], tf.string), 'width': tf.io.FixedLenFeature([], tf.int64), 'height': tf.io.FixedLenFeature([], tf.int64) } def _parse_function(example_proto): parsed_features = tf.io.parse_single_example(example_proto, feature_description) image = tf.io.decode_jpeg(parsed_features['image'], channels=3) label = tf.io.decode_raw(parsed_features['label'], tf.uint8) width = parsed_features['width'] height = parsed_features['height'] return (image, label), (width, height) return dataset.map(_parse_function) # 训练模型 def train_model(dataset): model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(None, None, 3)), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(dataset, epochs=10) # 加载数据集 train_dataset = load_dataset('/path/to/train.tfrecord') test_dataset = load_dataset('/path/to/test.tfrecord') # 训练模型 train_model(train_dataset) ``` 需要注意的是,以上示例代码只是一个简单的例子,需要根据具体的任务和数据集进行修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值