TensorFlow中读取图像数据的三种方式
本文面对三种常常遇到的情况,总结三种读取数据的方式,分别用于处理单张图片、大量图片,和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中读取图像数据的三种方式的更多相关文章
- Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式
Linux就这个范儿 第15章 七种武器 linux 同步IO: sync.fsync与fdatasync Linux中的内存大页面huge page/large page David Cut ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- Linux就这个范儿 第18章 这里也是鼓乐笙箫 Linux读写内存数据的三种方式
Linux就这个范儿 第18章 这里也是鼓乐笙箫 Linux读写内存数据的三种方式 P703 Linux读写内存数据的三种方式 1.read ,write方式会在用户空间和内核空间不断拷贝数据, ...
- MATLAB 显示输出数据的三种方式
MATLAB 显示输出数据的三种方式 ,转载 https://blog.csdn.net/qq_35318838/article/details/78780412 1.改变数据格式 当数据重复再命令行 ...
- 在Tomcat中部署web项目的三种方式
搬瓦工搭建SS教程 SSR免费节点:http://www.xiaokeli.me 在这里介绍在Tomcat中部署web项目的三种方式: 1.部署解包的webapp目录 2.打包的war文件 3.Man ...
- ajax数据提交数据的三种方式和jquery的事件委托
ajax数据提交数据的三种方式 1.只是字符串或数字 $.ajax({ url: 'http//www.baidu.com', type: 'GET/POST', data: {'k1':'v1'}, ...
- Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)
一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- C++读取字符串数据的两种方式
C++读取字符串数据的两种方式 对于同样的样例输入: ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Ride ...
- Tomcat中部署web应用的三种方式
Tomcat中部署web应用的三种方式(静态部署) 第一种,针对war或解压后的war,最为常用的是直接操作webapp目录,将完整的war包或者web应用直接放到webapp目录下.使用 ...
随机推荐
- 50个SQL语句(MySQL版) 问题三
--------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...
- 【Java8新特性】重复注解与类型注解,你真的学会了吗?
写在前面 在Java8之前,在某个类或者方法,字段或者参数上标注注解时,同一个注解只能标注一次.但是在Java8中,新增了重复注解和类型注解,也就是说,从Java8开始,支持在某个类或者方法,字段或者 ...
- Rocket - debug - Example: Read Memory
https://mp.weixin.qq.com/s/ChXNTbx94WDC72GvmE9bGA 介绍riscv-debug的使用实例:使用三种方法读取内存. 1. Using System Bus ...
- 如何从0创建一个react项目
1. 确保本机电脑安装了yarn和node: 2. 在需要安装的文件夹目录下输入:create-react-app +(项目名称): PS:上图使用的软件为webStorm 3. 此时一个简单的re ...
- Java实现 LeetCode 523 连续的子数组和(ง •_•)ง
523. 连续的子数组和 给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数. 示 ...
- Java实现 蓝桥杯VIP 算法提高 递归倒置字符数组
算法提高 递归倒置字符数组 时间限制:1.0s 内存限制:512.0MB 问题描述 完成一个递归程序,倒置字符数组.并打印实现过程 递归逻辑为: 当字符长度等于1时,直接返回 否则,调换首尾两个字符, ...
- Java实现 LeetCode 68 文本左右对齐
68. 文本左右对齐 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用"贪心算法"来放置 ...
- Java实现LeetCode_0001_Two Sum
import java.util.Arrays; import java.util.Scanner; public class TwoSum_1 { public static void main(S ...
- Volcano火山:容器与批量计算的碰撞
[摘要] Volcano是基于Kubernetes构建的一个通用批量计算系统,它弥补了Kubernetes在“高性能应用”方面的不足,支持TensorFlow.Spark.MindSpore等多个领域 ...
- Hive的压缩存储和简单优化
一.Hive的压缩和存储 1,MapReduce支持的压缩编码 压缩格式 工具 算法 文件扩展名 是否可切分 对应的编码/解码器 DEFLATE 无 DEFLATE .deflate 否 org.ap ...