cv_resnet18_human-detection 是基于 ModelScope 的 ResNet18 人体检测模型,采用 FasterRCNN 架构结合 DyHead 注意力机制实现人体检测任务。模型将输入图像中的人体以边界框形式输出,适用于监控场景的行人检测应用。
cv_resnet18_human-detection-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── test_human.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_human-detection/ 目录下:
pytorch_model.pt - 模型权重 (约 147MB)configuration.json - 模型配置mmcv_config.py - 完整模型架构定义pip install torch torch_npu opencv-python numpy运行推理脚本进行人体检测:
cd /data/ysws/agentsp/5-19-1/cv_resnet18_human-detection-ascend/
# 运行人体检测
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-19-1/cv_resnet18_human-detection-ascend/
# 运行完整精度测试
python3 inference.py precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
precision_test | 运行精度对比测试模式 | inference |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| Cls score 最大差异 | 0.192 | - | - |
| Bbox pred 最大差异 | 0.067 | - | - |
| 检测数量一致性 | 100 vs 100 | 相等 | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | ~4.00s |
| NPU 推理时间 | ~3.93s |
| 加速比 | ~1.02x |
| 输入 | 检测数量 | 输出文件 |
|---|---|---|
| test_human.jpg (533x948) | 100 人体 | result_visualization.jpg |
结果: NPU 推理功能正常,检测数量与 CPU 一致
完整测试日志保存在 log.txt
import torch
import cv2
import numpy as np
MODEL_PATH = "/data/ysws/agentsp/5-19-1/cv_resnet18_human-detection/iic/cv_resnet18_human-detection/pytorch_model.pt"
# 加载模型
model = HumanDetectionModel(MODEL_PATH)
device = torch.device("npu:0")
model = model.to(device)
model.eval()
# 读取图片
img = cv2.imread("test_human.jpg")
img_tensor, scale, _, _ = preprocess(img)
img_tensor = torch.from_numpy(img_tensor).unsqueeze(0).to(device)
# 推理
with torch.no_grad():
cls_score, bbox_pred = model(img_tensor)
# 解码检测结果
detections = decode_detections(cls_score, bbox_pred, scale, img.shape)
print(f"检测到 {len(detections)} 个人体")import os
image_dir = "/path/to/images"
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, _, _ = preprocess(img)
img_tensor = torch.from_numpy(img_tensor).unsqueeze(0).to(device)
# 推理
with torch.no_grad():
cls_score, bbox_pred = model(img_tensor)
detections = decode_detections(cls_score, bbox_pred, scale, img.shape)
# 保存可视化结果
vis_path = os.path.join(OUTPUT_DIR, f"result_{img_file}")
visualize(img, detections, vis_path)| 组件 | 说明 |
|---|---|
| backbone | ResNet18 特征提取器 |
| neck | FPN 特征金字塔 + DyHead 注意力 |
| rpn_head | 区域提议网络 |
| roi_head | 动态RoI头部检测器 |
从 mmcv_config.py 提取的关键参数:
# 图像预处理
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True
)
# 测试图像增强
img_scale=(1333, 800)
size_divisor=32
# 检测阈值
score_thr=0.6
nms_threshold=0.7 (rpn), 0.5 (rcnn)A: NPU 推理已针对大规模矩阵运算优化,当前加速比约 1.02x。批量处理可提高吞吐量。
A: 检查输入图片质量和预处理配置。模型在遮挡和小目标场景可能存在误检。
A: 支持 OpenCV 可读取的所有格式,包括 JPG、PNG、BMP 等。
本项目遵循 Apache-2.0 许可证