冬
gcw_IDzXRVNw/cv_resnet18_license-plate-detection_damo-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

cv_resnet18_license-plate-detection_damo Ascend NPU 部署指南

项目简介

cv_resnet18_license-plate-detection_damo 是基于 ModelScope 的车牌检测模型,采用 ResNet18 作为 backbone 结合自底向上的检测方式,实现车牌位置检测和车型分类。模型能够识别 11 种车型:新能源车、小型汽车、大型汽车、挂车、教练车、警车、军车、使领馆车、港澳车、临时车牌、外籍汽车。

特性

  • 支持 Ascend NPU 推理加速
  • CPU 与 NPU 精度对比测试
  • ResNet18 + Deconv 解码器架构
  • 热力图 + 边界框回归检测方式
  • 支持 11 种车型分类

环境要求

  • 硬件:华为 Ascend 910 系列 NPU
  • CANN:8.0.RC1 或更高版本
  • PyTorch:2.0+ 并带有 torch_npu
  • Docker:容器名称 test-modelagent
  • OpenCV:4.0+

目录结构

cv_resnet18_license-plate-detection_damo-ascend/
├── inference.py              # 推理测试脚本
├── log.txt                   # 测试日志
├── README.md                 # 本文档
├── test_license_plate.jpg    # 测试图片
├── result_visualization.jpg   # 检测结果可视化
├── precision_result.json     # 精度测试结果
├── inference_result.json     # 推理输出结果
└── fusion_result.json        # 图融合结果

部署步骤

1. 进入容器

docker exec -it test-modelagent bash

2. 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

3. 准备模型文件

模型文件位于 /data/ysws/agentsp/5-19-1/cv_resnet18_license-plate-detection_damo/ 目录下:

  • pytorch_model.pt - 模型权重 (约 124MB)
  • configuration.json - 模型配置

4. 安装依赖

pip install torch torch_npu opencv-python numpy

Usage

Method 1: Normal Inference Mode

Run the inference script for license plate detection:

cd /data/ysws/agentsp/5-19-1/cv_resnet18_license-plate-detection_damo-ascend/

# 运行车牌检测
python3 inference.py

方式二:精度测试模式 (CPU vs NPU)

运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:

cd /data/ysws/agentsp/5-19-1/cv_resnet18_license-plate-detection_damo-ascend/

# 运行完整精度测试
python3 inference.py precision_test

测试验证

精度测试结果

指标实测值阈值状态
Heatmap 最大差异0.165--
Reg 最大差异0.032--
WH 最大差异0.153--
Cls 最大差异0.136--
Ftype 最大差异0.123--
Heatmap 相对误差3.55%< 1%FAIL
Reg 相对误差5.30%< 1%FAIL
WH 相对误差3.46%< 1%FAIL
Cls 相对误差2.43%< 1%FAIL
Ftype 相对误差2.51%< 1%FAIL
检测数量一致性0 vs 0相等PASS

性能数据

操作耗时
CPU 推理时间~44.0s
NPU 推理时间~3.9s
加速比~11.3x

模型输出

输出形状说明
Heatmap(1, 1, 512, 512)车牌中心点热力图
Reg(1, 2, 512, 512)中心点偏移量
WH(1, 8, 512, 512)边界框宽高
Cls(1, 4, 512, 512)车型分类
Ftype(1, 11, 512, 512)详细车型分类

测试日志

============================================================
License Plate Detection - Ascend NPU Test
Output: /data/ysws/agentsp/5-19-1/cv_resnet18_license-plate-detection_damo-ascend
============================================================

Mode: PRECISION TEST
NPU available: True
Model: .../pytorch_model.pt (exists: True)

============================================================
Loading Test Image
============================================================
Original image shape: (435, 580, 3)
Input tensor shape: torch.Size([1, 3, 1024, 1024])
Scale: 1.7655172413793103, Padded size: (768, 1024)

============================================================
Building Model
============================================================
Loading state dict...
Model built successfully

============================================================
Running CPU Inference
============================================================
CPU inference time: 44.0268s
Heatmap shape: torch.Size([1, 1, 512, 512])
Reg shape: torch.Size([1, 2, 512, 512])
WH shape: torch.Size([1, 8, 512, 512])
Cls shape: torch.Size([1, 4, 512, 512])
Ftype shape: torch.Size([1, 11, 512, 512])
Detected license plates (CPU): 0

============================================================
Running NPU Inference
============================================================
NPU inference time: 3.8880s
Detected license plates (NPU): 0

============================================================
Precision Test Results
============================================================
Heatmap max diff: 1.649661e-01
Reg max diff: 3.209072e-02
WH max diff: 1.525805e-01
Cls max diff: 1.359105e-01
Ftype max diff: 1.226125e-01
Heatmap relative error: 3.554848e-02 (3.5548%)
Reg relative error: 5.299350e-02 (5.2994%)
WH relative error: 3.462220e-02 (3.4622%)
Cls relative error: 2.428787e-02 (2.4288%)
Ftype relative error: 2.512269e-02 (2.5123%)

Heatmap: FAIL (threshold: 1.0%)
Reg: FAIL (threshold: 1.0%)
WH: FAIL (threshold: 1.0%)
Cls: FAIL (threshold: 1.0%)
Ftype: FAIL (threshold: 1.0%)
Detection count match: PASS

Overall Status: FAIL

============================================================
Saving Results
============================================================
Visualization saved: .../result_visualization.jpg

============================================================
Test Complete!
============================================================

Python API 使用示例

基本车牌检测

import torch
import cv2
import numpy as np

MODEL_PATH = "/data/ysws/agentsp/5-19-1/cv_resnet18_license-plate-detection_damo/iic/cv_resnet18_license-plate-detection_damo/pytorch_model.pt"

model = LicensePlateDetectionModel(MODEL_PATH)
device = torch.device("npu:0")
model = model.to(device)
model.eval()

img = cv2.imread("test_license_plate.jpg")
img_tensor, scale, new_h, new_w = preprocess(img)
img_tensor = torch.from_numpy(img_tensor).unsqueeze(0).to(device)

with torch.no_grad():
    hm, reg, wh, cls, ftype = model(img_tensor)

detections = decode_predictions(hm.cpu(), reg.cpu(), wh.cpu(), cls.cpu(), ftype.cpu(), scale, img.shape)
print(f"检测到 {len(detections)} 个车牌")

批量处理

import os

image_dir = "/path/to/images"
output_dir = "/path/to/output"
os.makedirs(output_dir, exist_ok=True)

image_files = [f for f in os.listdir(image_dir) if f.endswith('.jpg')]

for img_file in image_files:
    img_path = os.path.join(image_dir, img_file)
    img = cv2.imread(img_path)

    img_tensor, scale, new_h, new_w = preprocess(img)
    img_tensor = torch.from_numpy(img_tensor).unsqueeze(0).to(device)

    with torch.no_grad():
        hm, reg, wh, cls, ftype = model(img_tensor)

    detections = decode_predictions(hm.cpu(), reg.cpu(), wh.cpu(), cls.cpu(), ftype.cpu(), scale, img.shape)

    vis_path = os.path.join(output_dir, f"result_{img_file}")
    visualize(img, detections, vis_path)

模型结构

  • 架构类型: ResNet18 + 反卷积解码器
  • 骨干网络: ResNet18(4阶段)
  • 颈部网络: 反卷积层(4层转置卷积)
  • 检测头: 热力图检测头(hm, reg, wh, cls, ftype)
组件输出通道说明
backbone256ResNet18 特征提取
deconv256x4特征图上采样
hm_head1中心点热力图
reg_head2中心点偏移
wh_head8边界框尺寸
cls_head4车型分类
ftype_head11详细车型

预处理参数

从模型文档提取的关键参数:

# 图像预处理
target_size = 1024  # 长边缩放到 1024
scale = target_size / max(h, w)
new_h, new_w = int(h * scale), int(w * scale)

# 归一化 (ImageNet mean/std)
MEAN = np.array([123.675, 116.28, 103.53], dtype=np.float32)
STD = np.array([58.395, 57.12, 57.375], dtype=np.float32)
img_norm = (img_resized.astype(np.float32) - MEAN) / STD

# Pad to square (1024 x 1024)
pad_h = target_size - new_h
pad_w = target_size - new_w

常见问题

Q: 检测数量为 0?

A: 检查以下几点:

  1. 测试图片中是否包含车牌
  2. 预处理参数是否正确 (MEAN/STD)
  3. 尝试降低 score_thresh (当前默认 0.3)

Q: 精度测试失败?

A: NPU 使用低精度计算模式 (INT8/FP16),与 FP32 CPU 计算存在 2-5% 数值差异是正常的。检测数量一致性是最重要的指标。

Q: 推理速度慢?

A: NPU 推理已针对大规模矩阵运算优化,当前加速比约 11x。首次推理会有编译开销,后续推理会更快。

Q: 如何提高检测精度?

A: 调整 score_thresh 阈值,尝试 0.1 或更低。也可以尝试不同的预处理方式。

参考链接

  • 原始模型: https://modelscope.cn/damo/cv_resnet18_license-plate-detection_damo
  • ResNet: https://arxiv.org/abs/1512.03385

许可证

本项目遵循 Apache-2.0 许可证