『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 用来计算导数.该 ...
随机推荐
- ADC裸机程序
硬件平台:JZ2440 实现功能:通过采集触摸屏ADC的电压值,推算触摸xy坐标 start.s init.c nand.c interrupt.c uart.c uart.h my_stdio.c ...
- java框架之Spring(1)-入门
介绍 概述 Spring 是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring 是于 2003 年兴起的一个轻量级的 J ...
- 微信小程序 条件渲染 wx:if
1.在框架中,我们用wx:if="{{condition}}"来判断是否需要渲染该代码块 <view wx:if="{{condition}}"> ...
- 【JVM】-NO.110.JVM.1 -【GC垃圾收集器】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- osx brew mysql
MariaDB Server is available for installation on macOS (formerly Mac OS X) via the Homebrew package m ...
- C#串口通信遇到的坑
C#串口通信中有一个DataReceived事件可以委托一个接收函数.此接收函数是运行在辅线程(secondary thread)上的.如果要在这个函数中修改主线程中的一些元素,比如UI界面上的变量的 ...
- delphi “div”、“mod”、“\”除法运算符的区别与使用方法(附带FORMAT使用方法)
Delphi中和除法相关的算术运算符有: div.mod和符号“\” 下面分别对他们的作用.操作数类型和返回值类型进行一下介绍: div:对2个整数进行除,取商,操作数需是integer类型,返回值也 ...
- MB SD Connect Compact 5 Error 95.53392.0 Solved
MB SD Connect Compact 5 is new released from MB Star company ,and its original version here the copy ...
- python基础(四)集合
[集合特点] 1.天生去重.循环 2 关系测试 -交集,差集,并集,(反向差集,对称差集) list = [1,2,3,4,5,3,6]list_2 =[2,3,5,7,8]list=set(lis ...
- Vue 组件&组件之间的通信 之 template模板引用与动态组件的使用
template模板引用 在component的template中书写大量的HTML元素很麻烦. Vue提供了<template>标签,可以在里边书写HTML,然后通过ID指定到组建内的t ...