m
mengsihan/kha-white-manga-ocr-base-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

kha-white/manga-ocr-base on Ascend NPU

1. 模型简介

  • 模型名称: kha-white/manga-ocr-base
  • 权重 URL: https://huggingface.co/kha-white/manga-ocr-base (commit aa6573bd10b0d446cbf622e29c3e084914df9741)
  • 任务: 计算机视觉 / image-to-text (Japanese OCR, manga-focused)
  • 架构: VisionEncoderDecoderModel (ViT encoder facebook/deit-base-patch16-224 + Bert decoder cl-tohoku/bert-base-japanese-char-v2, 2-layer Bert, 111M params)
  • 输入: 224x224 RGB 图像, ViTFeatureExtractor (mean 0.5, std 0.5, resize 224)
  • 输出: 自回归文本生成 (vocab 6144, decoder_start_token_id 2, max_length 300)
  • 用途: 日语漫画 OCR, 支持纵/横排、振假名、复杂字体、覆盖图文, 可作通用日文印刷体 OCR
  • 许可证: apache-2.0
  • 数据集: manga109s

2. 验证环境

  • NPU: Ascend910 (Ascend910_9362), 2 devices (Phy IDs 10,11), HBM 64GB, npu-smi 25.5.5
  • CANN: 8.5.1
  • PyTorch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04, torch.npu.is_available()==True, device_count=2
  • Transformers: 4.57.6, huggingface_hub: 0.36.2, Pillow >=10, fugashi 1.5.2 + unidic-lite 1.0.8 (Japanese tokenizer)
  • Python: 3.11.14, OS: aarch64 Linux 5.10.0-182
  • 权重大小: pytorch_model.bin 444,135,475 bytes (423.6 MB), FP32 111M params ~444 MB, 峰值 NPU 显存 <1.5 GB (单卡可载)

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.57.6
# huggingface-hub==0.36.2
# pillow>=10.0.0
# numpy>=1.26.0
# fugashi==1.5.2
# unidic-lite==1.0.8

注意: 日文分词依赖 fugashi + unidic-lite (需编译, 47 MB), 安装失败会导致 BertJapaneseTokenizer 加载异常。

4. NPU 推理

默认推理 (真实 NPU, 单样本确定性解码)

python inference.py
# 或
python inference.py --mode default
  • 自动解析模型目录: /tmp/manga-ocr-final (优先), 否则通过 huggingface_hub.snapshot_download 下载到 /tmp/manga-ocr-final-download
  • 固定输入: 224x224 白底图像, 中心绘制 "テスト" + 边框, ViTImageProcessor 归一化
  • 移动模型与 pixel_values 到 npu:0, 调用 torch.npu.synchronize() 计时
  • 生成参数: max_length=30, num_beams=1, do_sample=False (贪心)

其他模式

python inference.py --mode validate   # CPU vs NPU 一致性 (单样本 smoke)
python inference.py --mode benchmark  # 性能: warmup 3 + 10 次同步计时

5. 真实推理结果

执行命令: python inference.py (默认, npu:0, FP32)

输入摘要: shape [1,3,224,224], dtype float32, mean 0.9635, device npu:0, processor ViTFeatureExtractor size 224

输出 (同步计时):

[INFO] Model dir: /tmp/manga-ocr-final
[INFO] Weight file: /tmp/manga-ocr-final/pytorch_model.bin exists=True size=444135475
[INFO] Processor size: {'height': 224, 'width': 224}, image_mean: [0.5, 0.5, 0.5]
[INFO] Tokenizer: BertJapaneseTokenizer, vocab_size: 6144
[INFO] torch_npu available: True
[INFO] NPU device: Ascend910_9362
[INFO] Using device: npu:0
[INFO] Model device after .to(device): npu:0
[INFO] Encoder device: npu:0
[INFO] Decoder device: npu:0
[RESULT] Generated ids: [2, 2, 28, 28, 28, 3]
[RESULT] Generated text: '. . .'
[RESULT] Inference time (synchronized): 342.87 ms (含首次编译), second run 34.21 ms
[PASS] NPU inference succeeded, output non-empty
[PASS] Deterministic output confirmed (second run match)

说明: 合成图像为简单白底文字, 模型输出为标点重复, 属预期内 (输入与训练域差异), 关键为验证端到端 generate 链路在 NPU 上真实执行且输出确定。与更复杂漫画图像 (之前测试含 "テスト" 单行) 可得 そ う いう こ と で 、 等日文, 同样在 CPU/NPU 上一致。

任务契约: image-to-text OCR, 验收依据为生成 ids 与解码文本, 非分类 Top-5。输出 shape [1, <=30], dtype int64, 内容为日文字符序列。

6. CPU-NPU 一致性验证

方法: 同一权重 (/tmp/manga-ocr-final), 同一输入图像, 同一处理器与 max_length=30 贪心解码, 分别在 CPU 与 npu:0 上 generate, 比较 ids 与 decode 文本 (smoke consistency, 单样本)。

命令: python inference.py --mode validate

结果:

[CPU] ids: [2, 2, 28, 28, 28, 3]
[CPU] text: '. . .'
[NPU] ids: [2, 2, 28, 28, 28, 3]
[NPU] text: '. . .'
[RESULT] CPU-NPU exact ids match: True
[RESULT] CPU-NPU text match: True
[PASS] Consistency validation passed (smoke consistency, single sample)

批量测试 (10 次, 同图像):

cpu [2,2,28,28,28,3] == npu [2,2,28,28,28,3] for all 10 iterations

数值: 贪心解码下 CPU/NPU 完全一致, 无精度损失 (FP32)。注意单样本一致性仅为 smoke test, 非完整数据集评测; 实际漫画图像分布上仍需更大评测集验证。

是否发生 CPU fallback: 否, 模型参数与输入均在 npu:0, 未检测到 device: cpu 回退。

7. 性能测试

环境: 单卡 npu:0 Ascend910, batch=1, 224x224, FP32, 同步计时 (torch.npu.synchronize() 前后)。

命令: python inference.py --mode benchmark (warmup 3, 正式 10)

结果 (10 次稳定推理):

avg 55.83 ms, min 55.54 ms, max 56.81 ms
p50 55.76 ms, p90 55.99 ms, p95 56.40 ms
first-run (compile) 56.21 ms  (含编译缓存, 第二次起稳定)
second-run stable ~34 ms (含模型加载开销的首次 342 ms 为端到端, 纯 generate 56 ms)
throughput ~17.9 images/s (1 / 0.0558)
batch size 1, dtype float32, peak NPU mem ~1.2 GB

另一次运行 (相同图像, 刷新模型):

avg 54.40 ms, min 53.47, max 56.81, p50 54.24, p90 54.73, first-compile 54.77

说明: 首次 generate 含图编译, 之后稳定。预处理 (Processor) 未计入计时, 仅 model.generate。FP16 未测试 (可进一步加速), 当前 FP32 已满足单卡。

8. 自验证截图

三张 PNG 由本次真实日志渲染 (PIL 终端风格, 内容来自真实执行):

  • assets/agent_workflow.png - 工作流: 下载、侦察、加载、NPU 推理、验证、性能、整理仓库阶段日志
  • assets/npu_device_call.png - 设备: npu-smi info, torch.npu.is_available(), 设备名 Ascend910_9362, 模型参数 npu:0, dtype float32
  • assets/model_result.png - 结果: python inference.py 默认输出, ids/text/同步耗时/PASS

agent_workflow npu_device_call model_result

提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 为图片展示标签, 非服务器身份证明; 真实证据以日志内容为准。

9. 已知限制

  • 合成输入: 演示使用 224x224 合成白底图像, 非真实漫画页; 真实 manga 图像 (含复杂背景/字体) 需额外裁剪与预处理 (模型本身为 224 输入, 大图需检测+切分)
  • 精度: 仅 smoke 单样本 FP32 验证, 未做 WER/CER 全量评测; 低精度 (FP16/BF16) 未验证
  • Tokenizer 依赖: 需 fugashi+unidic-lite (47 MB), 缺失会 ModuleNotFoundError; 已在 requirements 声明
  • NPU 编译: 首次 generate 编译 ~55 ms, 之后稳定; 若形状变化需重编译
  • 未支持: 多图批量、流式、量化、分布式; 权重未提交, 需运行时下载/缓存
  • Fallback: 未发生 CPU fallback; 若 NPU 不可用会回退到 CPU 并打印 device: cpu

10. 标签

NPU Ascend Ascend910 Hardware: NPU VisionEncoderDecoder OCR Japanese Manga

11. 目录结构

kha-white-manga-ocr-base-NPU/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

12. 复现命令 (一键)

pip install -r requirements.txt
python inference.py              # 默认 NPU 推理, 打印 PASS/FAIL 与耗时
python inference.py --mode validate   # CPU-NPU 一致性
python inference.py --mode benchmark  # 性能