Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 0

tyarist/captcha-tensorflow

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
contribute
Sync branch
See difference Through Pull Request Sync
Sync branch
Through Pull Request Sync
A Pull Request will be created to the current
branch and will be merged in to complete the sync
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README

基于tensorflow的‘端到端’的字符型验证码识别

1 Abstract

验证码(CAPTCHA)的诞生本身是为了自动区分 自然人机器人 的一套公开方法, 但是近几年的人工智能技术的发展,传统的字符验证已经形同虚设。 所以,大家一方面研究和学习此代码时,另外一方面也要警惕自己的互联网系统的web安全问题。

Keywords: 人工智能,Python,字符验证码,CAPTCHA,识别,tensorflow,CNN,深度学习

2 Introduction

全自动区分计算机和人类的公开图灵测试(英语:Completely Automated Public Turing test to tell Computers and Humans Apart,簡稱CAPTCHA),俗称验证码,是一种区分用户是计算机或人的公共全自动程序 [1]

得益于基于卷积神经网络CNN的人工智能技术的发展,目前基于主流的深度学习框架的直接开发出 端到端不分割 的识别方式,而且在没有经过太多trick的情况下,已经可以达到95%以上的识别率。

传统的机器学习方法,对于多位字符验证码都是采用的 化整为零 的方法:先分割成最小单位,再分别识别,然后再统一。 卷积神经网络方法,直接采用 端到端不分割 的方法:输入整张图片,输出整个图片的标记结果,具有更强的通用性。

具体的区别如下图:

./images/two-ways-to-hack-captcha.png

端到端 的识别方法显然更具备优势,因为目前的字符型验证码为了防止被识别,多位字符已经完全融合粘贴在一起了,利用传统的技术基本很难实现分割了。本文重点推荐的就是 端到端 的方法。

3 引用声明

本文的绝大部分思路和代码都参考自此文:

http://blog.topspeedsnail.com/archives/10858

感谢:斗大的熊猫--《WTF Daily Blog》

本项目主要解决的问题是对某一模式的字符型验证进行端到端的识别。

输入内容:

./images/tf-captcha-python.png

模型预测结果:

./images/tf-captcha-python-result.png

4 本文工作

  • 解释了原作者代码注释中提到的关于sigmoid选型的困惑问题并应用到代码中
  • 将原作者的代码进行模块工程化,成为整体项目,方便研究的同学直接进行模式套用

原作者代码中:

def train_crack_captcha_cnn():
 output = crack_captcha_cnn()
 # loss
 #loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, Y))
 loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y))
 # 最后一层用来分类的softmax和sigmoid有什么不同?
 # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰
 optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
 ......

作者在代码的注释中出提出了这样的疑问:

softmaxsigmoid 的使用方式有疑问。

然后在文章下面读者评论区也都提到了此问题,在此进行整体解释一下。

原文中CNN的输出的维度是 MAX_CAPTCHA*CHAR_SET_LEN ,其实这些维度并不都是完全独立分布的, 但是使用sigmoid loss也是仍然可以的,相当于先用sigmoid进行了一次归一化,然后再将各个维度的值向目标值进行 回归 , 最后loss越小,两个向量的对应的值也越接近。 其实sigmoid是可以看成是一个多分类的问题,在这个例子上也能起到比较好的收敛效果

当然,关于分类的问题,看所有的机器学习框架里面,都是建议使用softmax进行最后的归一化操作,这其实相当于是一种 马太效应 : 让可能性大的分类的值变得更大,让可能性小的分量值变得更小。但是这有个前提,就是参与到softmax运算的一组数据,必须是 相关联 的, 所以如果要使用 softmax_cross_entropy_with_logits ,只需要将网络进行简单修改即可。把输出的维度做成二维[MAX_CAPTCHA, CHAR_SET_LEN], 然后使用softmax loss。

output = crack_captcha_cnn()×ばつ4
predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) # 36行,4列
label = tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN])

最后使用GPU训练的实验结果对比:

  • sigmoid方式。训练6000个step就能达到95%的准确率。
  • softmax方式。训练8千个step,达到90%的准确率;训练8万个step,达到94.7%(跑了大半天)

使用tensorboard对accuracy进行监控:

sigmoid-6千个step:

./images/2017-06-20-sigmoid-6k.png

softmax-8千个step:

./images/2017-06-22-softmax-8k.png

softmax-8万个step:

./images/2017-06-23-softmax-8w.png

整体来说,在这个例子里面,好像 sigmoid的收敛速度快些,当然这个可能是本项目里面的外界因素有利于sigmoid吧,至于具体原因,等后续再进行研究和解释吧,当然有可能根本解释不了,因为对于CNN,目前主流的意见都是:,反正效果就是好,但是不知道为啥, 科幻得近于玄幻 的一种技术。

项目文件介绍:

  • cfg.py 配置信息文件
  • cnn_sys.py CNN网络结构
  • data_iter.py 可迭代的数据集
  • gen_captcha.py 验证码生成器,直接使用程序生成带标记的数据
  • predict.py 加载训练好的模型,然后对输入的图片进行预测
  • train.py 对模型进行训练
  • utils.py 一些公共使用的方法

5 小结

本文主要只写原作者没有提到的内容,想了解原文的,可以直接去原作者页面。

6 Reference

[1] wiki-CAPTCHA 7 后续交流

如果有对相关技术有持续关注的兴趣的同学,欢迎加入QQ群:

592109504

或者手机QQ扫码加入:

./images/qq-group-592109504.jpg

8 友情链接

有朋友在此基础上,做了个中文汉字验证码的分类项目,请移步:

https://github.com/wang5150753/tensorflow
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

hack captcha by cnn with tensorflow
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nicely/captcha-tensorflow.git
git@gitee.com:nicely/captcha-tensorflow.git
nicely
captcha-tensorflow
captcha-tensorflow
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /