数据集文件名为characterData.zip,其中有65个文件夹
包含0-9,A-Z,以及各省简称
图片为12020的灰度图像
本次实验中,取其中的10%作为测试集,90%作为训练集
# 查看当前挂载的数据集目录, 该目录下的变更重启环境后会自动还原
# View dataset directory. This directory will be recovered automatically after resetting environment.
!ls /home/aistudio/data
data47142
#导入需要的包
import os
import zipfile
import random
import json
import cv2
import numpy as np
from PIL import Image
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import Linear
import matplotlib.pyplot as plt
from paddle.fluid.dygraph import Linear,Conv2D,Pool2D
2020-07-29 21:04:07,361-INFO: font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
2020-07-29 21:04:07,788-INFO: generated new fontManager
'''
参数配置
'''
train_parameters = {
"input_size": [1, 20, 20], #输入图片的shape
"class_dim": -1, #分类数
"src_path":"data/data47142/characterData.zip", #原始数据集路径
"target_path":"/home/aistudio/data/dataset", #要解压的路径
"train_list_path": "./train_data.txt", #train_data.txt路径
"eval_list_path": "./val_data.txt", #eval_data.txt路径
"label_dict":{}, #标签字典
"readme_path": "/home/aistudio/data/readme.json", #readme.json路径
"num_epochs": 20, #训练轮数
"train_batch_size": 32, #批次的大小
"learning_strategy": { #优化函数相关的配置
"lr": 0.001 #超参数学习率
}
}
def unzip_data(src_path,target_path):
'''
解压原始数据集,将src_path路径下的zip包解压至data/dataset目录下
'''
if(not os.path.isdir(target_path)):
z = zipfile.ZipFile(src_path, 'r')
z.extractall(path=target_path)
z.close()
else:
print("文件已解压")
def get_data_list(target_path,train_list_path,eval_list_path):
'''
生成数据列表
'''
#存放所有类别的信息
class_detail = []
#获取所有类别保存的文件夹名称
data_list_path=target_path
class_dirs = os.listdir(data_list_path)
if '__MACOSX' in class_dirs:
class_dirs.remove('__MACOSX')
# #总的图像数量
all_class_images = 0
# #存放类别标签
class_label=0
# #存放类别数目
class_dim = 0
# #存储要写进eval.txt和train.txt中的内容
trainer_list=[]
eval_list=[]
#读取每个类别
for class_dir in class_dirs:
if class_dir != ".DS_Store":
class_dim += 1
#每个类别的信息
class_detail_list = {}
eval_sum = 0
trainer_sum = 0
#统计每个类别有多少张图片
class_sum = 0
#获取类别路径
path = os.path.join(data_list_path,class_dir)
# print(path)
# 获取所有图片
img_paths = os.listdir(path)
for img_path in img_paths: # 遍历文件夹下的每个图片
if img_path =='.DS_Store':
continue
name_path = os.path.join(path,img_path) # 每张图片的路径
if class_sum % 10 == 0: # 每10张图片取一个做验证数据
eval_sum += 1 # eval_sum为测试数据的数目
eval_list.append(name_path + "\t%d" % class_label + "\n")
else:
trainer_sum += 1
trainer_list.append(name_path + "\t%d" % class_label + "\n")#trainer_sum测试数据的数目
class_sum += 1 #每类图片的数目
all_class_images += 1 #所有类图片的数目
# 说明的json文件的class_detail数据
class_detail_list['class_name'] = class_dir #类别名称
class_detail_list['class_label'] = class_label #类别标签
class_detail_list['class_eval_images'] = eval_sum #该类数据的测试集数目
class_detail_list['class_trainer_images'] = trainer_sum #该类数据的训练集数目
class_detail.append(class_detail_list)
#初始化标签列表
train_parameters['label_dict'][str(class_label)] = class_dir
class_label += 1
#初始化分类数
train_parameters['class_dim'] = class_dim
print(train_parameters)
#乱序
random.shuffle(eval_list)
with open(eval_list_path, 'a') as f:
for eval_image in eval_list:
f.write(eval_image)
#乱序
random.shuffle(trainer_list)
with open(train_list_path, 'a') as f2:
for train_image in trainer_list:
f2.write(train_image)
# 说明的json文件信息
readjson = {}
readjson['all_class_name'] = data_list_path #文件父目录
readjson['all_class_images'] = all_class_images
readjson['class_detail'] = class_detail
jsons = json.dumps(readjson, sort_keys=True, indent=4, separators=(',', ': '))
with open(train_parameters['readme_path'],'w') as f:
f.write(jsons)
print ('生成数据列表完成!')
def data_reader(file_list):
'''
自定义data_reader
'''
def reader():
with open(file_list, 'r') as f:
lines = [line.strip() for line in f]
for line in lines:
img_path, lab = line.strip().split('\t')
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.array(img).astype('float32')
img = img/255.0
yield img, int(lab)
return reader
'''
参数初始化
'''
src_path=train_parameters['src_path']
target_path=train_parameters['target_path']
train_list_path=train_parameters['train_list_path']
eval_list_path=train_parameters['eval_list_path']
batch_size=train_parameters['train_batch_size']
'''
解压原始数据到指定路径
'''
unzip_data(src_path,target_path)
#每次生成数据列表前,首先清空train.txt和eval.txt
with open(train_list_path, 'w') as f:
f.seek(0)
f.truncate()
with open(eval_list_path, 'w') as f:
f.seek(0)
f.truncate()
#生成数据列表
get_data_list(target_path,train_list_path,eval_list_path)
'''
构造数据提供器
'''
train_reader = paddle.batch(data_reader(train_list_path),
batch_size=batch_size,
drop_last=True)
eval_reader = paddle.batch(data_reader(eval_list_path),
batch_size=batch_size,
drop_last=True)
文件已解压
{'input_size': [1, 20, 20], 'class_dim': 65, 'src_path': 'data/data47142/characterData.zip', 'target_path': '/home/aistudio/data/dataset', 'train_list_path': './train_data.txt', 'eval_list_path': './val_data.txt', 'label_dict': {'0': 'e1', '1': 'X', '2': 'hu', '3': 'C', '4': 'P', '5': 'shan', '6': 'su', '7': '1', '8': 'M', '9': 'zang', '10': 'ji', '11': 'wan', '12': 'gui1', '13': 'yue', '14': 'Q', '15': 'ning', '16': 'B', '17': 'jin', '18': 'liao', '19': 'min', '20': 'H', '21': 'gan1', '22': '3', '23': 'S', '24': 'K', '25': 'meng', '26': '6', '27': '7', '28': '9', '29': 'gan', '30': 'A', '31': 'V', '32': 'D', '33': 'Y', '34': 'N', '35': 'F', '36': 'lu', '37': 'xin', '38': 'E', '39': 'G', '40': 'cuan', '41': 'yun', '42': 'jing', '43': 'T', '44': '4', '45': 'L', '46': 'sx', '47': '8', '48': 'qing', '49': 'hei', '50': '2', '51': 'yu', '52': 'R', '53': 'zhe', '54': 'qiong', '55': 'gui', '56': '0', '57': 'Z', '58': 'U', '59': 'jl', '60': 'xiang', '61': 'W', '62': 'J', '63': '5', '64': 'yu1'}, 'readme_path': '/home/aistudio/data/readme.json', 'num_epochs': 20, 'train_batch_size': 32, 'learning_strategy': {'lr': 0.001}}
生成数据列表完成!
Batch=0
Batchs=[]
all_train_accs=[]
def draw_train_acc(Batchs, train_accs):
title="training accs"
plt.title(title, fontsize=24)
plt.xlabel("batch", fontsize=14)
plt.ylabel("acc", fontsize=14)
plt.plot(Batchs, train_accs, color='green', label='training accs')
plt.legend()
plt.grid()
plt.show()
all_train_loss=[]
def draw_train_loss(Batchs, train_loss):
title="training loss"
plt.title(title, fontsize=24)
plt.xlabel("batch", fontsize=14)
plt.ylabel("loss", fontsize=14)
plt.plot(Batchs, train_loss, color='red', label='training loss')
plt.legend()
plt.grid()
plt.show()
#定义DNN网络
class MyDNN(fluid.dygraph.Layer):
'''
DNN网络
'''
def __init__(self):
super(MyDNN,self).__init__()
self.hidden1_1 = Conv2D(1,28,5,1) #通道数、卷积核个数、卷积核大小
self.hidden1_2 = Pool2D(pool_size=2,pool_type='max',pool_stride=1)
self.hidden2_1 = Conv2D(28,32,3,1)
self.hidden2_2 = Pool2D(pool_size=2,pool_type='max',pool_stride=1)
self.hidden3 = Conv2D(32,32,3,1)
self.hidden4 = Linear(32*10*10,65,act='softmax')
def forward(self,input): # forward 定义执行实际运行时网络的执行逻辑
'''前向计算'''
#print(input.shape)
x = self.hidden1_1(input)
#print(x.shape)
x = self.hidden1_2(x)
#print(x.shape)
x = self.hidden2_1(x)
#print(x.shape)
x = self.hidden2_2(x)
#print(x.shape)
x = self.hidden3(x)
#print(x.shape)
x = fluid.layers.reshape(x, shape=[-1, 32*10*10])
y = self.hidden4(x)
return y
with fluid.dygraph.guard():
model=MyDNN() #模型实例化
model.train() #训练模式
opt=fluid.optimizer.SGDOptimizer(learning_rate=train_parameters['learning_strategy']['lr'], parameter_list=model.parameters())#优化器选用SGD随机梯度下降,学习率为0.001.
epochs_num=train_parameters['num_epochs'] #迭代次数
for pass_num in range(epochs_num):
for batch_id,data in enumerate(train_reader()):
images=np.array([x[0].reshape(1,20,20) for x in data],np.float32)
labels = np.array([x[1] for x in data]).astype('int64')
labels = labels[:, np.newaxis]
image=fluid.dygraph.to_variable(images)
label=fluid.dygraph.to_variable(labels)
predict=model(image) #数据传入model
loss=fluid.layers.cross_entropy(predict,label)
avg_loss=fluid.layers.mean(loss)#获取loss值
acc=fluid.layers.accuracy(predict,label)#计算精度
if batch_id!=0 and batch_id%50==0:
Batch = Batch+50
Batchs.append(Batch)
all_train_loss.append(avg_loss.numpy()[0])
all_train_accs.append(acc.numpy()[0])
print("train_pass:{},batch_id:{},train_loss:{},train_acc:{}".format(pass_num,batch_id,avg_loss.numpy(),acc.numpy()))
avg_loss.backward()
opt.minimize(avg_loss) #优化器对象的minimize方法对参数进行更新
model.clear_gradients() #model.clear_gradients()来重置梯度
fluid.save_dygraph(model.state_dict(),'MyDNN')#保存模型
draw_train_acc(Batchs,all_train_accs)
draw_train_loss(Batchs,all_train_loss)
train_pass:0,batch_id:50,train_loss:[2.5459664],train_acc:[0.375]
train_pass:0,batch_id:100,train_loss:[2.475458],train_acc:[0.46875]
train_pass:0,batch_id:150,train_loss:[1.2719567],train_acc:[0.6875]
train_pass:0,batch_id:200,train_loss:[1.4321089],train_acc:[0.6875]
train_pass:0,batch_id:250,train_loss:[0.69725776],train_acc:[0.875]
train_pass:0,batch_id:300,train_loss:[1.1576208],train_acc:[0.75]
train_pass:0,batch_id:350,train_loss:[1.207567],train_acc:[0.75]
train_pass:0,batch_id:400,train_loss:[0.6369293],train_acc:[0.84375]
train_pass:0,batch_id:450,train_loss:[0.99959254],train_acc:[0.78125]
#模型评估
with fluid.dygraph.guard():
accs = []
model_dict, _ = fluid.load_dygraph('MyDNN')
model = MyDNN()
model.load_dict(model_dict) #加载模型参数
model.eval() #训练模式
for batch_id,data in enumerate(eval_reader()):#测试集
images=np.array([x[0].reshape(1,20,20) for x in data],np.float32)
labels = np.array([x[1] for x in data]).astype('int64')
labels = labels[:, np.newaxis]
image=fluid.dygraph.to_variable(images)
label=fluid.dygraph.to_variable(labels)
predict=model(image)
acc=fluid.layers.accuracy(predict,label)
accs.append(acc.numpy()[0])
avg_acc = np.mean(accs)
print(avg_acc)
0.96568626
# 对车牌图片进行处理,分割出车牌中的每一个字符并保存
license_plate = cv2.imread('work/车牌.png')
gray_plate = cv2.cvtColor(license_plate, cv2.COLOR_RGB2GRAY)
ret, binary_plate = cv2.threshold(gray_plate, 175, 255, cv2.THRESH_BINARY) #ret:阈值,binary_plate:根据阈值处理后的图像数据
# 按列统计像素分布
result = []
for col in range(binary_plate.shape[1]):
result.append(0)
for row in range(binary_plate.shape[0]):
result[col] = result[col] + binary_plate[row][col]/255
# print(result)
#记录车牌中字符的位置
character_dict = {}
num = 0
i = 0
while i < len(result):
if result[i] == 0:
i += 1
else:
index = i + 1
while result[index] != 0:
index += 1
character_dict[num] = [i, index-1]
num += 1
i = index
# print(character_dict)
#将每个字符填充,并存储
characters = []
for i in range(8):
if i==2:
continue
padding = (170 - (character_dict[i][1] - character_dict[i][0])) / 2
#将单个字符图像填充为170*170
ndarray = np.pad(binary_plate[:,character_dict[i][0]:character_dict[i][1]], ((0,0), (int(padding), int(padding))), 'constant', constant_values=(0,0))
ndarray = cv2.resize(ndarray, (20,20))
cv2.imwrite('work/' + str(i) + '.png', ndarray)
characters.append(ndarray)
def load_image(path):
img = paddle.dataset.image.load_image(file=path, is_color=False)
img = img.astype('float32')
img = img[np.newaxis, ] / 255.0
return img
#将标签进行转换
print('Label:',train_parameters['label_dict'])
match = {'A':'A','B':'B','C':'C','D':'D','E':'E','F':'F','G':'G','H':'H','I':'I','J':'J','K':'K','L':'L','M':'M','N':'N',
'O':'O','P':'P','Q':'Q','R':'R','S':'S','T':'T','U':'U','V':'V','W':'W','X':'X','Y':'Y','Z':'Z',
'yun':'云','cuan':'川','hei':'黑','zhe':'浙','ning':'宁','jin':'津','gan':'赣','hu':'沪','liao':'辽','jl':'吉','qing':'青','zang':'藏',
'e1':'鄂','meng':'蒙','gan1':'甘','qiong':'琼','shan':'陕','min':'闽','su':'苏','xin':'新','wan':'皖','jing':'京','xiang':'湘','gui':'贵',
'yu1':'渝','yu':'豫','ji':'冀','yue':'粤','gui1':'桂','sx':'晋','lu':'鲁',
'0':'0','1':'1','2':'2','3':'3','4':'4','5':'5','6':'6','7':'7','8':'8','9':'9'}
L = 0
LABEL ={}
for V in train_parameters['label_dict'].values():
LABEL[str(L)] = match[V]
L += 1
print(LABEL)
Label: {'0': 'Y', '1': 'hu', '2': '2', '3': 'cuan', '4': 'ji', '5': 'T', '6': '5', '7': '9', '8': 'yue', '9': 'yun', '10': 'hei', '11': 'H', '12': 'G', '13': 'L', '14': 'gan1', '15': '1', '16': '6', '17': 'qing', '18': 'K', '19': 'gui', '20': 'C', '21': 'gan', '22': 'yu', '23': 'X', '24': '8', '25': 'sx', '26': 'xiang', '27': 'S', '28': 'ning', '29': 'jin', '30': 'xin', '31': 'jl', '32': 'min', '33': 'B', '34': 'J', '35': 'yu1', '36': 'N', '37': 'gui1', '38': 'M', '39': '4', '40': 'D', '41': 'R', '42': 'shan', '43': '7', '44': 'E', '45': 'Q', '46': 'A', '47': 'U', '48': '3', '49': 'F', '50': 'W', '51': 'meng', '52': 'liao', '53': 'jing', '54': 'zang', '55': 'lu', '56': 'V', '57': 'Z', '58': '0', '59': 'su', '60': 'wan', '61': 'P', '62': 'zhe', '63': 'e1', '64': 'qiong'}
{'0': 'Y', '1': '沪', '2': '2', '3': '川', '4': '冀', '5': 'T', '6': '5', '7': '9', '8': '粤', '9': '云', '10': '黑', '11': 'H', '12': 'G', '13': 'L', '14': '甘', '15': '1', '16': '6', '17': '青', '18': 'K', '19': '贵', '20': 'C', '21': '赣', '22': '豫', '23': 'X', '24': '8', '25': '晋', '26': '湘', '27': 'S', '28': '宁', '29': '津', '30': '新', '31': '吉', '32': '闽', '33': 'B', '34': 'J', '35': '渝', '36': 'N', '37': '桂', '38': 'M', '39': '4', '40': 'D', '41': 'R', '42': '陕', '43': '7', '44': 'E', '45': 'Q', '46': 'A', '47': 'U', '48': '3', '49': 'F', '50': 'W', '51': '蒙', '52': '辽', '53': '京', '54': '藏', '55': '鲁', '56': 'V', '57': 'Z', '58': '0', '59': '苏', '60': '皖', '61': 'P', '62': '浙', '63': '鄂', '64': '琼'}
#构建预测动态图过程
with fluid.dygraph.guard():
model=MyDNN()#模型实例化
model_dict,_=fluid.load_dygraph('MyDNN')
model.load_dict(model_dict)#加载模型参数
model.eval()#评估模式
lab=[]
for i in range(8):
if i==2:
continue
infer_imgs = []
infer_imgs.append(load_image('work/' + str(i) + '.png'))
infer_imgs = np.array(infer_imgs)
infer_imgs = fluid.dygraph.to_variable(infer_imgs)
result=model(infer_imgs)
lab.append(np.argmax(result.numpy()))
print(lab)
display(Image.open('work/车牌.png'))
for i in range(len(lab)):
print(LABEL[str(lab[i])],end='')
[55, 46, 16, 24, 16, 44, 34]
鲁A686EJ
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sEAW7dGJ-1596028237587)(output_26_1.png)]