cv_manual_face-liveness_flrgb 是 RGB 人脸活体检测模型,用于判断输入图像中的人脸是否为来自认证设备端的近距离裸拍活体人脸,可广泛应用在人脸实时采集场景。
cv_manual_face-liveness_flrgb-ascend/
├── inference.py # 推理测试脚本
├── flrgb_npu.pt # TorchScript 模型
├── log.txt # 测试日志
├── README.md # 本文档
├── precision_result.json # 精度测试结果
└── test_input.png # 测试图片docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.shpip install opencv-python onnxruntime onnx2torch torch_npu numpyRun the inference script for face liveness detection:
cd /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flrgb-ascend/
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flrgb-ascend/
python3 inference.py precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
precision_test | 运行完整精度测试 | 普通推理模式 |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 绝对误差 | 0.000079 | < 0.01 | PASS |
| 相对误差 | 0.0563% | < 1.00% | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 0.0094s |
| NPU 推理时间 | 7.0664s |
| 加速比 | 0.0013x |
模型输出为 [batch, 2] 的张量,表示二分类概率:
分数越高表示假体的可能性越高。
============================================================
FLRGB Face Liveness Detection - Ascend NPU Test
Output: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flrgb-ascend
============================================================
Mode: PRECISION TEST
============================================================
Loading Model
============================================================
ONNX: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flrgb/iic/cv_manual_face-liveness_flrgb/model.onnx (exists: True)
PyTorch: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flrgb-ascend/flrgb_npu.pt (exists: True)
Test image: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flrgb/iic/cv_manual_face-liveness_flrgb/result.png
============================================================
Loading Test Image
============================================================
Image shape: (112, 112, 3) (H, W, C)
Input tensor shape: (1, 3, 112, 112)
============================================================
Running CPU Inference (ONNX)
============================================================
CPU output shape: (1, 2)
CPU score (fake probability): 0.140649
CPU time: 0.0094s
============================================================
Running NPU Inference (PyTorch)
============================================================
NPU output shape: torch.Size([1, 2])
NPU score (fake probability): 0.140728
NPU time: 7.0664s
Speedup: 0.00x
============================================================
Precision Test Results
============================================================
CPU score: 0.140649
NPU score: 0.140728
Absolute diff: 0.000079
Relative diff: 0.000563
Threshold (0.01): PASS
Status: PASS
============================================================
Test Complete!
============================================================{
"cpu_score": 0.14064925909042358,
"npu_score": 0.14072847366333008,
"cpu_time_sec": 0.009386301040649414,
"npu_time_sec": 7.066424608230591,
"speedup": 0.0013282956461060599,
"score_diff": 7.921457290649414e-05,
"rel_diff": 0.0005632064312795468,
"passed": true
}| 组件 | 说明 |
|---|---|
| 输入 | 112x112 RGB 图像 |
| Backbone | ResNet 特征提取 + 深度可分离卷积 |
| 输出 | [真实概率, 伪造概率] |
import torch
import cv2
import numpy as np
OUTPUT_DIR = "/data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flrgb-ascend"
model = torch.jit.load(f"{OUTPUT_DIR}/flrgb_npu.pt", map_location="npu:0")
model.eval()
img = cv2.imread("test.jpg")
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
mean = np.array([123.675, 116.28, 103.53], dtype=np.float32).reshape(1, 1, 3)
std = np.array([58.395, 57.12, 57.375], dtype=np.float32).reshape(1, 1, 3)
img = cv2.resize(img_rgb, (128, 128))
start = (128 - 112) // 2
img = img[start:start+112, start:start+112]
img = (img - mean) / std
img = np.transpose(img, (2, 0, 1))
img_tensor = torch.from_numpy(img).unsqueeze(0).float().to("npu:0")
with torch.no_grad():
output = model(img_tensor)
fake_prob = output[0][1].item()
print(f"Fake probability: {fake_prob:.4f}")A: 首次 NPU 推理包含模型编译开销。后续推理会更快。对于长期运行场景,NPU 具有显著优势。
A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。FLRGB 模型的相对误差应远低于 1%。
A: 分数越高表示输入图像中的人脸越可能是假体(纸张、电子屏等)。真实活体的分数较低。
本项目遵循 MIT 许可证