主要参考:https://www.tensorflow.org/api_guides/python/threading_and_queues#Queue_usage_overview

自动方式

For most use cases, the automatic thread startup and management provided by tf.train.MonitoredSession is sufficient. In the rare case that it is not, TensorFlow provides tools for manually managing your threads and queues.

与tf.read_file()、tf.image.decode_jpeg()、tfrecord API等函数配合,可以实现自动图片流并行读取

import tensorflow as tf

def simple_shuffle_batch(source, capacity, batch_size=10):
# Create a random shuffle queue.
queue = tf.RandomShuffleQueue(capacity=capacity,
min_after_dequeue=int(0.9*capacity),
shapes=source.shape, dtypes=source.dtype) # Create an op to enqueue one item.
enqueue = queue.enqueue(source) # Create a queue runner that, when started, will launch 4 threads applying
# that enqueue op.
num_threads = 4
qr = tf.train.QueueRunner(queue, [enqueue] * num_threads) # Register the queue runner so it can be found and started by
# <a href="../../api_docs/python/tf/train/start_queue_runners"><code>tf.train.start_queue_runners</code></a> later (the threads are not launched yet).
tf.train.add_queue_runner(qr) # Create an op to dequeue a batch
return queue.dequeue_many(batch_size) # create a dataset that counts from 0 to 99
input = tf.constant(list(range(100)))
input = tf.data.Dataset.from_tensor_slices(input)
input = input.make_one_shot_iterator().get_next() # Create a slightly shuffled batch from the sorted elements
get_batch = simple_shuffle_batch(input, capacity=20) # `MonitoredSession` will start and manage the `QueueRunner` threads.
with tf.train.MonitoredSession() as sess:
# Since the `QueueRunners` have been started, data is available in the
# queue, so the `sess.run(get_batch)` call will not hang.
while not sess.should_stop():
print(sess.run(get_batch))

手动方式

通过官方例程微调(以便能正常运行)得到,目前能运行,结果也正确,但是运行警告,尚未解决。

WARNING:tensorflow:From /home/work/Downloads/python_scripts/tensorflow_example/test_tf_queue_manual.py:52: QueueRunner.__init__ (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.

import tensorflow as tf
# Using Python's threading library.
import threading
import time batch_size = 10
thread_num = 3 print("-" * 50)
def MyLoop(coord, id):
step = 0
while not coord.should_stop():
step += 1
print("thread id: %02d, step: %02d, ...do something..." %(id, step))
time.sleep(0.01)
if step >= 5:
coord.request_stop() # Main thread: create a coordinator.
coord = tf.train.Coordinator() # Create thread_num threads that run 'MyLoop()'
threads = [threading.Thread(target=MyLoop, args=(coord,i)) for i in range(thread_num)] # Start the threads and wait for all of them to stop.
for t in threads:
t.start()
coord.join(threads) print("-" * 50) # create a dataset that counts from 0 to 99
example = tf.constant(list(range(100)))
example = tf.data.Dataset.from_tensor_slices(example)
example = example.make_one_shot_iterator().get_next() # Create a queue, and an op that enqueues examples one at a time in the queue.
queue = tf.RandomShuffleQueue(capacity=20,
min_after_dequeue=int(0.9*20),
shapes=example.shape,
dtypes=example.dtype)
enqueue_op = queue.enqueue(example) # Create a training graph that starts by dequeueing a batch of examples.
inputs = queue.dequeue_many(batch_size)
train_op = inputs # ...use 'inputs' to build the training part of the graph... # Create a queue runner that will run thread_num threads in parallel to enqueue examples.
qr = tf.train.QueueRunner(queue, [enqueue_op] * thread_num) # Launch the graph.
sess = tf.Session()
# Create a coordinator, launch the queue runner threads.
coord = tf.train.Coordinator()
enqueue_threads = qr.create_threads(sess, coord=coord, start=True) # Run the training loop, controlling termination with the coordinator.
try:
for step in range(1000000):
if coord.should_stop():
break
y = sess.run(train_op)
print(step, ", y =", y)
except Exception as e:
# Report exceptions to the coordinator.
coord.request_stop(e)
finally:
# Terminate as usual. It is safe to call `coord.request_stop()` twice.
coord.request_stop()
coord.join(threads)

tensorflow1.12 queue 笔记的更多相关文章

  1. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十三章:计算着色器(The Compute Shader)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十三章:计算着色器(The Compute Shader) 代码工程 ...

  2. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...

  3. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第四章:Direct 3D初始化

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第四章:Direct 3D初始化 学习目标 对Direct 3D编程在 ...

  4. 12.24笔记(关于//UIDynamic演练//多对象的附加行为//UIDynamic简单演练//UIDynamic//(CoreText框架)NSAttributedString)

          12.24笔记1.UIDynamic注意点:演示代码:上面中设置视图旋转的时候,需要注意设置M_PI_4时,视图两边保持平衡状态,达不到仿真效果.需要偏移下角度.2.吸附行为3.推动行为初 ...

  5. 12.22笔记(关于CALayer//Attributes//CALayer绘制图层//CALayer代理绘图//CALayer动画属性//CALayer自定义子图层//绘图pdf文件//绘图渐变效果)

    12.22笔记 pdf下载文件:https://www.evernote.com/shard/s227/sh/f81ba498-41aa-443b-81c1-9b569fcc34c5/f033b89a ...

  6. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 全书总结

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 全书总结 本系列文章中可能有很多翻译有问题或者错误的地方:并且有些章节 ...

  7. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- Direct12优化

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- Direct12优化 第一章:向量代数 1.向量计算的时候,使用XMV ...

  8. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画 学习目标 熟悉蒙皮动画的术语: 学习网格层级变换 ...

  9. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十二章:四元数(QUATERNIONS)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十二章:四元数(QUATERNIONS) 学习目标 回顾复数,以及 ...

随机推荐

  1. 第十二篇 -- QMainWindow与QAction(剪切-复制-粘贴)

    效果图: 按照上一节的方法,将剪切-复制-粘贴图标放置到工具栏后,为其指定槽函数.这些功能无需自己编写代码来实现,QPlainTextEdit提供了实现这些编辑功能的槽函数,如cut().copy() ...

  2. JS 高级程序设计3.5.1一元操作符 递增和递减操作符++ --

    var age =29 ++age; 在这个例子中,前置递增操作符把age的值变成了30.实际上,执行这个前置递增操作符与执行 一下操作的效果相同: var age=29; age =age+1;// ...

  3. 【阿菜用工具】利用 Web3.js 在 ganache 上部署以及调用智能合约

    合约部署 要部署的合约 pragma solidity ^0.4.23; contract test { uint256 value; function setValue(uint256 _value ...

  4. Typora PicGo Gitee博客写作好搭档

    利用Gitee仓库存放图片 1.首先在Gitee上创建一个公开的仓库,我这里创建了一个名叫resources的仓库: 2.在Gitee中获取私人令牌(个人设置界面中): 安装配置PicGo 1.下载自 ...

  5. 自行搭建网站和APP统计平台

    做过网站运营分析的朋友,一定知道 Google 统计.友盟统计以及百度统计,它们都是非常优秀的统计平台. 但不管怎么样,数据并没有掌握在网站拥有者的手中.有时候,某些业务场景不适合使用第三方统计平台, ...

  6. Mybatis学习笔记-ResultMap结果集映射

    解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...

  7. SpringCloud-Alibaba 最新的 依赖版本管理组合以及 整合gateway遇到的问题

    一般来说,cloud 与 alibaba 拥有版本组合说明文档wiki,不过这里可以做一个最新的组合的分享. <dependencyManagement> <dependencies ...

  8. mysql的安装,一步一步的教你

    1.下载mysql安装包 ,我这里安装的是mysql-5.6.41-winx64 (https://downloads.mysql.com/archives/community/) 选择自己的版本 我 ...

  9. Object.defineProperty 中 get set 用法

    就是两个函数,只要搞清楚get.set的执行时机就可以了.执行时机如下代码: <!DOCTYPE html> <html lang="en"> <he ...

  10. ASP.NET Corec初步使用Quartz.NET

    一.什么是Quartz.NET? Quartz.NET 是一个功能齐全的开源作业调度系统,可用于从最小的应用程序到大型企业系统. Quartz.NET是纯净的,它是一个.Net程序集,是非常流行的Ja ...