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的更多相关文章

  1. Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN

    http://m.blog.csdn.net/blog/wu010555688/24487301 本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep le ...

  2. Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)

    Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现 zouxy09@qq.com http://blog.csdn.net/zouxy09          自己平时看了一些论文, ...

  3. cnn(卷积神经网络)比较系统的讲解

    本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之 ...

  4. CNN(卷积神经网络)、RNN(循环神经网络)、DNN(深度神经网络)的内部网络结构有什么区别?

    https://www.zhihu.com/question/34681168 CNN(卷积神经网络).RNN(循环神经网络).DNN(深度神经网络)的内部网络结构有什么区别?修改 CNN(卷积神经网 ...

  5. CNN(卷积神经网络)、RNN(循环神经网络)、DNN,LSTM

    http://cs231n.github.io/neural-networks-1 https://arxiv.org/pdf/1603.07285.pdf https://adeshpande3.g ...

  6. day-16 CNN卷积神经网络算法之Max pooling池化操作学习

    利用CNN卷积神经网络进行训练时,进行完卷积运算,还需要接着进行Max pooling池化操作,目的是在尽量不丢失图像特征前期下,对图像进行downsampling. 首先看下max pooling的 ...

  7. Keras(四)CNN 卷积神经网络 RNN 循环神经网络 原理及实例

    CNN 卷积神经网络 卷积 池化 https://www.cnblogs.com/peng8098/p/nlp_16.html 中有介绍 以数据集MNIST构建一个卷积神经网路 from keras. ...

  8. TensorFlow——CNN卷积神经网络处理Mnist数据集

    CNN卷积神经网络处理Mnist数据集 CNN模型结构: 输入层:Mnist数据集(28*28) 第一层卷积:感受视野5*5,步长为1,卷积核:32个 第一层池化:池化视野2*2,步长为2 第二层卷积 ...

  9. tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图

    tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图 因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo. 更多教程:http://www.tensorflown ...

随机推荐

  1. 架构师养成记--11.Executor概述

    常用方法 Executors.newFiexdPool(int nThreads);固定线程数量的线程池: Executors.newSingleThreadExecutor();单个线程的线程池: ...

  2. Windows Store App 旋转特效

    使用Projection类可以实现界面元素的三维效果,它可以使界面上的元素在三维空间中沿着X轴.Y轴或者Z轴旋转一定的角度,在透视转换中此类又称为元素的Projection属性,用于对元素使用3D特效 ...

  3. 将PHP作为Shell脚本语言使用

    我们都知道.PHP是一种非常好的动态网页开发语言(速度飞快.开发周期短--).可是仅仅有非常少数的人意识到PHP也能够非常好的作为编写Shell脚本的语言,当PHP作为编写Shell脚本的语言时,他并 ...

  4. 类、对象以及jvm运行内存解析

    一.JVM内存的分析: 第一步:存放在硬盘上的程序首先要被加载到内存空间中. 第二步:内存中的jvm找到程序中main函数作为入口,然后开始执行. 第三步:执行过程中的内存管理:内存分为四个部分: 栈 ...

  5. 大数据学习(2)HDFS文件管理

    命令行管理HDFS [root@server1 bin]# hadoop fs Usage: hadoop fs [generic options] [-appendToFile <locals ...

  6. monkey----log分析要求

    对monkey测试过程中生成的XXX.log文件中进行关键字的查找.主要查找讯息如下: (1) anr项:即无响应,一般形式为ANR in org.codeaurora.bluetooth: (2)c ...

  7. layer.alert没有垂直居中

    经查找是因为 <!DOCTYPE html> 这句没有写在整个页面的最顶部,将其放在整个页面的第一行就可以了. ps:原理不是很清楚

  8. 07: linux中正则表达式与grep使用

    1.1 linux中正则表达式 1.^linux        以linux开头的行 2.$php         以php结尾的行 3..                匹配任意单字符 4..+  ...

  9. Mysql行转列的简单应用

    最近在复习过程中愈发觉得,有些东西久了不用,真的会忘~——~. 将上面的表格转换为下面的表格 我拼sql拼了好久还是没弄出来,还是偶然看到我以前的笔记,才想起有行转列这样的操作(太久没有写过复杂点的s ...

  10. c语言复制文件程序

    #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 1024*1024* ...