https://zhuanlan.zhihu.com/p/27238630

WholeFileReader

# 我们用一个具体的例子感受tensorflow中的数据读取。如图,
# 假设我们在当前文件夹中已经有A.jpg、B.jpg、C.jpg三张图片,
# 我们希望读取这三张图片5个epoch并且把读取的结果重新存到read文件夹中。 # 导入tensorflow
import tensorflow as tf # 新建一个Session
with tf.Session() as sess:
# 我们要读三幅图片A.jpg, B.jpg, C.jpg
filename = ['./data/A.png', './data/B.png', './data/C.png']
# string_input_producer会产生一个文件名队列
filename_queue = tf.train.string_input_producer(filename, shuffle=True, 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('data/test_%d.jpg' % i, 'wb') as f:
f.write(image_data)

http://blog.csdn.net/wayne2019/article/details/77884478

import tensorflow as tf
import os
import matplotlib.pyplot as plt def file_name(file_dir): #来自http://blog.csdn.net/lsq2902101015/article/details/51305825
for root, dirs, files in os.walk(file_dir): #模块os中的walk()函数遍历文件夹下所有的文件
print(root) #当前目录路径
print(dirs) #当前路径下所有子目录
print(files) #当前路径下所有非目录子文件 def file_name2(file_dir): #特定类型的文件
L=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.png':
L.append(os.path.join(root, file))
return L file_name('data')
path = file_name2('data')
print(path) #以下参考http://blog.csdn.net/buptgshengod/article/details/72956846 (十图详解TensorFlow数据读取机制)
#以及http://blog.csdn.net/uestc_c2_403/article/details/74435286 file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #创建输入队列
image_reader = tf.WholeFileReader()
key, image = image_reader.read(file_queue)
image = tf.image.decode_jpeg(image) with tf.Session() as sess:
tf.local_variables_initializer().run()
threads = tf.train.start_queue_runners(sess=sess)
for _ in path+path:
plt.figure
plt.imshow(image.eval())
plt.show()

read_file

import tensorflow as tf
import os
import matplotlib.pyplot as pltimport numpy as np print(tf.__version__) image_value = tf.read_file('data/A.png')
img = tf.image.decode_jpeg(image_value, channels=3) with tf.Session() as sess:
print(type(image_value)) # bytes
print(type(img)) # Tensor
print(type(img.eval())) # ndarray !!!
print(img.eval().shape)
print(img.eval().dtype)
plt.figure(1)
plt.imshow(img.eval())
plt.show()

gfile.FastGFile

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np print(tf.__version__) image_raw = tf.gfile.FastGFile('data/A.png','rb').read() #bytes
img = tf.image.decode_jpeg(image_raw) #Tensor
#img2 = tf.image.convert_image_dtype(img, dtype = tf.uint8) with tf.Session() as sess:
print(type(image_raw)) # bytes
print(type(img)) # Tensor
#print(type(img2)) print(type(img.eval())) # ndarray !!!
print(img.eval().shape)
print(img.eval().dtype) # print(type(img2.eval()))
# print(img2.eval().shape)
# print(img2.eval().dtype)
plt.figure(1)
plt.imshow(img.eval())
plt.show()

最新文章

  1. NodeJS:Error: Cannot find module 'jshint/src/cli'
  2. Total Commander 集成、调用 Beyond Compare比较文件
  3. [转]iOS7中UITextView contentsize改变时机
  4. python已字典为元素的数组排序
  5. collection中的retainAll()方法
  6. Swift3.0 iOS获取当前时间 - 年月日时分秒星期
  7. Majority Element
  8. Java 性能优化实战记录(3)--JVM OOM的分析和原因追查
  9. %SELECTALL
  10. 【CodeForces】【311C】Fetch the Treasures
  11. 利用Pattern和Mather来禁止特殊字符的输入
  12. PHP语言开发微信公众平台(订阅号)之curl命令
  13. JavaScript高级程序设计---学习笔记(四)
  14. [线性筛]P1865 A % B Problem
  15. c语言基础学习08_内存管理
  16. 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.
  17. mpvue——API请求封装(小程序原生)
  18. IO操作、PIL使用、Django的admin操作
  19. 汉语言处理工具pyhanlp的简繁转换
  20. [10]Windows内核情景分析---中断处理

热门文章

  1. 学习使用Jmeter做压力測试(一)--压力測试基本概念
  2. 关于视频YUV
  3. Cordova 快速入门记录
  4. mysql索引二
  5. UED、UCD、UE、UI、交互设计概念
  6. android 实现全屏代码
  7. Python 列表 min() 方法
  8. Centos7 通过yum源安装nginx
  9. 剑指offer代码 vs2013执行
  10. B/S打印解决方案参考