cv_manual_face-liveness_flxc 是静默炫彩人脸活体检测模型,用于判断输入图像中的人脸是否为来自认证设备端的近距离裸拍活体人脸。炫彩活体检测通过光在活体和假体上的不同反射情况来区分,具有对多种攻击鲁棒的特点。
cv_manual_face-liveness_flxc-ascend/
├── inference.py # 推理测试脚本
├── flxc_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 numpy运行推理脚本进行活体检测:
cd /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flxc-ascend/
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flxc-ascend/
python3 inference.py precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
precision_test | 运行完整精度测试 | 普通推理模式 |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 绝对误差 | 0.000064 | < 0.01 | PASS |
| 相对误差 | 0.1128% | < 1.00% | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 0.0076s |
| NPU 推理时间 | 7.6355s |
| 加速比 | 0.0010x |
模型输出为 [batch, 2] 的张量,表示二分类概率:
分数越高表示假体的可能性越高。
============================================================
FLXC Face Liveness Detection - Ascend NPU Test
Output: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flxc-ascend
============================================================
Mode: PRECISION TEST
============================================================
Loading Model
============================================================
ONNX: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flxc/iic/cv_manual_face-liveness_flxc/model.onnx (exists: True)
PyTorch: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flxc-ascend/flxc_npu.pt (exists: True)
Test image: /data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flxc/iic/cv_manual_face-liveness_flxc/algo.jpg
============================================================
Loading Test Image
============================================================
Image shape: (390, 888, 3) (H, W, C)
Input tensor shape: (1, 12, 112, 112)
============================================================
Running CPU Inference (ONNX)
============================================================
CPU output shape: (1, 2)
CPU score (fake probability): 0.056449
CPU time: 0.0076s
============================================================
Running NPU Inference (PyTorch)
============================================================
NPU output shape: torch.Size([1, 2])
NPU score (fake probability): 0.056386
NPU time: 7.6355s
Speedup: 0.00x
============================================================
Precision Test Results
============================================================
CPU score: 0.056449
NPU score: 0.056386
Absolute diff: 0.000064
Relative diff: 0.001128
Threshold (0.01): PASS
Status: PASS
============================================================
Test Complete!
============================================================{
"cpu_score": 0.056449,
"npu_score": 0.056386,
"cpu_time_sec": 0.0076,
"npu_time_sec": 7.6355,
"score_diff": 0.000064,
"rel_diff": 0.001128,
"passed": true
}| 组件 | 说明 |
|---|---|
| 输入 | 112x112 炫彩图像 (12通道) |
| Backbone | ResNet 特征提取 |
| 输出 | [real_prob, fake_prob] |
import torch
import cv2
import numpy as np
OUTPUT_DIR = "/data/ysws/agentsp/5-19-1/cv_manual_face-liveness_flxc-ascend"
model = torch.jit.load(f"{OUTPUT_DIR}/flxc_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_12ch = np.zeros((12, 112, 112), dtype=np.float32)
for c in range(3):
img_12ch[c::3] = img[c]
img_tensor = torch.from_numpy(img_12ch).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。FLXC 模型的相对误差应远低于 1%。
A: 分数越高表示输入图像中的人脸越可能是假体(纸张、电子屏等)。真实活体的分数较低。
A: 通过光在活体和假体上的不同反射情况来区分活体和假体,对多种攻击鲁棒,特别是深度有关的攻击(如非肤质3D面具和头模等)。
本项目遵循 MIT 许可证