[转]Theano下用CNN(卷积神经网络)做车牌中文字符OCR
Theano下用CNN(卷积神经网络)做车牌中文字符OCR
原文地址:http://m.blog.csdn.net/article/details?id=50989742
之前时间一直在看 Michael Nielsen 先生的 Deep Learning 教程。
用了他的代码在theano下测试了下中文车牌字符的识别。由于我没有GPU,简单的在进行了16个epoch之后,识别率达到了 98.41% ,由于图像本来质量就不高,达到这个识别率,效果挺不错了。
一共 31 类 车牌中文字符数据来源于中文车牌识别项目 EasyPR 的数据集 . 由于数据集分布很不均匀。可能会导致个别类别拟合不一致,而降低识别率。所以使用随机轻微扭曲图像的方式来生成新的数据以保证数据集各个类目的数量的均衡。
下面是用于轻微扭曲图像来生成更多样本的函数。
def rotRandrom(img,factor,size):
""" 使图像轻微的畸变 img 输入图像
factor 畸变的参数
size 为图片的目标尺寸 """
img = img.reshape(size);
shape = size; pts1 = np.float32([[0,0],[0,shape[0]],[shape[1],0],[shape[1],shape[0]]])
pts2 = np.float32([[r(factor),r(factor)],[0,shape[0]-r(factor)],[shape[1]-r(factor),0],[shape[1]-r(factor),shape[0]-r(factor)]])
M = cv2.getPerspectiveTransform(pts1,pts2);
dst = cv2.warpPerspective(img,M,(shape[0],shape[1]));
return dst.ravel();
在训练的时候 使用的CNN结构如下
激活函数都为 ReLu
Conv(kernel size 5*5 ) * 25个 feature map->
Pooling 2*2 ->
Conv(kernel size 5*5) * 16个feature map->
Pooling 2*2 ->
FullConnectedLayer 120 个 Neurons ->
FullConnectedLayer 84个 Neurons ->
Softmax Output 31类
import network3
from network3 import Network
from network3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer
from network3 import ReLU training_data, validation_data, test_data= network3.load_data_cPickle("./data.pkl")
mini_batch_size = 10
net = Network([
ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28),
filter_shape=(25, 1, 5, 5),
poolsize=(2, 2),
activation_fn=ReLU),
ConvPoolLayer(image_shape=(mini_batch_size, 25, 12, 12),
filter_shape=(16, 25, 5, 5),
poolsize=(2, 2),
activation_fn=ReLU),
FullyConnectedLayer(n_in=16*4*4, n_out=120, activation_fn=ReLU),
FullyConnectedLayer(n_in=120, n_out=84, activation_fn=ReLU),
SoftmaxLayer(n_in=84, n_out=31)], mini_batch_size )
net.SGD(training_data, 60, mini_batch_size, 0.03, validation_data, test_data, lmbda=0.1)
这个函数用于制作自己的数据。
def make_dataset(dirn):
set = [];
labels = [] ; def findinside(dirname,code,):
print "code",code;
print "dirname",dirname; for parent,dirnames,filenames in os.walk(dirname):
adder = 1400 - len(filenames)
len_d = len(filenames)
for filename in filenames:
path =parent+"/"+filename
if(path.endswith(".jpg")):
img = cv2.imread(path,cv2.CV_LOAD_IMAGE_GRAYSCALE);
img = cv2.resize(img,(28,28));
img = img.astype(np.float32)/255; set.append(img.ravel());
labels.append(code);
for i in range(adder):
c_index = int(np.random.rand() * len_d);
l_set = len(set)
set.append(rotrandom.rotRandrom( set[l_set-len_d + c_index],0.88,(28,28))); labels.append(code); print len(set),dirname,len(filenames) for parent,dirnames,filenames in os.walk(dirn):
num = len(dirnames);
for i in range(num):
c_path = dir_chars + "/"+ dirnames[i];
findinside(c_path,i); shuffle = np.random.permutation(len(set)); print len(set)
set = np.array(set);
labels = np.array(labels);
set, labels = set[shuffle], labels[shuffle]
train_n = int(0.9*len(set)) training_set,test_set = np.split(set, [train_n])
training_labels, test_labels = np.split(labels, [train_n])
print training_labels
validation_set = test_set.copy();
validation_labels = test_set.copy();
training_data = [training_set,training_labels]
validation_data = [validation_set,validation_labels] test_data = [test_set,test_labels] data = [ training_data, validation_data, test_data];
fileid = open("./data.pkl","wb")
cPickle.dump(data,fileid) dir_chars = "./charsChinese"
make_dataset(dir_chars);
在进行了第14个epoch之后获得了 98.41% 训练时间大概在10分钟左右。
Training mini-batch number 0
Training mini-batch number 1000
Training mini-batch number 2000
Training mini-batch number 3000
Epoch 0: validation accuracy 89.15%
This is the best validation accuracy to date.
The corresponding test accuracy is 89.15%
Training mini-batch number 4000
Training mini-batch number 5000
Training mini-batch number 6000
Training mini-batch number 7000
Epoch 1: validation accuracy 94.65%
This is the best validation accuracy to date.
The corresponding test accuracy is 94.65%
Training mini-batch number 8000
Training mini-batch number 9000
Training mini-batch number 10000
Training mini-batch number 11000
Epoch 2: validation accuracy 95.44%
This is the best validation accuracy to date.
The corresponding test accuracy is 95.44%
Training mini-batch number 12000
Training mini-batch number 13000
Training mini-batch number 14000
Training mini-batch number 15000
Epoch 3: validation accuracy 96.13%
This is the best validation accuracy to date.
The corresponding test accuracy is 96.13%
Training mini-batch number 16000
Training mini-batch number 17000
Training mini-batch number 18000
Training mini-batch number 19000
Epoch 4: validation accuracy 96.91%
This is the best validation accuracy to date.
The corresponding test accuracy is 96.91%
Training mini-batch number 20000
Training mini-batch number 21000
Training mini-batch number 22000
Training mini-batch number 23000
Epoch 5: validation accuracy 96.52%
Training mini-batch number 24000
Training mini-batch number 25000
Training mini-batch number 26000
Training mini-batch number 27000
Epoch 6: validation accuracy 96.87%
Training mini-batch number 28000
Training mini-batch number 29000
Training mini-batch number 30000
Training mini-batch number 31000
Epoch 7: validation accuracy 96.87%
Training mini-batch number 32000
Training mini-batch number 33000
Training mini-batch number 34000
Training mini-batch number 35000
Epoch 8: validation accuracy 97.58%
This is the best validation accuracy to date.
The corresponding test accuracy is 97.58%
Training mini-batch number 36000
Training mini-batch number 37000
Training mini-batch number 38000
Training mini-batch number 39000
Epoch 9: validation accuracy 97.49%
Training mini-batch number 40000
Training mini-batch number 41000
Training mini-batch number 42000
Epoch 10: validation accuracy 97.60%
This is the best validation accuracy to date.
The corresponding test accuracy is 97.60%
Training mini-batch number 43000
Training mini-batch number 44000
Training mini-batch number 45000
Training mini-batch number 46000
Epoch 11: validation accuracy 97.93%
This is the best validation accuracy to date.
The corresponding test accuracy is 97.93%
Training mini-batch number 47000
Training mini-batch number 48000
Training mini-batch number 49000
Training mini-batch number 50000
Epoch 12: validation accuracy 97.83%
Training mini-batch number 51000
Training mini-batch number 52000
Training mini-batch number 53000
Training mini-batch number 54000
Epoch 13: validation accuracy 98.04%
This is the best validation accuracy to date.
The corresponding test accuracy is 98.04%
Training mini-batch number 55000
Training mini-batch number 56000
Training mini-batch number 57000
Training mini-batch number 58000
Epoch 14: validation accuracy 98.20%
This is the best validation accuracy to date.
The corresponding test accuracy is 98.20%
Training mini-batch number 59000
Training mini-batch number 60000
Training mini-batch number 61000
Training mini-batch number 62000
Epoch 15: validation accuracy 97.86%
Training mini-batch number 63000
Training mini-batch number 64000
Training mini-batch number 65000
Training mini-batch number 66000
Epoch 16: validation accuracy 98.41%
This is the best validation accuracy to date.
The corresponding test accuracy is 98.41%
[转]Theano下用CNN(卷积神经网络)做车牌中文字符OCR的更多相关文章
- Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
http://m.blog.csdn.net/blog/wu010555688/24487301 本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep le ...
- Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)
Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现 zouxy09@qq.com http://blog.csdn.net/zouxy09 自己平时看了一些论文, ...
- cnn(卷积神经网络)比较系统的讲解
本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之 ...
- CNN(卷积神经网络)、RNN(循环神经网络)、DNN(深度神经网络)的内部网络结构有什么区别?
https://www.zhihu.com/question/34681168 CNN(卷积神经网络).RNN(循环神经网络).DNN(深度神经网络)的内部网络结构有什么区别?修改 CNN(卷积神经网 ...
- CNN(卷积神经网络)、RNN(循环神经网络)、DNN,LSTM
http://cs231n.github.io/neural-networks-1 https://arxiv.org/pdf/1603.07285.pdf https://adeshpande3.g ...
- day-16 CNN卷积神经网络算法之Max pooling池化操作学习
利用CNN卷积神经网络进行训练时,进行完卷积运算,还需要接着进行Max pooling池化操作,目的是在尽量不丢失图像特征前期下,对图像进行downsampling. 首先看下max pooling的 ...
- Keras(四)CNN 卷积神经网络 RNN 循环神经网络 原理及实例
CNN 卷积神经网络 卷积 池化 https://www.cnblogs.com/peng8098/p/nlp_16.html 中有介绍 以数据集MNIST构建一个卷积神经网路 from keras. ...
- TensorFlow——CNN卷积神经网络处理Mnist数据集
CNN卷积神经网络处理Mnist数据集 CNN模型结构: 输入层:Mnist数据集(28*28) 第一层卷积:感受视野5*5,步长为1,卷积核:32个 第一层池化:池化视野2*2,步长为2 第二层卷积 ...
- tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图
tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图 因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo. 更多教程:http://www.tensorflown ...
随机推荐
- [jQuery]html(),text(),val()方法的区别
1.HTML html():取得第一个匹配元素的html内容.这个函数不能用于XML文档.但可以用于XHTML文档 html(val):设置每一个匹配元素的html内容.这个函数不能用于XML文档.但 ...
- RMAN_学习笔记2_RMAN Setup配置和监控
2014-12-23 Created By BaoXinjian
- 关于@Html.Action()的异常“控制器或该控制器未实现 IController。”
解决之前: @Html.Action("BottomHelp", "Articles", new { num = 5}) 解决之后: @Html.Action( ...
- PaintCode调研
1. 背景 PaintCode是一款面向iOS和Mac应用开发者及设计师的矢量图形可视化开发工具.它可以让设计师把设计好的psd文件直接导入该工具,然后生成用Quartz 2D 产生的object ...
- Android事件分发机制(二)30分钟弄明白Touch事件分发机制
Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewGroup处理. View在 ...
- 10,SFDC 管理员篇 - 流程自动化
1,Process Builder Setup | Build | Create | Workflow & Approvals | Process Builder 当我们在对象中创建或者修改一 ...
- Ms sql将首字母大写
--辅助表 create table a ( a int ) declare @b int begin insert into a values(@b) end; go --表数据 ),id int) ...
- 学习java第二天
首先我们要知道,java是特分大小写的,基本上分为 类名 我们统一小写 如果是多级的 我们用点来隔开 比如 file.test.number1,类或者接口的话基本上大家都是首字母大写,常量全部大写,然 ...
- 开发中model,entity和pojo的区别
Entity接近原始数据,Model接近业务对象- Entity:是专用于EF的对数据库表的操作, Model:是为页面提供数据和数据校验的,所以两者可以并存 POJO:POJO是Plain Ordi ...
- 离线安装VS 2013开发工具的方法!
目前微软已正式发布了VS 2013的开发工具,但安装VS 2013开发工具前必须安装或升级到IE10,否则无法进行安装.本文主要介绍在Windows Server 2008 R2 SP1下离线安装IE ...