这是Nougat的官方存储库,Nougat是理解LaTeX数学和表格的学术文档PDF解析器。
项目页面: https://facebookresearch.github.io/nougat/
从点:
pip install nougat-ocr
从存储库:
pip install git+https://github.com/facebookresearch/nougat
注意,在Windows上:如果你想使用GPU,请确保首先安装正确的PyTorch版本。按照此处的说明进行操作
如果要从 API 调用模型或生成数据集,则存在额外的依赖项。安装方式
pip install "nougat-ocr[api]"或
pip install "nougat-ocr[dataset]"
获取 PDF 运行的预测
$ nougat path/to/file.pdf -o output_directory
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. --markdown Add postprocessing step for markdown compatibility. --no-skipping Don't apply failure detection heuristic.
缺省模型标记为 。如果要使用基本模型,请使用 。
0.1.0-small
0.1.0-base
$ nougat path/to/file.pdf -o output_directory -m 0.1.0-base
在输出目录中,每个PDF都将保存为一个文件,轻量级标记语言,主要与Mathpix Markdown兼容(我们使用LaTeX表格)。
.mmd
注意:在某些设备上,故障检测启发式方法无法正常工作。如果遇到很多响应,请尝试使用标志运行。相关: #11, #67
[MISSING_PAGE]--no-skipping
使用用于启动 API 的额外依赖项。叫
app.py
$ nougat_api
通过向 http://127.0.0.1:8503/predict/ 发出 POST 请求来获取 PDF 文件的预测。它还接受参数并限制计算以选择页码(包括边界)。
start
stop
响应是一个字符串,其中包含文档的降价文本。
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 中的开始/停止参数:http://127.0.0.1:8503/predict/?start=1&stop=5
生成你需要的数据集
.html
.tex
export PDFFIGURES_PATH="/path/to/binary.jar"
下次运行
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 |
降价输出目录 |
--workers WORKERS |
使用多少个进程 |
--dpi DPI |
页面将以什么分辨率保存 |
--timeout TIMEOUT |
每张纸的最长时间(秒) |
--tesseract |
每个页面的 Tesseract OCR 预测 |
最后创建一个包含所有图像路径、降价文本和元信息的文件。
jsonl
python -m nougat.dataset.create_index --dir path/paired/output --out index.jsonl
对于每个文件,你还需要生成一个查找映射以加快数据加载速度:
jsonl
python -m nougat.dataset.gen_seek file.jsonl
生成的目录结构如下所示:
root/ ├── images ├── train.jsonl ├── train.seek.map ├── test.jsonl ├── test.seek.map ├── validation.jsonl └── validation.seek.map
请注意,(此处 ) 中的 和 文件不再需要。这对于通过将文件量减半来推送到 S3 存储桶非常有用。
.mmd
.json
path/paired/output
images
要训练或微调牛轧糖模型,请运行
python train.py --config config/train_nougat.yaml
跑
python test.py --checkpoint path/to/checkpoint --dataset path/to/test.jsonl --save_path path/to/results.json
若要获取不同文本形式的结果,请运行
python -m nougat.metrics path/to/results.json
为什么我只得到 ?
[MISSING_PAGE]
Nougat接受了arXiv和PMC上发现的科学论文的培训。你正在处理的文档是否与此类似?文档使用什么语言?牛轧糖最适合英语论文,其他基于拉丁语的语言可能有效。中文、俄语、日语等将不起作用。如果满足这些要求,则可能是因为在 CPU 或较旧的 GPU 上计算时故障检测中的误报 (#11)。现在尝试传递标志。
--no-skipping
我可以从哪里下载模型检查点。
它们被上传到GitHub的发布部分。你也可以在程序首次执行期间下载它们。通过传递选择首选模型
--model 0.1.0-{base,small}
@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} }
这个仓库建立在甜甜圈仓库之上。
牛轧糖代码库在 MIT 下获得许可。
牛轧糖模型砝码在 CC-BY-NC 下获得许可。