黑马程序员技术交流社区
标题:
【上海校区】TensorFlow实战框架Chp7--TFRecord样例程序--写入、...
[打印本页]
作者:
不二晨
时间:
2018-7-11 09:31
标题:
【上海校区】TensorFlow实战框架Chp7--TFRecord样例程序--写入、...
TensorFlow实战框架Chp7–TFRecord样例程序–写入、读取
保存TFRecord文件的语法,可参考博客:
https://blog.csdn.net/xierhacker/article/details/72357651
写入
# -*- coding: utf-8 -*-
"""Created on Thu Jul 5 21:17:21 2018@author: muli"""
import
tensorflow
as
tf
from
tensorflow.examples.tutorials.mnist
import
input_data
import
numpy
as
np
# 定义函数转化变量类型。
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]))
# 将数据转化为tf.train.Example格式。
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) }))
return
example
# 读取mnist训练数据。
mnist = input_data.read_data_sets(
"./datasets/MNIST_data"
,dtype=tf.uint8, one_hot=
True
)images = mnist.train.imageslabels = mnist.train.labelspixels = images.shape[
1
]num_examples = mnist.train.num_examples
# 输出包含训练数据的TFRecord文件。
with
tf.python_io.TFRecordWriter(
"./TFRecord/output.tfrecords"
)
as
writer:
for
index
in
range(num_examples): example = _make_example(pixels, labels[index], images[index]) writer.write(example.SerializeToString())print(
"TFRecord训练文件已保存。"
)
# 读取mnist测试数据。
images_test = mnist.test.imageslabels_test = mnist.test.labelspixels_test = images_test.shape[
1
]num_examples_test = mnist.test.num_examples
# 输出包含测试数据的TFRecord文件
with
tf.python_io.TFRecordWriter(
"./TFRecord/output_test.tfrecords"
)
as
writer:
for
index
in
range(num_examples_test): example = _make_example( pixels_test, labels_test[index], images_test[index]) writer.write(example.SerializeToString())print(
"TFRecord测试文件已保存。"
)
读取
# -*- coding: utf-8 -*-
"""Created on Fri Jul 6 10:10:17 2018@author: muli"""
import tensorflow as tf
# 读取文件
reader = tf.TFRecordReader()
# 创建一个队列来维护输入文件列表,里面包含多个文件
filename_queue = tf.train.string_input_producer( [
"./TFRecord/output.tfrecords"
])
# 读取列表中的文件
_,serialized_example = reader.read(filename_queue)
# 解析读取的样例
features = tf.parse_single_example( serialized_example, features={
'image_raw'
:tf.FixedLenFeature([],tf.
string
),
'pixels'
:tf.FixedLenFeature([],tf.
int64
),
'label'
:tf.FixedLenFeature([],tf.
int64
) })
# 数据格式转换
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()
# 声明一个tf.train.Coordinator类来协同多个进程
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]) print(
"正在处理第"
+str(i+
1
)+
"个文件..."
)
【转载】原文地址:
https://blog.csdn.net/mr_muli/article/details/80936793
作者:
吴琼老师
时间:
2018-7-12 16:30
作者:
摩西摩西OvO
时间:
2018-7-19 17:08
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2