All Projects → LinXueyuanStdio → Latex_ocr

LinXueyuanStdio / Latex_ocr

Licence: apache-2.0
💎 数学公式识别

Projects that are alternatives of or similar to Latex ocr

Chinese sentiment
中文情感分析,CNN,BI-LSTM,文本分类
Stars: ✭ 216 (-0.92%)
Mutual labels:  jupyter-notebook
Tutorials
AI-related tutorials. Access any of them for free → https://towardsai.net/editorial
Stars: ✭ 204 (-6.42%)
Mutual labels:  jupyter-notebook
Python Sonic
Programming Music with Python, Sonic Pi and Supercollider
Stars: ✭ 217 (-0.46%)
Mutual labels:  jupyter-notebook
Python lectures
파이썬Python 강의에 사용되는 소스코드Source Code와 강의 자료들을 모은 repository 입니다.
Stars: ✭ 214 (-1.83%)
Mutual labels:  jupyter-notebook
Rl Adventure 2
PyTorch0.4 implementation of: actor critic / proximal policy optimization / acer / ddpg / twin dueling ddpg / soft actor critic / generative adversarial imitation learning / hindsight experience replay
Stars: ✭ 2,633 (+1107.8%)
Mutual labels:  jupyter-notebook
Amazon Sagemaker Mlops Workshop
Machine Learning Ops Workshop with SageMaker: lab guides and materials.
Stars: ✭ 215 (-1.38%)
Mutual labels:  jupyter-notebook
Epidemiology101
Epidemic Modeling for Everyone
Stars: ✭ 215 (-1.38%)
Mutual labels:  jupyter-notebook
Portrait Shadow Manipulation
Stars: ✭ 217 (-0.46%)
Mutual labels:  jupyter-notebook
Text Classification
Text Classification through CNN, RNN & HAN using Keras
Stars: ✭ 216 (-0.92%)
Mutual labels:  jupyter-notebook
Malware Detection
Malware Detection and Classification Using Machine Learning
Stars: ✭ 217 (-0.46%)
Mutual labels:  jupyter-notebook
Hyperspectral
Deep Learning for Land-cover Classification in Hyperspectral Images.
Stars: ✭ 215 (-1.38%)
Mutual labels:  jupyter-notebook
Notebooker
Productionise your Jupyter Notebooks as easily as you wrote them.
Stars: ✭ 215 (-1.38%)
Mutual labels:  jupyter-notebook
Tensorflow
Deep Learning Zero to All - Tensorflow
Stars: ✭ 216 (-0.92%)
Mutual labels:  jupyter-notebook
Understanding tensorflow nn
🔮Getting started with TensorFlow: Classifying Text with Neural Networks
Stars: ✭ 215 (-1.38%)
Mutual labels:  jupyter-notebook
Paddlehelix
Bio-Computing Platform featuring Large-Scale Representation Learning and Multi-Task Deep Learning “螺旋桨”生物计算工具集
Stars: ✭ 213 (-2.29%)
Mutual labels:  jupyter-notebook
Pytorch Superpoint
Superpoint Implemented in PyTorch: https://arxiv.org/abs/1712.07629
Stars: ✭ 214 (-1.83%)
Mutual labels:  jupyter-notebook
Deeplearning
Stars: ✭ 216 (-0.92%)
Mutual labels:  jupyter-notebook
Data Visualization
Data Visualization with Python
Stars: ✭ 217 (-0.46%)
Mutual labels:  jupyter-notebook
How To Read Pytorch
Quick, visual, principled introduction to pytorch code through five colab notebooks.
Stars: ✭ 218 (+0%)
Mutual labels:  jupyter-notebook
Stock market prediction
This is the code for "Stock Market Prediction" by Siraj Raval on Youtube
Stars: ✭ 217 (-0.46%)
Mutual labels:  jupyter-notebook

LaTeX OCR

Seq2Seq + Attention + Beam Search。

结构

1. 搭建环境

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

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 请自行安装

  2. 安装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/ 下的公式生成用于训练的图片。

一步训练

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. 评价

指标 训练分数 测试分数
perplexity 1.39 1.44
EditDistance 81.68 80.45
BLEU-4 78.21 75.42
ExactMatchScore 13.93 12.44

perplexity 是越接近1越好,其余3个指标是越大越好。ExactMatchScore 比较低,继续训练应该可以到 70 以上。机器不太好,训练太费时间了。

5. 模型的具体实现细节

总述

首先我们获取到足够的公式,对公式进行规范化处理,方便划分出字典。然后通过规范化的公式使用脚本生成图片,具体用到了latex和ghostscript和magick,同时保存哪个公式生成哪个图片,保存为公式-图片映射文件。这样我们得到了3个数据集:规范化的公式集,图片集,公式-图片映射集,还有个附赠品:latex字典。这个字典决定了模型的上限,也就是说,模型预测出的公式只能由字典里的字符组成,不会出现字典以外的字符。

然后构建模型。

模型分为3部分,数据生成器,神经网络模型,使用脚本。

数据生成器读取公式-图片映射文件,为模型提供(公式, 图片)的矩阵元组。

神经网络模型是 Seq2Seq + Attention + Beam Search。Seq2Seq的Encoder是CNN,Decoder是LSTM。Encoder和Decoder之间插入Attention层,具体操作是这样:Encoder到Decoder有个扁平化的过程,Attention就是在这里插入的。随Attention插入的还有我们自定义的一个op,用来导出Attention的数据,做Attention的可视化。

使用脚本包括构建脚本、训练脚本、测试脚本、预测脚本、评估脚本、可视化脚本。使用说明看上面的命令行就行。

训练过程根据epoch动态调整LearningRate。decoder可以选择用lstmgru,在configs/model.json里改就行。最后输出结果可以选择用 beam_searchgreedy,也是在configs/model.json里改。

数据获取和数据处理

我们只要获取到正确的latex公式就行。因为我们可以使用脚本将latex渲染出图片,所以就不用图片数据了。

原来我们想使用爬虫爬取arXiv的论文,然后通过正则表达式提取论文里的latex公式。

但是最后我们发现已经有人做了这个工作,所以就用了他们的公式数据。im2latex-100k , arXiv:1609.04938

现在我们获取到latex公式数据,下面进行规范化。

为什么要规范化:如果不规范化,我们构建字典时就只能是char wise,而latex中有很多是有特定排列的指令,比如\lim,这样模型需要花费额外的神经元来记住这些pattern,会使模型效果变差,也导致训练费时间。(有时根本不收敛...别问我怎么知道的...)

我们先手动在代码编辑器里对数据进行规范化,很玄学地用了一些正则表达式,一步一步进行规范化。

最后总结了一下,明确要构建的字典大概是什么样的,然后写了脚本来处理。

然后是通过公式生成图片,保存公式-图片映射文件,构建字典。

构建字典很简单,遍历公式文件的每一行,然后以空格符为分隔符分割成若干latex块,去掉每一块首尾空格,若非空则加入字典集,保证不重复。

保存公式-图片映射文件也很简单,就是在渲染出图片后,保存当前的公式在公式文件里的行号图片路径,写入映射文件里,也就是.matching.txt文件。图片文件名是直接用公式行号来命名的,比如1234.png 1234表示第1234行公式的公式图片是1234.png。所以知道了行号,就知道了公式图片路径。

通过公式生成图片稍微复杂一点,需要用到几个库:latex、ghostscript和magick。事实上用Katex也是可以的,katex是一个渲染latex公式的js库,体积小速度快。原来我们也是打算用这个库处理,后来因为环境问题放弃了。

latex原先我的环境里有了,这是用来生成pdf文件的。执行脚本后会得到A4纸大小的一页pdf。

ghostscript和magick绑定在一起,用来把pdf转化为png格式的图片。

转化为图片后,选定公式 padding 8个像素的方框,crop框外的空白,然后灰度化。

模型构建

让我鸽一段时间。。。有空再写!

6. 踩坑记录

win10 用 GPU 加速训练

装驱动后就行了。运行下面代码进行训练:

C:/Users/dlink/AppData/Local/Programs/Python/Python35/python.exe train.py --data=configs/data.json --vocab=configs/vocab.json --training=configs/training.json --model=configs/model.json --output=results/full/

我的环境比较奇葩,是win10+ubuntu wsl,也就是windows+linux子系统。我的GPU驱动装在win里面了,linux里没装,不过文件系统是共用的,所以用win的GPU驱动来训练模型。(嗯,python也有两套版本哈哈哈,都是python3.5)

如何可视化Attention层

在Attention层内自定义一个op,通过这个op把Attention传递到一个全局变量里。其他程序在模型预测完公式后,就可以在这个全局变量里获取到Attention。

致谢

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

论文:

  1. Show, Attend and Tell(Kelvin Xu...)
  2. Harvard's paper and dataset
  3. Seq2Seq for LaTeX generation.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].