Git Product home page Git Product logo

cnn_captcha's Introduction

cnn_captcha

use CNN recognize captcha by tensorflow.
本项目针对字符型图片验证码,使用tensorflow实现卷积神经网络,进行验证码识别。
项目封装了比较通用的校验、训练、验证、识别、API模块,极大的减少了识别字符型验证码花费的时间和精力。

项目已经帮助很多同学高效完成了验证码识别任务。 如果你在使用过程中出现了bug和做了良好的改进,欢迎提出issue和PR,作者会尽快回复,希望能和你共同完善项目。

如果你需要识别点选、拖拽类验证码,或者有目标检测需求,也可以参考这个项目nickliqian/darknet_captcha

时间表

2018.11.12

初版Readme.md

2018.11.21

加入关于验证码识别的一些说明

2018.11.24

优化校验数据集图片的规则

2018.11.26

新增train_model_v2.py文件,训练过程中同时输出训练集和验证集的准确率

2018.12.06

新增多模型部署支持,修复若干bug

2018.12.08

优化模型识别速度,支持api压力测试和统计耗时

2019.02.19

  1. 新增一种准确率计算方式
  2. TAG: v1.0

2019.04.12

  1. 只保留一种train_model.py文件
  2. 优化代码结构
  3. 把通用配置抽取到sample_config.jsoncaptcha_config.json
  4. 修复若干大家在issue提出的问题

2019.06.01

  1. 完善readme文档,文档不长,请大家一定要读完~
  2. 使用cnnlib目录存放神经网络结构代码
  3. 做了一版训练数据统计,大家可以参考我们的训练次数、时长和准确率
  4. TAG: v2.0

目录

1 项目介绍

2 如何使用

3 统计数据

4 开发说明

5 已知BUG

1 项目介绍

1.1 关于验证码识别

验证码识别大多是爬虫会遇到的问题,也可以作为图像识别的入门案例。目前通常使用如下几种方法:

方法名称 相关要点
tesseract 仅适合识别没有干扰和扭曲的图片,训练起来很麻烦
其他开源识别库 不够通用,识别率未知
付费OCR API 需求量大的情形成本很高
图像处理+机器学习分类算法 涉及多种技术,学习成本高,且不通用
卷积神经网络 一定的学习成本,算法适用于多类验证码

这里说一下使用传统的图像处理和机器学习算法,涉及多种技术:

  1. 图像处理
  • 前处理(灰度化、二值化)
  • 图像分割
  • 裁剪(去边框)
  • 图像滤波、降噪
  • 去背景
  • 颜色分离
  • 旋转
  1. 机器学习
  • KNN
  • SVM

使用这类方法对使用者的要求较高,且由于图片的变化类型较多,处理的方法不够通用,经常花费很多时间去调整处理步骤和相关算法。
而使用卷积神经网络,只需要通过简单的前处理,就可以实现大部分静态字符型验证码的端到端识别,效果很好,通用性很高。

这里列出目前常用的验证码生成库:

参考:Java验证全家桶

语言 验证码库名称 链接 样例
Java JCaptcha 示例 效果1 效果2 效果3
Java JCaptcha4Struts2
Java SimpleCaptcha 例子 效果1 效果2 效果3
Java kaptcha 例子 水纹效果 鱼眼效果 阴影效果
Java patchca 效果1
Java imageRandom
Java iCaptcha 效果1
Java SkewPassImage 效果1
Java Cage 效果1 效果2
Python captcha 例子 py_Captcha
Python pycapt 例子 pycapt
PHP Gregwar/Captcha 文档
PHP mewebstudio/captcha 文档

1.2 目录结构

1.2.1 基本配置

序号 文件名称 说明
1 conf/ 配置文件目录
2 sample/ 数据集目录
3 model/ 模型文件目录
4 cnnlib/ 封装CNN的相关代码目录

1.2.2 训练模型

序号 文件名称 说明
1 verify_and_split_data.py 验证数据集、拆分数据为训练集和测试集
2 network.py cnn网络基类
3 train_model.py 训练模型
4 test_batch.py 批量验证
5 gen_image/gen_sample_by_captcha.py 生成验证码的脚本
6 gen_image/collect_labels.py 用于统计验证码标签(常用于中文验证码)

1.2.3 web接口

序号 文件名称 说明
1 webserver_captcha_image.py 获取验证码接口
2 webserver_recognize_api.py 提供在线识别验证码接口
3 recognize_online.py 使用接口识别的例子
4 recognize_local.py 测试本地图片的例子
5 recognize_time_test.py 压力测试识别耗时和请求响应耗时

1.3 依赖

pip install -r requirements.txt

注意:如果需要使用GPU进行训练,请把文件中的tenforflow修改为tensorflow-gpu

1.4 模型结构

序号 层级
输入 input
1 卷积层 + 池化层 + 降采样层 + ReLU
2 卷积层 + 池化层 + 降采样层 + ReLU
3 卷积层 + 池化层 + 降采样层 + ReLU
4 全连接 + 降采样层 + Relu
5 全连接 + softmax
输出 output

2 如何使用

2.1 数据集

原始数据集可以存放在./sample/origin目录中。
为了便于处理,图片最好以2e8j_17322d3d4226f0b5c5a71d797d2ba7f7.jpg格式命名(标签_序列号.后缀)。

如果你没有训练集,你可以使用gen_sample_by_captcha.py文件生成训练集文件。 生成之前你需要修改相关配置conf/captcha_config.json(路径、文件后缀、字符集等)。

{
  "root_dir": "sample/origin/",  # 验证码保存路径
  "image_suffix": "png",         # 验证码图片后缀
  "characters": "0123456789",    # 生成验证码的可选字符
  "count": 1000,                 # 生成验证码的图片数量
  "char_count": 4,               # 每张验证码图片上的字符数量
  "width": 100,                  # 图片宽度
  "height": 60                   # 图片高度
}

2.2 配置文件

创建一个新项目前,需要自行修改相关配置文件conf/sample_config.json

{
  "origin_image_dir": "sample/origin/",  # 原始文件
  "new_image_dir": "sample/new_train/",  # 新的训练样本
  "train_image_dir": "sample/train/",    # 训练集
  "test_image_dir": "sample/test/",      # 测试集
  "api_image_dir": "sample/api/",        # api接收的图片储存路径
  "online_image_dir": "sample/online/",  # 从验证码url获取的图片的储存路径
  "local_image_dir": "sample/local/",    # 本地保存图片的路径
  "model_save_dir": "model/",            # 从验证码url获取的图片的储存路径
  "image_width": 100,                    # 图片宽度
  "image_height": 60,                    # 图片高度
  "max_captcha": 4,                      # 验证码字符个数
  "image_suffix": "png",                 # 图片文件后缀
  "char_set": "0123456789abcdefghijklmnopqrstuvwxyz",  # 验证码识别结果类别
  "use_labels_json_file": false,                       # 是否开启读取`labels.json`内容
  "remote_url": "http://127.0.0.1:6100/captcha/",      # 验证码远程获取地址
  "cycle_stop": 3000,                                  # 启动任务后的训练指定次数后停止
  "acc_stop": 0.99,                                    # 训练到指定准确率后停止
  "cycle_save": 500,                                   # 训练指定次数后定时保存模型
  "enable_gpu": 0,                                     # 是否开启GUP训练
  "train_batch_size": 128,                             # 训练时每次使用的图片张数,如果CPU或者GPU内存太小可以减少这个参数
  "test_batch_size": 100                               # 每批次测试时验证的图片张数,不要超过验证码集的总数
}

关于验证码识别结果类别,假设你的样本是中文验证码,你可以使用tools/collect_labels.py脚本进行标签的统计。 会生成文件gen_image/labels.json存放所有标签,在配置文件中设置use_labels_json_file = True开启读取labels.json内容作为结果类别

2.3 验证和拆分数据集

此功能会校验原始图片集的尺寸和测试图片是否能打开,并按照19:1的比例拆分出训练集和测试集。
所以需要分别创建和指定三个文件夹:origin,train,test用于存放相关文件。

也可以修改为不同的目录,但是最好修改为绝对路径。
文件夹创建好之后,执行以下命令即可:

python3 verify_and_split_data.py

一般会有类似下面的提示

>>> 开始校验目录:[sample/origin/]
开始校验原始图片集
原始集共有图片: 1001张
====以下1张图片有异常====
[第0张图片] [.DStore] [文件后缀不正确]
========end
开始分离原始图片集为:测试集(5%)和训练集(95%)
共分配1000张图片到训练集和测试集,其中1张为异常留在原始目录
测试集数量为:50
训练集数量为:950
>>> 开始校验目录:[sample/new_train/]
【警告】找不到目录sample/new_train/,即将创建
开始校验原始图片集
原始集共有图片: 0张
====以下0张图片有异常====
未发现异常(共 0 张图片)
========end
开始分离原始图片集为:测试集(5%)和训练集(95%)
共分配0张图片到训练集和测试集,其中0张为异常留在原始目录
测试集数量为:0
训练集数量为:0

程序会同时校验和分割origin_image_dirnew_image_dir两个目录中的图片;后续有了更多的样本,可以把样本放在new_image_dir目录中再次执行verify_and_split_data
程序会把无效的文件留在原文件夹。

此外,当你有新的样本需要一起训练,可以放在sample/new目录下,再次运行python3 verify_and_split_data.py即可。
需要注意的是,如果新的样本中有新增的标签,你需要把新的标签增加到char_set配置中或者labels.json文件中。

2.4 训练模型

创建好训练集和测试集之后,就可以开始训练模型了。
训练的过程中会输出日志,日志展示当前的训练轮数、准确率和loss。
此时的准确率是训练集图片的准确率,代表训练集的图片识别情况
例如:

第10次训练 >>> 
[训练集] 字符准确率为 0.03000 图片准确率为 0.00000 >>> loss 0.1698757857
[验证集] 字符准确率为 0.04000 图片准确率为 0.00000 >>> loss 0.1698757857

字符准确率和图片准确率的解释:

假设:有100张图片,每张图片四个字符,共400个字符。我们这里把任务拆分为为需要识别400个字符
字符准确率:识别400的字符中,正确字符的占比。
图片准确率:100张图片中,4个字符完全识别准确的图片占比。

这里不具体介绍tensorflow安装相关问题,直奔主题。
确保图片相关参数和目录设置正确后,执行以下命令开始训练:

python3 train_model.py

也可以根据train_model.pymain函数中的代码调用类开始训练或执行一次简单的识别演示。

由于训练集中常常不包含所有的样本特征,所以会出现训练集准确率是100%而测试集准确率不足100%的情况,此时提升准确率的一个解决方案是增加正确标记后的负样本。

2.5 批量验证

使用测试集的图片进行验证,输出准确率。

python3 test_batch.py

同样可以根据main函数中的代码调用类开始验证。

2.6 启动WebServer

项目已经封装好加载模型和识别图片的类,启动web server后调用接口就可以使用识别服务。
启动web server

python3 webserver_recognize_api.py

接口url为http://127.0.0.1:6000/b

2.7 调用接口识别

使用requests调用接口:

url = "http://127.0.0.1:6000/b"
files = {'image_file': (image_file_name, open('captcha.jpg', 'rb'), 'application')}
r = requests.post(url=url, files=files)

返回的结果是一个json:

{
    'time': '1542017705.9152594',
    'value': 'jsp1',
}

文件recognize_local.py是使用接口识别本地的例子,这个例子运行成功,那么识别验证码的一套流程基本上是走了一遍了。
在线识别验证码是显示中常用场景,文件recognize_online.py是使用接口在线识别的例子,参见:## 2.11 在线识别

2.8 部署

部署的时候,把webserver_recognize_api.py文件的最后一行修改为如下内容:

app.run(host='0.0.0.0',port=5000,debug=False)

然后开启端口访问权限,就可以通过外网访问了。
另外为了开启多进程处理请求,可以使用uwsgi+nginx组合进行部署。
这部分可以参考:Flask部署选择

2.9 部署多个模型

部署多个模型: 在webserver_recognize_api.py文件汇总,新建一个Recognizer对象;
并参照原有up_image函数编写的路由和识别逻辑。

Q = Recognizer(image_height, image_width, max_captcha, char_set, model_save_dir)

注意修改这一行:

value = Q.rec_image(img)

2.10 在线识别

在线识别验证码是显示中常用场景,即实时获取目标验证码来调用接口进行识别。
为了测试的完整性,这里搭建了一个验证码获取接口,通过执行下面的命令启动:

python webserver_captcha_image.py

启动后通过访问此地址:http://127.0.0.1:6100/captcha/可以接收到验证码图片的二进制流文件。
具体进行在线识别任务的demo参见:recognize_online.py

3 数据统计

3.1 训练数据统计

由于很多同学提出,“需要训练多久呀?”、“准确率可以达到多少?”、“为什么我的准确率一直是0?”类似的疑问。
这一小节,使用默认配置(2019.06.02),把训练过程中的数据做了统计,给大家做一个展示。
本次测试条件如下:

  • 验证码:本项目自带生成验证码程序,数字+小写英文
  • 数量:20000张
  • 计算引擎:GPU
  • GPU型号:笔记本,GTX 950X 2G显卡

经过测试: 5000次,25分钟,训练集字符准确率84%,图片准确率51%;
9190次,46分钟,训练集字符准确率100%,图片准确率100%;
12000,60分钟,测试集的准确率基本上已经跑不动了。

使用test_batch.py测试,日志如下:

100个样本识别耗时6.513171672821045秒,准确率37.0%

有37%的准确率,可以说是识别成功的第一步了。

曲线图如下:
训练集-
train_acc

测试集-
test_acc

3.2 压力测试和统计数据

提供了一个简易的压力测试脚本,可以统计api运行过程中识别耗时和请求耗时的相关数据,不过图需要自己用Excel拉出来。
打开文件recognize_time_test.py,修改main函数下的test_file路径,这里会重复使用一张图片来访问是被接口。
最后数据会储存在test.csv文件中。
使用如下命令运行:

python3 recognize_time_test.py
----输出如下
2938,5150,13:30:25,总耗时:29ms,识别:15ms,请求:14ms
2939,5150,13:30:25,总耗时:41ms,识别:21ms,请求:20ms
2940,5150,13:30:25,总耗时:47ms,识别:16ms,请求:31ms

这里对一个模型进行了两万次测试后,一组数据test.csv。 把test.csv使用箱线图进行分析后可以看到:
压力测试结果

  • 单次请求API总耗时(平均值):27ms
  • 单次识别耗时(平均值):12ms
  • 每次请求耗时(平均值):15ms
    其中有:请求API总耗时 = 识别耗时 + 请求耗时

4 开发说明

  • 20190209
  1. 目前tensorboard展示支持的不是很好。
  • 20190601
  1. 最近比较忙,issue回的有点慢,请大家见谅
  2. dev分支开发到一半一直没时间弄,今天儿童节花了一下午时间更新了一下:)
  3. 感谢看到这里的你,谢谢你的支持

4 已知BUG

  1. 使用pycharm启动recognize_api.py文件报错
2018-12-01 00:35:15.106333: W T:\src\github\tensorflow\tensorflow\core\framework\op_kernel.cc:1273] OP_REQUIRES failed at save_restore_tensor.cc:170 : Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on ./model/: Not found: FindFirstFile failed for: ./model : ϵͳ�Ҳ���ָ����·����
; No such process
......
tensorflow.python.framework.errors_impl.InvalidArgumentError: Unsuccessful TensorSliceReader constructor: Failed to get matching files on ./model/: Not found: FindFirstFile failed for: ./model : ϵͳ\udcd5Ҳ\udcbb\udcb5\udcbdָ\udcb6\udca8\udcb5\udcc4·\udcbe\udcb6\udca1\udca3
; No such process
	 [[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

由pycharm默认设置了工作空间,导致读取相对路径的model文件夹出错。 解决办法:编辑运行配置,设置工作空间为项目目录即可。 bug_api启动失败

  1. FileNotFoundError: [Errno 2] No such file or directory: 'xxxxxx'
    目录下有文件夹不存在,在指定目录创建好文件夹即可。

  2. api程序在运行过程中内存越占越大
    结果查阅资料:链接
    在迭代循环时,不能再包含任何张量的计算表达式,否在会内存溢出。 将张量的计算表达式放到init初始化执行后,识别速度得到极大的提升。

  3. 加载多个模型报错 原因是两个Recognizer对象都使用了默认的Graph。 解决办法是在创建对象的时候不使用默认Graph,新建graph,这样每个Recognizer都使用不同的graph,就不会冲突了。

  4. Flask程序用于生产 可以参考官方文档:Flask的生产配置

  5. OOM happens

Hint: If you want to see a list of allocated tensors when OOM happens,
add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

尽可能关闭其他占用GPU或者CPU的任务,或者减小sample_config.json中的train_batch_size参数。

cnn_captcha's People

Contributors

1225358814 avatar aboutmydreams avatar lwabish avatar nickliqian avatar shineyangjg avatar wangyeqiang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cnn_captcha's Issues

recognize_api调用的问题

问题1 这边想要识别两种验证码 但是在recognize_api文件中创建两个Recognizer对象会报错
ValueError: Variable wc1 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at: 网上的答案和本项目中的环境不太一致,不能够理解

问题2 recognize_api批量调用后 内存会固定上涨 原因是调用 tf.argmax() self.sess.run() 两方法多出来的内存 不能够正常回收,不知如何解决 我的解决办法:隔一段时间新建Recognizer对象。
不知是否有正确的办法解决此问题

意外被强制关机后无法重新开始

基础环境:
Ubuntu 16.04,采用pip了安装tensorflow

起因:
训练模型时意外遭遇强制关机,重新开机后运行train_model_v2.py,无法开始训练,显示“Killed”然后就停止

做过的尝试:

  1. 已尝试过删除model 和__pycache__ 文件夹后运行训练脚本,问题依旧存在。
  2. 尝试过重新将图片全部移回origin文件夹并重新运行verify_and_split_data.py后重新运行训练脚本 ,问题依旧存在。

完整输出如下:

ubuntu@VM-118-117-ubuntu:~/tensorflow$python3 train_model_v2.py  
-->图片尺寸: 60 X 160                                             
-->验证码长度: 4
-->验证码共10类 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
-->使用测试集为 ./sample/train/
-->使验证集为 ./sample/test/
>>> Start model test
>>> input batch images shape: (100, 9600)
>>> input batch labels shape: (100, 40)
>>> input x: Tensor("Reshape:0", shape=(?, 60, 160, 1), dtype=float32)
>>> convolution 3:  (?, 8, 20, 128)
>>> input batch predict shape: (?, 40)
>>> End model test
2019-02-23 21:34:13.487696: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-02-23 21:34:13.654589: W tensorflow/core/framework/allocator.cc:122] Allocation of 83886080 exceeds 10% of system memory.
2019-02-23 21:34:13.700510: W tensorflow/core/framework/allocator.cc:122] Allocation of 83886080 exceeds 10% of system memory.
2019-02-23 21:34:13.742455: W tensorflow/core/framework/allocator.cc:122] Allocation of 83886080 exceeds 10% of system memory.
2019-02-23 21:34:13.770453: W tensorflow/core/framework/allocator.cc:122] Allocation of 83886080 exceeds 10% of system memory.
2019-02-23 21:34:13.817177: W tensorflow/core/framework/allocator.cc:122] Allocation of 83886080 exceeds 10% of system memory.
Killed
ubuntu@VM-118-117-ubuntu:~/tensorflow$

python3 verify_and_split_data.py error

Mac OS 10.13.4
python3.6
cat ~/.matplotlib/matplotlibrc
backend: TkAgg

sample_conf.image_width = 200
sample_conf.image_height = 50
sample_conf.max_captcha = 5
sample_conf.remote_url = "https://www.epubit.com/kaptcha.jpg?v=0.24931664260119102"

train 149
test 7

-->使用测试集为 ./sample/train/

Start model test
0:100
Traceback (most recent call last):
File "train_model.py", line 251, in
main()
File "train_model.py", line 245, in main
tm = TrainModel(train_image_dir, char_set, model_save_dir)
File "train_model.py", line 56, in init
batch_x, batch_y = self.get_batch(0, size=100)
File "train_model.py", line 120, in get_batch
batch_y[i, :] = self.text2vec(label) # 生成 oneHot
File "train_model.py", line 100, in text2vec
idx = i * self.char_set_len + self.char_set.index(ch)
ValueError: ' ' is not in list

怎么区分验证码是否为不同种类

市面上的英数4位验证码有好多种,我训练其中一种之后,用很相似的另一种验证码(宽高一样)去识别,准确率很低诶,有没有方案去解决啊,大侠

GPU利用率很低,但是显存用了3G

显卡1050ti . 在跑模型的时候,GPU利用率很低 基本在10%以下,但是显存用了3G. 训练大概5W张图片吧.
以下为日志...感觉并没有比CPU快多少,起码,看不出来1小时能够跑完,不知道是为什么?验证码是使用你项目中自带的验证码生成的.其他代码没有改动.我还需要提供其他的信息吗?
我想问,为什么GPU利用率这么低还有训练速度也不高,正常吗?

F:\ANACONDA\python.exe F:/python/platform/cnn_captcha/train_model.py
-->图片尺寸: 60 X 100
-->验证码长度: 5
-->验证码共36类 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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']
-->使用测试集为 ./data/train/

Start model test
input batch images shape: (100, 6000)
input batch labels shape: (100, 180)
input x: Tensor("Reshape:0", shape=(?, 60, 100, 1), dtype=float32)
convolution 3: (?, 8, 13, 128)
input batch predict shape: (?, 180)
End model test
2019-05-03 22:46:14.018690: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.018859: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.019010: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.019196: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.019349: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.019501: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.019653: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.019806: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2019-05-03 22:46:14.416401: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:887] Found device 0 with properties:
name: GeForce GTX 1050 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.62
pciBusID 0000:01:00.0
Total memory: 4.00GiB
Free memory: 3.30GiB
2019-05-03 22:46:14.416596: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:908] DMA: 0
2019-05-03 22:46:14.416841: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:918] 0: Y
2019-05-03 22:46:14.416942: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0)
第10次训练 >>> 字符准确率为 0.030000001192092896 图片准确率为 0.0 >>> loss 0.159394308924675
第20次训练 >>> 字符准确率为 0.07000000029802322 图片准确率为 0.0 >>> loss 0.16239339113235474
第30次训练 >>> 字符准确率为 0.06599999964237213 图片准确率为 0.0 >>> loss 0.14425653219223022
第40次训练 >>> 字符准确率为 0.04800000414252281 图片准确率为 0.0 >>> loss 0.1394306719303131
第50次训练 >>> 字符准确率为 0.056000005453825 图片准确率为 0.0 >>> loss 0.13513873517513275
第60次训练 >>> 字符准确率为 0.04200000315904617 图片准确率为 0.0 >>> loss 0.1354529857635498
第70次训练 >>> 字符准确率为 0.07000000029802322 图片准确率为 0.0 >>> loss 0.1325652152299881
第80次训练 >>> 字符准确率为 0.06600000709295273 图片准确率为 0.0 >>> loss 0.1318882256746292
第90次训练 >>> 字符准确率为 0.07000000774860382 图片准确率为 0.0 >>> loss 0.13114270567893982
第100次训练 >>> 字符准确率为 0.046000003814697266 图片准确率为 0.0 >>> loss 0.1314479410648346
第110次训练 >>> 字符准确率为 0.0560000017285347 图片准确率为 0.0 >>> loss 0.12947437167167664
第120次训练 >>> 字符准确率为 0.05400000512599945 图片准确率为 0.0 >>> loss 0.1295069456100464
第130次训练 >>> 字符准确率为 0.04800000414252281 图片准确率为 0.0 >>> loss 0.12882718443870544
第140次训练 >>> 字符准确率为 0.04000000283122063 图片准确率为 0.0 >>> loss 0.12955181300640106
第150次训练 >>> 字符准确率为 0.06400000303983688 图片准确率为 0.0 >>> loss 0.12943406403064728
第160次训练 >>> 字符准确率为 0.06400000303983688 图片准确率为 0.0 >>> loss 0.12891033291816711

训练集准确率

结果:训练集和验证集正确率相差太远是为什么,log如下:)
第10次训练 >>> [训练集] 准确率为 0.9674999713897705 >>> loss 0.01326522696763277
>>> [验证集] 准确率为 0.17000000178813934 >>> loss 0.01326522696763277
第20次训练 >>> [训练集] 准确率为 1.0 >>> loss 0.005544836632907391
>>> [验证集] 准确率为 0.23000000417232513 >>> loss 0.005544836632907391
第30次训练 >>> [训练集] 准确率为 0.9375 >>> loss 0.0032186058815568686
>>> [验证集] 准确率为 0.19249999523162842 >>> loss 0.0032186058815568686
步骤:
(1.训练集的图片原来有1000张,开始使用train_model.py进行训练集识别,7000多次训练 准确率依然在15%左右转动,所以人为停止了训练,此过程从未进行过test_batch.py验证测试集;
2. 将训练集图片减少到600张,使用train_model2.py开始训练,就出现了log中所示。PS:进行第二步时,第一步骤产生的模型文件未删除)

不情之请

请原谅我的不礼貌,有个不情之请,不知道作者是否可以写一个支持多GPU的版本!感谢。

字符准确率和图片准确率?

字符准确率:
acc_char = sess.run(accuracy_char_count, feed_dict={self.X: batch_x_verify, self.Y: batch_y_verify, self.keep_prob: 1.})
图片准确率:
acc_image = sess.run(accuracy_image_count, feed_dict={self.X: batch_x_verify, self.Y: batch_y_verify, self.keep_prob: 1.})
啥是字符准确率?字符准确率和图片准确率的区别是什么?

模型准确率计算有问题

修正为即可:
accuracy = tf.reduce_mean(tf.reduce_min(tf.cast(correct_pred, tf.float32), axis=1))
我已经提了pull request,作者有空可以合下代码

找不到模型文件

训练是会报这个错Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching for ./model/,请问有人知道什么问题吗?

执行test_batch报错

File "E:\anaconda\lib\site-packages\tensorflow\python\training\saver.py", line 1312, in restore
err, "a mismatch between the current graph and the graph")

InvalidArgumentError: Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Assign requires shapes of both tensors to match. lhs shape= [13312,1024] rhs shape= [23040,1024]
[[node save/Assign_9 (defined at C:/Users/1/.spyder-py3/captcha/test_batch.py:161) ]]

自己生成图片训练,acc 一直是0.1左右

自己生成图片训练,acc 一直是0.1左右,不知道问题出在哪了,是不是数据的原因?

第160次训练 >>>
[训练集] 字符准确率为 0.10000 图片准确率为 0.00000 >>> loss 0.1013903320
[验证集] 字符准确率为 0.10750 图片准确率为 0.00000 >>> loss 0.1013903320
第170次训练 >>>
[训练集] 字符准确率为 0.12750 图片准确率为 0.00000 >>> loss 0.1000958011
[验证集] 字符准确率为 0.08500 图片准确率为 0.00000 >>> loss 0.1000958011
第180次训练 >>>
[训练集] 字符准确率为 0.12750 图片准确率为 0.00000 >>> loss 0.0998161957
[验证集] 字符准确率为 0.11250 图片准确率为 0.00000 >>> loss 0.0998161957
第190次训练 >>>
[训练集] 字符准确率为 0.12750 图片准确率为 0.00000 >>> loss 0.0985096842
[验证集] 字符准确率为 0.10750 图片准确率为 0.00000 >>> loss 0.0985096842
第200次训练 >>>
[训练集] 字符准确率为 0.10000 图片准确率为 0.00000 >>> loss 0.0990609303
[验证集] 字符准确率为 0.08000 图片准确率为 0.00000 >>> loss 0.0990609303
第210次训练 >>>
[训练集] 字符准确率为 0.13000 图片准确率为 0.00000 >>> loss 0.1007201374
[验证集] 字符准确率为 0.07250 图片准确率为 0.00000 >>> loss 0.1007201374
第220次训练 >>>
[训练集] 字符准确率为 0.13750 图片准确率为 0.00000 >>> loss 0.0987524837
[验证集] 字符准确率为 0.10250 图片准确率为 0.00000 >>> loss 0.0987524837
第230次训练 >>>
[训练集] 字符准确率为 0.10000 图片准确率为 0.00000 >>> loss 0.0986539647
[验证集] 字符准确率为 0.06500 图片准确率为 0.00000 >>> loss 0.0986539647
第240次训练 >>>
[训练集] 字符准确率为 0.10750 图片准确率为 0.00000 >>> loss 0.0977340937
[验证集] 字符准确率为 0.08750 图片准确率为 0.00000 >>> loss 0.0977340937
第250次训练 >>>
[训练集] 字符准确率为 0.12500 图片准确率为 0.00000 >>> loss 0.0980810747
[验证集] 字符准确率为 0.10250 图片准确率为 0.00000 >>> loss 0.0980810747
第260次训练 >>>
[训练集] 字符准确率为 0.10000 图片准确率为 0.00000 >>> loss 0.0975745544
[验证集] 字符准确率为 0.05250 图片准确率为 0.00000 >>> loss 0.0975745544
第270次训练 >>>
[训练集] 字符准确率为 0.12750 图片准确率为 0.00000 >>> loss 0.0960663259
[验证集] 字符准确率为 0.10750 图片准确率为 0.00000 >>> loss 0.0960663259

root_dir = "../sample/python_captcha/" # 图片储存路径 image_suffix = "png" # 图片储存后缀 characters = "0123456789" # 图片上显示的字符集 # characters = "0123456789abcdefghijklmnopqrstuvwxyz" count = 1000 # 生成多少张样本 char_count = 4 # 图片上的字符数量

# 验证码字符相关参数 sample_conf.char_set = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

过拟合问题的解决办法

你好,我用train_model_v2.py训练模型时出现过拟合,训练准确率100%但是测试准确率一直80%浮动,如何才能提高测试准确率呢?请指教。

图片准确率一直是0

图片和干净,但是训练了3000step,
字符准确率为 0.96749999344348907 图片准确率为 0

recognize_api越跑越慢

由于不太懂python,但是代码成功跑起来了 ,刚开始识别图片响应时间在ms级别,然后一直缓慢下来,大概运行到2万次请求,响应时间到了2s左右

调用接口上传图片到webserver 时间很长

环境:启动了两个api server,并且使用nginx进行了负载均衡,

现象:用jmeter模拟接口http://127.0.0.1:6000/b 上传图片到Webserver时(jmeter只启动一个线程),发现
经常长时间的未上传到服务器;
jmeter运行20分钟才上传50次左右的请求,这是什么原因老是卡在上传过程呢?

关于验证码数量的问题

如果训练集有一万张的话,训练一次是一万张都训练吗,还是随机抽取多少张训练呀,验证的时候是什么情况呢,希望大佬不吝赐教

关于图片格式的问题

新手一枚,在看到 “2e8j_17322d3d4226f0b5c5a71d797d2ba7f7.jpg格式命名(标签_序列号.后缀)”这里时产生疑问,标签号是图片里的内容,序列号是什么,谢谢

请问这个方法适用如下的验证码图片吗

0dk0_num16
0m4u_num21
0mo1_num22
0o44_num24
我的origin数量一共1076张,根据verify_and_split_data.py自动分成train和test数据。配置参数如下: "image_width": 65,
"image_height": 25,
"max_captcha": 4,
"image_suffix": "jpg",
"char_set": "0123456789abcdefghijklmnopqrstuvwxyz",
"use_labels_json_file": false,
"remote_url": "http://127.0.0.1:6100/captcha/",
"cycle_stop": 20000,
"acc_stop": 0.99,
"cycle_save": 500,
"enable_gpu": 0
问题:图片准确率低

第19970次训练 >>>
[训练集] 字符准确率为 0.33500 图片准确率为 0.03000 >>> loss 0.0000869953
[验证集] 字符准确率为 0.38000 图片准确率为 0.01000 >>> loss 0.0000869953
第19980次训练 >>>
[训练集] 字符准确率为 0.35000 图片准确率为 0.03000 >>> loss 0.0001388737
[验证集] 字符准确率为 0.42000 图片准确率为 0.04000 >>> loss 0.0001388737
第19990次训练 >>>
[训练集] 字符准确率为 0.36000 图片准确率为 0.01000 >>> loss 0.0000536628
[验证集] 字符准确率为 0.34750 图片准确率为 0.00000 >>> loss 0.0000536628
第20000次训练 >>>
[训练集] 字符准确率为 0.36500 图片准确率为 0.02000 >>> loss 0.0000577325
[验证集] 字符准确率为 0.41000 图片准确率为 0.03000 >>> loss 0.0000577325

训练集准确率

10万python验证码,训练500次,准确率0.02,是否有问题?

标签_序列号.后缀,这个序列号是文件的md5值吗?

原始数据集可以存放在./sample/origin目录中
为了便于处理,图片最好以2e8j_17322d3d4226f0b5c5a71d797d2ba7f7.jpg格式命名(标签_序列号.后缀)

17322d3d4226f0b5c5a71d797d2ba7f7 正好32个字符,是验证码图片的md5值吗?

关于中文识别

你好,测试了一下中文识别。3600个字,10W张标注好的单字图片。学习很快,99%停止。测试时基本上50%的识别率,请问这个该如何提高识别率呢。

关于增加负样本

你好,我想请教一下,应该怎么增加负样本,如果我只训练数字,那么负样本是没有数字的背景还是什么?有没有相关的demo呀

关于训练集的数量

这训练集大概要多少?手动打了100张貌似什么效果都没有 目前还想不到如何生成验证码

训练的时候报错

在用测试集进行训练的时候报错了,是要在model文件夹下面放现成的模型吗?
image
不好意思是新手,直接跑的代码

能否自己调整训练集跟验证集集的数量

你好,我这边用了10w张生成的验证码训练。使用GPU,大概在一小时左右把训练集正确率训练到了百分百,但是验证集集这里的准确率还在40-50%左右。途中遇到了几个问题:
1.训练的时候,每3000轮就会停下,需要手动等3000轮完成再运行。这次训练了接近几万轮,基本都是每3000轮就要自己动一下,能不能一下直接训练完成。
2.在数字,小写字母,大写字母都存在的情况下,使用单单1000张验证码完全不能起到训练泛模型的作用,所以我想能不能自己把训练集跟验证集调整为一样,然后只对这一千字进行训练识别,虽然这样只能作用于这一千张验证码,但是我想试一下。
3.如果想达到95%以上的正确率,是否千万张图片是必须的。
4.我在readme跟别的issues都看到你说了:增加负样本训练来提升准确率,请问这个需要怎么实现。

OP_REQUIRES failed at save_restore_v2_ops.cc:137 : Failed precondition: Failed to rename

……
第1500次训练 >>> 
[训练集] 字符准确率为 0.50500 图片准确率为 0.28000 >>> loss 0.0003628575
[验证集] 字符准确率为 0.38000 图片准确率为 0.00000 >>> loss 0.0003628575
2019-03-15 18:19:42.964549: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at save_restore_v2_ops.cc:137 : Failed precondition: Failed to rename: ./model/.data-00000-of-00001.tempstate1615166308275179972 to: ./model/.data-00000-of-00001 : ��һ����������ʹ�ô��ļ��������޷����ʡ�
; Broken pipe

执行train_model.py,出错是什么问题

convolution 1: Tensor("dropout/mul:0", shape=(?, 27, 65, 32), dtype=float32)
Backend TkAgg is interactive backend. Turning interactive mode on.
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.4\helpers\pydev\pydevd.py", line 1668, in
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.4\helpers\pydev\pydevd.py", line 1662, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.4\helpers\pydev\pydevd.py", line 1072, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.4\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/Project/cnn_captcha-master/train_model.py", line 282, in
main()
File "D:/Project/cnn_captcha-master/train_model.py", line 277, in main
tm.train_cnn() # 开始训练模型
File "D:/Project/cnn_captcha-master/train_model.py", line 196, in train_cnn
y_predict = self.model()
File "D:/Project/cnn_captcha-master/train_model.py", line 157, in model
print(">>> convolution 1: ", conv1.shape)
AttributeError: 'Tensor' object has no attribute 'shape'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.