浅谈TensorFlow中读取图像数据的三种方式


Posted in Python onJune 30, 2020

 本文面对三种常常遇到的情况,总结三种读取数据的方式,分别用于处理单张图片、大量图片,和TFRecorder读取方式。并且还补充了功能相近的tf函数。

1、处理单张图片

我们训练完模型之后,常常要用图片测试,有的时候,我们并不需要对很多图像做测试,可能就是几张甚至一张。这种情况下没有必要用队列机制。

import tensorflow as tf
import matplotlib.pyplot as plt

def read_image(file_name):
 img = tf.read_file(filename=file_name)  # 默认读取格式为uint8
 print("img 的类型是",type(img));
 img = tf.image.decode_jpeg(img,channels=0) # channels 为1得到的是灰度图,为0则按照图片格式来读
 return img

def main( ):
 with tf.device("/cpu:0"):

  # img_path是文件所在地址包括文件名称,地址用相对地址或者绝对地址都行 
   img_path='./1.jpg'
   img=read_image(img_path)
   with tf.Session() as sess:
   image_numpy=sess.run(img)
   print(image_numpy)
   print(image_numpy.dtype)
   print(image_numpy.shape)
   plt.imshow(image_numpy)
   plt.show()

if __name__=="__main__":
 main()

"""

输出结果为:

img 的类型是 <class 'tensorflow.python.framework.ops.Tensor'>
[[[196 219 209]
  [196 219 209]
  [196 219 209]
  ...

 [[ 71 106  42]
  [ 59  89  39]
  [ 34  63  19]
  ...
  [ 21  52  46]
  [ 15  45  43]
  [ 22  50  53]]]
uint8
(675, 1200, 3)
"""

 

和tf.read_file用法相似的函数还有tf.gfile.FastGFile  tf.gfile.GFile,只是要指定读取方式是'r' 还是'rb' 。

2、需要读取大量图像用于训练

这种情况就需要使用Tensorflow队列机制。首先是获得每张图片的路径,把他们都放进一个list里面,然后用string_input_producer创建队列,再用tf.WholeFileReader读取。具体请看下例:

def get_image_batch(data_file,batch_size):
 data_names=[os.path.join(data_file,k) for k in os.listdir(data_file)]
 
 #这个num_epochs函数在整个Graph是local Variable,所以在sess.run全局变量的时候也要加上局部变量。 
 filenames_queue=tf.train.string_input_producer(data_names,num_epochs=50,shuffle=True,capacity=512)
 reader=tf.WholeFileReader()
 _,img_bytes=reader.read(filenames_queue)
 image=tf.image.decode_png(img_bytes,channels=1) #读取的是什么格式,就decode什么格式
 #解码成单通道的,并且获得的结果的shape是[?, ?,1],也就是Graph不知道图像的大小,需要set_shape
 image.set_shape([180,180,1]) #set到原本已知图像的大小。或者直接通过tf.image.resize_images,tf.reshape()
 image=tf.image.convert_image_dtype(image,tf.float32)
 #预处理 下面的一句代码可以换成自己想使用的预处理方式
 #image=tf.divide(image,255.0) 
 return tf.train.batch([image],batch_size)

这里的date_file是指文件夹所在的路径,不包括文件名。第一句是遍历指定目录下的文件名称,存放到一个list中。当然这个做法有很多种方法,比如glob.glob,或者tf.train.match_filename_once

全部代码如下:

import tensorflow as tf
import os
def read_image(data_file,batch_size):
 data_names=[os.path.join(data_file,k) for k in os.listdir(data_file)]
 filenames_queue=tf.train.string_input_producer(data_names,num_epochs=5,shuffle=True,capacity=30)
 reader=tf.WholeFileReader()
 _,img_bytes=reader.read(filenames_queue)
 image=tf.image.decode_jpeg(img_bytes,channels=1)
 image=tf.image.resize_images(image,(180,180))

 image=tf.image.convert_image_dtype(image,tf.float32)
 return tf.train.batch([image],batch_size)

def main( ):
 img_path=r'F:\dataSet\WIDER\WIDER_train\images\6--Funeral' #本地的一个数据集目录,有足够的图像
 img=read_image(img_path,batch_size=10)
 image=img[0] #取出每个batch的第一个数据
 print(image)
 init=[tf.global_variables_initializer(),tf.local_variables_initializer()]
 with tf.Session() as sess:
  sess.run(init)
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(sess=sess,coord=coord)
  try:
   while not coord.should_stop():
    print(image.shape)
  except tf.errors.OutOfRangeError:
   print('read done')
  finally:
   coord.request_stop()
  coord.join(threads)


if __name__=="__main__":
 main()

"""

输出如下:

(180, 180, 1)
(180, 180, 1)
(180, 180, 1)
(180, 180, 1)
(180, 180, 1)
"""

这段代码可以说写的很是规整了。注意到init里面有对local变量的初始化,并且因为用到了队列,当然要告诉电脑什么时候队列开始, tf.train.Coordinator 和 tf.train.start_queue_runners 就是两个管理队列的类,用法如程序所示。

与 tf.train.string_input_producer相似的函数是 tf.train.slice_input_producer。 tf.train.slice_input_producer和tf.train.string_input_producer的第一个参数形式不一样。等有时间再做一个二者比较的博客

 3、对TFRecorder解码获得图像数据

其实这块和上一种方式差不多的,更重要的是怎么生成TFRecorder文件,这一部分我会补充到另一篇博客上。

仍然使用 tf.train.string_input_producer。

import tensorflow as tf
import matplotlib.pyplot as plt
import os
import cv2
import numpy as np
import glob

def read_image(data_file,batch_size):
 files_path=glob.glob(data_file)
 queue=tf.train.string_input_producer(files_path,num_epochs=None)
 reader = tf.TFRecordReader()
 print(queue)
 _, serialized_example = reader.read(queue)
 features = tf.parse_single_example(
  serialized_example,
  features={
   'image_raw': tf.FixedLenFeature([], tf.string),
   'label_raw': tf.FixedLenFeature([], tf.string),
  })
 image = tf.decode_raw(features['image_raw'], tf.uint8)
 image = tf.cast(image, tf.float32)
 image.set_shape((12*12*3))
 label = tf.decode_raw(features['label_raw'], tf.float32)
 label.set_shape((2))
 # 预处理部分省略,大家可以自己根据需要添加
 return tf.train.batch([image,label],batch_size=batch_size,num_threads=4,capacity=5*batch_size)

def main( ):
 img_path=r'F:\python\MTCNN_by_myself\prepare_data\pnet*.tfrecords' #本地的几个tf文件
 img,label=read_image(img_path,batch_size=10)
 image=img[0]
 init=[tf.global_variables_initializer(),tf.local_variables_initializer()]
 with tf.Session() as sess:
  sess.run(init)
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(sess=sess,coord=coord)
  try:
   while not coord.should_stop():
    print(image.shape)
  except tf.errors.OutOfRangeError:
   print('read done')
  finally:
   coord.request_stop()
  coord.join(threads)


if __name__=="__main__":
 main()

在read_image函数中,先使用glob函数获得了存放tfrecord文件的列表,然后根据TFRecord文件是如何存的就如何parse,再set_shape;这里有必要提醒下parse的方式。我们看到这里用的是tf.decode_raw ,因为做TFRecord是将图像数据string化了,数据是串行的,丢失了空间结果。从features中取出image和label的数据,这时就要用 tf.decode_raw  解码,得到的结果当然也是串行的了,所以set_shape 成一个串行的,再reshape。这种方式是取决于你的编码TFRecord方式的。

再举一种例子:

reader=tf.TFRecordReader()
_,serialized_example=reader.read(file_name_queue)
features = tf.parse_single_example(serialized_example, features={
 'data': tf.FixedLenFeature([256,256], tf.float32), ###
 'label': tf.FixedLenFeature([], tf.int64),
 'id': tf.FixedLenFeature([], tf.int64)
})
img = features['data']
label =features['label']
id = features['id']

这个时候就不需要任何解码了。因为做TFRecord的方式就是直接把图像数据append进去了。

参考链接:

https://blog.csdn.net/qq_34914551/article/details/86286184

到此这篇关于浅谈TensorFlow中读取图像数据的三种方式的文章就介绍到这了,更多相关TensorFlow 读取图像数据内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Windows下实现Python2和Python3两个版共存的方法
Jun 12 Python
Django项目开发中cookies和session的常用操作分析
Jul 03 Python
python操作excel文件并输出txt文件的实例
Jul 10 Python
使用Flask集成bootstrap的方法
Jul 24 Python
Python3 单行多行万能正则匹配方法
Jan 07 Python
Python离线安装PIL 模块的方法
Jan 08 Python
python二进制读写及特殊码同步实现详解
Oct 11 Python
Python使用turtle库绘制小猪佩奇(实例代码)
Jan 16 Python
python numpy实现多次循环读取文件 等间隔过滤数据示例
Mar 14 Python
python实现图像外边界跟踪操作
Jul 13 Python
Python爬虫获取豆瓣电影并写入excel
Jul 31 Python
python调试工具Birdseye的使用教程
May 25 Python
python中 _、__、__xx__()区别及使用场景
Jun 30 #Python
Django实现内容缓存实例方法
Jun 30 #Python
Pytorch 卷积中的 Input Shape用法
Jun 29 #Python
Python闭包装饰器使用方法汇总
Jun 29 #Python
使用已经得到的keras模型识别自己手写的数字方式
Jun 29 #Python
Python接口测试环境搭建过程详解
Jun 29 #Python
python字典的值可以修改吗
Jun 29 #Python
You might like
PHP STRING 陷阱原理说明
2010/07/24 PHP
php socket客户端及服务器端应用实例
2014/07/04 PHP
PHP使用feof()函数读文件的方法
2014/11/07 PHP
PHP生成条形图的方法
2014/12/10 PHP
Yii2下session跨域名共存的解决方案
2017/02/04 PHP
php设计模式之中介者模式分析【星际争霸游戏案例】
2020/03/23 PHP
JavaScript实现继承的4种方法总结
2014/10/16 Javascript
一款基于jQuery的图片场景标注提示弹窗特效
2015/01/05 Javascript
基于jQuery实现在线选座之高铁版
2015/08/24 Javascript
JS实现简单的二维矩阵乘积运算
2016/01/26 Javascript
jQuery Select下拉框操作小结(推荐)
2016/07/22 Javascript
利用JavaScript如何查询某个值是否数组内
2017/07/30 Javascript
用js实现before和after伪类的样式修改的示例代码
2017/09/07 Javascript
关于react-router/react-router-dom v4 history不能访问问题的解决
2018/01/08 Javascript
vue弹窗组件使用方法
2018/04/28 Javascript
vue.js将时间戳转化为日期格式的实现代码
2018/06/05 Javascript
微信小程序实现星级评分和展示
2018/07/05 Javascript
微信小程序+腾讯地图开发实现路径规划绘制
2019/05/22 Javascript
vue 解决遍历对象显示的顺序不对问题
2019/11/07 Javascript
JavaScript语句错误throw、try及catch实例解析
2020/08/18 Javascript
Vue Elenent实现表格相同数据列合并
2020/11/30 Vue.js
[15:23]教你分分钟做大人:虚空假面
2014/10/30 DOTA
python计算时间差的方法
2015/05/20 Python
python爬虫URL重试机制的实现方法(python2.7以及python3.5)
2018/12/18 Python
Python3实现zip分卷压缩过程解析
2019/10/09 Python
Flask缓存静态文件的具体方法
2020/08/02 Python
Sublime Text3最新激活注册码分享适用2020最新版 亲测可用
2020/11/12 Python
h5移动端调用支付宝、微信支付的实现
2020/06/08 HTML / CSS
美国最大的旗帜经销商:Carrot-Top
2018/02/26 全球购物
微软马来西亚官方网站:Microsoft马来西亚
2019/11/22 全球购物
意大利网上药房:Farmacia 33
2020/01/27 全球购物
北承题目(C++)
2012/05/16 面试题
财务会计大学生自我评价
2014/04/09 职场文书
合理化建议书
2015/02/04 职场文书
中学生学习保证书
2015/02/26 职场文书
在职证明书模板
2015/06/15 职场文书