g
gcw_coj3XaOd/MidnightRunner_Misc
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

4x-UltraSharp ESRGAN - 昇腾 NPU 推理部署

1. 模型简介

模型名称: MidnightRunner/Misc (4x-UltraSharp variant)

模型链接: HuggingFace

模型描述: 4x-UltraSharp 是基于 ESRGAN 架构的超分辨率模型,使用 23 个 RRDB(残差密集残差块)进行 4 倍图像超分辨率重建。属于 MidnightRunner/Misc 工具箱中的 UltraSharp 变体。

模型架构: ESRGAN (Enhanced Super-Resolution Generative Adversarial Network)

  • 23× RRDB (Residual-in-Residual Dense Block)
  • 每个 RRDB 包含 3 个 RDB (Residual Dense Block)
  • 每个 RDB 包含 5 个卷积层(密集连接)
  • 上采样:最近邻插值 + 卷积(2次 2x = 4x)

参数规模: 16,697,987 (16.7M)

输入规格:

  • 类型:RGB 图像 (3 channels)
  • Shape:(1, 3, H, W),任意尺寸
  • 值域:[0, 1] float32

输出规格:

  • 类型:RGB 图像 (3 channels)
  • Shape:(1, 3, H4, W4)
  • 值域:[0, 1] float32

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
Pillow>= 9.0图像处理
numpy>= 1.20数组处理
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch_npu pillow numpy

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型权重

从 MidnightRunner/Misc 仓库下载 4x-UltraSharp.pth:

wget https://ai.gitcode.com/hf_mirrors/MidnightRunner/Misc/resolve/main/4x-UltraSharp.pth

3.3 运行推理

# 单张图片推理
python3 inference.py --model-path 4x-UltraSharp.pth --image input.png

# 批量推理
python3 inference.py --model-path 4x-UltraSharp.pth --input-dir ./inputs --output-dir ./outputs

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填4x-UltraSharp.pth 路径
--imagestrNone输入图片路径
--input-dirstrNone批量输入目录
--output-dirstr/tmp/esrgan_output输出目录
--deviceint0NPU 设备编号

4. 推理成功日志

4.1 单条推理日志

[设备] torch_npu=2.9.0.post1+gitee7ba04, device=npu:0
[模型] 加载 4x-UltraSharp ESRGAN 权重...
[模型] 加载完成 (312ms), 参数量: 16,697,987
[数据] 生成测试图像: /tmp/test_input.png (64x64)
[输入] 64x64
[输出] 256x256, 耗时=149.7ms
[文件] 保存至: /tmp/esrgan_output/result_4x.png
[状态] SUCCESS

5. 测试样例及输出结果

样例 1:64x64 测试图 4x 超分辨率

运行命令:

python3 inference.py --model-path 4x-UltraSharp.pth

输出:

[输入] 64x64 (测试图)
[输出] 256x256, 耗时=149.7ms
[状态] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 合成梯度测试图 (64x64)

评测指标:

指标结果
输入尺寸64×64
输出尺寸256×256 (4x)
推理耗时~150ms (npu:0)
参数量16,697,987

评测命令:

python3 inference.py --model-path 4x-UltraSharp.pth

8. NPU 配置说明

  • 设备: Ascend 910 (npu:0)
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.9.0.post1+gitee7ba04
  • 推理精度: FP32
  • 单次推理耗时: ~150ms (64x64 -> 256x256)

9. 已知问题

无