TFRecord输入数据样式
tensorflow提供了一种统一的格式来储存数据,这就是TFRecord,TFRecord文件中的数据都是通过tf.train.Example Protacol Buffer的格式来储存的,底层定义中包含了一个从属性名称到取值的字典,比如把一张解码前的图像储存为一个字符串,图像所对应的类别编号存为整数列表。
下面学习了tensorflow怎么吧输入图像数据转化为TFRecord数据
首先设置生成整数型和字符串型的属性的属性:
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]))
定义保存属性,labels,images,pixels:
images = mnist.train.images
labels = mnist.train.labels
pixels = images.shape[1]
将图像矩阵转化为字符串,吧所有信息写入Example Protocol Buffer这个数据结构中,然后吧一个example写入TFRecord文件
def _make_example(pixels, label, image):
image_raw = image.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'pixels': _int64_feature(pixels),
'label': _int64_feature(np.argmax(label)),
'image_raw': _bytes_feature(image_raw)
}))
with tf.python_io.TFRecordWriter("output.tfrecords") as writer:
for index in range(num_examples):
example = _make_example(pixels, labels[index], images[index])
writer.write(example.SerializeToString())
print("TFRecord训练文件已保存。")
以上程序将图像数据储存在TFRecord中,下面学习怎么从TFRecord中读出数据
首先需要创建一个reader读取TFRecord中的样例,并创建一个队列来维护输入文件列表,读文件样例也可以通过read_up_to一次性读取多个样例
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(["output.tfrecords"])
_,serialized_example = reader.read(filename_queue)
解析样例,tensorflow有两种属性解析的方法,一种是用tf.FixedLenFeature,这种方法解析的结果是一个Tensor,另一种方法是通过tf.VarLenFeature,这种方法解析得到的结果是SparseTensor,用于处理稀疏数据。
PS:这里解析数据的格式要和上面程序写入数据的格式一致!!!
然后通过tf.decode_raw吧字符串解析为图像对应的像素数组,关键!
images = tf.decode_raw(features['image_raw'],tf.uint8)
labels = tf.cast(features['label'],tf.int32)
pixels = tf.cast(features['pixels'],tf.int32)
sess = tf.Session()
启动多线程处理输入数据,这一点之后要详细学,这个狠狠很重要!!!
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
for i in range(10):
image, label, pixel = sess.run([images, labels, pixels])
明儿玩去咯!!放几天假!!!