Giter Site home page Giter Site logo

latex_ocr's Introduction

LaTeX OCR

本项目增强版:LaTeX_OCR_PRO

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.

latex_ocr's People

Contributors

dependabot[bot] avatar linxueyuanstdio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

latex_ocr's Issues

关于测试集的问题

我是CV的初学者,想通过你的这个项目有一些对识别的了解。通过阅读你的代码我发现在生成测试集的时候,用了和训练数据时候一样的DataGenerator,同时用了formula和image两个指标,按道理不是应该要么知道formula得到image,要么知道image得到formula的吗?同时知道的话不是应该是训练集吗?

环境配置问题

MagickCore/magick.c:100:12: fatal error: 'libxml/parser.h' file not found

模型问题

我跑了一下full分支的数据,在第2个epoch之后就开始过拟合了。是否参数设置有问题呢?你是否跑完了全量数据呢?

关于方程组

我在test.norm.txt第一行加入了一个方程组的latex
\begin{cases}& \text{ if } x=5 \ & \text{ if } x=6 \end{cases}
但生成的图片不太对,
image

期望生成这样的
image
要怎么处理呢

small准确率

跑了一下small的数据,准确率很低,请问这是正常现象还是有问题呢?

25/25 [==============================] - 3s - loss: 318.912090 - perplexity: inf - lr: 0.100000

  • Training: - 3s - loss: 318.912090 - perplexity: inf - lr: 0.100000
  • Evaluating...
    writting file results/small/formulas_val/ref.txt
    writting file results/small/formulas_val/hyp_0.txt
    writting file results/small/formulas_val/hyp_1.txt
    Loaded 30 formulas from results/small/formulas_val/ref.txt
    Loaded 30 formulas from results/small/formulas_val/hyp_0.txt
  • Eval: BLEU-4 is 0.00 || ExactMatchScore is 0.00 || perplexity is -212269996680525689170079392416506714400026115682911821216976265628196223383119470307957417682148579524265885659877825415860269457788221744218112.00 || EditDistance is 7.41
  • Elapsed time: 9.26, learning rate: 0.10000

训练问题

训练的时候出现了
image
显存占用特别的大,是为什么呢?
而且训练速度很慢,用的是泰坦5,GPU占用率只有20+,请问是正常现象还是出了什么问题了呢?

训练问题

大佬能不能提供一下练好的数据啊 小破机太弱了练了一个礼拜练不动啊

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.