docs/version3.x/module_usage/table_structure_recognition.md
表格结构识别是表格识别系统中的重要组成部分,能够将不可编辑表格图片转换为可编辑的表格形式(例如html)。表格结构识别的目标是对表格的行、列和单元格位置进行识别,该模块的性能直接影响到整个表格识别系统的准确性和效率。表格结构识别模块会输出表格区域的html代码,这些代码将作为输入传递给表格识别产线进行后续处理。
<table> <tr> <th>模型</th><th>模型下载链接</th> <th>精度(%)</th> <th>GPU推理耗时(ms) [常规模式 / 高性能模式]</th> <th>CPU推理耗时(ms) [常规模式 / 高性能模式]</th> <th>模型存储大小(MB)</th> <th>介绍</th> </tr> <tr> <td>SLANet</td> <td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/SLANet_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/SLANet_pretrained.pdparams">训练模型</a></td> <td>59.52</td> <td>23.96 / 21.75</td> <td>- / 43.12</td> <td>6.9</td> <td rowspan="1">SLANet 是百度飞桨视觉团队自研的表格结构识别模型。该模型通过采用 CPU 友好型轻量级骨干网络 PP-LCNet、高低层特征融合模块 CSP-PAN、结构与位置信息对齐的特征解码模块 SLA Head,大幅提升了表格结构识别的精度和推理速度。</td> </tr> <tr> <td>SLANet_plus</td> <td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/SLANet_plus_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/SLANet_plus_pretrained.pdparams">训练模型</a></td> <td>63.69</td> <td>23.43 / 22.16</td> <td>- / 41.80</td> <td>6.9</td> <td rowspan="1">SLANet_plus 是百度飞桨视觉团队自研的表格结构识别模型 SLANet 的增强版。相较于 SLANet,SLANet_plus 对无线表、复杂表格的识别能力得到了大幅提升,并降低了模型对表格定位准确性的敏感度,即使表格定位出现偏移,也能够较准确地进行识别。 </td> </tr> <tr> <td>SLANeXt_wired</td> <td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/SLANeXt_wired_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/SLANeXt_wired_pretrained.pdparams">训练模型</a></td> <td rowspan="2">69.65</td> <td rowspan="2">85.92 / 85.92</td> <td rowspan="2">- / 501.66</td> <td rowspan="2">351</td> <td rowspan="2">SLANeXt 系列是百度飞桨视觉团队自研的新一代表格结构识别模型。相较于 SLANet 和 SLANet_plus,SLANeXt 专注于对表格结构进行识别,并且对有线表格(wired)和无线表格(wireless)的识别分别训练了专用的权重,对各类型表格的识别能力都得到了明显提高,特别是对有线表格的识别能力得到了大幅提升。</td> </tr> <tr> <td>SLANeXt_wireless</td> <td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/SLANeXt_wireless_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/SLANeXt_wireless_pretrained.pdparams">训练模型</a></td> </tr> </table>推理耗时仅包含模型推理耗时,不包含前后处理耗时。表格中的“常规模式”耗时对应本地 <code>paddle_static</code> 推理引擎。
<strong>测试环境说明:</strong>
<ul> <li><b>性能测试环境</b> <ul> <li><strong>测试数据集:</strong>内部自建的高难度中文表格识别数据集。</li> <li><strong>硬件配置:</strong> <ul> <li>GPU:NVIDIA Tesla T4</li> <li>CPU:Intel Xeon Gold 6271C @ 2.60GHz</li> </ul> </li> <li><strong>软件环境:</strong> <ul> <li>Ubuntu 20.04 / CUDA 11.8 / cuDNN 8.9 / TensorRT 8.6.1.6</li> <li>paddlepaddle-gpu 3.0.0 / paddleocr 3.0.3</li> </ul> </li> </ul> </li> <li><b>推理模式说明</b></li> </ul> <table border="1"> <thead> <tr> <th>模式</th> <th>GPU配置</th> <th>CPU配置</th> <th>加速技术组合</th> </tr> </thead> <tbody> <tr> <td>常规模式</td> <td>FP32精度 / 无TRT加速</td> <td>FP32精度 / 8线程</td> <td>PaddleInference</td> </tr> <tr> <td>高性能模式</td> <td>选择先验精度类型和加速策略的最优组合</td> <td>FP32精度 / 8线程</td> <td>选择先验最优后端(Paddle/OpenVINO/TRT等)</td> </tr> </tbody> </table>❗ 在快速开始前,请先安装 PaddleOCR 的 wheel 包,详细请参考 安装教程。
使用一行命令即可快速体验:
paddleocr table_structure_recognition -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/table_recognition.jpg
上述示例默认使用 <code>paddle_static</code> 推理引擎,请先按照飞桨框架安装完成 PaddlePaddle 安装。
如果选择 transformers 作为推理引擎,请确保已配置 Transformers 环境,然后执行如下命令:
# 使用 transformers 引擎进行推理
paddleocr table_structure_recognition -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/table_recognition.jpg \
--engine transformers
在大多数场景下,默认的 paddle_static 推理引擎通常具备更好的推理性能,建议优先使用。
<b>注:</b>PaddleOCR 官方模型默认从 HuggingFace 获取,如运行环境访问 HuggingFace 不便,可通过环境变量修改模型源为 BOS:PADDLE_PDX_MODEL_SOURCE="BOS",未来将支持更多主流模型源;
您也可以将表格结构识别的模块中的模型推理集成到您的项目中。运行以下代码前,请您下载示例图片到本地。
from paddleocr import TableStructureRecognition
model = TableStructureRecognition(model_name="SLANet")
output = model.predict(input="table_recognition.jpg", batch_size=1)
for res in output:
res.print(json_format=False)
res.save_to_json("./output/res.json")
上述示例默认使用 <code>paddle_static</code> 推理引擎,请先按照飞桨框架安装完成 PaddlePaddle 安装。
如果选择 transformers 作为推理引擎,请确保已配置 Transformers 环境,然后执行如下代码:
from paddleocr import TableStructureRecognition
model = TableStructureRecognition(
model_name="SLANet",
engine="transformers",
)
output = model.predict(input="table_recognition.jpg", batch_size=1)
for res in output:
res.print(json_format=False)
res.save_to_json("./output/res.json")
在大多数场景下,默认的 paddle_static 推理引擎通常具备更好的推理性能,建议优先使用。
训练后的模型如果想使用 paddle_dynamic 或 transformers 引擎,请参考后文 推理引擎 中的 权重转换 部分将模型由 pdparams 格式通过 PaddleX 转换为 safetensors 格式。
运行后,得到的结果为:
{'res': {'input_path': 'table_recognition.jpg', 'page_index': None, 'bbox': [[42, 2, 390, 2, 388, 27, 40, 26], [11, 35, 89, 35, 87, 63, 11, 63], [113, 34, 192, 34, 186, 64, 109, 64], [219, 33, 399, 33, 393, 62, 212, 62], [413, 33, 544, 33, 544, 64, 407, 64], [12, 67, 98, 68, 96, 93, 12, 93], [115, 66, 205, 66, 200, 91, 111, 91], [234, 65, 390, 65, 385, 92, 227, 92], [414, 66, 537, 67, 537, 95, 409, 95], [7, 97, 106, 97, 104, 128, 7, 128], [113, 96, 206, 95, 201, 127, 109, 127], [236, 96, 386, 96, 381, 128, 230, 128], [413, 96, 534, 95, 533, 127, 408, 127]], 'structure': ['<html>', '<body>', '<table>', '<tr>', '<td', ' colspan="4"', '>', '</td>', '</tr>', '<tr>', '<td></td>', '<td></td>', '<td></td>', '<td></td>', '</tr>', '<tr>', '<td></td>', '<td></td>', '<td></td>', '<td></td>', '</tr>', '<tr>', '<td></td>', '<td></td>', '<td></td>', '<td></td>', '</tr>', '</table>', '</body>', '</html>'], 'structure_score': 0.99948007}}
参数含义如下:
<ul> <li><code>input_path</code>:输入的待预测表格图像的路径</li> <li><code>page_index</code>:如果输入是PDF文件,则表示当前是PDF的第几页,否则为 <code>None</code></li> <li><code>boxes</code>:预测的表格单元格信息,一个列表,由预测的若干表格单元格坐标组成。特别地, SLANeXt 系列模型预测的表格单元格无效</li> <li><code>structure</code>:预测的表格结构Html表达式,一个列表,由预测的若干Html关键字按顺序组成</li> <li><code>structure_score</code>:预测表格结构的置信度</li> </ul> 相关方法、参数等说明如下:<b>说明:</b> 如果设置为<code>None</code>,则使用<code>PP-LCNet_x1_0_table_cls</code>。</td>
<td><code>str|None</code></td> <td><code>None</code></td> </tr> <tr> <td><code>model_dir</code></td> <td><b>含义:</b>模型存储路径。</td> <td><code>str|None</code></td> <td><code>None</code></td> </tr> <tr> <td><code>device</code></td> <td><b>含义:</b>用于推理的设备。<b>说明:</b> <b>例如:</b><code>"cpu"</code>、<code>"gpu"</code>、<code>"npu"</code>、<code>"gpu:0"</code>、<code>"gpu:0,1"</code>。
如指定多个设备,将进行并行推理。
默认情况下,优先使用 GPU 0;若不可用则使用 CPU。
</td> <td><code>str|None</code></td> <td><code>None</code></td> </tr> <tr> <td><code>engine</code></td> <td><b>含义:</b>推理引擎。 <b>说明:</b>支持 <code>None</code>(默认值)、<code>paddle</code>、<code>paddle_static</code>、<code>paddle_dynamic</code>、<code>transformers</code>。保持为默认值 <code>None</code> 时,本地推理默认使用 <code>paddle_static</code> 引擎。详细说明、取值、兼容性规则与示例请参见 <a href="../inference_engine.md">推理引擎与配置说明</a>。</td> <td><code>str|None</code></td> <td><code>None</code></td> </tr> <tr> <td><code>engine_config</code></td> <td><b>含义:</b>推理引擎配置。 <b>说明:</b>推荐与 <code>engine</code> 搭配使用。详细字段、兼容性规则与示例请参见 <a href="../inference_engine.md">推理引擎与配置说明</a>。</td> <td><code>dict|None</code></td> <td><code>None</code></td> </tr> <tr> <td><code>enable_hpi</code></td> <td><b>含义:</b>是否启用高性能推理。</td> <td><code>bool</code></td> <td><code>False</code></td> </tr> <tr> <td><code>use_tensorrt</code></td> <td><b>含义:</b>是否启用 Paddle Inference 的 TensorRT 子图引擎。<b>说明:</b> 如果模型不支持通过 TensorRT 加速,即使设置了此标志,也不会使用加速。
对于 CUDA 11.8 版本的飞桨,兼容的 TensorRT 版本为 8.x(x>=6),建议安装 TensorRT 8.6.1.6。
</td> <td><code>bool</code></td> <td><code>False</code></td> </tr> <tr> <td><code>precision</code></td> <td><b>含义:</b>当使用 Paddle Inference 的 TensorRT 子图引擎时设置的计算精度。<b>说明:</b> <b>可选项:</b><code>"fp32"</code>、<code>"fp16"</code>。</td>
<td><code>str</code></td> <td><code>"fp32"</code></td> </tr> <tr> <td><code>enable_mkldnn</code></td> <td> <b>含义:</b>是否启用 MKL-DNN 加速推理。<b>说明:</b> 如果 MKL-DNN 不可用或模型不支持通过 MKL-DNN 加速,即使设置了此标志,也不会使用加速。
</td> <td><code>bool</code></td> <td><code>True</code></td> </tr> <tr> <td><code>mkldnn_cache_capacity</code></td> <td> <b>含义:</b>MKL-DNN 缓存容量。 </td> <td><code>int</code></td> <td><code>10</code></td> </tr> <tr> <td><code>cpu_threads</code></td> <td><b>含义:</b>在 CPU 上推理时使用的线程数量。</td> <td><code>int</code></td> <td><code>10</code></td> </tr> </tbody> </table><b>说明:</b>
<ul> <li><b>Python Var</b>:如 <code>numpy.ndarray</code> 表示的图像数据</li> <li><b>str</b>:如图像文件或者PDF文件的本地路径:<code>/root/data/img.jpg</code>;<b>如URL链接</b>,如图像文件或PDF文件的网络URL:<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/img_rot180_demo.jpg">示例</a>;<b>如本地目录</b>,该目录下需包含待预测图像,如本地路径:<code>/root/data/</code>(当前不支持目录中包含PDF文件的预测,PDF文件需要指定到具体文件路径)</li> <li><b>list</b>:列表元素需为上述类型数据,如<code>[numpy.ndarray, numpy.ndarray]</code>,<code>["/root/data/img1.jpg", "/root/data/img2.jpg"]</code>,<code>["/root/data1", "/root/data2"]</code></li> </ul> </td> <td><code>Python Var|str|list</code></td> <td></td> </tr> <tr> <td><code>batch_size</code></td> <td><b>含义:</b>批大小。<b>说明:</b> 可设置为任意正整数。</td>
<td><code>int</code></td> <td>1</td> </tr> </table>如果以上模型在您的场景上效果仍然不理想,您可以尝试以下步骤进行二次开发,此处以训练 SLANet_plus 举例,其他模型替换对应配置文件即可。首先,您需要准备表格结构识别的数据集,可以参考表格结构识别 Demo 数据的格式准备,准备好后,即可按照以下步骤进行模型训练和导出,导出后,可以将模型快速集成到上述 API 中。此处以表格结构识别 Demo 数据示例。在训练模型之前,请确保已经按照安装文档安装了 PaddleOCR 所需要的依赖。
# 下载示例数据集
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/table_rec_dataset_examples.tar
tar -xf table_rec_dataset_examples.tar
# 下载 SLANet_plus 预训练模型
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/SLANet_plus_pretrained.pdparams
PaddleOCR 对代码进行了模块化,训练 SLANet_plus 识别模型时需要使用 SLANet_plus 的配置文件。
训练命令如下:
#单卡训练 (默认训练方式)
python3 tools/train.py -c configs/table/SLANet_plus.yml \
-o Global.pretrained_model=./SLANet_plus_pretrained.pdparams
Train.dataset.data_dir=./table_rec_dataset_examples \
Train.dataset.label_file_list='[./table_rec_dataset_examples/train.txt]' \
Eval.dataset.data_dir=./table_rec_dataset_examples \
Eval.dataset.label_file_list='[./table_rec_dataset_examples/val.txt]'
#多卡训练,通过--gpus参数指定卡号
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py \
-c configs/table/SLANet_plus.yml \
-o Global.pretrained_model=./SLANet_plus_pretrained.pdparams
-o Global.pretrained_model=./PP-OCRv5_server_det_pretrained.pdparams \
Train.dataset.data_dir=./table_rec_dataset_examples \
Train.dataset.label_file_list='[./table_rec_dataset_examples/train.txt]' \
Eval.dataset.data_dir=./table_rec_dataset_examples \
Eval.dataset.label_file_list='[./table_rec_dataset_examples/val.txt]'
您可以评估已经训练好的权重,如,output/xxx/xxx.pdparams,使用如下命令进行评估:
# 注意将pretrained_model的路径设置为本地路径。若使用自行训练保存的模型,请注意修改路径和文件名为{path/to/weights}/{model_name}。
# demo 测试集评估
python3 tools/eval.py -c configs/table/SLANet_plus.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams
Eval.dataset.data_dir=./table_rec_dataset_examples \
Eval.dataset.label_file_list='[./table_rec_dataset_examples/val.txt]'
python3 tools/export_model.py -c configs/table/SLANet_plus.yml -o \
Global.pretrained_model=output/xxx/xxx.pdparams \
Global.save_inference_dir="./SLANet_plus_infer/"
导出模型后,静态图模型会存放于当前目录的./SLANet_plus_infer/中,在该目录下,您将看到如下文件:
./SLANet_plus_infer/
├── inference.json
├── inference.pdiparams
├── inference.yml
至此,二次开发完成,该静态图模型可以直接集成到 PaddleOCR 的 API 中。
训练后的模型如果想使用 paddle_dynamic 或 transformers 引擎,请参考后文 推理引擎 中的 权重转换 部分将模型由 pdparams 格式通过 PaddleX 转换为 safetensors 格式。
关于推理引擎的详细说明、取值、兼容性规则与示例请参见 <a href="../inference_engine.md">推理引擎与配置说明</a>。
<strong>测试环境说明:</strong>
<ul> <li><strong>测试数据:</strong>[示例图片](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/table_recognition.jpg)</li> <li><strong>硬件配置:</strong> <ul> <li>GPU:NVIDIA A100 40G</li> <li>CPU:Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz</li> </ul> </li> <li><strong>软件环境:</strong> <ul> <li>Ubuntu 22.04 / CUDA 12.6 / cuDNN 9.5</li> <li>paddlepaddle-gpu 3.2.1 / paddleocr 3.5 / transformers 5.4.0 / torch 2.10</li> </ul> </li> </ul>使用推理引擎时,系统会自动下载官方预训练模型。若需使用自训练模型配合 paddle_dynamic 或 transformers 引擎,请参考 PaddleX 表格结构识别模块权重转换 部分,将 pdparams 格式通过 PaddleX 转换为 safetensors 格式,即可无缝集成到 PaddleOCR 的 API 中进行推理。