docs/version3.x/module_usage/table_cells_detection.md
表格单元格检测模块是表格识别任务的关键组成部分,负责在表格图像中定位和标记每个单元格区域,该模块的性能直接影响到整个表格识别过程的准确性和效率。表格单元格检测模块通常会输出各个单元格区域的边界框(Bounding Boxes),这些边界框将作为输入传递给表格识别相关产线进行后续处理。
<table> <tr> <th>模型</th><th>模型下载链接</th> <th>mAP(%)</th> <th>GPU推理耗时(ms) [常规模式 / 高性能模式]</th> <th>CPU推理耗时(ms) [常规模式 / 高性能模式]</th> <th>模型存储大小(MB)</th> <th>介绍</th> </tr> <tr> <td>RT-DETR-L_wired_table_cell_det</td> <td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/RT-DETR-L_wired_table_cell_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/RT-DETR-L_wired_table_cell_det_pretrained.pdparams">训练模型</a></td> <td rowspan="2">82.7</td> <td rowspan="2">33.47 / 27.02</td> <td rowspan="2">402.55 / 256.56</td> <td rowspan="2">124</td> <td rowspan="2">RT-DETR 是一个实时的端到端目标检测模型。百度飞桨视觉团队基于 RT-DETR-L 作为基础模型,在自建表格单元格检测数据集上完成预训练,实现了对有线表格、无线表格均有较好性能的表格单元格检测。 </td> </tr> <tr> <td>RT-DETR-L_wireless_table_cell_det</td> <td><a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/RT-DETR-L_wireless_table_cell_det_infer.tar">推理模型</a>/<a href="https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/RT-DETR-L_wireless_table_cell_det_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_cells_detection -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_cells_detection -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 TableCellsDetection
model = TableCellsDetection(model_name="RT-DETR-L_wired_table_cell_det")
output = model.predict("table_recognition.jpg", threshold=0.3, batch_size=1)
for res in output:
res.print(json_format=False)
res.save_to_img("./output/")
res.save_to_json("./output/res.json")
上述示例默认使用 <code>paddle_static</code> 推理引擎,请先按照飞桨框架安装完成 PaddlePaddle 安装。
如果选择 transformers 作为推理引擎,请确保已配置 Transformers 环境,然后执行如下代码:
from paddleocr import TableCellsDetection
model = TableCellsDetection(
model_name="RT-DETR-L_wired_table_cell_det",
engine="transformers",
)
output = model.predict("table_recognition.jpg", threshold=0.3, batch_size=1)
for res in output:
res.print(json_format=False)
res.save_to_img("./output/")
res.save_to_json("./output/res.json")
在大多数场景下,默认的 paddle_static 推理引擎通常具备更好的推理性能,建议优先使用。
训练后的模型如果想使用 paddle_dynamic 或 transformers 引擎,请参考后文 推理引擎 中的 权重转换 部分将模型由 pdparams 格式通过 PaddleX 转换为 safetensors 格式。
运行后,得到的结果为:
{'res': {'input_path': 'table_recognition.jpg', 'page_index': None, 'boxes': [{'cls_id': 0, 'label': 'cell', 'score': 0.9698355197906494, 'coordinate': [2.3011515, 0, 546.29926, 30.530712]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9690820574760437, 'coordinate': [212.37508, 64.62493, 403.58868, 95.61413]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9668057560920715, 'coordinate': [212.46791, 30.311079, 403.7182, 64.62613]}, {'cls_id': 0, 'label': 'cell', 'score': 0.966505229473114, 'coordinate': [403.56082, 64.62544, 546.83215, 95.66117]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9662341475486755, 'coordinate': [109.48873, 64.66485, 212.5177, 95.631294]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9654079079627991, 'coordinate': [212.39197, 95.63037, 403.60852, 126.78792]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9653300642967224, 'coordinate': [2.2320926, 64.62229, 109.600494, 95.59732]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9639787673950195, 'coordinate': [403.5752, 30.562355, 546.98975, 64.61531]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9636150002479553, 'coordinate': [2.1537683, 30.410172, 109.568306, 64.62762]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9631900191307068, 'coordinate': [2.0534437, 95.57448, 109.57601, 126.71458]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9631181359291077, 'coordinate': [403.65976, 95.68139, 546.84766, 126.713394]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9614537358283997, 'coordinate': [109.56504, 30.391184, 212.65425, 64.6444]}, {'cls_id': 0, 'label': 'cell', 'score': 0.9607433080673218, 'coordinate': [109.525795, 95.62622, 212.44917, 126.8258]}]}}
参数含义如下:
<ul> <li><code>input_path</code>:输入的待预测图像的路径</li> <li><code>page_index</code>:如果输入是PDF文件,则表示当前是PDF的第几页,否则为 <code>None</code></li> <li><code>boxes</code>:预测的目标框信息,一个字典列表。每个字典代表一个检出的目标,包含以下信息: <ol start="1" type="1"> <li><code>cls_id</code>:类别ID,一个整数</li> <li><code>label</code>:类别标签,一个字符串</li> <li><code>score</code>:目标框置信度,一个浮点数</li> <li><code>coordinate</code>:目标框坐标,一个浮点数列表,格式为<code>[xmin, ymin, xmax, ymax]</code></li> </ol> </li> </ul> 可视化图像如下:相关方法、参数等说明如下:
<b>说明:</b> 如果设置为<code>None</code>,则使用<code>RT-DETR-L_wired_table_cell_det</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> <tr> <td><code>img_size</code></td> <td><b>含义:</b>输入图像大小。<b>说明:</b>
<ul> <li><b>int</b>:如<code>640</code>,表示将输入图像resize到640x640大小。</li> <li><b>list</b>:如<code>[640, 512]</code>,表示将输入图像resize到宽为640,高为512大小。</li> </ul> </td> <td><code>int|list|None</code></td> <td>None</td> </tr> <tr> <td><code>threshold</code></td> <td><b>含义:</b>用于过滤掉低置信度预测结果的阈值。<b>说明:</b>
<ul> <li><b>float</b>:如 <code>0.2</code>,表示过滤掉所有阈值小于0.2的目标框。/li> <li><b>dict</b>:字典的键为 <code>int</code> 类型,代表类别ID;值为 <code>float</code> 类型阈值。如 <code>{0: 0.45, 2: 0.48, 7: 0.4}</code>,表示对类别ID为0的类别应用阈值0.45、类别ID为1的类别应用阈值0.48、类别ID为7的类别应用阈值0.4。</li> <li><b>None</b>:使用模型默认配置。 </ul> </td> <td><code>float|dict|None</code></td> <td><code>None</code></td> </tr> </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> <tr> <td><code>threshold</code></td> <td><b>含义:</b>参数含义与实例化参数基本相同。<b>说明:</b> 设置为<code>None</code>表示使用实例化参数,否则该参数优先级更高。</td>
<td><code>float|dict|None</code></td> <td>None</td> </tr> </table>由于 PaddleOCR 并不直接提供表格单元格检测模块的训练,因此,如果需要训练表格单元格检测模型,可以参考 PaddleX 表格单元格检测模块二次开发部分进行训练。训练后的模型可以无缝集成到 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 中进行推理。