• Stars
    star
    1,052
  • Rank 43,619 (Top 0.9 %)
  • Language
    Jupyter Notebook
  • License
    GNU General Publi...
  • Created about 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎨 数学公式识别增强版:中英文手写印刷公式、支持初级符号推导(数据结构基于 LaTeX 抽象语法树)Math Formula OCR Pro, supports handwrite, Chinese-mixed formulas and simple symbol reasoning (based on LaTeX AST).

LaTeX_OCR_PRO

数学公式识别,增强:中文公式、手写公式

Seq2Seq + Attention + Beam Search。结构如下:

1. 搭建环境

  1. python3.5 + tensorflow1.12.2
  2. [可选] latex (latex 转 pdf)
  3. [可选] ghostscript (图片处理)
  4. [可选] magick (pdf 转 png)

如果你想直接训练,不想自己构建数据集:

  1. [可选] 新开一个虚拟环境
    virtualenv env35 --python=python3.5
    source env35/bin/activate
  2. 安装依赖
    pip install -r requirements.txt     // cpu 版
    pip install -r requirements-gpu.txt // gpu 版
  3. 下载数据集
    git submodule init
    git submodule update
    如果 git 速度太慢,您也可以手动下载数据集,放到 data 目录下。数据集仓库在 https://github.com/LinXueyuanStdio/Data-for-LaTeX_OCR

如果你想自己构建数据集,然后再训练:

Linux

一键安装

make install-linux

  1. 安装本项目依赖
virtualenv env35 --python=python3.5
source env35/bin/activate
pip install -r requirements.txt
  1. 安装 latex (latex 转 pdf)
sudo apt-get install texlive-latex-base
sudo apt-get install texlive-latex-extra
  1. 安装 ghostscript
sudo apt-get update
sudo apt-get install ghostscript
sudo apt-get install libgs-dev
  1. 安装magick (pdf 转 png)
wget http://www.imagemagick.org/download/ImageMagick.tar.gz
tar -xvf ImageMagick.tar.gz
cd ImageMagick-7.*; \
./configure --with-gslib=yes; \
make; \
sudo make install; \
sudo ldconfig /usr/local/lib
rm ImageMagick.tar.gz
rm -r ImageMagick-7.*
Mac

一键安装

make install-mac

  1. 安装本项目依赖
sudo pip install -r requirements.txt
  1. LaTeX

我们需要 pdflatex,可以傻瓜式一键安装:http://www.tug.org/mactex/mactex-download.html

  1. 安装magick (pdf 转 png)
wget http://www.imagemagick.org/download/ImageMagick.tar.gz
tar -xvf ImageMagick.tar.gz
cd ImageMagick-7.*; \
./configure --with-gslib=yes; \
make;\
sudo make install; \
rm ImageMagick.tar.gz
rm -r ImageMagick-7.*

2. 开始训练

生成小数据集、训练、评价

提供了样本量为 100 的小数据集,方便测试。只需 2 分钟就可以根据 ./data/small.formulas/ 下的公式生成用于训练的图片。

注意:样本量很小,是无法有效训练模型的。这个小数据集仅用于确认代码有没有 bug。如果用于预测,那结果极差,因为数据不够。

一步训练

make small

  1. 生成数据集

    用 LaTeX 公式生成图片,同时保存公式-图片映射文件,生成字典 只用运行一次

    # 默认
    python build.py
    # 或者
    python build.py --data=configs/data_small.json --vocab=configs/vocab_small.json
  2. 训练

    # 默认
    python train.py
    # 或者
    python train.py --data=configs/data_small.json --vocab=configs/vocab_small.json --training=configs/training_small.json --model=configs/model.json --output=results/small/
    
  3. 评价预测的公式

    # 默认
    python evaluate_txt.py
    # 或者
    python evaluate_txt.py --results=results/small/
    
  4. 评价数学公式图片

    # 默认
    python evaluate_img.py
    # 或者
    python evaluate_img.py --results=results/small/
    
生成完整数据集、训练、评价

根据公式生成 70,000+ 数学公式图片需要 2-3 个小时

一步训练

make full

  1. 生成数据集

    用 LaTeX 公式生成图片,同时保存公式-图片映射文件,生成字典 只用运行一次

    python build.py --data=configs/data.json --vocab=configs/vocab.json
    
  2. 训练

    python train.py --data=configs/data.json --vocab=configs/vocab.json --training=configs/training.json --model=configs/model.json --output=results/full/
    
  3. 评价预测的公式

    python evaluate_txt.py --results=results/full/
    
  4. 评价数学公式图片

    python evaluate_img.py --results=results/full/
    

3. 可视化

可视化训练过程

用 tensorboard 可视化训练过程

小数据集

cd results/small
tensorboard --logdir ./

完整数据集

cd results/full
tensorboard --logdir ./
可视化预测过程

打开 visualize_attention.ipynb,一步步观察模型是如何预测 LaTeX 公式的。

或者运行

# 默认
python visualize_attention.py
# 或者
python visualize_attention.py --image=data/images_test/6.png --vocab=configs/vocab.json --model=configs/model.json --output=results/full/

可在 --output 下生成预测过程的注意力图。

4. 部署

部署为 Django 应用
  1. 安装部署需要的环境
    pip install django
  2. 开启服务
    python manage.py runserver 0.0.0.0:8010
  3. 开启图片服务
    cd data/images_train
    python -m SimpleHTTPServer 8020
  4. 使用方法 在输入框里依次输入 0.png, 1.png 等等,即可看到结果

5. 评价

指标 训练分数 测试分数
perplexity 1.12 1.13
EditDistance 94.16 93.36
BLEU-4 91.03 90.47
ExactMatchScore 49.30 46.22

perplexity 是越接近1越好,其余3个指标是越大越好。

其中 EditDistance 和 BLEU-4 已达到业内先进水平

将 perplexity 训练到 1.03 左右,ExactMatchScore 还可以再升,应该可以到 70 以上。

机器不太好,训练太费时间了。

6. 更多细节

  1. 模型实现细节

    包括数据获取、数据处理、模型架构、训练细节

  2. 解决方案

    包括 “如何可视化 Attention 层”、“在 win10 用 GPU 加速训练” 等等

7. 致谢

十分感谢 Harvard 和 Guillaume Genthial 、Kelvin Xu 等人提供巨人的肩膀。

论文:

  1. Show, Attend and Tell(Kelvin Xu...)
  2. Harvard's paper and dataset
  3. Seq2Seq for LaTeX generation.

8. 相关项目

LaTeX_OCR 的 PyTorch 版: https://github.com/qs956/Latex_OCR_Pytorch by @qs956

More Repositories

1

PythonDataMining

📔 在学院的书架上发现了一本不带脑子就能读懂的书《Python数据挖掘与实战》
Jupyter Notebook
618
star
2

LaTeX_OCR

💎 数学公式识别 Math Formula OCR
Jupyter Notebook
486
star
3

DictionaryData

高质量英语字典,400多本单词书+60000多个单词!单词书包括小学、中学、高中、考研、考博、出国(GRE、托福等等)等等,难道它不香吗?
282
star
4

chatgpt-review-rebuttal-extension

ChatGPT - Review & Rebuttal: A browser extension for generating reviews and rebuttals, powered by ChatGPT. 利用 ChatGPT 生成审稿意见和回复的浏览器插件
TypeScript
249
star
5

lifecycle-component

🏆 一套组件化规范,基于组件的生命周期,支持渐进式组件化、渐进式组件变插件化,高度自由的成长性架构
Java
141
star
6

PyTorch-DDPM

500 行代码实现降噪扩散模型 DDPM,干净无依赖
Jupyter Notebook
122
star
7

MLang

Android 动态化多语言框架,支持语言包的动态下发、升级、删除,一处安装,到处使用
Java
108
star
8

DragBoardView

⭐ Android 看板,支持项拖拽、列拖拽。Draggable kanban/board view for Android.
Java
107
star
9

Data-for-LaTeX_OCR

LaTeX OCR 的数据仓库
90
star
10

TFLEX

[NeurIPS 2023] TFLEX: Temporal Feature-Logic Embedding Framework for Complex Reasoning over Temporal Knowledge Graph
Jupyter Notebook
29
star
11

Python-100-Days-CheatSheet

百日python,路线:语法->爬虫->网站->数据科学->机器学习->深度学习
12
star
12

KGE-toolbox

知识图谱基础设施
Python
11
star
13

notes

📖 机器学习笔记 Notes for Machine Learning
Jupyter Notebook
9
star
14

ranger_icon_plugin

Ranger plugin that adds icon support to Ranger
Python
7
star
15

EthereumDappTutorial

💡 以太坊去中心化网页app
JavaScript
7
star
16

data_cookbook

数据处理、数据可视化。有关pandas、pyecharts、d3等,轮子代码备忘,以后直接抄。
5
star
17

QubitE

code for "QubitE: Qubit Embedding for Knowledge Graph Completion"
Python
5
star
18

Numerical-Solution-of-Differential-Eequations

微分方程数值解法 Matlab 实现
MATLAB
4
star
19

LearnTelegram

一个长期的学习 Telegram 的计划,加中文注释供自己理解
Java
4
star
20

hash2face

map Hash to Face using GAN.
Python
4
star
21

AnimPlayer

公主连结人物精灵动画播放器
HTML
4
star
22

SmsDelayer

Android 手机短信转发器,转发至另一手机短信、邮箱、服务器。含服务端代码。
Java
4
star
23

FLEX

code for "FLEX: Feature-Logic Embedding Framework for CompleX Knowledge Graph Reasoning"
Python
4
star
24

RecommenderSystem_PyTorch

速通推荐系统
Jupyter Notebook
3
star
25

streamlit-markdown

a streaming markdown component for streamlit with LaTeX, Mermaid, Table, code support. A drop-in replacement for st.markdown.
JavaScript
3
star
26

project-TimeCat

时光猫官网
HTML
3
star
27

project-recite-words

被动背单词!每 1000 毫秒显示一个单词,就算一个小时记住只 5 %,也是 3600 * 0.05 = 180 个! 只要重复次数足够多,效率低又有什么呢?
HTML
3
star
28

tutorial_matlab

matlab
2
star
29

BGEA

Python
2
star
30

EchoEA

code for "EchoEA: Echo Information between Entities and Relations for Entity Alignment"
Python
2
star
31

DemoAppleBackend

练手项目,熟悉一下 Spring Boot
Java
1
star
32

KG_datasets

知识图谱常用数据集
1
star
33

TimeCatGradle

时光猫的 gradle 构建脚本,统一配置
Kotlin
1
star
34

LinXueyuanStdio

为 github 的个人主页添加一个README吧!!!
1
star
35

gotools

handy tools written with golang
Go
1
star
36

timecat-doc

时光猫文档
1
star
37

LearnManim

随便学一下3b1b画图
Python
1
star
38

LinXueyuanStdio.github.io

Blog for recording my learning process.
HTML
1
star
39

TimeCatModuleTemplate

时光猫业务组件模板
Kotlin
1
star
40

GradleDependancyReport

android 依赖可视化管理:包大小、包依赖、kotlin支持、打开浏览器查看
Groovy
1
star
41

TimeCatRepoTemplate

时光猫仓库模板
Java
1
star
42

learnGit

learn git from demand
1
star
43

TimeCatPlugin-PluginManager

时光猫插件 插件管理,插件上传
Java
1
star
44

SAMView

单Activity多View框架,不使用Fragment
Java
1
star
45

TimeCatModule-Plugin

时光猫组件 插件宿主
Java
1
star
46

TimeCatShadow

从腾讯的 Shadow 而来
Java
1
star
47

TimeCatMaven

打包aar减少编译时间
1
star
48

CodeFilesTemplate

CodeFileStruct Template for MachineLearning 代码文件组织结构模板
Python
1
star
49

settings

some useful settings
Shell
1
star
50

latex_template_mcm

2018美赛论文模板
TeX
1
star
51

UEdbq

伤害优衣的家伙在哪里?噢,是我啊,优衣对不起😁
Java
1
star
52

ComponentAnalysis

组件化分析
Vue
1
star
53

UAIcup

UAI CUP https://biendata.com/competition/UAI/
Jupyter Notebook
1
star