TFRecord 输入数据格式
TensorFlow提供了一种统一的方式来存储数据,这个格式就是TFRecord。TFRecord 文件中的数据都是通过tf.train.Example Protocol Buffer 的格式存储的。以下代码给出了tf.train.Example 的定义。
message Examp1e {
Features features = 1;
};
message Features {
map<string, Feature> feature = 1;
};
message Feature {
oneof kind (
BytesList bytes list = 1;
F1oatList f1oat list = 2;
Int64List int64 list = 3;
)
};
从以上代码可以看出tf.train.Example 的数据结构是比较简洁的。tf.train.Example 中包含了一个从属性名称到取值的字典。其中属性名称为一个字符串,属性的取值可以为字符串BytesList,实数列表FloatList或者整数列表Int64List。
将MNIST输入数据转化为TFRecord 的格式
以下代码展示了如何将MNIST数据保存为TFRecord格式。
import tensorflow as tf
from load_mnist import load_mnist
import numpy as np
#加载数据
mnist = load_mnist(filepath="../../MNIST/MNIST_data")
images = mnist.trainDatas[:, :784]
labels = mnist.trainDatas[:, 784:]
num_examples = mnist.trainDatas.shape[0]
pixels = images.shape[1]
#输出TFRecord文件地址
filename = "mnist_train.tfrecords"
#创建一个writer来写TFRecord文件
writer = tf.python_io.TFRecordWriter(path=filename)
#生成整数型的属性。
def _int64_feature(value):
return tf.train.Feature(int64_list = tf.train.Int64List(value=[value]))
#生成字符串型的属性。
def _bytes_feature(value):
return tf.train.Feature(bytes_list = tf.train.BytesList(value=[value]))
for i in range(num_examples): #遍历每一个样本
#将图像转化成一个字符串
image_raw = images[i].tostring()
#讲一个样本(样例)转化为Example Protocol Buffer ,并将所有的信息写入这个数据结构。
#首先定义一个feature字典
feature = {
'pixels': _int64_feature(pixels),
'labels': _int64_feature(np.argmax(labels[i])),
'image_raw': _bytes_feature(image_raw)
}
#然后创建tf.train.Features
features = tf.train.Features(feature=feature)
#最后创建tf.train.Example
example = tf.train.Example(features=features)
#将一个tf.train.Example写入到TFRecord文件中
writer.write(example.SerializeToString())
writer.close()
以上程序可以将MNIST 数据集中所有的训练数据存储到一个TFRecord 文件中。当数据量较大时,也可以将数据写入多个TFRecord 文件。
以下程序给出了如何读取TFRecord文件中的数据。
#创建一个reader来读取TFRecord文件中的样例。
reader = tf.TFRecordReader()
#创建一个队列来维护输入文件列表
filename_queue = tf.train.string_input_producer(['./mnist_output.tfrecords'])
#从文件中读出一个样例。也可以使用read_up_to 函数一次性读取多个样例。
_, serialized_example = reader.read(filename_queue)
#解析读入的一个样例。如果需要解析多个样例,可以用parse_example 函数。
# TensorFlow 提供两种不同的属性解析方法。一种是方法是tf.FixedLenFeature ,
#这种方法解析的结果为一个Tensor. 另一种方法是tf.VarLenFeature, 这种方法
#得到的解析结果为SparseTensor ,用于处理稀疏数据。这里解析数据的格式需要和
#上面程序写入数据的格式一致。
features = tf.parse_single_example(serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'pixels': tf.FixedLenFeature([], tf.int64),
'labels':tf.FixedLenFeature([], tf.int64)
})
# tf.decode_raw 可以将字符串解析成图像对应的像素数组。
images = tf.decode_raw(features['image_raw'], tf.uint8)
labels = tf.cast(features['labels'], tf.int32)
pixels = tf.cast(features['pixels'], tf.int32)
session = tf.Session()
#启动多线程处理输入数据
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=session, coord=coord)
#每次运行可以读取TFRecord 文件中的一个样例。当所有样例都读完之后,在此样例中程序
#会在重头读取。
image, label, pixel = session.run([images, labels, pixels])
注意在重新读取之后,为什么image.shape = (6272, )而不是784,而且在8倍降采样之后,等于原图。
TensorFlow 图像处理函数
图像编码处理
一张RGB 色彩模式的图像可以看成一个三维矩阵,矩阵中的每一个数表示了图像上不同位置,不同颜色的亮度。然而图像在存储时并不是直接记录这些矩阵中的数字,而是记录经过压缩编码之后的结果。所以要将一张图像还原成一个三维矩阵,需要解码的过程。TensorFlow 提供了对Jpeg 和png 格式图像的编码/解码函数。以下代码示范了如何使用TensorFlow 中对Jpeg 格式图像的编码/解码函数。