一次基于Tensorflow+CNN的验证码识别之旅

对于本次基于卷积神经网络识别验证码有着非常大的兴趣,所以尝试性地去做了测试,过程当中踩了不少坑,也参考了许多前辈的博客和教程,最终识别率可达到98.25%

一、下图是训练的过程:

训练过程


二、实验的情况简介:

实验环境:Python3.6Centos 7.3Tensorflow 1.9

训练的过程是放到远程服务器上跑的,1H1G的配置,没有GPU,所以训练总耗时5小时

在本地机器Win10+MX150+Python3.6+Tensorflow1.9环境下,CUDA总是找不到xxx90.dll,按照网上的大佬博客记录来还是无法解决,遂放弃而转移到服务器上慢慢跑

验证码的来源是 Python中一个用于生成验证码的captcha库,其生成的验证码UCkV如下:

UCKV

本次实验中为了验证训练效果,从速度上考虑,只做了数字验证码的识别


三、实验过程:

需要验证码,首先我想到自己用PHP简单写了一个验证码的生成程序,也是为了验证模型的准确率,发现如果读文件的形式效率不是很高,故直接采用在python下的captcha库来生成有一定复杂度的验证码。

如下是生成验证码的程序,可生成数字+字母大小写的任意长度验证码

# coding:utf-8
# name:captcha_gen.py

import random
import numpy as np
from PIL import Image
from captcha.image import ImageCaptcha


NUMBER = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
LOW_CASE = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z']
UP_CASE = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
           'V', 'W', 'X', 'Y', 'Z']

CAPTCHA_LIST = NUMBER
CAPTCHA_LEN = 4            # 验证码长度
CAPTCHA_HEIGHT = 60        # 验证码高度
CAPTCHA_WIDTH = 160        # 验证码宽度


def random_captcha_text(char_set=CAPTCHA_LIST, captcha_size=CAPTCHA_LEN):
    """
    随机生成定长字符串
    :param char_set: 备选字符串列表
    :param captcha_size: 字符串长度
    :return: 字符串
    """
    captcha_text = [random.choice(char_set) for _ in range(captcha_size)]
    return ''.join(captcha_text)


def gen_captcha_text_and_image(width=CAPTCHA_WIDTH, height=CAPTCHA_HEIGHT, save=None):
    """
    生成随机验证码
    :param width: 验证码图片宽度
    :param height: 验证码图片高度
    :param save: 是否保存(None)
    :return: 验证码字符串,验证码图像np数组
    """
    image = ImageCaptcha(width=width, height=height)
    # 验证码文本
    captcha_text = random_captcha_text()
    captcha = image.generate(captcha_text)
    # 保存
    if save:
        image.write(captcha_text, './img/' + captcha_text + '.jpg')
    captcha_image = Image.open(captcha)
    # 转化为np数组
    captcha_image = np.array(captcha_image)
    return captcha_text, captcha_image


if __name__ == '__main__':
    t, im = gen_captcha_text_and_image(save=True)
    print(t, im.shape)      # (60, 160, 3)

然后编写一个工具库,用于调用验证码生成程序来生成训练集

# -*- coding:utf-8 -*-
# name: util.py

import numpy as np
from captcha_gen import gen_captcha_text_and_image
from captcha_gen import CAPTCHA_LIST, CAPTCHA_LEN, CAPTCHA_HEIGHT, CAPTCHA_WIDTH


def convert2gray(img):
    """
    图片转为黑白,3维转1维
    :param img: np
    :return:  灰度图的np
    """
    if len(img.shape) > 2:
        img = np.mean(img, -1)
    return img


def text2vec(text, captcha_len=CAPTCHA_LEN, captcha_list=CAPTCHA_LIST):
    """
    验证码文本转为向量
    :param text:
    :param captcha_len:
    :param captcha_list:
    :return: vector 文本对应的向量形式
    """
    text_len = len(text)    # 欲生成验证码的字符长度
    if text_len > captcha_len:
        raise ValueError('验证码最长4个字符')
    vector = np.zeros(captcha_len * len(captcha_list))      # 生成一个一维向量 验证码长度*字符列表长度
    for i in range(text_len):
        vector[captcha_list.index(text[i])+i*len(captcha_list)] = 1     # 找到字符对应在字符列表中的下标值+字符列表长度*i 的 一维向量 赋值为 1
    return vector


def vec2text(vec, captcha_list=CAPTCHA_LIST, captcha_len=CAPTCHA_LEN):
    """
    验证码向量转为文本
    :param vec:
    :param captcha_list:
    :param captcha_len:
    :return: 向量的字符串形式
    """
    vec_idx = vec
    text_list = [captcha_list[int(v)] for v in vec_idx]
    return ''.join(text_list)


def wrap_gen_captcha_text_and_image(shape=(60, 160, 3)):
    """
    返回特定shape图片
    :param shape:
    :return:
    """
    while True:
        t, im = gen_captcha_text_and_image()
        if im.shape == shape:
            return t, im


def get_next_batch(batch_count=60, width=CAPTCHA_WIDTH, height=CAPTCHA_HEIGHT):
    """
    获取训练图片组
    :param batch_count: default 60
    :param width: 验证码宽度
    :param height: 验证码高度
    :return: batch_x, batch_yc
    """
    batch_x = np.zeros([batch_count, width * height])
    batch_y = np.zeros([batch_count, CAPTCHA_LEN * len(CAPTCHA_LIST)])
    for i in range(batch_count):    # 生成对应的训练集
        text, image = wrap_gen_captcha_text_and_image()
        image = convert2gray(image)     # 转灰度numpy
        # 将图片数组一维化 同时将文本也对应在两个二维组的同一行
        batch_x[i, :] = image.flatten() / 255
        batch_y[i, :] = text2vec(text)  # 验证码文本的向量形式
    # 返回该训练批次
    return batch_x, batch_y


if __name__ == '__main__':
    x, y = get_next_batch(batch_count=1)    # 默认为1用于测试集
    print(x, y)

然后编辑训练程序

# -*- coding:utf-8 -*-
# name: model_train.py

import tensorflow as tf
from datetime import datetime
from util import get_next_batch
from captcha_gen import CAPTCHA_HEIGHT, CAPTCHA_WIDTH, CAPTCHA_LEN, CAPTCHA_LIST


def weight_variable(shape, w_alpha=0.01):
    """
    初始化权值
    :param shape:
    :param w_alpha:
    :return:
   """
    initial = w_alpha * tf.random_normal(shape)
    return tf.Variable(initial)


def bias_variable(shape, b_alpha=0.1):
    """
    初始化偏置项
    :param shape:
    :param b_alpha:
    :return:
    """
    initial = b_alpha * tf.random_normal(shape)
    return tf.Variable(initial)


def conv2d(x, w):
    """
    卷基层 :局部变量线性组合,步长为1,模式‘SAME’代表卷积后图片尺寸不变,即零边距
    :param x:
    :param w:
    :return:
    """
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')


def max_pool_2x2(x):
    """
    池化层:max pooling,取出区域内最大值为代表特征, 2x2 的pool,图片尺寸变为1/2
    :param x:
    :return:
    """
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')


def cnn_graph(x, keep_prob, size, captcha_list=CAPTCHA_LIST, captcha_len=CAPTCHA_LEN):
    """
    三层卷积神经网络
    :param x:   训练集 image x
    :param keep_prob:   神经元利用率
    :param size:        大小 (高,宽)
    :param captcha_list:
    :param captcha_len:
    :return: y_conv
    """
    # 需要将图片reshape为4维向量
    image_height, image_width = size
    x_image = tf.reshape(x, shape=[-1, image_height, image_width, 1])

    # 第一层
    # filter定义为3x3x1, 输出32个特征, 即32个filter
    w_conv1 = weight_variable([3, 3, 1, 32])    # 3*3的采样窗口,32个(通道)卷积核从1个平面抽取特征得到32个特征平面
    b_conv1 = bias_variable([32])
    h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)    # rulu激活函数
    h_pool1 = max_pool_2x2(h_conv1)     # 池化
    h_drop1 = tf.nn.dropout(h_pool1, keep_prob)      # dropout防止过拟合

    # 第二层
    w_conv2 = weight_variable([3, 3, 32, 64])
    b_conv2 = bias_variable([64])
    h_conv2 = tf.nn.relu(conv2d(h_drop1, w_conv2) + b_conv2)
    h_pool2 = max_pool_2x2(h_conv2)
    h_drop2 = tf.nn.dropout(h_pool2, keep_prob)

    # 第三层
    w_conv3 = weight_variable([3, 3, 64, 64])
    b_conv3 = bias_variable([64])
    h_conv3 = tf.nn.relu(conv2d(h_drop2, w_conv3) + b_conv3)
    h_pool3 = max_pool_2x2(h_conv3)
    h_drop3 = tf.nn.dropout(h_pool3, keep_prob)

    """
    原始:60*160图片 第一次卷积后 60*160 第一池化后 30*80
    第二次卷积后 30*80 ,第二次池化后 15*40
    第三次卷积后 15*40 ,第三次池化后 7.5*20 = > 向下取整 7*20
    经过上面操作后得到7*20的平面
    """

    # 全连接层
    image_height = int(h_drop3.shape[1])
    image_width = int(h_drop3.shape[2])
    w_fc = weight_variable([image_height*image_width*64, 1024])     # 上一层有64个神经元 全连接层有1024个神经元
    b_fc = bias_variable([1024])
    h_drop3_re = tf.reshape(h_drop3, [-1, image_height*image_width*64])
    h_fc = tf.nn.relu(tf.matmul(h_drop3_re, w_fc) + b_fc)
    h_drop_fc = tf.nn.dropout(h_fc, keep_prob)

    # 输出层
    w_out = weight_variable([1024, len(captcha_list)*captcha_len])
    b_out = bias_variable([len(captcha_list)*captcha_len])
    y_conv = tf.matmul(h_drop_fc, w_out) + b_out
    return y_conv


def optimize_graph(y, y_conv):
    """
    优化计算图
    :param y: 正确值
    :param y_conv:  预测值
    :return: optimizer
    """
    # 交叉熵代价函数计算loss 注意logits输入是在函数内部进行sigmod操作
    # sigmod_cross适用于每个类别相互独立但不互斥,如图中可以有字母和数字
    # softmax_cross适用于每个类别独立且排斥的情况,如数字和字母不可以同时出现
    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=y_conv))
    # 最小化loss优化 AdaminOptimizer优化
    optimizer = tf.train.AdamOptimizer(1e-3).minimize(loss)
    return optimizer


def accuracy_graph(y, y_conv, width=len(CAPTCHA_LIST), height=CAPTCHA_LEN):
    """
    偏差计算图,正确值和预测值,计算准确度
    :param y: 正确值 标签
    :param y_conv:  预测值
    :param width:   验证码预备字符列表长度
    :param height:  验证码的大小,默认为4
    :return:    正确率
    """
    # 这里区分了大小写 实际上验证码一般不区分大小写,有四个值,不同于手写体识别
    # 预测值
    predict = tf.reshape(y_conv, [-1, height, width])   #
    max_predict_idx = tf.argmax(predict, 2)
    # 标签
    label = tf.reshape(y, [-1, height, width])
    max_label_idx = tf.argmax(label, 2)
    correct_p = tf.equal(max_predict_idx, max_label_idx)    # 判断是否相等
    accuracy = tf.reduce_mean(tf.cast(correct_p, tf.float32))
    return accuracy


def train(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH, y_size=len(CAPTCHA_LIST)*CAPTCHA_LEN):
    """
    cnn训练
    :param height: 验证码高度
    :param width:   验证码宽度
    :param y_size:  验证码预备字符列表长度*验证码长度(默认为4)
    :return:
    """
    # cnn在图像大小是2的倍数时性能最高, 如果图像大小不是2的倍数,可以在图像边缘补无用像素
    # 在图像上补2行,下补3行,左补2行,右补2行
    # np.pad(image,((2,3),(2,2)), 'constant', constant_values=(255,))

    acc_rate = 0.95     # 预设模型准确率标准
    # 按照图片大小申请占位符
    x = tf.placeholder(tf.float32, [None, height * width])
    y = tf.placeholder(tf.float32, [None, y_size])
    # 防止过拟合 训练时启用 测试时不启用 神经元使用率
    keep_prob = tf.placeholder(tf.float32)
    # cnn模型
    y_conv = cnn_graph(x, keep_prob, (height, width))
    # 优化
    optimizer = optimize_graph(y, y_conv)
    # 计算准确率
    accuracy = accuracy_graph(y, y_conv)
    # 启动会话.开始训练
    saver = tf.train.Saver()
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())     # 初始化
    step = 0    # 步数
    while 1:
        batch_x, batch_y = get_next_batch(64)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: 0.75})
        # 每训练一百次测试一次
        if step % 100 == 0:
            batch_x_test, batch_y_test = get_next_batch(100)
            acc = sess.run(accuracy, feed_dict={x: batch_x_test, y: batch_y_test, keep_prob: 1.0})
            print(datetime.now().strftime('%c'), ' step:', step, ' accuracy:', acc)
            # 准确率满足要求,保存模型
            if acc > acc_rate:
                model_path = "./model/captcha.model"
                saver.save(sess, model_path, global_step=step)
                acc_rate += 0.01
                if acc_rate > 0.99:     # 准确率达到99%则退出
                    break
        step += 1
    sess.close()


if __name__ == '__main__':
    train()

训练程序将准确率超过0.95的模型保存到./model/文件夹下

测试模型效果:

# -*- coding:utf-8 -*-
# name: model_test.py

import tensorflow as tf
from model_train import cnn_graph
from captcha_gen import gen_captcha_text_and_image
from util import vec2text, convert2gray
from util import CAPTCHA_LIST, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LEN
from PIL import Image


def captcha2text(image_list, height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH):
    """
    验证码图片转化为文本
    :param image_list:
    :param height:
    :param width:
    :return:
    """
    x = tf.placeholder(tf.float32, [None, height * width])
    keep_prob = tf.placeholder(tf.float32)
    y_conv = cnn_graph(x, keep_prob, (height, width))
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, tf.train.latest_checkpoint('model/'))
        predict = tf.argmax(tf.reshape(y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2)
        vector_list = sess.run(predict, feed_dict={x: image_list, keep_prob: 1})
        vector_list = vector_list.tolist()
        text_list = [vec2text(vector) for vector in vector_list]
        return text_list


if __name__ == '__main__':
    text, image = gen_captcha_text_and_image()
    img = Image.fromarray(image)
    image = convert2gray(image)
    image = image.flatten() / 255
    pre_text = captcha2text([image])
    print("验证码正确值:", text, ' 模型预测值:', pre_text)
    img.show()

在测试几次中均100%预测正确:

预测

如上 32 黏在一起是不好分辨的,但是训练出来的模型效果还不错!


四、踩坑之痛:

1.验证码生成之痛:

这部分就很痛了,生成验证码首先去研究了一下PHP验证码的生成,还是粘贴一下代码吧:

<!-- code.php  -->

<?php
header("Content-type: image/PNG");


//生成验证码,用于样本
function getCode($num,$w,$h,$code) {

  //创建图片,定义颜色值
  $im = imagecreate($w, $h);
  $red = imagecolorallocate($im, 255, 0, 0);
  $black = imagecolorallocate($im, 0, 0, 0);
  $bgcolor = imagecolorallocate($im, 255, 255, 255);
  //填充背景
  imagefill($im, 0, 0, $bgcolor);

  //在画布上随机生成大量红点,起干扰作用;
  /*for ($i = 0; $i < 80; $i++) {
    imagesetpixel($im, rand(0, $w), rand(0, $h), $red);
  }*/
  //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
  $strx = rand(3, 8);
  for ($i = 0; $i < $num; $i++) {
    $strpos = rand(1, 6);
    imagestring($im, 8, $strx, $strpos, substr($code, $i, 1), $black);
    $strx += rand(10, 12);
  }
  imagepng($im,'img/'.$code.'.png');//输出图片
  imagedestroy($im);//释放图片所占内存
}

$count = 0;
$code_num = 1000;
while($count < 3000){
  getCode(4,50,22,$code_num);
  $count += 1;
  $code_num += 1;
}

exit(0);
?>

运行后访问./img/ 目录:

验证码PHP

为了简单所以生成验证码比较简单,就感觉没什么学习的价值,到文件读取验证码效率还是很低的

如上图生成的验证码可以直接利用 Tesseract OCR 识别OK了,而且经过灰度处理,降噪,识别正确率90%左右


2. CUDA 安装之痛

CPU来跑训练模型确实太慢了,想要使用GPU来加速,遂选择安装CUDA

反反复复,下载了cuda9.0 9.1 10.0 三个版本,每个基本在 1.5G 大小,反反复复地安装,最终选择放弃,CSDN 上的那些 只复制转载却标原创 的文章能不能少点(误人子弟),555~

原创

cuda 对应着不同的 tensorflow 版本,和 cuDNN,都需要反反复复卸载重装!

下次去网吧跑一下吧,感觉网吧用的显卡性能不错!

推荐一篇不错的文章:《windows10下安装Tensorflow-GPU跑深度学习(Nvidia-MX150)》


五、总结:

这次对于验证码识别技术的探索过程,历时5天,受益匪浅。机器学习真是个好东西,后续会深入研究,好好学习,天天向上!

学习快乐


六、文章参考:


七、附件下载

Github: https://github.com/dyboy2017/captcha_check

发表评论 / Comment

用心评论~

金玉良言 / Appraise
Y.LV 1
2022-12-11 13:39
先问一下作者您用到的算法原理和网络结构是啥,希望作者各给回复
LV 1
2019-11-12 21:25
很好的文章!
JinnrryLV 1
2019-11-05 16:09
作者在吗?请问您的代码如果要转成pb模型保存该怎么改呢?输出层的名字是叫init吗?我在您github上有提issue,麻烦看一眼呗
mooncLV 1
2019-05-03 19:16
试了一下加上大小写之后,目前跑了4000多了,但是正确率依旧在0.015-0.02……这是要跑多久才能有所收效……
头像
DYBOY站长已认证
2019-05-08 08:03
@moonc:github上有具体的训练过程记录:https://github.com/dyboy2017/captcha_check

加上之后肯定需要更多的训练时间,放到服务器上跑跑~
头像
小明LV 1
2019-09-16 08:20
@moonc:我也是差不多,而且我都训练了13000多次了还是这么低
ponyLV 2
2019-04-01 16:55
不错的文章

Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/blog.dyboy.cn/content/templates/dyblog/footer.php:56) in /www/wwwroot/blog.dyboy.cn/include/lib/view.php on line 23