cnn-captcha-trainer

冬晨夕阳 2021-01-14 09:53:24 5124

本文简介如何使用captcha_trainer开源框架来进行验证码识别训练。

项目下载地址: https://github.com/kerlomz/captcha_trainer


安装

跟随 requirements.txt 文件一键安装或者自行安装。
安装完成之后,在app.py文件启动界面。

GUI界面中的功能介绍:
在这里插入图片描述

在app.py中可以看到详细代码和描述

    # 训练集源路径 - 标签
    self.dataset_train_path_text
    # 训练集源路径 - 输入控件
    self.source_train_path_listbox
    # 训练集源路径 - 按钮
    self.btn_browse_train
    # 验证集源路径 - 标签
    self.label_edge
    # 验证集源路径 - 输入控件
    self.source_validation_path_listbox
    # 训练集源路径 - 按钮
    self.btn_browse_validation
    # 卷积层 - 下拉框
    self.comb_neu_cnn
    # 循环层 - 标签
    self.neu_recurrent_text
    # 循环层 - 下拉框
    self.comb_recurrent
    # 循环层单元数 - 标签
    self.units_num_text
    # 损失函数 - 标签
    self.loss_func_text
    # 损失函数 - 下拉框
    self.comb_loss
    # 优化器 - 标签
    self.optimizer_text

network中网络实现:

  • CNN.py
  • DenseNet.py
  • GRU.py
  • LSTM.py
  • MobileNet.py
  • ResNet.py
    比如LSTM:
import tensorflow as tf
from config import RunMode, ModelConfig
from network.utils import NetworkUtils

class LSTM(object):
    """
    LSTM 网络实现
    """
    def __init__(self, model_conf: ModelConfig, inputs: tf.Tensor, utils: NetworkUtils):
        """
        :param model_conf: 配置
        :param inputs: 网络上一层输入 tf.keras.layers.Input / tf.Tensor 类型
        :param utils: 网络工具类
        """
        self.model_conf = model_conf
        self.inputs = inputs
        self.utils = utils
        self.layer = None

    def build(self):
        """
        循环层构建参数
        :return: 返回循环层的输出层
        """
        with tf.keras.backend.name_scope('LSTM'):
            mask = tf.keras.layers.Masking()(self.inputs)
            self.layer = tf.keras.layers.LSTM(
                units=self.model_conf.units_num * 2,
                return_sequences=True,
                input_shape=mask.shape,
                dropout=0.2,
                recurrent_dropout=0.1
            )
            outputs = self.layer(mask, training=self.utils.is_training)
        return outputs

class BiLSTM(object):

    def __init__(self, model_conf: ModelConfig, inputs: tf.Tensor, utils: NetworkUtils):
        """同上"""
        self.model_conf = model_conf
        self.inputs = inputs
        self.utils = utils
        self.layer = None

    def build(self):
        """同上"""
        with tf.keras.backend.name_scope('BiLSTM'):
            mask = tf.keras.layers.Masking()(self.inputs)
            self.layer = tf.keras.layers.Bidirectional(
                layer=tf.keras.layers.LSTM(
                    units=self.model_conf.units_num,
                    return_sequences=True,
                ),
                input_shape=mask.shape,
            )
            outputs = self.layer(mask, training=self.utils.is_training)
        return outputs

class LSTMcuDNN(object):

    def __init__(self, model_conf: ModelConfig, inputs: tf.Tensor, utils: NetworkUtils):
        """同上"""
        self.model_conf = model_conf
        self.inputs = inputs
        self.utils = utils
        self.layer = None

    def build(self):
        """同上"""
        with tf.keras.backend.name_scope('LSTM'):
            self.layer = tf.keras.layers.CuDNNLSTM(
                units=self.model_conf.units_num * 2,
                return_sequences=True,
            )
            outputs = self.layer(self.inputs, training=self.utils.is_training)
        return outputs

class BiLSTMcuDNN(object):

    def __init__(self, model_conf: ModelConfig, inputs: tf.Tensor, utils: NetworkUtils):
        """同上"""
        self.model_conf = model_conf
        self.inputs = inputs
        self.utils = utils
        self.layer = None

    def build(self):
        """同上"""
        with tf.keras.backend.name_scope('BiLSTM'):
            self.layer = tf.keras.layers.Bidirectional(
                layer=tf.keras.layers.CuDNNLSTM(
                    units=self.model_conf.units_num,
                    return_sequences=True
                )
            )
            outputs = self.layer(self.inputs, training=self.utils.is_training)
        return outputs


数据集下载

数据量不多有1w张验证码图片。
验证码数据集: https://pan.baidu.com/s/12iH5lpoXLAOTEiaQpoz7jg
提取码: r5ux
在这里插入图片描述

开始训练

输入项目名字,选择训练集,标签数,样本大小,结束条件等。
然后 Start Training 开始训练。

在这里插入图片描述

我之前安装的 tensorflow2.0.0,不太想退版本,所以使用 CPU来进行训练,训练速度较为缓慢。

建议按照作者的推荐使用 tensorflow-gpu==1.14.0。

另外为了减少时间 End Accuracy 可以修改低一点。平均损失End Cost可以高一点。学习率修改大一点。

在这里插入图片描述
在这里插入图片描述

查看训练过程 (如果感兴趣的话)

可以使用 tensorBoard 来查看训练过程。
cmd输出 tensorboard --logdir \路径\captcha_trainer\projects\weiboCaptcha-CNNX-GRU-H64-CTC-C1\model
然后访问 http://localhost:6006/
在这里插入图片描述
其实通过 model\checkpoint 文件也可以查看当前训练进度。

训练结束会在项目路径的out下看到以下结构的文件,pb为模型,yaml为模型配置文件,下面该到部署环节了。

部署

项目地址:https://github.com/kerlomz/captcha_platform

这里web服务调用仅作为一个例子,你可以使用项目提供的 tornado_server.py、flask_server.py 来部署,也可以使用gRPC集群远程调用。

部署方法:
将训练好的model.yaml放在模型文件夹中,并将model.pb放在图形文件夹中(如果不存在则创建),然后启动服务即可。


Informed

The default value requires that.
Demonstrates. Py: an example of how to call a prediction method.
The model folder is used to store model configuration files, such as model. Yaml
The graph folder is used to store compiled models, such as model. Pb
The deployment service will automatically load all models in the model configuration. When adding a new model configuration, the corresponding compiled model in the graph folder will be loaded automatically. Therefore, if you need to add it, please copy the corresponding compiled model to the graph path first, and then add the model configuration.

声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包 95 9 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
冬晨夕阳
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区