Tensorflow学习之图像的读取

tensorflow学习之图像的读取。

图像的相关知识

三要素:长度、宽度、通道数

image-20200906081845766

三要素与张量的关系

指定3-D张量:

image-20200906082002808

图像基本操作

目的:

1、增加图片数据的统一性

2、所有图片转换成指定大小

3、缩小图片数据量,防止增加开销

操作:缩小图片大小

tf.image.resize_images(images, size): 缩小图片

  • images:4-D形状[batch, height, width, channels]或3-D形状的张量[height, width, channels]的图片数据

  • size:1-D int32张量:new_height, new_width,图像的新尺寸

  • 返回4-D格式或者3-D格式图片

图片批处理案例

图像读取API

图像读取器:将文件的全部内容作为值输出的读取器,tf.WholeFileReader

  • return:读取器实例

  • read(file_queue):输出将是一个文件名(key)和该文件的内容(值)

图像解码器: tf.image.decode_jpeg(contents)

  • 将JPEG编码的图像解码为uint8张量

  • return:uint8张量,3-D形状[height, width, channels]

  • tf.image.decode_png(contents)

  • 将PNG编码的图像解码为uint8或uint16张量

  • return:张量类型,3-D形状[height, width, channels]

图片批处理案例流程

1、构造图片文件队列

2、构造图片阅读器

3、读取图片数据

4、处理图片数据

代码实现

读取图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tensorflow as tf
import os

# 1、找到文件放入列表
file_name=os.listdir('./Data/day_5/dog/')
filelist=[os.path.join('./Data/day_5/dog/',file) for file in file_name]

# 2、构造文件队列
file_queue=tf.train.string_input_producer(filelist)

# 构造阅读器去读取文件内容(默认读取一张图片)
reader=tf.WholeFileReader()

key,value=reader.read(file_queue)

# print(value)

# 3、对读取的图片进行解码
image=tf.image.decode_jpeg(value)
# print(image)

# 4、处理图片的大小(统一大小)
image_resize=tf.image.resize_images(image,[200,200])
# print(image_resize)

# 注意:在批处理之前要求所有数据的形状必须定义
image_resize.set_shape([200,200,3])

# 5、进行批处理
image_batch=tf.train.batch([image_resize],batch_size=20,num_threads=1,capacity=20)

# 开启会话运行结构
with tf.Session() as sess:
# 定义一个线程协调器
coord=tf.train.Coordinator()

# 开启读取文件的线程
threads=tf.train.start_queue_runners(sess,coord=coord)

# 打印读取内容
print(sess.run([image_batch]))

# 回收子线程
coord.request_stop()
coord.join(threads)

读取二进制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import tensorflow as tf
import os

# 构造文件列表
file_name=os.listdir('./Data/day_5/cifar-10-batches-bin/')
filelist=[os.path.join('./Data/day_5/cifar-10-batches-bin/',file) for file in file_name if file[-3:]=='bin']

# 构造文件队列
file_queue=tf.train.string_input_producer(filelist)

# 构造阅读器
reader=tf.FixedLengthRecordReader(3073)

key,value=reader.read(file_queue)

# 解码内容
label_image=tf.decode_raw(value,tf.uint8)

# 分割出图片和标签数据
label=tf.cast(tf.slice(label_image,[0],[1]),tf.int32)
image=tf.slice(label_image,[1],[3072])

# 对图片格式进行改变
image_reshape=tf.reshape(image,[32,32,3])

# 批处理
image_batch,label_batch=tf.train.batch([image_reshape,label],batch_size=10,num_threads=1,capacity=10)

# 开启会话运行结构
with tf.Session() as sess:
# 定义一个线程协调器
coord=tf.train.Coordinator()

# 开启读取文件的线程
threads=tf.train.start_queue_runners(sess,coord=coord)

# 打印读取内容
print(sess.run([image_batch,label_batch]))

# 回收子线程
coord.request_stop()
coord.join(threads)

将图片转化为tfrecords进行存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 构造一个tfrecords文件
writer=tf.python_io.TFRecordWriter('./Data/tem/')

# 循环将所有文件写入,每个样本要构造example协议
for i in range(10):
# 取出第i个图片的特征值和目标值
image=image_batch[i].eval().tostring()
label=int(label_batch[i].eval()[0])

# 构造一个样本的example
example=tf.train.Example(features=tf.train.Features(feature={
'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image])),
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))
}))

# 写入单独样本
writer.write(example.SerializeToString()

writer.close()
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2020 WuXei Si
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信