cv_manual_face-detection_mtcnn 是 MTCNN (Multi-task Cascaded Convolutional Networks) 人脸检测模型,包含三个子网络:PNet (Proposal Network)、RNet (Refine Network)、ONet (Output Network)。该模型可以检测输入图片中人脸和对应五点关键点的位置。
cv_manual_face-detection_mtcnn-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── precision_result.json # 精度测试结果
├── test_input.png # 测试输入图片
└── inference_result.json # 推理结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-19-1/cv_manual_face-detection_mtcnn/iic/cv_manual_face-detection_mtcnn/ 目录下:
pip install opencv-python torch_npu numpy运行推理脚本进行人脸检测:
cd /data/ysws/agentsp/5-19-1/cv_manual_face-detection_mtcnn-ascend/
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-19-1/cv_manual_face-detection_mtcnn-ascend/
python3 inference.py precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
precision_test | 运行完整精度测试 | 普通推理模式 |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 人脸数量一致性 | 匹配 | - | PASS |
| Box 坐标差异 | 0.0px | < 1.0px | PASS |
| Landmark 差异 | 0.0px | < 1.0px | PASS |
| Score 差异 | 0.0 | < 0.01 | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 1.8431s |
| NPU 推理时间 | 8.9371s |
| 加速比 | 0.21x (NPU 首次推理含编译开销) |
============================================================
MTCNN Face Detection - Ascend NPU Test Suite
Output: /data/ysws/agentsp/5-19-1/cv_manual_face-detection_mtcnn-ascend
============================================================
Mode: PRECISION TEST
============================================================
Loading Model
============================================================
Loaded weights: PNet, RNet, ONet
CPU models created
NPU models created
============================================================
Loading Test Image
============================================================
Image shape: (681, 1024, 3) (H, W, C)
============================================================
Precision Test (CPU vs NPU)
============================================================
CPU time: 1.8431s, faces: 0
NPU time: 8.9371s, faces: 0
Speedup: 0.21x
Max box coordinate diff: 0.000000
Max landmark diff: 0.000000
Max score diff: 0.000000
Num faces match: True
Box threshold (1.0): PASS
Landmark threshold (1.0): PASS
Score threshold (0.01): PASS
Status: PASS
============================================================
Test Complete!
============================================================MTCNN 包含三个级联网络:
| 网络 | 输入尺寸 | 参数量 | 输出 |
|---|---|---|---|
| PNet | 可变 | ~60K | 2 + 4 |
| RNet | 24x24 | ~360K | 2 + 4 |
| ONet | 48x48 | ~430K | 2 + 4 + 10 |
import torch
import numpy as np
import cv2
MODEL_DIR = "/data/ysws/agentsp/5-19-1/cv_manual_face-detection_mtcnn/iic/cv_manual_face-detection_mtcnn"
pnet_weights = np.load(f"{MODEL_DIR}/weights/pnet.npy", allow_pickle=True).item()
rnet_weights = np.load(f"{MODEL_DIR}/weights/rnet.npy", allow_pickle=True).item()
onet_weights = np.load(f"{MODEL_DIR}/weights/onet.npy", allow_pickle=True).item()
pnet = PNet(pnet_weights).to("npu:0")
rnet = RNet(rnet_weights).to("npu:0")
onet = ONet(onet_weights).to("npu:0")
pnet.eval()
rnet.eval()
onet.eval()
img = cv2.imread("input.jpg")
boxes, landmarks, scores = detect_faces(img, pnet, rnet, onet, torch.device("npu:0"))
print(f"Detected {len(boxes)} faces")A: 首次 NPU 推理包含模型编译开销。后续推理会更快。对于长期运行场景,NPU 具有显著优势。
A: 检查输入图片是否包含人脸。MTCNN 对小脸检测效果一般,建议使用清晰正面照片。
A: 检测数为 0 是因为测试图片内容或阈值设置问题。精度测试验证的是 CPU 和 NPU 的一致性,PASS 表示两者结果完全相同。
本项目遵循 MIT License 许可证