返回模型
说明文档
latex_finetuned (ONNX)
这是 tjoab/latex_finetuned 的 ONNX 版本。它通过 此 Hugging Face Space 自动转换并上传。
使用 Transformers.js
请参阅 image-to-text 的 pipeline 文档:https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.ImageToTextPipeline
TrOCR-LaTeX(在数学手写数据上微调)
将您的手写数学公式转换为整洁的 LaTeX 代码。
这是 microsoft/trocr-base-handwritten 的微调版本,
一个基于 Transformer 的光学字符识别模型,经过适配以处理手写数学图像和结构化数学语法。
数据
在 Google 的 MathWriting 数据集上进行微调。包含超过 500,000 个手写数学表达式的数字墨水数据,通过人工标注或程序生成获得。
预期用途与限制
您可以使用此模型对单个数学表达式进行 OCR。
对于非常长的表达式,性能会有所下降(由于图像预处理,3:2 的宽高比效果最佳)。
- 创建表达式分块方案,将图像分割为子图像并分别处理,以绕过此限制。
- 若要处理多个表达式,您需要将成组的表达式切分为单个表达式。
如何使用
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
# 辅助函数(路径为 JPEG 或 PNG 格式)
def open_PIL_image(image_path: str) -> Image.Image:
image = Image.open(image_path)
if image_path.split('.')[-1].lower() == 'png':
image = Image.composite(image, PIL.Image.new('RGB', image.size, 'white'), image)
return image
# 从 Hugging Face 加载模型和处理器
processor = TrOCRProcessor.from_pretrained('tjoab/latex_finetuned')
model = VisionEncoderDecoderModel.from_pretrained('tjoab/latex_finetuned')
# 批量加载所有图像
images = [open_PIL_image(path) for path in paths]
# 预处理图像
preproc_image = processor.image_processor(images=images, return_tensors=\"pt\").pixel_values
# 生成并解码 token
# 注意:max_length 的默认值很小,如果不设置,推理结果经常会被截断
pred_ids = model.generate(preproc_image, max_length=128)
latex_preds = processor.batch_decode(pred_ids, skip_special_tokens=True)
训练细节
- 小批量大小:8
- 优化器:Adam
- 学习率调度器:余弦
fp16混合精度- 使用
torch.cuda.amp的自动混合精度(AMP)进行训练,以减少内存使用。
- 使用
- 梯度累积
- 用于模拟更大的有效批量大小,同时保持每步的内存消耗较低。
- 每 8 个小批量执行一次优化器步骤。
评估
使用字符错误率(CER)评估性能,定义为:
CER = (替换数 + 插入数 + 删除数) / 真实标注中的总字符数
-
✅ 为什么选择 CER?
- 数学表达式对结构非常敏感。即使错乱一个字符也可能完全改变其含义。
x^2vs.x_2\frac{a}{b}vs.\frac{b}{a}
- CER 会对语法上的小错误进行惩罚。
- 数学表达式对结构非常敏感。即使错乱一个字符也可能完全改变其含义。
-
评估得出的 CER 为 14.9%。
BibTeX 与引用
原始 TrOCR 模型在以下论文中介绍:
TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models 作者 Li et al.
您可以在 他们的仓库 中找到源代码。
@misc{li2021trocr,
title={TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models},
author={Minghao Li and Tengchao Lv and Lei Cui and Yijuan Lu and Dinei Florencio and Cha Zhang and Zhoujun Li and Furu Wei},
year={2021},
eprint={2109.10282},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
onnx-community/latex_finetuned-ONNX
作者 onnx-community
image-to-text
transformers.js
↓ 1
♥ 0
创建时间: 2025-11-05 05:52:18+00:00
更新时间: 2025-11-05 05:52:45+00:00
在 Hugging Face 上查看文件 (27)
.gitattributes
README.md
config.json
generation_config.json
merges.txt
onnx/decoder_model.onnx
ONNX
onnx/decoder_model_bnb4.onnx
ONNX
onnx/decoder_model_fp16.onnx
ONNX
onnx/decoder_model_int8.onnx
ONNX
onnx/decoder_model_q4.onnx
ONNX
onnx/decoder_model_q4f16.onnx
ONNX
onnx/decoder_model_quantized.onnx
ONNX
onnx/decoder_model_uint8.onnx
ONNX
onnx/encoder_model.onnx
ONNX
onnx/encoder_model_bnb4.onnx
ONNX
onnx/encoder_model_fp16.onnx
ONNX
onnx/encoder_model_int8.onnx
ONNX
onnx/encoder_model_q4.onnx
ONNX
onnx/encoder_model_q4f16.onnx
ONNX
onnx/encoder_model_quantized.onnx
ONNX
onnx/encoder_model_uint8.onnx
ONNX
preprocessor_config.json
quantize_config.json
special_tokens_map.json
tokenizer.json
tokenizer_config.json
vocab.json