244 lines
9.5 KiB
Markdown
Raw Normal View History

---
comments: true
---
# 文档类视觉语言模型模块使用教程
## 一、概述
文档类视觉语言模型是当前一种前沿的多模态处理技术,旨在解决传统文档处理方法的局限性。传统方法往往局限于处理特定格式或预定义类别的文档信息,而文档类视觉语言模型能够融合视觉与语言信息,理解并处理多样化的文档内容。通过结合计算机视觉与自然语言处理技术,模型可以识别文档中的图像、文本及其相互关系,甚至能理解复杂版面结构中的语义信息。这使得文档处理更加智能化、灵活化,具备更强的泛化能力,在自动化办公、信息提取等领域展现出广阔的应用前景。
## 二、支持模型列表
<table>
<tr>
<th>模型</th><th>模型下载链接</th>
<th>模型存储大小GB</th>
<th>介绍</th>
</tr>
<tr>
<td>PP-DocBee-2B</td><td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-DocBee-2B_infer.tar">推理模型</a></td>
<td>4.2</td>
<td rowspan="2">PP-DocBee 是飞桨团队自研的一款专注于文档理解的多模态大模型,在中文文档理解任务上具有卓越表现。该模型通过近 500 万条文档理解类多模态数据集进行微调优化各种数据集包括了通用VQA类、OCR类、图表类、text-rich文档类、数学和复杂推理类、合成数据类、纯文本数据等并设置了不同训练数据配比。在学术界权威的几个英文文档理解评测榜单上PP-DocBee基本都达到了同参数量级别模型的SOTA。在内部业务中文场景类的指标上PP-DocBee也高于目前的热门开源和闭源模型。</td>
</tr>
<tr>
<td>PP-DocBee-7B</td><td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-DocBee-7B_infer.tar">推理模型</a></td>
<td>15.8</td>
</tr>
</table>
## 三、快速开始
> ❗ 在快速开始前,请先安装 PaddleOCR 的 wheel 包,详细请参考 [安装教程](../ppocr/installation.md)。
使用一行命令即可快速体验:
```bash
paddleocr doc_vlm -i "{'image': 'https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/medal_table.png', 'query': '识别这份表格的内容'}"
```
您也可以将开放文档类视觉语言模型模块中的模型推理集成到您的项目中。运行以下代码前,请您下载[示例图片](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/medal_table.png)到本地。
```python
from paddleocr import DocVLM
model = DocVLM(model_name="PP-DocBee-2B")
results = model.predict(
input={"image": "medal_table.png", "query": "识别这份表格的内容"},
batch_size=1
)
for res in results:
res.print()
res.save_to_json(f"./output/res.json")
```
运行后,得到的结果为:
```bash
{'res': {'image': 'medal_table.png', 'query': '识别这份表格的内容', 'result': '| 名次 | 国家/地区 | 金牌 | 银牌 | 铜牌 | 奖牌总数 |\n| --- | --- | --- | --- | --- | --- |\n| 1 | 中国CHN | 48 | 22 | 30 | 100 |\n| 2 | 美国USA | 36 | 39 | 37 | 112 |\n| 3 | 俄罗斯RUS | 24 | 13 | 23 | 60 |\n| 4 | 英国GBR | 19 | 13 | 19 | 51 |\n| 5 | 德国GER | 16 | 11 | 14 | 41 |\n| 6 | 澳大利亚AUS | 14 | 15 | 17 | 46 |\n| 7 | 韩国KOR | 13 | 11 | 8 | 32 |\n| 8 | 日本JPN | 9 | 8 | 8 | 25 |\n| 9 | 意大利ITA | 8 | 9 | 10 | 27 |\n| 10 | 法国FRA | 7 | 16 | 20 | 43 |\n| 11 | 荷兰NED | 7 | 5 | 4 | 16 |\n| 12 | 乌克兰UKR | 7 | 4 | 11 | 22 |\n| 13 | 肯尼亚KEN | 6 | 4 | 6 | 16 |\n| 14 | 西班牙ESP | 5 | 11 | 3 | 19 |\n| 15 | 牙买加JAM | 5 | 4 | 2 | 11 |\n'}}
```
运行结果参数含义如下:
- `image`: 表示输入待预测图像的路径
- `query`: 表述输入待预测的文本信息
- `result`: 模型预测的结果信息
预测结果打印可视化如下:
```bash
| 名次 | 国家/地区 | 金牌 | 银牌 | 铜牌 | 奖牌总数 |
| --- | --- | --- | --- | --- | --- |
| 1 | 中国CHN | 48 | 22 | 30 | 100 |
| 2 | 美国USA | 36 | 39 | 37 | 112 |
| 3 | 俄罗斯RUS | 24 | 13 | 23 | 60 |
| 4 | 英国GBR | 19 | 13 | 19 | 51 |
| 5 | 德国GER | 16 | 11 | 14 | 41 |
| 6 | 澳大利亚AUS | 14 | 15 | 17 | 46 |
| 7 | 韩国KOR | 13 | 11 | 8 | 32 |
| 8 | 日本JPN | 9 | 8 | 8 | 25 |
| 9 | 意大利ITA | 8 | 9 | 10 | 27 |
| 10 | 法国FRA | 7 | 16 | 20 | 43 |
| 11 | 荷兰NED | 7 | 5 | 4 | 16 |
| 12 | 乌克兰UKR | 7 | 4 | 11 | 22 |
| 13 | 肯尼亚KEN | 6 | 4 | 6 | 16 |
| 14 | 西班牙ESP | 5 | 11 | 3 | 19 |
| 15 | 牙买加JAM | 5 | 4 | 2 | 11 |
```
相关方法、参数等说明如下:
* `DocVLM`实例化文档类视觉语言模型(此处以`PP-DocBee-2B`为例),具体说明如下:
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>可选项</th>
<th>默认值</th>
</tr>
</thead>
<tr>
<td><code>model_name</code></td>
<td>模型名称</td>
<td><code>str</code></td>
<td></td>
<td><code></code></td>
</tr>
<tr>
<td><code>model_dir</code></td>
<td>模型存储路径</td>
<td><code>str</code></td>
<td></td>
<td></td>
</tr>
<tr>
<td><code>device</code></td>
<td>模型推理设备</td>
<td><code>str</code></td>
<td>支持指定GPU具体卡号如“gpu:0”其他硬件具体卡号如“npu:0”CPU如“cpu”。</td>
<td><code>gpu:0</code></td>
</tr>
<tr>
<td><code>use_hpip</code></td>
<td>是否启用高性能推理插件。目前暂不支持。</td>
<td><code>bool</code></td>
<td></td>
<td><code>False</code></td>
</tr>
<tr>
<td><code>hpi_config</code></td>
<td>高性能推理配置。目前暂不支持。</td>
<td><code>dict</code> | <code>None</code></td>
<td></td>
<td><code>None</code></td>
</tr>
</table>
* 其中,`model_name` 必须指定,指定 `model_name` 后,默认使用 PaddleX 内置的模型参数,在此基础上,指定 `model_dir` 时,使用用户自定义的模型。
* 调用文档类视觉语言模型的 `predict()` 方法进行推理预测,该方法会返回一个结果列表。另外,本模块还提供了 `predict_iter()` 方法。两者在参数接受和结果返回方面是完全一致的,区别在于 `predict_iter()` 返回的是一个 `generator`,能够逐步处理和获取预测结果,适合处理大型数据集或希望节省内存的场景。可以根据实际需求选择使用这两种方法中的任意一种。`predict()` 方法参数有 `input``batch_size`,具体说明如下:
<table>
<thead>
<tr>
<th>参数</th>
<th>参数说明</th>
<th>参数类型</th>
<th>可选项</th>
<th>默认值</th>
</tr>
</thead>
<tr>
<td><code>input</code></td>
<td>待预测数据</td>
<td><code>dict</code></td>
<td>
<code>Dict</code>, 需要根据具体的模型确定如PP-DocBee系列的输入为{'image': image_path, 'query': query_text}
</td>
<td></td>
</tr>
<tr>
<td><code>batch_size</code></td>
<td>批大小</td>
<td><code>int</code></td>
<td>整数(目前仅支持为1)</td>
<td>1</td>
</tr>
</table>
* 对预测结果进行处理每个样本的预测结果均为对应的Result对象且支持打印、保存为`json`文件的操作:
<table>
<thead>
<tr>
<th>方法</th>
<th>方法说明</th>
<th>参数</th>
<th>参数类型</th>
<th>参数说明</th>
<th>默认值</th>
</tr>
</thead>
<tr>
<td rowspan = "3"><code>print()</code></td>
<td rowspan = "3">打印结果到终端</td>
<td><code>format_json</code></td>
<td><code>bool</code></td>
<td>是否对输出内容进行使用 <code>JSON</code> 缩进格式化</td>
<td><code>True</code></td>
</tr>
<tr>
<td><code>indent</code></td>
<td><code>int</code></td>
<td>指定缩进级别,以美化输出的 <code>JSON</code> 数据,使其更具可读性,仅当 <code>format_json</code><code>True</code> 时有效</td>
<td>4</td>
</tr>
<tr>
<td><code>ensure_ascii</code></td>
<td><code>bool</code></td>
<td>控制是否将非 <code>ASCII</code> 字符转义为 <code>Unicode</code>。设置为 <code>True</code> 时,所有非 <code>ASCII</code> 字符将被转义;<code>False</code> 则保留原始字符,仅当<code>format_json</code><code>True</code>时有效</td>
<td><code>False</code></td>
</tr>
<tr>
<td rowspan = "3"><code>save_to_json()</code></td>
<td rowspan = "3">将结果保存为json格式的文件</td>
<td><code>save_path</code></td>
<td><code>str</code></td>
<td>保存的文件路径,当为目录时,保存文件命名与输入文件类型命名一致</td>
<td></td>
</tr>
<tr>
<td><code>indent</code></td>
<td><code>int</code></td>
<td>指定缩进级别,以美化输出的 <code>JSON</code> 数据,使其更具可读性,仅当 <code>format_json</code><code>True</code> 时有效</td>
<td>4</td>
</tr>
<tr>
<td><code>ensure_ascii</code></td>
<td><code>bool</code></td>
<td>控制是否将非 <code>ASCII</code> 字符转义为 <code>Unicode</code>。设置为 <code>True</code> 时,所有非 <code>ASCII</code> 字符将被转义;<code>False</code> 则保留原始字符,仅当<code>format_json</code><code>True</code>时有效</td>
<td><code>False</code></td>
</tr>
</table>
* 此外,也支持通过属性获取预测结果,具体如下:
<table>
<thead>
<tr>
<th>属性</th>
<th>属性说明</th>
</tr>
</thead>
<tr>
<td rowspan = "1"><code>json</code></td>
<td rowspan = "1">获取预测的<code>json</code>格式的结果</td>
</tr>
</table>
## 四、二次开发
当前模块暂时不支持微调训练,仅支持推理集成。关于该模块的微调训练,计划在未来支持。