{"id":22354865,"url":"https://github.com/pprp/captcha.pytorch","last_synced_at":"2025-08-03T23:35:29.645Z","repository":{"id":39405885,"uuid":"219237224","full_name":"pprp/captcha.Pytorch","owner":"pprp","description":"验证码识别比赛code，针对数字，大小写字母能达到97-99%的准确率","archived":false,"fork":false,"pushed_at":"2024-07-25T10:57:45.000Z","size":2820,"stargazers_count":130,"open_issues_count":6,"forks_count":37,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T00:06:13.855Z","etag":null,"topics":["captcha","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pprp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-03T01:41:04.000Z","updated_at":"2025-04-06T12:39:54.000Z","dependencies_parsed_at":"2024-12-25T09:10:36.778Z","dependency_job_id":"22e2db5f-a77a-4be5-a330-1275e3ce4c19","html_url":"https://github.com/pprp/captcha.Pytorch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2Fcaptcha.Pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2Fcaptcha.Pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2Fcaptcha.Pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprp%2Fcaptcha.Pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pprp","download_url":"https://codeload.github.com/pprp/captcha.Pytorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248981261,"owners_count":21193147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["captcha","pytorch"],"created_at":"2024-12-04T13:15:35.597Z","updated_at":"2025-04-15T00:06:31.919Z","avatar_url":"https://github.com/pprp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 计算机挑战赛 2019 验证码识别竞赛 一等奖方案\n\n\n### \u003ccenter\u003e1. Background\u003c/center\u003e\n\n基于\u003chttps://github.com/dee1024/pytorch-captcha-recognition\u003e进行改进，原版中数据集采用的captcha库自动生成的图片，可以随意制定生成数量，并且相对而言生成的图片比较简单。\n\n本次项目是全国高校计算机能力挑战赛中的人工智能赛道里的验证码识别，该比赛需要识别26（大写）+26（小写）+数字（10）= 62个字符，随机组成的四位验证码图片。训练集仅有5000张，并且识别的难度系数较大，人眼有时候也很容易识别出错。\n\n\n\n### \u003ccenter\u003e2. Environment\u003c/center\u003e\n\n- 显存：4G+\n\n- Ubuntu16.04\n\n- numpy\n\n- pandas\n\n- torch==1.3.1\n\n- torchnet==0.0.4\n\n- torchvision==0.2.0\n\n- tqdm\n\n- visdom\n\n\u003e 可通过pip install -r requirements.txt进行环境的安装\n\n### \u003ccenter\u003e3. Dataset\u003c/center\u003e\n\n![1575527368225](./fig/1575527368225.png)\n\n比赛提供的数据集如图所示，120$\\times$40的像素的图片，然后标签是由图片名称提供的。训练集测试集划分：80%的数据用于训练集，20%的数据用于测试集。\n\n- 训练图片个数为：3988\n\n- 测试图片个数为：1000\n\n训练的数据还是明显不够的，考虑使用数据增强，通过查找github上很多做数据增强的库，最终选择了Augmentor库作为图像增强的库。\n\n安装方式：`pip install Augmentor`\n\nAPI: \u003chttps://augmentor.readthedocs.io/en/master/index.html\u003e\n\n由于验证码与普通的分类图片有一定区别，所以选择的增强方式有一定局限，经过几轮实验，最终选取了distortion类的方法作为具体增强方法，输入为训练所用的图片，输出设置为原来图片个数的2倍，具体代码见dataAug.py, 核心代码如下：\n\n```python\ndef get_distortion_pipline(path, num):\n    p = Augmentor.Pipeline(path)\n    p.zoom(probability=0.5, min_factor=1.05, max_factor=1.05)\n    p.random_distortion(probability=1, grid_width=6, grid_height=2, magnitude=3)\n    p.sample(num)\n    return p\n```\n\n将得到的图片重命名为auged_train文件夹，最终数据集组成如下：\n\n```\nroot \n\t- data\n\t\t- train:3988张\n\t\t- test:1000张\n\t\t- auged_train:7976张\n```\n\n\u003e 如果需要数据集请联系我，联系方式在最后\n\u003e\n\u003e 链接：https://pan.baidu.com/s/13BmN7Na4ESTPAgyfBAHMxA \n\u003e 提取码：v4nk\n\n数据集结构的组织,从网盘下载数据以后，按照以下文件夹格式进行组织：\n\n```\n- data\n\t- train\n\t- test\n\t- auged_train\n```\n\n然后就可以训练了。\n\n### \u003ccenter\u003e4. Structure\u003c/center\u003e\n\n```\nroot \n\t- config\n\t\t- parameters.py 主要包括超参数，最重要的是learning rate\n\t- lib\n\t\t- center_loss.py 将center loss引入，用于训练\n\t\t- dataset.py 包装Dataset，针对train文件夹和auged_train文件夹内容各自写了一个处理类\n\t\t- generate_captcha.py 生成简单的数据集，在没有官方数据集的情况下\n\t\t- optimizer.py RAdam, AdamW, label smooth等新的optimizer\n\t\t- scheduler.py 新增了warmup机制\n\t- model\n\t\t- BNNeck.py 基于resnet18使用了bnnect结构，来自罗浩大佬行人检测中的trick\n\t\t- CaptchaNet.py 手工构建的一个简单网络，原有库提供的\n\t\t- dense.py 更改backbone，使用dense121作为backbone，其他也可以更改\n\t\t- dualpooling.py 在resnet18基础上添加了dual pooling，增加了信息\n\t\t- IBN.py 使用ibn模块，以resnet18为基础\n\t\t- model.py resnet18，添加dropout\n\t\t- res18.py 引入了attention机制和dual pooling\n\t\t- senet.py 将senet作为backbone\n\t- result\n\t\t- submission.csv 结果保存\n\t- utils\n\t\t- cutoff.py 数据增强方法，不适合验证码，可以用在普通图片\n\t\t- dataAug.py 使用Agumentor进行数据增强\n\t\t- Visdom.py 使用visdom记录log，进行可视化\n- predict.py 引入了多模型预测，然后分析结果\n- run.py 与predict类似，不过是单模型的预测\n- test.py 规定测试模型权重，待测试图片路径，对测试集进行测试\n- train.py 模型的训练，每个epoch先训练所有的train,然后训练所有的auged_train图片\n```\n\n### \u003ccenter\u003e5. Result\u003c/center\u003e\n\n最好结果：\n\nResNet18+Dropout(0.5)+RAdam+DataAugmentation+lr(3e-4) = 98.4%测试集准确率，线上A榜：97%\n\n![1575530185666](./fig/1575530185666.png)\n\n\n\n模型分析：分析四个模型，python predict.py 观察预测出错的结果，评判模型好坏，最终选择了0号模型。\n\n![1575530264099](./fig/1575530264099.png)\n\n### \u003ccenter\u003e6. Procedure\u003c/center\u003e\n\n调参过程记录：null代表未记录\n\n| Name       | item1    | item2                  | item3   | item4 | item5     | 测试:线上       |\n| ---------- | -------- | ---------------------- | ------- | ----- | --------- | --------------- |\n| baseline0  | ResNet18 | lr=1e-3                | 4:1划分 | Adam  |           | 88%:84%         |\n| baseline1  | ResNet34 | lr=1e-3                | 4:1划分 | Adam  |           | 90%:84%         |\n| baseline2  | ResNet18 | lr=1e-3                | 4:1划分 | RAdam |           | null:**90%**    |\n| baseline3  | ResNet18 | lr=3e-4                | 4:1划分 | RAdam |           | 未收敛          |\n| baseline4  | ResNet18 | lr=1e-1                | 4:1划分 | RAdam |           | 96.4%:87%       |\n| baseline5  | ResNet18 | lr=1e-1                | 4:1划分 | RAdam | aug0      | 98%:**93%**     |\n| baseline6  | ResNet18 | lr=1e-1                | 9:1划分 | RAdam | aug1      | 60%:null        |\n| baseline7  | ResNet18 | lr=1e-3                | 4:1划分 | RAdam | aug2      | null:94%        |\n| baseline8  | ResNet18 | lr=1e-3                | 4:1划分 | AdamW | aug2      | 98.4%:92.56%    |\n| baseline9  | ResNet18 | lr=1e-3                | 4:1划分 | RAdam | aug3      | null:93.52%     |\n| baseline10 | ResNet18 | lr=1e-3                | 4:1划分 | RAdam | aug4      | null:94.16%     |\n| baseline11 | ResNet18 | lr=1e-3                | 9:1划分 | RAdam | aug5      | 60%:null        |\n| baseline12 | ResNet18 | lr=3.5e-4              | 4:1划分 | RAdam | aug2      | null:**94.72%** |\n| baseline13 | ResNet18 | lr=3.5e-4 decay:6e-4   | 4:1划分 | RAdam | aug2      | null:**95.16%** |\n| baseline14 | ResNet18 | lr=3.5e-4 decay:7e-4   | 4:1划分 | RAdam | aug2      | bad             |\n| baseline15 | ResNet18 | lr=3.5e-5 decay:6.5e-4 | 4:1划分 | RAdam | aug2      | null:95%        |\n| baseline16 | ResNet18 | lr=3.5e-5 decay:6.5e-4 | 4:1划分 | RAdam | drop(0.5) | null:97%        |\n\n以上的aug代表数据增强：\n\n- **aug0:** +distrotion\n\n- **aug1**: 9:1划分+扩增3倍\n\n- **aug2**: +distortion+zoom\n\n- **aug3:** +tilt+扩增两倍\n\n- **aug4:** aug2+aug3混合\n\n- **aug5:** 9:1划分 +tilt倾斜\n\n数据增强示意图：\n\n|     example1      |     example2      |     example3      |\n| :---------------: | :---------------: | :---------------: |\n| ![1](./fig/1.gif) | ![2](./fig/2.gif) | ![3](./fig/3.gif) |\n\n后期由于错过了提交时间，只能进行测试集上的测试，主要方案有以下：\n\n- learning rate scheduler尝试：CosineAnnealingLR, ReduceLROnPlateau,StepLR,MultiStepLR\n- 更改backbone: senet, densenet\n- 在res18基础上添加：attention机制，dual pooling, ibn模块，bnneck等\n- 尝试center loss，收敛很慢，但是效果应该不错\n\n还未尝试的方案：\n\n- label smooth \n- 多模型ensemble\n\n---\n\n\n- CSDN:\u003chttps://blog.csdn.net/DD_PP_JJ\u003e\n\n- 博客园:\u003chttps://www.cnblogs.com/pprp\u003e\n\n    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpprp%2Fcaptcha.pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpprp%2Fcaptcha.pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpprp%2Fcaptcha.pytorch/lists"}