manga-ocr-base 昇腾 NPU 适配
1. 简介
- 模型: kha-white/manga-ocr-base
- 任务类型: 日语漫画 OCR (图像到文本)
- 框架: PyTorch + transformers + torch_npu
- 参数量: 111.0M
- 架构: VisionEncoderDecoderModel (ViT 编码器 + BERT 解码器)
2. 验证环境
| 组件 | 版本 |
|---|
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| transformers | 4.57.6 |
| CANN | 8.5.1 |
| NPU 设备 | Ascend910_9362 |
3. 推理步骤
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export PATH="/usr/local/python3.11.14/bin:$PATH"
cd /opt/atomgit/submit/xujiashuai/manga-ocr-base
python inference.py
4. 测试样例及输出结果
输入: 224x224 白色背景 + 黑色条纹的合成测试图像
==============================================================
kha-white/manga-ocr-base — Ascend NPU Adaptation
==============================================================
Device: npu:0
Load time: 0.32s
[INFO] parameters=111.0M
[INFO] pixel_values=(1, 3, 224, 224)
[RESULT] cpu_latency=287.75ms runs=1
[RESULT] encoder_shape=(1, 197, 768)
[RESULT] encoder_cosine=1.000002 max_abs_diff=0.010915 mean_abs_diff=0.000849
[RESULT] decoder_logits_shape=(1, 4, 6144)
[RESULT] avg_latency=6.05ms runs=10
Model: manga-ocr-base | Category: CV/OCR
Architecture: VisionEncoderDecoderModel (ViT encoder + BERT decoder)
Device: npu:0
Load time: 0.32s | Encoder inference: 6.05ms
Cosine similarity (CPU vs NPU): 1.000002
Status: [SUCCESS]
5. 性能参考
| 指标 | CPU | NPU | 加速比 |
|---|
| 编码器推理延迟 | 287.75 ms | 6.05 ms | 47.56x |
| 模型加载 | - | 0.32 s | - |
6. 精度评测
- 编码器输出余弦相似度 (CPU vs NPU): 1.000002
- 最大绝对差异: 0.010915
- 平均绝对差异: 0.000849
- 输出形状: (1, 197, 768)
7. 注意事项
- 使用 HuggingFace
VisionEncoderDecoderModel.from_pretrained 加载本地权重
- ViTFeatureExtractor 已弃用,建议后续迁移到 ViTImageProcessor
- 编码器推理仅测试 ViT encoder 部分,解码器需配合 tokenizer 使用
- NPU 推理前需执行
source /usr/local/Ascend/ascend-toolkit/set_env.sh 初始化环境