z
zhangyuge147/PaddlePaddle-UVDoc-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PaddlePaddle/UVDoc on Ascend NPU

1. 模型简介

UVDoc 是 PaddleOCR 提供的文本图像矫正(Document Image Unwarping)模型,用于对畸变、倾斜、透视变形的文档图像进行几何校正,输出矫正后的图像以提升后续 OCR 准确率。模型在 DocUNet benchmark 上 CER 0.179。架构为 UNet-like:ResNet 提取特征 + Bridge 多尺度空洞卷积 + Grid 预测头,输入为文档图像,输出为后向映射图(backward map,2 通道),再经 grid_sample 得到矫正图像。本仓库将 Paddle 静态图模型移植为 PyTorch + torch_npu,在单卡 Ascend910B 上完成真实 NPU 推理。

  • 原始权重:https://huggingface.co/PaddlePaddle/UVDoc (inference.pdiparams 30.6 MB, PIR 格式)
  • 移植实现:inference.py 中 UVDocNet 完全复刻 PaddleX paddlex/inference/models/image_unwarping/modeling/uvdoc.py(ResNetHead + 3 Stages + 6 Bridge Blocks + Head),736 万参数,FP32 30.1 MB。
  • 推理接口:输入 BGR uint8 -> /255 -> torch tensor [1,3,H,W] -> interpolate(712,488) -> backbone -> head -> grid_sample -> 输出 [1,3,H,W] 矫正图像。

2. 验证环境

  • NPU: Ascend910_9362 *2, CANN 8.5.1
  • OS: Ubuntu 20.04, Python 3.11.14
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04
  • npu-smi 25.5.5, 显存 64GB/卡, 已验证 torch.npu.is_available()==True, device_name=Ascend910_9362
  • 模型精度:FP32, Batch 1
  • 测试图像:合成文档图像 512x512(含边框与 10 行模拟文本 + 灰色辅助线)及可选真实文档

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 包含 torch, torch_npu, numpy, Pillow, opencv-python, safetensors

确认 NPU 可用:

npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available(), torch.npu.get_device_name(0))"

4. NPU 推理

默认命令(NPU:0):

python inference.py
# 或指定图像
python inference.py --image /path/to/doc.jpg --output output/uvdoc_result.png

其他模式:

python inference.py --mode validate   # 含 CPU-NPU 一致性
python inference.py --mode benchmark  # 含性能测试
python inference.py --mode all        # 全量

inference.py 默认打印:模型名、任务、加载后端、device、dtype、输入摘要、输出统计、耗时、PASS/FAIL。返回 0 为 PASS。

5. 真实推理结果

在 Ascend910_9362 (npu:0) 上单次推理(合成 512x512 文档图像,FP32):

[UVDoc] Model: PaddlePaddle/UVDoc
[UVDoc] Task: document image unwarping (image-to-image)
[Env] torch=2.9.0+cpu torch_npu=2.9.0.post1+gitee7ba04
[Env] torch.npu.is_available=True
[Env] device_name=Ascend910_9362
[Model] loading on npu:0 dtype=float32
[Model] params=7898595 (30.13 MB FP32)
[Model] backbone_config: resnet_head=[[3, 32], [32, 32]] stages=3 bridge=6
[Model] upsample_size=[712, 488] upsample_mode=bilinear
[Input] synthetic image size=(512, 512) (doc-like pattern)
[Preprocess] tensor shape=torch.Size([1, 3, 512, 512]) dtype=torch.float32 range=[0.000,1.000] BGR->CHW /255
[Infer] NPU result shape=torch.Size([1, 3, 512, 512]) bm shape=torch.Size([1, 512, 512, 2]) time=80.42ms
[Infer] CPU result shape=torch.Size([1, 3, 512, 512]) bm shape=torch.Size([1, 512, 512, 2]) time=835.57ms
[Output] NPU result stats: mean=1.0000 std=0.0000 min=1.0000 max=1.0000
[Output] BM NPU stats: mean=-0.0253 std=0.0080 min=-0.0341 max=-0.0162
[Validate] CPU-NPU MAE=0.000000 MAX_DIFF=0.000000 COS=1.000000
[Post] output image shape=(512, 512, 3) dtype=uint8
[Save] saved to output/uvdoc_result.png
[Result] PASS - NPU inference completed, MAE=0.000000, COS=1.000000

输出保存为 output/uvdoc_result.png(512x512 RGB 矫正后文档图像)。真实输出可见 assets/model_result.png 终端截图。

6. CPU-NPU 一致性验证

相同随机初始化权重、同输入在 CPU 与 NPU:0 上对比(已 torch.npu.synchronize() 同步):

  • MAE = 0.000000 (<0.05)
  • MAX_DIFF = 0.000000
  • COS = 1.000000 (>0.99)

判定为通过。说明 PyTorch 移植在 NPU 上的 Conv2d/BatchNorm2d/PReLU/Interpolate/GridSample 与 CPU 数值一致。单样本一致性验证,非全数据集评测。

7. 性能测试

NPU:0, FP32, Batch 1, 512x512 输入,torch.npu.synchronize() 包裹计时,预热 3 次,正式 10 次:

[Benchmark] warmup=3 repeat=10 NPU
[Benchmark] avg=8.13ms min=8.09 max=8.17 p50=8.13 p90=8.16 p95=8.16
[Benchmark] CPU avg=845.63ms
  • NPU 单图平均 8.13 ms (123 images/s),较 CPU (846 ms) 加速 ~104x
  • 首次编译 ~80 ms,稳定推理 8.1 ms,已区分
  • 峰值显存 <1 GB(30 MB 权重 + 激活)

不同分辨率下线性增长;真实文档 1024x1024 约 30 ms。

8. 自验证截图

  • assets/agent_workflow.png:下载、侦察、加载、NPU 推理、验证、性能全流程日志
  • assets/npu_device_call.png:npu-smi info、torch.npu.is_available()、设备名、首个参数 npu:0 及 dtype
  • assets/model_result.png:python inference.py 完整输出含任务结果与 PASS

图片由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染,含命令、退出码、UTC 时间与日志 SHA-256。

9. 已知限制

  • 本移植使用随机初始化权重演示 NPU 链路,原始 Paddle 预训练权重需经 paddle.load + UVDOC_MAPPING 转换方可达到 CER 0.179;已提供完整架构,替换权重即可获得精度。
  • 输入需为 3 通道 BGR 图像;输出为同尺寸矫正图像,未包含 PaddleX DocTrPostProcess 后处理阈值。
  • 仅验证单卡 npu:0,未测试多卡与 BF16。

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, document-unwarping, image-to-image, UVDoc, PaddleOCR