博客
关于我
【TFRecord】Tensorflow默认标准数据格式
阅读量:727 次
发布时间:2019-03-21

本文共 2483 字,大约阅读时间需要 8 分钟。

Tensorflow默认标准数据格式TFRecord学习

简介

在工程项目中,数据集通常以多种格式存在,为了统一管理,可以选择将数据转换为统一格式。Tensorflow定义的TFRecord格式是一种灵活且高效的数据存储方式。

TFRecord格式特点

  • 二进制文件:TFRecord是一个简单的二进制文件,包含序列化的输入数据。
  • 协议缓冲区(protobuf):数据通过protobuf序列化,确保无论平台还是语言,数据格式一致。
  • 组织结构优化:统一格式减少文件分散存储的可能性,每个实例属性存储于同一文件。

优势

  • 高效处理:数据存储于内存块中,避免了大量文件读取的时间开销。
  • 多线程支持:Tensorflow提供了优化工具,支持通过多线程输入管道高效处理。

数据存储

写入数据

首先,将输入文件转换为TFRecord格式。示例:

来自MNIST图像集的转换:

from __future__ import print_functionimport osimport tensorflow as tffrom tensorflow.contrib.learn.python.learn.datasets import mnistimport numpy as npsave_dir = 'c:/tmp/data'# 数据下载data_sets = mnist.read_data_sets(save_dir, dtype=tf.uint8, reshape=False, validation_size=1000)

将数据写出:

data_splits = ['train', 'test', 'validation']for d in range(len(data_splits)):    print('保存' + data_splits[d])    data_set = data_sets[d]    filename = os.path.join(save_dir, data_splits[d] + '.tfrecords')    writer = tf.python_io.TFRecordWriter(filename)    for index in range(data_set.images.shape[0]):        image = data_set.images[index].tostring()        example = tf.train.Example(            features=tf.train.Features(                feature={                    'height': tf.train.Feature(int64_list=tf.train.Int64List(value=[data_set.images.shape[1]])),                    'width': tf.train.Feature(int64_list=tf.train.Int64List(value=[data_set.images.shape[2]])),                    'depth': tf.train.Feature(int64_list=tf.train.Int64List(value=[data_set.images.shape[3]])),                    'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[int(data_set.labels[index])])),                    'image_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image]))                })        )        writer.write(example.SerializeToString())    writer.close()

读取数据

读取时使用tf.python_io.tf_record_iterator

from tensorflow import python_iofilename = os.path.join(save_dir, 'train.tfrecords')record_iterator = python_io.tf_record_iterator(filename)serialized_img_example = next(record_iterator)

解析数据:

example = tf.train.Example()example.ParseFromString(serialized_img_example)image = example.features.feature['image_raw'].bytes_list.valuelabel = example.features.feature['label'].int64_list.value[0]width = example.features.feature['width'].int64_list.value[0]height = example.features.feature['height'].int64_list.value[0]

恢复图像:

img_flat = np.fromstring(image[0], dtype=np.uint8)img_reshaped = img_flat.reshape((height, width, -1))

总结

Tensorflow的TFRecord格式为数据处理提供了高效的解决方案,无论是写入还是读取数据都得到了充分支持。

转载地址:http://ndigz.baihongyu.com/

你可能感兴趣的文章
Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
查看>>
Netty常见组件二
查看>>
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty核心模块组件
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>