『TensorFlow』第十一弹_队列&多线程&TFRecod文件_我辈当高歌
TF数据读取队列机制详解
一、TFR文件多线程队列读写操作
TFRecod文件写入操作
import tensorflow as tf
def _int64_feature(value):
# value必须是可迭代对象
# 非int的数据使用bytes取代int64即可
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
num_shards = 2
instance_perPshard = 2
for i in range(num_shards):
filename = ('FTR/data.tfrecords-%.5d-of-%.5d' % (i, num_shards))
writer = tf.python_io.TFRecordWriter(filename) #<---------书写器打开
for j in range(instance_perPshard):
example = tf.train.Example(features=tf.train.Features(feature={ #<---------书写入缓冲区
'i':_int64_feature(i),
'j':_int64_feature(j)
}))
writer.write(example.SerializeToString()) #<---------书写入实际文件
writer.close() #<---------书写器关闭
TFRecod文件读取操作
默认多线程,这个默认的多线程过程用于维护文件名队列
'''读取TFR''' files = ["FTR/data.tfrecords-00000-of-00002","FTR/data.tfrecords-00001-of-00002"]
# files = tf.train.match_filenames_once("FTR/data.tfrecords-*") # 输入文件名列表
# 返回QueueRunner & FIFOQueue
# 打乱顺序&加入队列 和 输出队列获取文件 属于单独的线程
filename_queue = tf.train.string_input_producer(files, shuffle=False) #<---------输入文件队列
reader = tf.TFRecordReader() #<---------读取器打开
_,serialized_example = reader.read(filename_queue) #<---------读取原始文件
features = tf.parse_single_example( #<---------读取解析后文件
serialized_example,
features={
'i':tf.FixedLenFeature([],tf.int64),
'j':tf.FixedLenFeature([],tf.int64)
}) with tf.Session() as sess:
tf.global_variables_initializer().run()
coord = tf.train.Coordinator() #<---------多线程
threads = tf.train.start_queue_runners(sess=sess,coord=coord) #<---------文件名队列填充线程启动
for i in range(6):
print(sess.run([features['i'],features['j']])) #<---------实际会话中启动读取过程
coord.request_stop() #<---------多线程
coord.join(threads) #<---------多线程
TFRecod文件打包操作
打包机制:
——————多线程调用前面的节点计算入队
——————批量出队并打包
所以不需要修改解析读取数据过程为循环之类的,可以说很是方便
example_batch, label_batch = tf.train.batch([example, label], #<---------多线程batch生成
batch_size=batch_size,
num_threads=3,
capacity=capacity) example_batch, label_batch = tf.train.shuffle_batch([example, label], #<---------多线程随机batch生成
batch_size=batch_size,
num_threads=3,
capacity=capacity,
min_after_dequeue=30) 由于元素太少随机意义就不大了,所以多了个参数
使用范例,
files = ["FTR/data.tfrecords-00000-of-00002","FTR/data.tfrecords-00001-of-00002"]
# files = tf.train.match_filenames_once("FTR/data.tfrecords-*") # 输入文件名列表
# 返回QueueRunner & FIFOQueue
# 打乱顺序&加入队列 和 输出队列获取文件 属于单独的线程
filename_queue = tf.train.string_input_producer(files, shuffle=False) #<---------输入文件队列
reader = tf.TFRecordReader() #<---------读取
_,serialized_example = reader.read(filename_queue) #<---------读取
features = tf.parse_single_example( #<---------读取
serialized_example,
features={
'i':tf.FixedLenFeature([],tf.int64),
'j':tf.FixedLenFeature([],tf.int64)
}) example, label = features['i'], features['j']
batch_size = 2
capacity = 1000 + 3 * batch_size # 入队单个样例,出队batch
# 可以指定多个线程同时执行入队操作
example_batch, label_batch = tf.train.batch([example, label], #<---------多线程batch生成
batch_size=batch_size,
num_threads=3,
capacity=capacity)
with tf.Session() as sess:
tf.global_variables_initializer().run()
coord = tf.train.Coordinator() #<---------多线程管理器
threads = tf.train.start_queue_runners(sess=sess,coord=coord) #<---------文件名队列填充线程启动
for i in range(3):
cur_example_batch, cur_label_batch = sess.run([example_batch, label_batch])
print(cur_example_batch, cur_label_batch)
coord.request_stop() #<---------多线程关闭
coord.join(threads)
这个输出每一行前为image(代指),后为label,第一行的数据对实际为0-0,0-1:
[0 0] [0 1]
[1 1] [0 1]
[0 0] [0 1]
二、图片文件使用TFR读写测试
read的二进制数据直接进行_bytes_feature化就可以写入文件,使用tf.string类型读出图片数据后可以直接decode解码之(推测tf中string对应二进制数据类型)。
把一张图片写入TFR中:
import tensorflow as tf
import matplotlib.pyplot as plt def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
img_raw = tf.gfile.FastGFile('123123.jpeg','rb').read() filename = ('FTR/image.tfrecords')
writer = tf.python_io.TFRecordWriter(filename) #<---------书写
example = tf.train.Example(features=tf.train.Features(feature={ #<---------书写
'image':_bytes_feature(img_raw),
'label':_int64_feature(1)
}))
writer.write(example.SerializeToString()) #<---------书写
writer.close()
从TFR中读取图片数据并解码绘制出来:
filename_queue = tf.train.string_input_producer(['FTR/image.tfrecords'], shuffle=False) #<---------输入文件队列
reader = tf.TFRecordReader() #<---------读取
_,serialized_example = reader.read(filename_queue) #<---------读取
features = tf.parse_single_example( #<---------读取
serialized_example,
features={
'image':tf.FixedLenFeature([],tf.string),
'label':tf.FixedLenFeature([],tf.int64)
})
img = tf.image.decode_jpeg(features['image'])
with tf.Session() as sess:
tf.global_variables_initializer().run() coord = tf.train.Coordinator() # <---------多线程
threads = tf.train.start_queue_runners(sess=sess, coord=coord) # <---------文件名队列填充线程启动
# img_raw, label = sess.run([features['image'], features['label']])
image = sess.run(img)
plt.imshow(image)
plt.show()
coord.request_stop() # <---------多线程
coord.join(threads) # <---------多线程
三、图片文件直接使用队列读写操作
仅仅示范了维护图片文件名队列的读写,没有过多的其他操作
reader = tf.WholeFileReader():新的读取器,应该是范用性二进制文件读取器
# 导入tensorflow
import tensorflow as tf # 新建一个Session
with tf.Session() as sess:
# 我们要读三幅图片A.jpg, B.jpg, C.jpg
filename = ['123.png', '123123.jpeg']
# string_input_producer会产生一个文件名队列
filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5)
# reader从文件名队列中读数据。对应的方法是reader.read
reader = tf.WholeFileReader() #<---------注意读取器不一样了
key, value = reader.read(filename_queue)
# tf.train.string_input_producer定义了一个epoch变量,要对它进行初始化
tf.local_variables_initializer().run()
# 使用start_queue_runners之后,才会开始填充队列
threads = tf.train.start_queue_runners(sess=sess)
i = 0
while True:
i += 1
# 获取图片数据并保存
image_data = sess.run(value)
with open('test_%d.jpg' % i, 'wb') as f:
f.write(image_data)
四、队列文件使用范式
文件名队列创建->读取解析文件->打包解析好的文件->多线程启动图训练(多线程指被使用的部分其实还是文件读取)
import tensorflow as tf '''创建文件列表''' files = tf.train.match_filenames_once("Records/output.tfrecords")
filename_queue = tf.train.string_input_producer(files, shuffle=False) '''解析TFRecord文件里的数据''' # 读取文件。 reader = tf.TFRecordReader()
_,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)
}) decoded_images = tf.decode_raw(features['image_raw'],tf.uint8)
retyped_images = tf.cast(decoded_images, tf.float32)
labels = tf.cast(features['label'],tf.int32)
#pixels = tf.cast(features['pixels'],tf.int32)
images = tf.reshape(retyped_images, [784]) '''将文件以100个为一组打包''' min_after_dequeue = 10000
batch_size = 100
capacity = min_after_dequeue + 3 * batch_size image_batch, label_batch = tf.train.shuffle_batch([images, labels],
batch_size=batch_size,
capacity=capacity,
min_after_dequeue=min_after_dequeue) '''训练模型''' def inference(input_tensor, weights1, biases1, weights2, biases2):
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1)
return tf.matmul(layer1, weights2) + biases2 # 模型相关的参数
INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500
REGULARAZTION_RATE = 0.0001
TRAINING_STEPS = 5000 weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1))
biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE])) weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1))
biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE])) y = inference(image_batch, weights1, biases1, weights2, biases2) # 计算交叉熵及其平均值
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=label_batch)
cross_entropy_mean = tf.reduce_mean(cross_entropy) # 损失函数的计算
regularizer = tf.contrib.layers.l2_regularizer(REGULARAZTION_RATE)
regularaztion = regularizer(weights1) + regularizer(weights2)
loss = cross_entropy_mean + regularaztion # 优化损失函数
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss) # 初始化回话并开始训练过程。
with tf.Session() as sess:
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# 循环的训练神经网络。
for i in range(TRAINING_STEPS):
if i % 1000 == 0:
print("After %d training step(s), loss is %g " % (i, sess.run(loss))) sess.run(train_step)
coord.request_stop()
coord.join(threads)
『TensorFlow』第十一弹_队列&多线程&TFRecod文件_我辈当高歌的更多相关文章
- 『TensorFlow』第七弹_保存&载入会话_霸王回马
首更: 由于TensorFlow的奇怪形式,所以载入保存的是sess,把会话中当前激活的变量保存下来,所以必须保证(其他网络也要求这个)保存网络和载入网络的结构一致,且变量名称必须一致,这是caffe ...
- 『TensorFlow』第十弹_队列&多线程_道路多坎坷
一.基本队列: 队列有两个基本操作,对应在tf中就是enqueue&dequeue tf.FIFOQueue(2,'int32') import tensorflow as tf '''FIF ...
- 『TensorFlow』第三弹_可视化框架介绍_悄悄问圣僧
添加记录节点 -> 汇总记录节点 -> run汇总节点 -> [书写器生成]书写入文件 [-> 刷新缓冲区] 可视化关键点: 注意, 1.with tf.name_scope( ...
- 『MXNet』第十一弹_符号式编程初探
一.符号分类 符号对我们想要进行的计算进行了描述, 下图展示了符号如何对计算进行描述. 我们定义了符号变量A, 符号变量B, 生成了符号变量C, 其中, A, B为参数节点, C为内部节点! mxne ...
- 『PyTorch』第十一弹_torch.optim优化器
一.简化前馈网络LeNet import torch as t class LeNet(t.nn.Module): def __init__(self): super(LeNet, self).__i ...
- 『PyTorch』第十一弹_torch.optim优化器 每层定制参数
一.简化前馈网络LeNet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 im ...
- 『TensorFlow』专题汇总
TensorFlow:官方文档 TensorFlow:项目地址 本篇列出文章对于全零新手不太合适,可以尝试TensorFlow入门系列博客,搭配其他资料进行学习. Keras使用tf.Session训 ...
- 『TensorFlow』模型保存和载入方法汇总
『TensorFlow』第七弹_保存&载入会话_霸王回马 一.TensorFlow常规模型加载方法 保存模型 tf.train.Saver()类,.save(sess, ckpt文件目录)方法 ...
- 『PyTorch x TensorFlow』第六弹_从最小二乘法看自动求导
TensoFlow自动求导机制 『TensorFlow』第二弹_线性拟合&神经网络拟合_恰是故人归 下面做了三个简单尝试, 利用包含gradients.assign等tf函数直接构建图进行自动 ...
随机推荐
- 【Finchley】【新特性】Spring Cloud Finchley 新特性
Finchley 正式版的发布貌似经历了相当长的时间,这次的重大发布主要带来了以下 4 项重大更新. 重大更新 1.新增 Spring Cloud Gateway 组件 Spring Cloud Ga ...
- 【特性】Redis4.0新特性
模块系统 Redis 4.0 发生的最大变化就是加入了模块系统, 这个系统可以让用户通过自己编写的代码来扩展和实现 Redis 本身并不具备的功能, 具体使用方法可以参考 antirez 的博文< ...
- How do I extract a single column from a data.frame as a data.frame
Say I have a data.frame: df <- data.frame(A=c(10,20,30),B=c(11,22,33), C=c(111,222,333)) A B C ...
- 比赛总结——牛客网 NOIP赛前集训营提高组模拟第一场
第一场打的很惨淡啊 t1二分+前缀最小值没想出来,20分的暴力也挂了,只有10分 t2数位dp,调了半天,结果因为忘了判0的特殊情况WA了一个点,亏死 t3emmmm.. 不会 imone说是DSU ...
- (转)Awesome Knowledge Distillation
Awesome Knowledge Distillation 2018-07-19 10:38:40 Reference:https://github.com/dkozlov/awesome-kno ...
- Bytom猜谜合约使用指南
准备工作: 1.安装全节点钱包V1.0.5以上并同步完成: 2.已经发行一种资产,发行资产的方法具体见文章<如何在Bytom上发布资产?> 3.准备好一些BTM作为手续费: 设置谜语(锁定 ...
- 剥开比原看代码16:比原是如何通过/list-transactions显示交易信息的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- Nuget 打包 for .Net Standart project
Create .NET Standard packages with Visual Studio 2015 Publishing packages nuge.exe 放在项目目录中 nuget spe ...
- 使用axios实现上传视频进度条
这是最终的效果图 先介绍一下axios的使用:中文的axios官方介绍 首先定义一个uploadTest方法,写在vue文件的methods里 该方法有三个参数,分别是参数,和两个回调函数,参数就是我 ...
- progressBar显示百分比
this.lab_AllFiles.Text = progressBarAllFile.Value * 100 / progressBarAllFile.Maximum + "%" ...