Y
YYPGD/table-transformer-detection
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

table-transformer-detection 昇腾NPU部署文档

1. 模型简介

模型名称: table-transformer-detection(microsoft/table-transformer-detection) 模型链接: https://ai.gitcode.com/hf_mirrors/microsoft/table-transformer-detection 模型描述: 微软提出的 Table Transformer(TATR)系列表格检测模型,基于 DETR 检测范式在 PubTables1M 数据集上训练,用于检测整页文档图像中的表格区域。模型输出两类目标:table 与 table rotated(旋转表格),是文档智能/表格识别流水线(版面分析 → 表格结构识别 Table Transformer Recognition)的第一环。 模型架构: DETR 风格的 Detection Transformer——ResNet-18 骨干 + 6 层 Transformer 编码器/解码器(d_model=256, 8 头注意力),目标查询 100 个;输入最长边缩放至 800(ImageNet 均值方差归一化),输出边界框与类别置信度。 参数规模: 约 29M(model.safetensors 约 115MB)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1推理引擎(共享环境提供,NPU 场景配套 torch_npu)
transformers>= 4.40TableTransformerForObjectDetection / AutoImageProcessor
timm>= 1.0骨干网络组件
numpy / Pillow最新版图像预处理与数组处理
matplotlib最新版结果可视化
昇腾驱动CANN 8.0.RC2+推荐最新版

安装命令:

pip install --target /mnt/workspace/table-transformer-detection/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers>=4.40" "timm>=1.0" safetensors numpy pillow matplotlib

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
# 权重已就位(transformers 格式)
ls /home/developer/models/table-transformer-detection/   # config.json model.safetensors preprocessor_config.json ...

3.2 运行推理

PYTHONPATH=/mnt/workspace/table-transformer-detection/libs python3 inference.py --model_path /home/developer/models/table-transformer-detection

脚本内置程序化绘制的含两个表格区域的文档页示例图,只传 --model_path 即可跑通;检测结果可视化自动保存到 assets/model_result.png。

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/table-transformer-detection本地权重目录(离线加载)
--devicestr自动(npu > cuda > cpu)推理设备
--thresholdfloat0.5检测结果置信度阈值
--outputstrassets/model_result.png结果可视化保存路径

4. 测试样例及输出结果

样例 1:内置示例图表格检测

输入:

python3 inference.py --model_path /home/developer/models/table-transformer-detection

输出:

[INFO] device = npu
[INFO] 模型加载完成 load_time=x.xxs (table-transformer-detection, labels=['table', 'table rotated'])
[RESULT] 输入张量 shape=(1, 3, 800, 1066) dtype=torch.float32 -> 检出 N 个表格区域
[RESULT] det#0: table score=0.xxxx box=[...]
...
[INFO] 推理完成 耗时/elapsed=x.xx s (input=800x1040, batch=1, device=npu)
[RESULT] status=ok device=npu 耗时/elapsed=x.xx

样例 2:指定设备运行

输入:

python3 inference.py --model_path /home/developer/models/table-transformer-detection --device cpu

输出:

每行输出 [RESULT] 各检出表格区域的类别、置信度与边界框,末行含 耗时/elapsed 字段,status=ok

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果

6. 注意事项

  • 权重为 transformers 格式,通过 TableTransformerForObjectDetection.from_pretrained(local_files_only=True) 加载,设备自动选择 npu > cuda > cpu;
  • 全部权重仅从本地 --model_path 加载,推理全程离线,不联网下载任何文件;
  • 输入张量统一 float32、shape (1, 3, H, W)(预处理由 AutoImageProcessor 完成:短边缩放至 max_size=800 保持长宽比、ImageNet 归一化 mean=[0.485,0.456,0.406]、std=[0.229,0.224,0.225]);NPU 不支持 float64 算子,勿改动 dtype;
  • 注意力实现使用 eager(已显式指定 attn_implementation="eager"),勿用 flash_attention_2;
  • 后处理使用模型自带 post_process_object_detection(阈值默认 0.5),标签 id2label={0:'table', 1:'table rotated'} 取自 config.json;
  • 如有精度差异请与官方 pipeline(HuggingFace microsoft/table-transformer-detection 表格检测示例)输出对比确认。