cv_resnet18_license-plate-detection_damo 是基于 ModelScope 的车牌检测模型,采用 ResNet18 作为 backbone 结合自底向上的检测方式,实现车牌位置检测和车型分类。模型能够识别 11 种车型:新能源车、小型汽车、大型汽车、挂车、教练车、警车、军车、使领馆车、港澳车、临时车牌、外籍汽车。
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 # 图融合结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-19-1/cv_resnet18_license-plate-detection_damo/ 目录下:
pytorch_model.pt - 模型权重 (约 124MB)configuration.json - 模型配置pip install torch torch_npu opencv-python numpyRun the inference script for license plate detection:
cd /data/ysws/agentsp/5-19-1/cv_resnet18_license-plate-detection_damo-ascend/
# 运行车牌检测
python3 inference.py运行精度对比测试,验证 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!
============================================================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)| 组件 | 输出通道 | 说明 |
|---|---|---|
| backbone | 256 | ResNet18 特征提取 |
| deconv | 256x4 | 特征图上采样 |
| hm_head | 1 | 中心点热力图 |
| reg_head | 2 | 中心点偏移 |
| wh_head | 8 | 边界框尺寸 |
| cls_head | 4 | 车型分类 |
| ftype_head | 11 | 详细车型 |
从模型文档提取的关键参数:
# 图像预处理
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_wA: 检查以下几点:
A: NPU 使用低精度计算模式 (INT8/FP16),与 FP32 CPU 计算存在 2-5% 数值差异是正常的。检测数量一致性是最重要的指标。
A: NPU 推理已针对大规模矩阵运算优化,当前加速比约 11x。首次推理会有编译开销,后续推理会更快。
A: 调整 score_thresh 阈值,尝试 0.1 或更低。也可以尝试不同的预处理方式。
本项目遵循 Apache-2.0 许可证