Giter Club home page Giter Club logo

simplehtr's Introduction

使用 TensorFlow 进行手写文本识别

  • 2023/2 更新:提供网络演示
  • 2023/1 更新:请参阅HTRPipeline获取读取整页的包
  • 2021/2 更新:识别行级文本(多个单词)
  • 2021/1 更新:更强大的模型、更快的数据加载器、字束搜索解码器也适用于 Windows
  • 2020 更新:代码与 TF2 兼容

使用 TensorFlow (TF) 实现的手写文本识别 (HTR) 系统,并在 IAM 离线 HTR 数据集上进行训练。该模型将单个单词或文本行(多个单词)的图像作为输入输出识别的文本。验证集中 3/4 的单词被正确识别,字符错误率约为 10%。

高温

运行演示

  • 下载预训练模型之一
  • 将下载的 zip 文件的内容放入model存储库的目录中
  • 进入src目录
  • 运行推理代码:
    • 执行python main.py以在单词图像上运行模型
    • 执行python main.py --img_file ../data/line.png以在文本行的图像上运行模型

使用文本行模型时的输入图像和预期输出如下所示。

测试

> python main.py
Init with stored values from ../model/snapshot-13
Recognized: "word"
Probability: 0.9806370139122009

测试

> python main.py --img_file ../data/line.png
Init with stored values from ../model/snapshot-13
Recognized: "or work on line level"
Probability: 0.6674373149871826

命令行参数

  • --mode:在“训练”、“验证”和“推断”之间选择。默认为“推断”。
  • --decoder:从 CTC 解码器“bestpath”、“beamsearch”和“wordbeamsearch”中选择。默认为“最佳路径”。对于选项“wordbeamsearch”,请参阅下面的详细信息。
  • --batch_size:批量大小。
  • --data_dir:包含 IAM 数据集的目录(带有子目录imggt)。
  • --fast:使用LMDB可以更快地加载图像。
  • --line_mode:训练阅读文本行而不是单个单词。
  • --img_file:用于推理的图像。
  • --dump:将 NN 的输出转储到保存在文件夹中的 CSV 文件dump。可用作CTCDecoder的输入。

集成Word Beam搜索解码

可以使用字束搜索解码器来代替 TF 附带的两个解码器单词被限制为字典中包含的单词,但仍然可以识别任意非单词字符串(数字、标点符号)。下图显示了一个示例,其中词束搜索能够识别正确的文本,而其他解码器则失败。

解码器比较

请按照以下说明集成字束搜索解码:

  1. 克隆存储库CTCWordBeamSearch
  2. pip install .通过在 CTCWordBeamSearch 存储库的根级别运行来编译和安装
  3. --decoder wordbeamsearch执行时指定命令行选项main.py以实际使用解码器

该字典是在训练和验证模式下使用 IAM 数据集中包含的所有单词(即还包括验证集中的单词)自动创建的,并保存到文件中data/corpus.txt。此外,可以在文件中找到手动创建的单词字符列表model/wordCharList.txt。波束宽度设置为 50 以符合普通波束搜索解码的波束宽度。

在 IAM 数据集上训练模型

准备数据集

请按照以下说明获取 IAM 数据集:

  • 在此网站免费注册
  • 下载words/words.tgz
  • 下载ascii/words.txt
  • 在磁盘上为数据集创建一个目录,并创建两个子目录:imggt
  • 放入words.txt目录gt_
  • 将内容(目录a01, a02, ...)放入目录words.tgzimg

跑步训练

  • model如果您想从头开始训练,请从目录中删除文件
  • 进入src目录并执行python main.py --mode train --data_dir path/to/IAM
  • IAM 数据集分为 95% 训练数据和 5% 验证数据
  • 如果指定了该选项--line_mode,则模型将在通过将多个单词图像组合成一个而创建的文本行图像上进行训练
  • 训练在固定数量的 epoch 后停止但没有改善

预训练的单词模型是在 GTX 1050 Ti 上使用以下命令进行训练的:

python main.py --mode train --fast --data_dir path/to/iam  --batch_size 500 --early_stopping 15

以及线路模型:

python main.py --mode train --fast --data_dir path/to/iam  --batch_size 250 --early_stopping 10

快速图像加载

即使仅使用小型 GPU,从磁盘加载和解码 png 图像文件也是瓶颈。数据库LMDB用于加速图像加载:

  • 转到该目录并使用指定的 IAM 数据目录src运行create_lmdb.py --data_dir path/to/iam
  • lmdb将在 IAM 数据目录中创建一个包含 LMDB 文件的子文件夹
  • 训练模型时,添加命令行选项--fast

数据集应位于 SSD 驱动器上。使用该--fast选项和 GTX 1050 Ti 对单个单词进行训练大约需要 3 小时,批量大小为 500。对文本行进行训练需要更长的时间。

型号信息

该模型是我为论文实现的 HTR 系统的精简版本。剩下的就是以可接受的精度识别文本的最低限度。它由 5 个 CNN 层、2 个 RNN (LSTM) 层以及 CTC 损失和解码层组成。有关更多详细信息,请参阅这篇Medium 文章

参考

simplehtr's People

Contributors

githubharald avatar n1snt avatar yuanzhongqiao avatar chazzz avatar tosemml avatar alexeevrevan avatar reed-jones avatar

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.