---
title: qwen-vl-latex
canonical_url: "https://www.modelscope.cn/models/fxzfxz/qwen-vl-latex"
md_url: "https://www.modelscope.cn/models/fxzfxz/qwen-vl-latex.md"
repository: fxzfxz/qwen-vl-latex
chinese_name: "数学公式识别"
last_updated: 2025-01-22
license: "Apache License 2.0"
pipeline_tag: ocr-recognition
tasks:
  - ocr-recognition
base_model:
  - Qwen/Qwen-VL-Chat
base_model_relation: finetune
parameters: 19.4B
tensor_type:
  - F32
  - BF16
library_name:
  - safetensors
  - pytorch
frameworks:
  - PyTorch
language:
  - zh
downloads: 69
stars: 7
---

# qwen-vl-latex

> qwen-vl-latex - fxzfxz 在 ModelScope 开源的模型。微调1(微调准确率较高，但可能会造成过拟合)：

fxzfxz/qwen-vl-latex 是 ModelScope 魔搭社区上的 19.4B 参数ocr-recognition模型，采用 Apache License 2.0 许可，基于 Qwen/Qwen-VL-Chat 构建。

- **Repository**: fxzfxz/qwen-vl-latex
- **License**: Apache License 2.0
- **Tasks**: ocr-recognition
- **Parameters**: 19.4B
- **Base model**: Qwen/Qwen-VL-Chat
- **Downloads**: 69
- **Stars**: 7
- **Last updated**: 2025-01-22

Source: https://www.modelscope.cn/models/fxzfxz/qwen-vl-latex

---

微调脚本：

微调1(微调准确率较高，但可能会造成过拟合)：

```bash
CUDA_VISIBLE_DEVICES=0 \
swift sft \
    --model Qwen/Qwen-VL-Chat \
    --train_type lora \
    --dataset 'AI-ModelScope/LaTeX_OCR#4000' \
    --torch_dtype bfloat16 \
    --split_dataset_ratio 0.01 \
    --num_train_epochs 3 \
    --per_device_train_batch_size 2 \
    --per_device_eval_batch_size 2 \
    --learning_rate 1e-4 \
    --lora_rank 16 \
    --target_modules all-linear \
    --gradient_accumulation_steps 8 \
    --seed 42 \
    --eval_batch_size 2 \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 10 \
    --max_length 2048 \
    --output_dir LaTex/output \
    --system "你是一个专用于数学公式图像识别并输出其LaTex格式的助手，请把识别到的公式以LaTex格式输出，无需输出除LaTex代码外的其他内容" \
    --warmup_ratio 0.05 \
    --model_author swift \
    --model_name latex_qwen_vl
```

微调1中的相关图像见/finetune1/images，根据eval/loss结果，我们选择迭代次数为7000的checkpoint。（如下图）

<div style="display: flex; justify-content: space-between;">
  <img src="finetune1/images/train_token_acc.png" alt="Image 1" style="width: 30%; margin-right: 1%;">
  <img src="finetune1/images/train_loss.png" alt="Image 2" style="width: 30%; margin-right: 1%;">
  <img src="finetune1/images/eval_loss.png" alt="Image 3" style="width: 30%;">
</div>


微调2(降低学习率、加入正则化、增加lora秩等确保训练平稳并防止过拟合和加快训练):

```bash
CUDA_VISIBLE_DEVICES=0 \
swift sft \
    --model Qwen/Qwen-VL-Chat \
    --train_type lora \
    --dataset 'AI-ModelScope/LaTeX_OCR#4000' \
    --torch_dtype bfloat16 \
    --split_dataset_ratio 0.01 \
    --num_train_epochs 3 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --learning_rate 5e-5 \
    --lora_rank 32 \
    --target_modules all-linear \
    --gradient_accumulation_steps 16 \
    --seed 42 \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 10 \
    --max_length 2048 \
    --output_dir LaTex/output \
    --system "你是一个数学公式图像识别专家，专门将输入图像中的数学公式识别并转换为LaTeX代码。请确保输出仅为LaTeX格式的公式，不包含任何额外的文本或说明。" \
    --warmup_ratio 0.05 \
    --model_author swift \
    --model_name latex_qwen_vl \
    --weight_decay 0.01
```
微调2中的相关图像见/finetune2/images，根据eval/loss结果，我们选择最后的checkpoint。

<div style="display: flex; justify-content: space-between;">
  <img src="finetune2/images/train_token_acc.png" alt="Image 1" style="width: 30%; margin-right: 1%;">
  <img src="finetune2/images/train_loss.png" alt="Image 2" style="width: 30%; margin-right: 1%;">
  <img src="finetune2/images/eval_loss.png" alt="Image 3" style="width: 30%;">
</div>

推理采用手动推理，具体代码请见文件infer.ipynb，实验环境如下：

PyTorch Version: 2.3.1+cu121

CUDA Available: True

CUDA Device Name: NVIDIA A10

CUDA Device Count: 1

推理显存使用率为：90%

使用AI-ModelScope/LaTeX_OCR的测试集推理，推理结果请参见：results_test.json文件，正确的标签参见：labels_test.json文件。

下面列举出其中两条结果，可供参考：

（1）

标签： ( { \cal L } _ { a } g ) _ { i j } = 0 , ( { \cal L } _ { a } H ) _ { i j k } = 0 

推理结果： ( \cal L _ { \alpha } g ) _ { i j } = 0 , \ ( \cal L _ { \alpha } H ) _ { i j k } = 0 


（2）


标签： \langle T _ { z z } \rangle = - 3 \times \frac { \pi ^ { 2 } } { 1 4 4 0 a ^ { 4 } } 

推理结果： \langle T _ { z z } \rangle = - 3 \times \frac { \pi ^ { 2 } } { 1 4 4 0 \mathrm { i n t } ^ { 4 } } 


（3）


标签： S _ { s t a t } = 2 \pi \sqrt { N _ { 5 } ^ { ( 1 ) } N _ { 5 } ^ { ( 2 ) } N _ { 5 } ^ { ( 3 ) } } \left( \sqrt { n } + \sqrt { \bar { n } } \right) 

推理结果： S _ { \mathrm { s t a t } } = 2 \pi \sqrt { \frac { N ( 1 ) } { N _ { B } ^ { ( 2 ) } N _ { F } ^ { ( 3 ) } } } \left( \sqrt { N _ { B } ^ { ( 2 ) } } + \sqrt { N _ { F } ^ { ( 3 ) } } \right) 

渲染结果如下：

![渲染结果](result_example.png "渲染结果")

由此可见模型对应较复杂的公式识别的仍然不是很准确，需要进一步调整数据集和训练策略




SDK下载
```bash
#安装ModelScope
pip install modelscope
```
```python
#SDK模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('fxzfxz/qwen-vl-latex')
```
Git下载
```
#Git模型下载
git clone https://www.modelscope.cn/fxzfxz/qwen-vl-latex.git
```

<p style="color: lightgrey;">如果您是本模型的贡献者，我们邀请您根据<a href="https://modelscope.cn/docs/ModelScope%E6%A8%A1%E5%9E%8B%E6%8E%A5%E5%85%A5%E6%B5%81%E7%A8%8B%E6%A6%82%E8%A7%88" style="color: lightgrey; text-decoration: underline;">模型贡献文档</a>，及时完善模型卡片内容。</p>
