tensorflow直接读取图像数据之gfile,WholeFileReader,read_file
来源:互联网 发布:淘宝的天天特价在哪里 编辑:程序博客网 时间:2024/04/28 08:44
在初涉tensorflow的时候,一般采用公开数据集进行训练,对于数据集的格式以及读取很不清楚,所以这里给出tensorflow直接读取图像序列的方法,自己进行数据集的构建。下面给出集中可以参考的方法。
一、单张图片读取
1.tensorflow的gfile读取图像,matplotlib用于可视化
使用tensorflow里面给出了一个函数用来读取图像,tf.gfile.FastGFile(‘path’, ‘rb’).read()。读取结果是最原始的图像,没有经过解码。如果要显示读入的图像,则学要解码,tf.image.decode_jepg和tf.image.decode_png分别用于解码jpg格式和png格式的图像,得到图像的像素值,这个像素值可以用于显示图像。
import matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npimage_raw = tf.gfile.FastGFile('00000011.jpg','rb').read() #bytes,相对路径img = tf.image.decode_jpeg(image_raw) #Tensorwith tf.Session() as sess: print(type(image_raw)) # bytes print(type(img)) # Tensor print(type(img.eval())) # ndarray print(img.eval().shape)#(240,320,3) print(img.eval().dtype)#uint8 plt.figure(1) plt.imshow(img.eval()) plt.show()
结果:decode输出是Tensor,eval后是ndarray
<class 'bytes'><class 'tensorflow.python.framework.ops.Tensor'><class 'numpy.ndarray'>(240, 320, 3)uint8
图1 读取显示的结果
2.tensorflow的WholeFileReader输入queue
在之前的文章中也使用到了队列读取(http://blog.csdn.net/xuan_zizizi/article/details/78400839),差别在于读取队列的reader不一样,这里首先创建图像的输入队列,然后定义reader,读取序列,读取的进行解码,再得到ndaarry。
import matplotlib.pyplot as pltimport tensorflow as tf import numpy as np path = '/home/zcm/tensorf/00000011.jpg'#绝对路径file_queue = tf.train.string_input_producer([path]) #创建输入队列image_reader = tf.WholeFileReader() #reader_, image = image_reader.read(file_queue) #reader读取序列image = tf.image.decode_jpeg(image) #解码,tensorwith tf.Session() as sess: coord = tf.train.Coordinator() #协同启动的线程 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #启动线程运行队列 sess.run(image) print(type(image))#tensor coord.request_stop() #停止所有的线程 coord.join(threads) print(type(image.eval()))#ndarray print(image.eval().shape)#240×320×3 print(image.eval().dtype)#uint8 plt.figure(1) plt.imshow(image.eval()) plt.show()
结果:decode输出是Tensor,eval后是ndarray,显示图和图1一样,略
<class 'tensorflow.python.framework.ops.Tensor'><class 'numpy.ndarray'>(240, 320, 3)uint8
3.tensorflow的read_file,decode输出是Tensor,eval后是ndarray
与方法1的gfile读取差别不大,只是换了一个函数read_file
import matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npimage_value = tf.read_file('00000011.jpg')#相对路径,读取img = tf.image.decode_jpeg(image_value)#解码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()
结果:decode输出是Tensor,eval后是ndarray,显示图和图1一样,略
<class 'tensorflow.python.framework.ops.Tensor'><class 'tensorflow.python.framework.ops.Tensor'><class 'numpy.ndarray'>(240, 320, 3)uint8
参考文献:
http://www.cnblogs.com/denny402/p/7092851.html
http://blog.csdn.net/uestc_c2_403/article/details/72689908
http://blog.csdn.net/uestc_c2_403/article/details/74435286
http://blog.csdn.net/wayne2019/article/details/77884478
http://blog.csdn.net/buptgshengod/article/details/72956846
- tensorflow直接读取图像数据之gfile,WholeFileReader,read_file
- tensorflow.python.platform.gfile
- tensorflow数据读取之tfrecords
- TensorFlow-tf.gfile()模块
- Tensorflow直接读取二进制文件
- Tensorflow图像预处理,Numpy读取数据踩坑
- tensorflow读取数据之CSV格式
- TensorFlow学习笔记之读取数据概述
- Cximage 库使用,直接读取图像数据到内存。
- Tensorflow直接读取CSV文件
- tf.gfile的应用 来自tensorflow官网
- TensorFlow图像数据预处理
- tensorflow图像数据预处理
- tensorflow爬坑行:数据读取
- Tensorflow图片数据读取
- Tensorflow读取数据
- Tensorflow读取数据
- tensorflow读取文件数据
- Hive分区partition详解
- js中的=>
- Verilog学习笔记基本语法篇(七)········ 生成块
- 作业2
- Innodb事务种类及实现
- tensorflow直接读取图像数据之gfile,WholeFileReader,read_file
- 欢迎使用CSDN-markdown编辑器
- Verilog学习笔记基本语法篇(八)········ 结构说明语句
- hdu 2612Find a way (BFS~)
- Cobub无码埋点关键技术的实现
- 简单排序之冒泡排序和选择排序
- HDU 4009 Transfer water
- Python的pickle模块详解
- 基于dubbo框架下的RPC通讯协议性能测试