深度学习算法之CNN-验证码识别

使用CNN算法,特征提取使用二维向量:

X, Y, testX, testY = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    testX = testX.reshape([-1, 28, 28, 1])

实例化CNN算法并训练10轮:

model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit({'input': X}, {'target': Y}, n_epoch=5,
               validation_set=({'input': testX}, {'target': testY}),
               snapshot_step=100, show_metric=True, run_id='mnist')

整个过程如下:

(1)读取MNIST数据集数据。

(2)转换成二维向量。

(3)按照文件划分为训练集合测试集。

(4)使用CNN算法在训练集上训练,获得模型数据

(5)使用模型数据在测试集上进行预测。

(6)验证CNN算法预测效果。

# -*- coding: utf-8 -*-

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
from sklearn.neural_network import MLPClassifier
from sklearn.feature_extraction.text import CountVectorizer
import os
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn import svm
from sklearn import neighbors

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist

#构建卷积神经网络
def do_cnn_2d(X, Y, testX, testY ):
    # Building convolutional network
    network = input_data(shape=[None, 28, 28, 1], name='input')
    network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
    network = max_pool_2d(network, 2)
    network = local_response_normalization(network)
    network = fully_connected(network, 128, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 256, activation='tanh')
    network = dropout(network, 0.8)
    network = fully_connected(network, 10, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.01,
                         loss='categorical_crossentropy', name='target')

    # Training
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.fit({'input': X}, {'target': Y}, n_epoch=5,
               validation_set=({'input': testX}, {'target': testY}),
               snapshot_step=100, show_metric=True, run_id='mnist')
if __name__ == "__main__":
    print("Hello MNIST")
    # 2d,2维提取特征
    X, Y, testX, testY = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    testX = testX.reshape([-1, 28, 28, 1])

    #cnn
    do_cnn_2d(X, Y, testX, testY)

运行过程,得到如下结果,准确率可达97.52%。

深度学习算法之CNN-验证码识别_第1张图片

你可能感兴趣的:(深度学习算法之CNN-验证码识别)