Nougat - 学术文档PDF解析(LaTeX数学、表格)

文章目录


一、关于 Nougat

Nougat (N eural O ptical U nderstanding for A cademic Documents)

Nougat是理解LaTeX数学和表格的 学术文档PDF解析器。


二、安装

来自pip:

shell 复制代码
pip install nougat-ocr

来自存储库:

shell 复制代码
pip install git+https://github.com/facebookresearch/nougat

注意,在Windows上:如果您想使用GPU,请确保首先安装正确的PyTorch版本。按照这里的说明

如果要从API调用模型或生成数据集,则存在额外的依赖项。通过

shell 复制代码
pip install "nougat-ocr[api]"`或`pip install "nougat-ocr[dataset]"

三、获取PDF的预测


1、CLI

获取PDF运行的预测

shell 复制代码
$ nougat path/to/file.pdf -o output_directory

目录或文件的路径,其中每一行都是PDF的路径,也可以作为位置参数传递

shell 复制代码
$ nougat path/to/directory -o output_directory

shell 复制代码
usage: nougat [-h] [--batchsize BATCHSIZE] [--checkpoint CHECKPOINT] [--model MODEL] [--out OUT]
              [--recompute] [--markdown] [--no-skipping] pdf [pdf ...]

positional arguments:
  pdf                   PDF(s) to process.

options:
  -h, --help            show this help message and exit
  --batchsize BATCHSIZE, -b BATCHSIZE
                        Batch size to use.
  --checkpoint CHECKPOINT, -c CHECKPOINT
                        Path to checkpoint directory.
  --model MODEL_TAG, -m MODEL_TAG
                        Model tag to use.
  --out OUT, -o OUT     Output directory.
  --recompute           Recompute already computed PDF, discarding previous predictions.
  --full-precision      Use float32 instead of bfloat16. Can speed up CPU conversion for some setups.
  --no-markdown         Do not add postprocessing step for markdown compatibility.
  --markdown            Add postprocessing step for markdown compatibility (default).
  --no-skipping         Don't apply failure detection heuristic.
  --pages PAGES, -p PAGES
                        Provide page numbers like '1-4,7' for pages 1 through 4 and page 7. Only works for single PDFs.

默认模型标记为0.1.0-small。如果要使用基本模型,请使用0.1.0-base

shell 复制代码
$ nougat path/to/file.pdf -o output_directory -m 0.1.0-base

在输出目录中,每个PDF都将保存为.mmd文件,轻量级置标语言,主要与MathpixMarkdown兼容(我们使用LaTeX表)。

注意:在某些设备上,故障检测启发式无法正常工作。如果您遇到很多[MISSING_PAGE]响应,请尝试使用--no-skipping标志运行。相关:#11#67


2、API

使用app.py启动API的额外依赖项。调用

shell 复制代码
$ nougat_api

通过向http://127.0.0.1:8503/predict/发出POST请求来获得PDF文件的预测。它还接受参数`start`和`stop`以限制选择页码的计算(包括边界)。

响应是带有文档降价文本的字符串。

shell 复制代码
curl -X 'POST' \
  'http://127.0.0.1:8503/predict/' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'file=@<PDFFILE.pdf>;type=application/pdf'

要将转换限制为第1页到第5页,请使用请求URL中的start/stop参数:http://127.0.0.1:8503/predict/?start=1\&stop=5


四、数据集

生成数据集

要生成您需要的数据集

  1. 包含PDF的目录
  2. 包含具有相同文件夹结构的.html文件(LaTeXML处理的.tex文件)的目录
  3. 二进制文件pdffigures2和相应的环境变量export PDFFIGURES_PATH="/path/to/binary.jar"

然后运行

shell 复制代码
python -m nougat.dataset.split_htmls_to_pages --html path/html/root --pdfs path/pdf/root --out path/paired/output --figure path/pdffigures/outputs

其他参数包括

参数 描述
--recompute 所有拆分
--markdown MARKDOWN Markdown输出目录
--workers WORKERS 使用多少进程
--dpi DPI 页面将保存在什么分辨率
--timeout TIMEOUT 最大每张纸的时间(以秒为单位)
--tesseract Tesseract每页的OCR预测

最后创建一个jsonl文件,其中包含所有图像路径、markdown文本和元信息。

shell 复制代码
python -m nougat.dataset.create_index --dir path/paired/output --out index.jsonl

对于每个jsonl文件,您还需要生成一个查找映射以加快数据加载速度:

shell 复制代码
python -m nougat.dataset.gen_seek file.jsonl

生成的目录结构如下所示:

shell 复制代码
root/
├── images
├── train.jsonl
├── train.seek.map
├── test.jsonl
├── test.seek.map
├── validation.jsonl
└── validation.seek.map

请注意,path/paired/output中的.mmd.json文件(此处为images)不再需要。 这对于通过将文件量减半推送到S3存储桶很有用。


五、训练

要训练或微调Nougat模型,请运行

shell 复制代码
python train.py --config config/train_nougat.yaml

六、评估

运行

shell 复制代码
python test.py --checkpoint path/to/checkpoint --dataset path/to/test.jsonl --save_path path/to/results.json

要获取不同文本模式的结果,请运行

shell 复制代码
python -m nougat.metrics path/to/results.json

七、其它

1、常见问题解答

  • 为什么我只得到[MISSING_PAGE]

    Nougat 接受了在arxiv和PMC上发现的科学论文的培训。你正在处理的文件与此相似吗? 文件用什么语言?Nougat 最适合英文文件,其他拉丁语言可能也可以。中文、俄文、日文等将不起作用 。 如果满足这些要求,可能是因为在CPU或较旧的GPU上计算时故障检测中的误报(#11)。现在尝试传递--no-skipping标志。

  • 我可以从哪里下载模型检查点。

    它们在这里上传到GitHub的发布部分,您也可以在程序第一次执行期间下载它们通过传递选择首选的首选模型--model 0.1.0-{base,small}


2、引文

shell 复制代码
@misc{blecher2023nougat,
      title={Nougat: Neural Optical Understanding for Academic Documents}, 
      author={Lukas Blecher and Guillem Cucurull and Thomas Scialom and Robert Stojnic},
      year={2023},
      eprint={2308.13418},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}

3、致谢

此存储库建立在Donut存储库之上。


4、许可证

Nougat 代码库在麻省理工学院获得许可。

Nougat 模型重量在CC-BY-NC下获得许可。


2024-07-21(日)

相关推荐
海绵波波1071 天前
Zotero使用(一)PDF文件导入不会自动识别
pdf
2401_856926931 天前
图片转PDF技巧揭秘:四款高效工具推荐!
学习·pdf·图片转pdf·图片转pdf工具
alex18011 天前
python实现多个pdf文件合并
java·python·pdf
似璟如你1 天前
Vue点击按钮生成pdf文件/Vue点击按钮生成png图片
pdf
deephub1 天前
概率分布深度解析:PMF、PDF和CDF的技术指南
人工智能·python·机器学习·pdf·概率论
William.csj1 天前
PDF——压缩大小的方法
pdf
百事牛2 天前
【方法】如何禁止PDF转换成其他格式文件?
windows·pdf
IT 青年2 天前
信息安全数学基础(7)最小公倍数
数学·信息安全·最小公倍数
IT 青年2 天前
信息安全数学基础(9)素数的算数基本定理
数学·信息安全·素数的算数基本定理
白手小弟2 天前
docker部署Stirling-PDF
docker·容器·pdf