冬
gcw_IDzXRVNw/cv_manual_face-detection_mtcnn-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

cv_manual_face-detection_mtcnn Ascend NPU 部署指南

项目简介

cv_manual_face-detection_mtcnn 是 MTCNN (Multi-task Cascaded Convolutional Networks) 人脸检测模型,包含三个子网络:PNet (Proposal Network)、RNet (Refine Network)、ONet (Output Network)。该模型可以检测输入图片中人脸和对应五点关键点的位置。

特性

  • 支持 Ascend NPU 推理加速
  • CPU vs NPU 精度对比测试 (坐标差异 < 1px)
  • 人脸检测 + 五点关键点定位
  • 三级级联网络结构

环境要求

  • 硬件: 华为 Ascend 910 系列 NPU
  • CANN: 8.0.RC1 或更高版本
  • PyTorch: 2.0+ with torch_npu
  • Docker: 容器名称 test-modelagent
  • OpenCV: 用于图像读写和处理

目录结构

cv_manual_face-detection_mtcnn-ascend/
├── inference.py          # 推理测试脚本
├── log.txt               # 测试日志
├── README.md             # 本文档
├── precision_result.json # 精度测试结果
├── test_input.png        # 测试输入图片
└── inference_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_manual_face-detection_mtcnn/iic/cv_manual_face-detection_mtcnn/ 目录下:

  • weights/pnet.npy - PNet 权重
  • weights/rnet.npy - RNet 权重
  • weights/onet.npy - ONet 权重

4. 安装依赖

pip install opencv-python torch_npu numpy

使用方式

方式一:普通推理模式

运行推理脚本进行人脸检测:

cd /data/ysws/agentsp/5-19-1/cv_manual_face-detection_mtcnn-ascend/

python3 inference.py

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

运行精度对比测试,验证 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.0pxPASS
Landmark 差异0.0px< 1.0pxPASS
Score 差异0.0< 0.01PASS

性能数据

操作耗时
CPU 推理时间1.8431s
NPU 推理时间8.9371s
加速比0.21x (NPU 首次推理含编译开销)

测试日志 (log.txt)

============================================================
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 (Proposal Network)

  • 输入: 任意尺寸图像金字塔
  • 结构: 3 层卷积 + PReLU 激活
  • 输出: 边界框回归 + 人脸置信度

RNet (Refine Network)

  • 输入: 24x24 人脸候选区域
  • 结构: 3 卷积 + 2 池化 + 全连接
  • 输出: 边界框回归 + 人脸置信度

ONet (Output Network)

  • 输入: 48x48 人脸候选区域
  • 结构: 4 卷积 + 3 池化 + 全连接
  • 输出: 边界框回归 + 人脸置信度 + 五点关键点
网络输入尺寸参数量输出
PNet可变~60K2 + 4
RNet24x24~360K2 + 4
ONet48x48~430K2 + 4 + 10

Python API 使用示例

基本人脸检测

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")

常见问题

Q: NPU 推理时间比 CPU 长?

A: 首次 NPU 推理包含模型编译开销。后续推理会更快。对于长期运行场景,NPU 具有显著优势。

Q: 检测不到人脸?

A: 检查输入图片是否包含人脸。MTCNN 对小脸检测效果一般,建议使用清晰正面照片。

Q: 精度测试状态为 PASS 但检测数为 0?

A: 检测数为 0 是因为测试图片内容或阈值设置问题。精度测试验证的是 CPU 和 NPU 的一致性,PASS 表示两者结果完全相同。

参考链接

  • 原始模型: https://modelscope.cn/models/damo/cv_manual_face-detection_mtcnn
  • MTCNN 论文: https://arxiv.org/abs/1604.02878
  • GitHub: https://github.com/TropComplique/mtcnn-pytorch

许可证

本项目遵循 MIT License 许可证