liucu/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
  • 论文/项目: Manga OCR — Vision Encoder Decoder for Japanese manga OCR, robust to vertical/horizontal text, furigana, diverse fonts
  • 架构: VisionEncoderDecoderModel
    • Encoder: ViT facebook/deit-base-patch16-224 — hidden_size 768, 12 layers, 12 heads, patch_size 16, image_size 224, intermediate 3072
    • Decoder: BertForMaskedLM cl-tohoku/bert-base-japanese-char-v2 — hidden_size 768, 2 layers, 12 heads, vocab_size 6144 (character-level Japanese, vocab.txt), max_length 300, decoder_start_token_id 2 ([CLS])
    • 总参数: ~111M (444MB pytorch_model.bin, FP32)
    • 输入: 224x224x3 RGB, ViTFeatureExtractor normalize mean 0.5 std 0.5, rescale 1/255
    • 输出: 生成序列 max_length 30 (可调), greedy (num_beams=1), 解码为日文字符拼接
  • 适配路线: vision-encoder / OCR image-to-text — 非分类模型,使用生成文本一致性验收(CPU vs NPU token exact match)
  • 加载后端: transformers VisionEncoderDecoderModel + ViTFeatureExtractor, 本地权重加载 (snapshot_download 缓存 /opt/atomgit/.cache/huggingface/hub/models--kha-white--manga-ocr-base), trust_remote_code=False, 手动 vocab.txt 字符解码避免 fugashi/unidic_lite 依赖
  • Task: 计算机视觉 — manga OCR / Japanese printed text recognition
  • 权重大小: 444,135,475 bytes (0.414 GiB) pytorch_model.bin + processor/tokenizer 配置

2. 验证环境

项值
NPUAscend910_9362 (npu-smi 25.5.5, CANN 8.5.1)
设备npu:0 (PhyID 6, Bus 0000:0B:00.0), 64GB HBM, 2 devices
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
torch.npu.is_available()True
transformers4.40.0
huggingface_hub0.36.2
Pillow11.x
Python3.11.14
验证日期2026-08-21 UTC
输入合成确定性图像 224x224x3 白底+黑色2px边框, 零随机, ViT 224
dtypefloat32 (FP32) — Base 模型 FP32 原生, 未做 FP16/BF16 切换
batch1

环境检查命令:

npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))"
python scripts/check_npu.py --output env_check.json

3. 安装依赖

pip install -r requirements.txt
# requirements.txt
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.40.0
# huggingface_hub==0.34.0
# Pillow>=10.0.0
# numpy>=1.24.0

系统已配置 HF_ENDPOINT=https://hf-mirror.com 与代理,无需手动设置。

模型下载(首次):

python -c "from huggingface_hub import snapshot_download; print(snapshot_download(repo_id='kha-white/manga-ocr-base'))"
# 缓存至 ~/.cache/huggingface/hub/models--kha-white--manga-ocr-base

4. NPU 推理

默认命令(npu:0 真实推理,3次 warmup + 10次计时,含 CPU-NPU 一致性检查):

python inference.py

其他模式:

python inference.py --mode validate   # 显式 CPU-NPU 一致性验证
python inference.py --mode benchmark  # 10次稳定计时 + 吞吐
python inference.py --device cpu      # 纯 CPU 对比
python inference.py --model-dir /path/to/local/snapshot

inference.py 默认打印: 模型名/任务/后端/device/dtype/输入摘要/真实生成文本与 token ids/分位延迟/吞吐/显存/最终 PASS/FAIL。

5. 真实推理结果

NPU npu:0 FP32, 合成图 224x224, max_length=30, num_beams=1, decoder_start_token_id=2:

Mode: default, Device: npu:0, Model dir: /opt/atomgit/.cache/huggingface/hub/models--kha-white--manga-ocr-base/snapshots/aa6573bd10b0d446cbf622e29c3e084914df9741
Model: kha-white/manga-ocr-base
Model weight URL: https://huggingface.co/kha-white/manga-ocr-base
Task: 图像 OCR (VisionEncoderDecoder image-to-text)
Architecture: VisionEncoderDecoderModel (ViT encoder + BERT Japanese char decoder, 111M)
Loading backend: transformers VisionEncoderDecoderModel + ViTFeatureExtractor, trust_remote_code=False
Weight: .../pytorch_model.bin (444MB, FP32)
Requested device: npu:0, dtype: float32
torch.npu.is_available(): True, device_name: Ascend910_9362, device_count: 2
Vocab: 6144 tokens (vocab.txt), sample: [CLS]=2 [SEP]=3 こ=869
Processor: ViTFeatureExtractor size=224, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]
Loading model from ... (0.34s, 111.01M)
Encoder: vit hidden=768 layers=12
Decoder: bert hidden=768 layers=2 vocab=6144
Decoder start token id: 2
Model moved to npu:0, first param device: npu:0, dtype: torch.float32
Input: synthetic deterministic image 224x224x3, white+black pattern, seed deterministic
Preprocessed pixel_values shape: (1, 3, 224, 224), dtype: torch.float32, range: [-1.000, 1.000]
Input device after .to(npu:0): npu:0
Warmup 3x ...
Running NPU inference (torch.npu.synchronize around generate) ...
  Iter 1: ids=[2, 2, 879, 926, 897, 828, 3] ... tokens=['[CLS]', '[CLS]', 'そ', 'れ', 'は', '、', '[SEP]'] ... text='それは、' latency=32.6ms
Latency (n=10) avg=31.9ms min=31.6ms max=32.6ms p50=31.8ms p90=32.6ms p95=32.6ms
Throughput: 31.30 images/s (batch=1)
NPU memory free=64.80GB total=65.79GB used=0.98GB
CPU-NPU consistency validation (greedy, max_length=30, same seed image) ...
  CPU ids: [2, 2, 879, 926, 897, 828, 3]
  NPU ids: [2, 2, 879, 926, 897, 828, 3]
  Exact token id match: True
  CPU text: 'それは、'
  NPU text: 'それは、'
  Text match: True
[PASS] CPU-NPU consistency (exact match)
Model param device: npu:0
FINAL: PASS - NPU inference successful, CPU-NPU consistent, latency reported
  • 输入: 合成白底+黑边 224x224, 经 ViTFeatureExtractor 后 pixel_values shape (1,3,224,224) float32 [-1,1]
  • NPU 生成: ids=[2,2,879,926,897,828,3] 对应 ['[CLS]','[CLS]','そ','れ','は','、','[SEP]'] 文本 それは、 (soreha, 常见日文开头)
  • 验证策略: 固定样例 smoke consistency — 单张合成图的 CPU vs NPU greedy 生成 exact match(token ids 完全一致,文本一致),非完整数据集评测。已通过 stricter exact match,比近似阈值更可靠。

6. CPU-NPU 一致性验证

检查结果
CPU ids[2, 2, 879, 926, 897, 828, 3]
NPU ids[2, 2, 879, 926, 897, 828, 3]
Exact matchTrue
CPU textそれは、
NPU textそれは、
Text matchTrue
模型参数 devicenpu:0
dtypefloat32
判定[PASS] CPU-NPU consistency

测试方法: 同步合成图, max_length=30, num_beams=1, greedy, 分别在 cpu 与 npu:0 上 model.generate,比较 torch.equal 与字符串相等。NPU 计时均包裹 torch.npu.synchronize()。

7. 性能测试

All timings with torch.npu.synchronize() before/after model.generate, warmup 3, measured 10:

指标值
batch1, image 224x224x3, FP32, npu:0
avg31.9 ms
min31.6 ms
max32.6 ms
p5031.8 ms
p9032.6 ms
p9532.6 ms
throughput31.30 images/s
单次生成 (含解码)~32 ms (端到端 VisionEncoderDecoder.generate)
模型加载0.34s
权重444MB
峰值 NPU 显存used 0.98GB / 64.80GB free / 65.79GB total
Warmup3, 测量 10

--mode benchmark 复用同一计时策略。大模型可降低次数并在 README 说明,本模型 10 次已稳定。

8. 自验证截图

三张终端证据图由本次真实日志通过 scripts/render_xterm_evidence.mjs --style raw 生成:

  • 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

Agent workflow NPU device call Model result

9. 已知限制

  • 当前验证为 smoke consistency (单张合成图 CPU vs NPU exact match),非 Manga109 全量精度评测
  • 输入为合成白底图,若使用真实漫画图像或复杂场景,生成文本与延迟可能不同,但 NPU 链路已验证
  • 使用 FP32, 未启用 FP16/BF16 优化;Ascend910 上可尝试 FP16 降延迟但需重新验证一致性
  • 解码为炭级 vocab.txt 直接映射,未使用 BertJapaneseTokenizer + fugashi/unidic_lite (体积 250MB 且需 MeCab),推理阶段无需编码日文输入,不影响 image-to-text 生成与解码
  • ViTFeatureExtractor 已 deprecated,未来可迁移至 ViTImageProcessor 但行为一致
  • Batch>1 / 多图流水未测试,当前仅 batch=1
  • 模型在 NPU 上为单卡 npu:0,分布式未验证

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, vision, ocr, manga-ocr, vision-encoder-decoder, vit, bert-japanese