最近一个大作业需要进行图像处理可是网上的资源太分散,于是想整合网上资源,形成一个系统:
主要包括
- 图片预处理
- 图片转TFrecord格式
- TFrecord格式转图片查看
- 简单神经网络搭建
- TFrecord格式在神经网络中的读取
- batch方法提取数据
- 神经网络性能评价
- 总结
1.图片预处理
主要是车牌分类,将每一类数据按照不同文件夹进行保存。每张图片格式大概是180*60.
2.图片转TFrecord格式
tfrecord数据文件是一种将图像数据和标签统一存储的二进制文件,能更好的利用内存,在tensorflow中快速的复制,移动,读取,存储等。
tfrecord会根据你选择输入文件的类,自动给每一类打上同样的标签。如在本例中,只有0,1 两类,想知道文件夹名与label关系的,可以自己保存起来。
参考链接:https://blog.csdn.net/v1_vivian/article/details/77898414
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 3 09:32:31 2018
@author: Decheng Liu
制作数据集 读取车牌数据集
https://blog.csdn.net/zhangjunp3/article/details/79627824
"""
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
# 保存为TFrecords
cwd = r'H:\博客\20181115 TFrecord\classes\\'
classes = {'Full_occlusion' , 'Normal', 'Semi_occlusion', 'Unsuspended'}
class_map = {} # 文件名与label关系,保存便于查看
writer = tf.python_io.TFRecordWriter('car_License_plate_train.tfrecords') #要生成的文件
for index ,name in enumerate(classes):
class_path = cwd + name + '\\'
class_map[index] = name
for img_name in os.listdir(class_path):
img_path = class_path + img_name # 每一个图片的地址
img = Image.open(img_path)
img = img.resize((180,60))
plt.imshow(img)
print (img_path)
img_raw = img.tobytes() # 将图片转换为二进制
example = tf.train.Example(features=tf.train.Features(feature={
'label':tf.train.Feature(int64_list = tf.train.Int64List(value=[index])),
'img_raw':tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
})) #example 对象对label和image数据进行封装
writer.write(example.SerializeToString()) # 序列化为字符串
plt.show()
print(class_path)
writer.close()
txtfile = open('class_map.txt','w+')
for key in class_map.keys():
txtfile.writelines(str(key)+":"+class_map[key]+"\n")
txtfile.close()
这时生成一个TFrecord文件以及一个标签映射文件如下:
0:Semi_occlusion
1:Normal
2:Unsuspended
3:Full_occlusion
3.TFrecord格式转图片便于查看与处理
读取TFRecord的数据,进行解析,此时使用了文件队列以及多线程