冬
gcw_IDzXRVNw/manga-ocr-base-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Manga OCR Base 昇腾 NPU 适配部署文档

1. 模型简介

Manga OCR 是用于识别日本漫画中文字符的 OCR(光学字符识别)模型,主要针对漫画场景优化。

本模型是 Base 版本,采用 Vision Encoder Decoder 框架。

模型架构

属性值
模型类型VisionEncoderDecoderModel
编码器DeiT-base (facebook/deit-base-patch16-224)
解码器BERT Japanese (cl-tohoku/bert-base-japanese-char-v2)
编码器隐藏层维度768
解码器隐藏层维度768
Transformer 层数编码器 12 / 解码器 2
Attention 头数编码器 12 / 解码器 12
参数量~440M
权重文件大小~444 MB

模型特点

  • Vision Encoder Decoder 框架: 结合视觉编码器和语言解码器
  • DeiT-base 编码器: 强大的视觉特征提取能力
  • BERT Japanese 解码器: 专门针对日文字符优化
  • 支持竖排和横排文本: 适应漫画中常见的排版方式
  • 支持 ruby(振り仮名): 可识别漫画中的注音假名
  • 支持复杂背景: 文本覆盖在图像上的场景

输入输出格式

  • 输入: RGB 图像 (224x224)
  • 输出: 日文文本字符串

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 2 GB NPU 显存(Manga OCR Base 推理峰值约 1-2 GB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.10
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
transformers4.40.0+VisionEncoderDecoder 模型加载
Pillow最新版图像处理
numpy最新版数值计算
fugashi最新版日文分词器( MecabTokenizer 依赖)
unidic-lite最新版日文词典( MecabTokenizer 依赖)

2.3 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 安装模型依赖(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple \
    transformers>=4.40.0 Pillow numpy fugashi unidic-lite

# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent1/manga-ocr-base-ascend

# 确认上游权重存在
ls -la /workspace/agent2/kha-white/manga-ocr-base/

# 期望输出:config.json pytorch_model.bin (~444MB) tokenizer ...

步骤 2:检查 NPU 环境

# 检查 NPU 设备状态
npu-smi info

# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"

步骤 3:运行推理脚本

# 基本用法:默认参数
python inference.py --device npu:0

# 查看 NPU 显存使用
python inference.py --device npu:0 --show_npu_memory

# 指定其他 NPU 卡
python inference.py --device npu:1

4. 完整测试用例

测试用例 1:标准图像 OCR 识别(NPU:0)

conda activate pt2100
cd /workspace/agent1/manga-ocr-base-ascend
python inference.py --device npu:0

实际输出:

[W822 08:26:23.746321550 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W822 08:26:33.078452110 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
============================================================
Manga OCR Base 昇腾 NPU 推理
============================================================

[Device] npu:0
[Model Path] /workspace/agent2/kha-white/manga-ocr-base

[Loading] Model and processors...
  Model loaded from pretrained
  Processor and tokenizer loaded

[Model Loaded] Allocated: 0.41 GB, Reserved: 0.47 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.0       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          3967 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1231608       | python                   | 598                     |
+===========================+===============+====================================================+


[Creating] Dummy manga-style image...

[Input Image] Size: (224, 224)

[Running Inference]...

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.4       42                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          3967 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1231608       | python                   | 598                     |
+===========================+===============+====================================================+


[Generating text]...

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.5       42                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4023 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1231608       | python                   | 654                     |
+===========================+===============+====================================================+


[Results]
  Generated text: そ れ で も 、
  Generated IDs shape: torch.Size([1, 8])
[Memory After Inference] Allocated: 0.41 GB, Reserved: 0.50 GB

[Done]

✅ 验证要点:

  • 模型加载成功: VisionEncoderDecoderModel + Processor + Tokenizer 全部加载
  • NPU 推理稳定: 模型完整运行在 NPU 上,generate 阶段 NPU 进程稳定
  • NPU 显存峰值: Allocated 0.41 GB, Reserved 0.50 GB(实际使用约 654 MB 进程内存)
  • 输出形状: Generated IDs (1, 8) - 生成的 token 序列
  • 进程监控: npu-smi 显示 Python 进程 (PID 1231608) 在 NPU 上运行
  • 日文生成: 使用随机图像输入,生成日文文本 "そ れ で も 、"

5. 常见问题解答

Q1:提示 "NPU 不可用" 怎么办?

解决方案:

  1. 确认已安装 CANN 驱动并设置环境变量(source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh)
  2. 运行 npu-smi info 检查设备状态
  3. 确认 PyTorch 和 torch_npu 版本匹配

Q2:生成文本乱码或无意义?

解释:Manga OCR 是日文 OCR 模型,使用随机图像输入时生成的是无意义日文字符。实际使用时需要输入包含日文的漫画图像。

Q3:如何处理实际漫画图像?

解决方案:

from PIL import Image
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, BertJapaneseTokenizer

# 加载模型和处理器
model = VisionEncoderDecoderModel.from_pretrained("你的模型路径")
processor = ViTImageProcessor.from_pretrained("你的模型路径")
tokenizer = BertJapaneseTokenizer.from_pretrained("你的模型路径")

# 读取漫画图像
image = Image.open("manga_page.png")

# OCR 识别
pixel_values = processor(images=image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values)
text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(text)

6. 文件结构

manga-ocr-base-ascend/
├── inference.py              # 推理脚本(torch_npu 推理)
├── README.md                 # 本文档
├── requirements.txt          # 依赖列表
└── assets/                   # 资源占位目录

上游权重目录(只读,未被修改):

/workspace/agent2/kha-white/manga-ocr-base/
├── config.json               # VisionEncoderDecoder 配置
├── pytorch_model.bin         # 权重(约 444MB)
├── preprocessor_config.json  # 图像预处理器配置
├── tokenizer_config.json     # 分词器配置
├── vocab.txt                 # 词汇表
└── README.md                 # 原始文档

7. requirements.txt - 依赖列表

# Manga OCR Base 昇腾 NPU 推理环境依赖

# 核心依赖(必须)
torch>=2.0.0
torch_npu
transformers>=4.40.0
Pillow
numpy
fugashi
unidic-lite

使用方式:

# 激活环境
conda activate pt2100

# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt

8. 参考资源

  • Manga OCR GitHub
  • HuggingFace 模型页
  • DeiT 论文
  • BERT Japanese 模型
  • 华为昇腾文档
  • torch_npu GitHub