Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Training (训练)

Amatist_Kurisu edited this page Jul 15, 2018 · 4 revisions

需要确保安装了 Composer 以及 PHP 版本大于 7 以及安装了GD扩展

如果需要训练自己的字典,推荐使用 git 拉取的方式,而非使用 composer 引入

  1. 使用 git 将源文件拉取
    git clone https://github.com/Kuri-su/CAPTCHA_Reader.git
  1. 使用Composer生成ClassMap
    composer update
    # 如果此处运行成功,会在输出 Success 的信息,并 CAPTCHA_Reader 目录下生成一个 verdor 文件夹
  1. 修改配置文件 因为训练过程中分为 训练测试 过程,所以需要配置 Config/app.phpConfig/training.php 两个文件

    此前,假设你已经写好了四个步骤的类。

    • GetImageInfo.example.php
    • Pretreatment.example.php
    • Cutting.example.php
    • Identify.example.php

    并且已经将 学习样本集和测试集分别放到了 sample/StudySamples/examplesample/TestSamples/example/0

    此时修改 Config/training.phpstudyGroup 数组,改成对应的类,按照顺序

    • GetImageInfo
    • Pretreatment
    • Cutting
    • Identify
    <?php
    'studyGroup' => [
        'example' => [
            [
                \CAPTCHAReader\src\App\GetImageInfo\GetImageInfo::class,
                \CAPTCHAReader\src\App\Pretreatment\Pretreatment::class,
                \CAPTCHAReader\src\App\Cutting\Cutting::class,
                \CAPTCHAReader\src\App\Identify\Identify::class,
            ],
        ],
    ],

    并指定样本的位置,在 studySampleGrouptestSampleGroup 数组中添加我们的样本集位置,键名请保持和方案名一致

    <?php
        'studySampleGroup' => [
            ...
            'example'   => __DIR__ . '/../../sample/StudySamples/example/',
        ],
    
        'testSampleGroup'  => [
             ...
            'example'   => __DIR__ . '/../../sample/TestSamples/example/0/',
        ],

    然后在 Config/app.php 中的 componentGroup 数组中添加一个元素

    <?php
    'componentGroup' => [
        'ZhengFangNormal' => [...],
        'QinGuoNormal' => [...],
        'TianYiNormal' => [...],
        'NeeaNormal' => [...],
        'example' => [
            'components' => [
               \CAPTCHAReader\src\App\GetImageInfo\GetImageInfo::class,
                \CAPTCHAReader\src\App\Pretreatment\Pretreatment::class,
                \CAPTCHAReader\src\App\Cutting\Cutting::class,
                \CAPTCHAReader\src\App\Identify\Identify::class,
            ],
            'dictionary' => 'GetImageInfo-Pretreatment-Cutting-Identify.json',
        ],
    ],

    并修改 Config/app.php 中的 useGroup,使用这个 example 方案

    <?php
    'useGroup' => 'example',
  2. 开始训练 执行命令,开始训练

        php training/AddSamples/Test.php

    这个脚本干的事就是 去获得一个 training/AddSamples/AddSamplesAuto.php 类实例,然后运行里面的 run 方法。

    这个 training/AddSamples/AddSamplesAuto.php 类干的事情如下 * 遍历读取我们配置的 训练集和测试集 文件夹内的全部的图片文件路径 * 然后不断的使用 配置的方案去对 训练集中的文件 进行识别,若识别结果和实际结果一致,则通过,若未通过,则记录一段字符串到我们设置的字典文件中去,字典文件的路径在 src/Dictionary内 * 然后到了指定的阈值时,会使用测试集开始验证,并返回验证结果。(在 AddSamplesAuto.php 的 109 行修改阈值)

    当全部的训练文件用完或者识别率达到我们设定的值,则训练结束

Clone this wiki locally