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 推理。
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。/255 -> torch tensor [1,3,H,W] -> interpolate(712,488) -> backbone -> head -> grid_sample -> 输出 [1,3,H,W] 矫正图像。torch.npu.is_available()==True, device_name=Ascend910_9362pip 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))"默认命令(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。
在 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 终端截图。
相同随机初始化权重、同输入在 CPU 与 NPU:0 上对比(已 torch.npu.synchronize() 同步):
判定为通过。说明 PyTorch 移植在 NPU 上的 Conv2d/BatchNorm2d/PReLU/Interpolate/GridSample 与 CPU 数值一致。单样本一致性验证,非全数据集评测。
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不同分辨率下线性增长;真实文档 1024x1024 约 30 ms。
assets/agent_workflow.png:下载、侦察、加载、NPU 推理、验证、性能全流程日志assets/npu_device_call.png:npu-smi info、torch.npu.is_available()、设备名、首个参数 npu:0 及 dtypeassets/model_result.png:python inference.py 完整输出含任务结果与 PASS图片由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染,含命令、退出码、UTC 时间与日志 SHA-256。
paddle.load + UVDOC_MAPPING 转换方可达到 CER 0.179;已提供完整架构,替换权重即可获得精度。DocTrPostProcess 后处理阈值。npu:0,未测试多卡与 BF16。Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, document-unwarping, image-to-image, UVDoc, PaddleOCR