『TensorFlow』从磁盘读取数据
一、输入流水线读取数据流程
1). 创建文件名列表
相关函数:tf.train.match_filenames_once
2). 创建文件名队列
相关函数:tf.train.string_input_producer
3). 创建Reader读取数据
tf.ReaderBase 、 tf.TFRecordReader 、 tf.TextLineReader 、 tf.WholeFileReader 、 tf.IdentityReader 、 tf.FixedLengthRecordReader …
4).创建decoder解码器转换格式
tf.decode_csv 、 tf.decode_raw 、 tf.image.decode_image …
5). 创建样例队列
相关函数:tf.train.shuffle_batch
二、常用Reader、decoder介绍
CSV文件读取
阅读器:tf.TextLineReader
解析器:tf.decode_csv
二进制文件读取
阅读器:tf.FixedLengthRecordReader
解析器:tf.decode_raw
图像文件读取
阅读器:tf.WholeFileReader
解析器:tf.image.decode_image, tf.image.decode_gif, tf.image.decode_jpeg, tf.image.decode_png
TFRecords文件读取
阅读器:tf.TFRecordReader
解析器:tf.parse_single_example
又或者使用slim提供的简便方法:slim.dataset.Dataset以及slim.dataset_data_provider.DatasetDataProvider方法,一般slim.dataset.Dataset作为函数返回,需要接收Reader和Decoder作为参数。
def get_split(record_file_name, num_sampels, size):
reader = tf.TFRecordReader keys_to_features = {
"image/encoded": tf.FixedLenFeature((), tf.string, ''),
"image/format": tf.FixedLenFeature((), tf.string, 'jpeg'),
"image/height": tf.FixedLenFeature([], tf.int64, tf.zeros([], tf.int64)),
"image/width": tf.FixedLenFeature([], tf.int64, tf.zeros([], tf.int64)),
} items_to_handlers = {
"image": slim.tfexample_decoder.Image(shape=[size, size, 3]),
"height": slim.tfexample_decoder.Tensor("image/height"),
"width": slim.tfexample_decoder.Tensor("image/width"),
} decoder = slim.tfexample_decoder.TFExampleDecoder(
keys_to_features, items_to_handlers
)
return slim.dataset.Dataset(
data_sources=record_file_name,
reader=reader,
decoder=decoder,
items_to_descriptions={},
num_samples=num_sampels
) def get_image(num_samples, resize, record_file="image.tfrecord", shuffle=False):
provider = slim.dataset_data_provider.DatasetDataProvider(
get_split(record_file, num_samples, resize), # slim.dataset.Dataset 做参数
shuffle=shuffle
)
[data_image] = provider.get(["image"]) # Provider通过TFR字段获取batch size数据
return data_image
三、以图片文件为例
filename_queue = tf.train.string_input_producer(filenames,
shuffle=shuffle, num_epochs=epochs)
reader = tf.WholeFileReader()
_, img_bytes = reader.read(filename_queue)
image = tf.image.decode_png(img_bytes, channels=3)
if png else tf.image.decode_jpeg(img_bytes, channels=3)
1.建立文件名队列
filename_queue = tf.train.string_input_producer(filenames)
2.阅读器初始化 & 单次读取规则设定
# 初始化阅读器,这里以定长字节阅读器为例,实际读取图片一般使用WholeFileReader
reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
# 指定被阅读文件
result.key, value = reader.read(filename_queue)
3.单次读取数据decode
# Convert from a string to a vector of uint8 that is record_bytes long.
# read出来的是一个二进制的string,将它解码依照uint8格式解码
record_bytes = tf.decode_raw(value, tf.uint8)
…… ……
由于读取来的tensor不具有静态shape,需要使用tensor.set_shape()指定shape(或者在处理中显示的赋予shape如使用reshape等函数),否则无法建立图
read_input.label.set_shape([1])
4.输入入网络
将最后的规则tensor传入batch生成池节点中,输出的张量可以直接feed进网络
images_train, labels_train = cifar10_input.distorted_inputs(data_dir=data_dir,
batch_size=batch_size) …… …… image_batch, label_batch = sess.run([images_train, labels_train])
_, loss_value = sess.run(
[train_op, loss],
feed_dict={image_holder:image_batch, label_holder:label_batch})
5.初始化队列(相关的线程控制器组件添加也在这里)
# 启动数据增强队列
tf.train.start_queue_runners()
附上线程控制组件使用示意,
import tensorflow as tf sess = tf.Session()
coord = tf.train.coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord) # 训练过程 coord.request_stop()
coord.join(threads)
『TensorFlow』从磁盘读取数据的更多相关文章
- 『TensorFlow』SSD源码学习_其五:TFR数据读取&数据预处理
Fork版本项目地址:SSD 一.TFR数据读取 创建slim.dataset.Dataset对象 在train_ssd_network.py获取数据操作如下,首先需要slim.dataset.Dat ...
- 『TensorFlow』专题汇总
TensorFlow:官方文档 TensorFlow:项目地址 本篇列出文章对于全零新手不太合适,可以尝试TensorFlow入门系列博客,搭配其他资料进行学习. Keras使用tf.Session训 ...
- 『TensorFlow』TFR数据预处理探究以及框架搭建
一.TFRecord文件书写效率对比(单线程和多线程对比) 1.准备工作 # Author : Hellcat # Time : 18-1-15 ''' import os os.environ[&q ...
- 『TensorFlow』模型保存和载入方法汇总
『TensorFlow』第七弹_保存&载入会话_霸王回马 一.TensorFlow常规模型加载方法 保存模型 tf.train.Saver()类,.save(sess, ckpt文件目录)方法 ...
- 『TensorFlow』SSD源码学习_其一:论文及开源项目文档介绍
一.论文介绍 读论文系列:Object Detection ECCV2016 SSD 一句话概括:SSD就是关于类别的多尺度RPN网络 基本思路: 基础网络后接多层feature map 多层feat ...
- 『TensorFlow』滑动平均
滑动平均会为目标变量维护一个影子变量,影子变量不影响原变量的更新维护,但是在测试或者实际预测过程中(非训练时),使用影子变量代替原变量. 1.滑动平均求解对象初始化 ema = tf.train.Ex ...
- 『TensorFlow』读书笔记_降噪自编码器
『TensorFlow』降噪自编码器设计 之前学习过的代码,又敲了一遍,新的收获也还是有的,因为这次注释写的比较详尽,所以再次记录一下,具体的相关知识查阅之前写的文章即可(见上面链接). # Aut ...
- 『TensorFlow』流程控制
『PyTorch』第六弹_最小二乘法对比PyTorch和TensorFlow TensorFlow 控制流程操作 TensorFlow 提供了几个操作和类,您可以使用它们来控制操作的执行并向图中添加条 ...
- 『TensorFlow』梯度优化相关
tf.trainable_variables可以得到整个模型中所有trainable=True的Variable,也是自由处理梯度的基础 基础梯度操作方法: tf.gradients 用来计算导数.该 ...
随机推荐
- SQL Server 2008中的CDC(Change Data Capture)功能使用及释疑
SQL Server 2008中的CDC(Change Data Capture)功能使用及释疑 关键词:CDC 原文:http://www.cnblogs.com/chenxizhang/arc ...
- Linux 防火墙:Netfilter iptables
一.Netfilter 简介 (1) Netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制,而 iptables 只是操作 netfilter 的一个命令行工具(2 ...
- .net core 获取不到session 和cookies的值
在启动类的configure services()方法中,设置选项.checkconsent必需=context=false;如下: services.Configure<CookiePolic ...
- 访问GitLab的PostgreSQL数据库,查询、修改、替换等操作
1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...
- 019-并发编程-java.util.concurrent之-Semaphore 信号量
一.概述 Semaphore是一个计数信号量.从概念上将,Semaphore包含一组许可证.如果有需要的话,每个acquire()方法都会阻塞,直到获取一个可用的许可证.每个release()方法都会 ...
- 解决跨域问题-jsonp&cors
跨域的原因 浏览器的同源策略 同源策略是浏览器上为安全性考虑实施的非常重要的安全策略. 指的是从一个域上加载的脚本不允许访问另外一个域的文档属性. 举个例子:比如一个恶意网站的页面通过iframe嵌入 ...
- element-ui table表格展开行每次只能展开一行
https://www.jianshu.com/p/a59c22202f2c <template> <el-table @expand-change="expandSele ...
- python进阶(二) 多进程+协程
我们大多数的时候使用多线程,以及多进程,但是python中由于GIL全局解释器锁的原因,python的多线程并没有真的实现 实际上,python在执行多线程的时候,是通过GIL锁,进行上下文切换线程执 ...
- RedHat7.之.图形化切换
RedHat7.之.图形化切换 从黑窗口(纯指令输入界面)切换到图形化界面,使用root用户执行指令:startx 指令:startx 如有问题,欢迎纠正!!! 如有转载,请标明源处:https:// ...
- Centos7.2 Install subversion server
l 安装svn yum install subversion l 查看svn版本 svnserve --version l 创建svn版本库目录 mkdir -p /projects/ ...