--- comments: true --- # 公式识别模块使用教程 ## 一、概述 公式识别模块是OCR(光学字符识别)系统中的关键组成部分,负责将图像中的数学公式转换为可编辑的文本或计算机可识别的格式。该模块的性能直接影响到整个OCR系统的准确性和效率。公式识别模块通常会输出数学公式的 LaTeX 或 MathML 代码,这些代码将作为输入传递给文本理解模块进行后续处理。 ## 二、支持模型列表
| 模型 | 模型下载链接 | normed edit distance | BLEU score | ExpRate (%) | 模型存储大小 (M) | 介绍 |
|---|---|---|---|---|---|---|
| LaTeX_OCR_rec | 推理模型/训练模型 | 0.8821 | 0.0823 | 40.01 | 89.7 M | LaTeX-OCR是一种基于自回归大模型的公式识别算法,通过采用 Hybrid ViT 作为骨干网络,transformer作为解码器,显著提升了公式识别的准确性 |
校验结果文件具体内容为:
{
"done_flag": true,
"check_pass": true,
"attributes": {
"train_samples": 9452,
"train_sample_paths": [
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0109284.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0217434.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0166758.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0022294.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/val_0071799.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0017043.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0026204.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0209202.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/val_0157332.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0232582.png"
],
"val_samples": 1050,
"val_sample_paths": [
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0070221.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0157901.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0085392.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0196480.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0096180.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0136149.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0143310.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0004560.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0115191.png",
"../dataset/ocr_rec_latexocr_dataset_example/images/train_0015323.png"
]
},
"analysis": {
"histogram": "check_dataset/histogram.png"
},
"dataset_path": "./dataset/ocr_rec_latexocr_dataset_example",
"show_type": "image",
"dataset_type": "LaTeXOCRDataset"
}
上述校验结果中,check_pass 为 True 表示数据集格式符合要求,其他部分指标的说明如下:
attributes.train_samples:该数据集训练集样本数量为 9452;attributes.val_samples:该数据集验证集样本数量为 1050;attributes.train_sample_paths:该数据集训练集样本可视化图片相对路径列表;attributes.val_sample_paths:该数据集验证集样本可视化图片相对路径列表;另外,数据集校验还对数据集中所有类别的样本数量分布情况进行了分析,并绘制了分布直方图(histogram.png):

(1)数据集格式转换
公式识别支持 MSTextRecDataset格式的数据集转换为 LaTeXOCRDataset格式(PKL格式),数据集格式转换的参数可以通过修改配置文件中 CheckDataset 下的字段进行设置,配置文件中部分参数的示例说明如下:
CheckDataset:convert:enable: 是否进行数据集格式转换,公式识别支持 MSTextRecDataset格式的数据集转换为 LaTeXOCRDataset格式,默认为 True;src_dataset_type: 如果进行数据集格式转换,则需设置源数据集格式,默认为 MSTextRecDataset;例如,您想将 MSTextRecDataset格式的数据集转换为 LaTeXOCRDataset格式,则需将配置文件修改为:
......
CheckDataset:
......
convert:
enable: True
src_dataset_type: MSTextRecDataset
......
随后执行命令:
python main.py -c paddlex/configs/formula_recognition/LaTeX_OCR_rec.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/ocr_rec_latexocr_dataset_example
数据转换执行之后,原有标注文件会被在原路径下重命名为 xxx.bak。
以上参数同样支持通过追加命令行参数的方式进行设置:
python main.py -c paddlex/configs/formula_recognition/LaTeX_OCR_rec.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/ocr_rec_latexocr_dataset_example \
-o CheckDataset.convert.enable=True \
-o CheckDataset.convert.src_dataset_type=MSTextRecDataset
(2)数据集划分
数据集划分的参数可以通过修改配置文件中 CheckDataset 下的字段进行设置,配置文件中部分参数的示例说明如下:
CheckDataset:split:enable: 是否进行重新划分数据集,为 True 时进行数据集格式转换,默认为 False;train_percent: 如果重新划分数据集,则需要设置训练集的百分比,类型为 0-100 之间的任意整数,需要保证和 val_percent 值加和为100;例如,您想重新划分数据集为 训练集占比90%、验证集占比10%,则需将配置文件修改为:
......
CheckDataset:
......
split:
enable: True
train_percent: 90
val_percent: 10
......
随后执行命令:
python main.py -c paddlex/configs/formula_recognition/LaTeX_OCR_rec.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/ocr_rec_latexocr_dataset_example
数据划分执行之后,原有标注文件会被在原路径下重命名为 xxx.bak。
以上参数同样支持通过追加命令行参数的方式进行设置:
python main.py -c paddlex/configs/formula_recognition/LaTeX_OCR_rec.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/ocr_rec_latexocr_dataset_example \
-o CheckDataset.split.enable=True \
-o CheckDataset.split.train_percent=90 \
-o CheckDataset.split.val_percent=10
output,如需指定保存路径,可通过配置文件中 -o Global.output 字段进行设置。在完成模型训练后,所有产出保存在指定的输出目录(默认为./output/)下,通常有以下产出:
train_result.json:训练结果记录文件,记录了训练任务是否正常完成,以及产出的权重指标、相关文件路径等;
train.log:训练日志文件,记录了训练过程中的模型指标变化、loss 变化等;config.yaml:训练配置文件,记录了本次训练的超参数的配置;.pdparams、.pdema、.pdopt.pdstate、.pdiparams、.pdmodel:模型权重相关文件,包括网络参数、优化器、EMA、静态图网络参数、静态图网络结构等;在模型评估时,需要指定模型权重文件路径,每个配置文件中都内置了默认的权重保存路径,如需要改变,只需要通过追加命令行参数的形式进行设置即可,如-o Evaluate.weight_path=./output/best_accuracy/best_accuracy.pdparams。
在完成模型评估后,会产出evaluate_result.json,其记录了评估的结果,具体来说,记录了评估任务是否正常完成,以及模型的评估指标,包括 exp_rate ;