ModelScope: iic/cv_gpen_image-portrait-enhancement-hires
任务: 人像高清增强 (Portrait Hi-Res Enhancement)
框架: PyTorch + torch_npu
适配硬件: 华为昇腾 Ascend 910
验证状态: ✅ 已通过 (PSNR 68.81 dB, SSIM 0.999917)
GPEN (GAN Prior Embedded Network) 将 GAN 先验嵌入到深度神经网络中,用于人像面部增强。本模型为高分辨率版本,输出 1024×1024 增强图像。
模型架构:
size=1024, style_dim=512, n_mlp=8, channel_multiplier=2, narrow=1, isconcat=Truechannel_multiplier=2, 输入 512×512| 依赖 | 版本 |
|---|---|
| Python | 3.10+ |
| PyTorch | 2.9.0+ |
| torch_npu | 2.9.0+ |
| CANN | 8.5.1+ |
| OpenCV | 4.x |
| NumPy | 1.x |
| scikit-image | 0.19+ (仅基准测试) |
硬件: 华为昇腾 910 / 910B (Atlas 800 A2/A3)
pip install modelscope
modelscope download --model iic/cv_gpen_image-portrait-enhancement-hires模型文件位于 model_files/ 目录下:
pytorch_model.pt — FullGenerator 权重net_d.pt — Discriminator 权重(推理不需要)python inference_simple.py --input photo.jpg --output enhanced.jpgpython inference_simple.py --input photo.jpg --output enhanced.jpg --device npupython inference.py --input photo.jpg --output enhanced.jpg --device npupython modelscope_pipeline.py --input photo.jpg --output enhanced.jpg --device npu自验证运行日志已归档至 RUN_LOG.md,包含完整终端输出和结构化 JSON 报告。
关键结论:
| 指标 | CPU | NPU (Ascend 910) | 比值 |
|---|---|---|---|
| 单帧延迟 | 15,520 ms | 66.7 ms | 233x 加速 |
| 吞吐量 | 0.06 fps | 15.00 fps | — |
| 完整管线 | — | 331.0 ms | — |
| PSNR | — | 68.81 dB | ✅ 优秀 |
| SSIM | — | 0.999917 | ✅ 接近完美 |
| 最大像素差 | — | 1 | ✅ 不可感知 |
复现命令:
cd gpen-portrait-enhancement && python run_verify.py
| 项目 | 值 |
|---|---|
| 硬件 | Atlas 800 A2 (Ascend910_9362 ×2) |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| CANN | 8.5.1 |
| 测试图片 | marilyn_monroe_4.jpg (412×550 → 1024×1024) |
| 测试日期 | 2026-05-20 |
| 设备 | 推理延迟 | 吞吐量 | 加速比 |
|---|---|---|---|
| CPU (aarch64) | 15,520 ms | 0.06 fps | 1x |
| Ascend 910 NPU | 66.7 ms | 15.00 fps | 233x |
| 完整管线 (含检测+融合) | 331.0 ms | 3.02 fps | — |
单帧推理: batch_size=1, 1024×1024 输入, warmup=1 + runs=3
| 指标 | 值 | 评价 |
|---|---|---|
| PSNR | 68.81 dB | ✅ 优秀 (>40 dB) |
| SSIM | 0.999917 | ✅ 接近完美 |
| 最大像素差 | 1 | ✅ 无感知差异 |
| 平均像素差 | 0.0086 | ✅ 极小 |
| 像素完全一致率 | 99.14% | ✅ |
结论: NPU 推理精度与 CPU 高度一致,PSNR 68.81 dB 远超 40 dB 精度标准,像素级差异不可感知。
由于 ModelScope 原生 Pipeline 的 gpen.py 缺少 isconcat 参数,导致加载官方权重时通道维度不匹配。
本项目提供了 NPU 兼容的 Pipeline 封装,API 与 ModelScope 完全一致:
from modelscope_pipeline import GPENPortraitEnhancementPipeline, OutputKeys
# 创建 Pipeline(指定 NPU 设备)
pipeline = GPENPortraitEnhancementPipeline('model_files/', device='npu')
# 单张图片增强
result = pipeline('input.jpg')
cv2.imwrite('output.jpg', result[OutputKeys.OUTPUT_IMG])
# 也可传入 numpy 数组
import cv2
img = cv2.imread('input.jpg')
result = pipeline(img)# NPU 推理
python modelscope_pipeline.py --input photo.jpg --output enhanced.jpg --device npu
# CPU 推理
python modelscope_pipeline.py --input photo.jpg --output enhanced.jpg --device cpu
# 禁用超分预处理
python modelscope_pipeline.py --input photo.jpg --output enhanced.jpg --device npu --no_sr| 项目 | ModelScope 原生 | 本项目 |
|---|---|---|
isconcat 参数 | ❌ 不支持 | ✅ 默认 True |
| NPU 设备 | ❌ 仅 cuda/cpu | ✅ cpu/cuda/npu |
| 权重加载 | strict=True 可能失败 | strict=True ✅ 完全匹配 |
| API 兼容性 | — | ✅ OutputKeys 兼容 |
import cv2
from gpen_model import FullGenerator
import torch
# 加载模型
model = FullGenerator(1024, 512, 8, channel_multiplier=2, narrow=1, isconcat=True)
ckpt = torch.load('model_files/pytorch_model.pt', map_location='cpu', weights_only=False)
model.load_state_dict(ckpt, strict=True)
model.to('npu:0').eval()
# 预处理
img = cv2.imread('input.jpg')
img_resized = cv2.resize(img, (1024, 1024))
tensor = (img_resized.astype(np.float32) / 255.0 - 0.5) / 0.5
tensor = torch.from_numpy(tensor).permute(2, 0, 1).unsqueeze(0).to('npu:0')
# 推理
with torch.no_grad():
output, _ = model(tensor, return_latents=False)
# 后处理
result = ((output.squeeze().cpu().permute(1, 2, 0).clamp(-1, 1) + 1) * 127.5).numpy().astype(np.uint8)
cv2.imwrite('enhanced.jpg', result)运行自验证脚本,自动完成 CPU/NPU 推理 + 精度对比 + 报告生成:
cd gpen-portrait-enhancement
python run_verify.py输出文件:
verify_output/cpu_enhanced.png — CPU 增强结果verify_output/npu_enhanced.png — NPU 增强结果verify_output/pipeline_npu_enhanced.png — 完整管线 NPU 结果verify_output/verify_report.json — 结构化验证报告# 完整 CPU vs NPU 精度+性能基准测试
python precision_benchmark.py --model_dir model_files --warmup 2 --runs 5
# 指定测试图片
python precision_benchmark.py --input photo.jpg --runs 10输出文件:
benchmark_results/precision_report.json — 结构化报告benchmark_results/cpu_output.png — CPU 增强结果benchmark_results/npu_output.png — NPU 增强结果benchmark_results/diff_heatmap.png — 差异热力图gpen-portrait-enhancement/
├── model_files/ # 模型权重
│ ├── pytorch_model.pt # FullGenerator
│ └── net_d.pt # Discriminator
├── gpen_model.py # 模型定义 (FullGenerator + Discriminator)
├── modelscope_pipeline.py # ModelScope 风格 Pipeline (NPU 兼容)
├── inference.py # 完整推理管线 (检测+对齐+增强+融合)
├── inference_simple.py # 简单推理脚本 (仅增强)
├── run_verify.py # 自验证推理脚本 (一键截图验证)
├── precision_benchmark.py # CPU vs NPU 精度&性能基准测试
├── retinaface_detection.py # RetinaFace 人脸检测
├── align_faces.py # 人脸对齐
├── face_quality.py # 人脸质量评估
├── arcface.py # ArcFace 人脸识别
├── batch_process.py # 批量处理脚本
├── RUN_LOG.md # 运行日志(完整终端输出 + JSON 报告)
├── verify_output/ # 自验证输出
│ ├── cpu_enhanced.png # CPU 增强结果
│ ├── npu_enhanced.png # NPU 增强结果
│ ├── pipeline_npu_enhanced.png# 完整管线 NPU 结果
│ └── verify_report.json # 结构化验证报告
├── benchmark_results/ # 基准测试结果
│ ├── precision_report.json # 基准测试报告
│ ├── cpu_output.png # CPU 增强结果
│ ├── npu_output.png # NPU 增强结果
│ └── diff_heatmap.png # 差异热力图
└── README.md # 本文档原始 ModelScope 代码 gpen_model.py 的架构与预训练权重不匹配,已修复以下问题:
narrow 参数: 原代码默认 narrow=0.5,权重需要 narrow=1isconcat / feat_multiplier: 权重使用 isconcat=True(即 feat_multiplier=2),原代码默认 isconcat=Falsesize 参数: 权重为 size=1024(1024×1024 输出),原代码默认 size=512channels 字典以匹配权重的 conv/toRGB 层维度net_d.pt 权重结构
final_conv 和 final_linear 维度# FullGenerator
FullGenerator(
size=1024, # 输出分辨率 1024×1024
style_dim=512, # Style vector 维度
n_mlp=8, # Mapping network 层数
channel_multiplier=2,# 通道倍数
narrow=1, # 通道缩窄系数 (1=不缩窄)
isconcat=True # 使用 concat 模式 (feat_multiplier=2)
)
# Discriminator (推理不需要)
Discriminator(
size=1024, # 输入参考大小
channel_multiplier=2 # 通道倍数
)
# 实际输入 512×512, 7个ResBlock下采样到4×4Q: NPU 推理比 CPU 快多少?
A: 约 233 倍加速(15.5s → 67ms,单张 1024×1024)。
Q: NPU 和 CPU 的输出一致吗?
A: PSNR 68.81 dB, SSIM 0.9999,最大像素差仅 1,视觉完全一致。
Q: 输入图像需要是 1024×1024 吗?
A: 不需要。模型内部会自动 resize 到 1024×1024,增强后 resize 回原始尺寸。
Q: 能否处理多人脸?
A: 可以。使用 inference.py(完整管线)会自动检测所有人脸并逐一增强。
Q: 推理需要 Discriminator 吗?
A: 不需要。推理只需要 FullGenerator。Discriminator 仅用于训练。
Q: 为什么 ModelScope 原生 Pipeline 不能用?
A: ModelScope 的 gpen.py 缺少 isconcat 参数,导致权重 toRGB 层通道维度不匹配(checkpoint [32,3,1,1] vs model [64,3,1,1])。本项目使用修复后的 gpen_model.py 解决此问题。
Q: 如何运行自验证?
A: 执行 python run_verify.py,终端会输出完整推理日志(含环境、延迟、精度),适合截图验证。