cv_csrnet_image-color-enhance-models 是 CSRNet(Conditional Sequential Modulation for Efficient Global Image Retouching)图像调色模型。该模型通过计算全局调整参数并将之作用于条件网络得到的特征,实现高效轻便的图像色彩增强。输入为待调色的图像,输出为调色后的图像。
cv_csrnet_image-color-enhance-models-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── precision_result.json # 精度测试结果
├── test_input.png # 测试输入图片
├── output_cpu.png # CPU 输出图片
└── output_npu.png # NPU 输出图片docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models/iic/cv_csrnet_image-color-enhance-models/ 目录下:
pip install opencv-python torch_npu numpy运行推理脚本进行图像增强:
cd /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models-ascend/
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models-ascend/
python3 inference.py precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
precision_test | 运行完整精度测试 | 普通推理模式 |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 最大相对误差 | 0.0012% | < 1.00% | PASS |
| 平均相对误差 | 0.0008% | < 1.00% | PASS |
| SSIM | 1.000000 | - | 完美一致 |
| PSNR | inf dB | - | 完美一致 |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 0.7644s |
| NPU 推理时间 | 3.9262s |
| 加速比 | 0.19x (NPU 首次推理含编译开销) |
| 项目 | 说明 |
|---|---|
| 输入尺寸 | 3 x 341 x 512 (C, H, W) |
| 输出尺寸 | 3 x 341 x 512 (C, H, W) |
| 图像范围 | [0, 1] 归一化 |
| 处理方式 | 残差增强 |
============================================================
CSRNet Image Color Enhancement - Ascend NPU Test Suite
Output: /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models-ascend
============================================================
Mode: PRECISION TEST
============================================================
Loading Test Image
============================================================
Image shape: (3, 341, 512) (C, H, W)
Image range: [0.0392, 0.9569]
Saved test input: /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models-ascend/test_input.png
============================================================
CSRNet Precision Test (CPU vs NPU)
============================================================
--- CPU Inference ---
Device: cpu
CPU time: 0.7644s
CPU output shape: torch.Size([1, 3, 341, 512])
--- NPU Inference ---
Device: npu:0
NPU time: 3.9262s
NPU output shape: torch.Size([1, 3, 341, 512])
============================================================
Comparing Results
============================================================
Max absolute error: 1.418591e-05
Max relative error: 1.161404e-05 (0.0012%)
Mean absolute error: 9.253113e-06
Mean relative error: 7.575550e-06 (0.0008%)
Speedup: 0.19x
PSNR: inf dB
SSIM: 1.000000
Threshold: 1.0%
Status: PASS
Saved CPU output: /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models-ascend/output_cpu.png
Saved NPU output: /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models-ascend/output_npu.png
Precision result saved: /data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models-ascend/precision_result.json
============================================================
Test Complete!
============================================================| 组件 | 说明 |
|---|---|
| cond_net | 条件网络 (32 通道, 7x7 卷积核) |
| cond_scale | 全局 Scale 调制 (32->64, 32->64, 32->3) |
| cond_shift | 全局 Shift 调制 (32->64, 32->64, 32->3) |
| conv1/2/3 | 主网络 1x1 卷积 (3->64, 64->64, 64->3) |
从 checkpoint 提取的关键参数:
import torch
import cv2
import numpy as np
MODEL_DIR = "/data/ysws/agentsp/5-19-1/cv_csrnet_image-color-enhance-models/iic/cv_csrnet_image-color-enhance-models"
model = CSRNet()
state = torch.load(f"{MODEL_DIR}/pytorch_model.pt", map_location='cpu')
model.load_state_dict(state['params'])
model = model.to("npu:0")
model.eval()
img = cv2.imread("input.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
img = np.transpose(img, (2, 0, 1))
img_tensor = torch.from_numpy(img).unsqueeze(0).to("npu:0")
with torch.no_grad():
output = model(img_tensor)A: 首次 NPU 推理包含模型编译开销。后续推理会更快。对于长期运行场景,NPU 具有显著优势。
A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。CSRNet 的相对误差应远低于 1%。
A: CSRNet 支持任意尺寸输入,模型会自动处理。只需确保图像归一化到 [0, 1] 范围。
A: 确保输入图像格式正确(RGB)。如有问题,可检查 test_input.png 与原始图像的差异。
本项目遵循 Apache-2.0 许可证