MiniFASNet for RK3588: face liveness
Compiled models (.rnpu) for the Rockchip RK3588 / RK3588S NPU, ready to
load. They are fp16 builds for the NPU stack of
RCDL, a C++17 / Python library for vision
and speech on these boards. RCDL's own compiler made them from ONNX, and its
own runtime runs them by driving the rknpu kernel driver directly. That means
no librknnrt and no rknn-toolkit2, only the rknpu driver the board image already
has. There is no .rknn build of these models. The stack is described in
docs/OPEN_NPU.md.
Upstream: Silent-Face-Anti-Spoofing, MiniFASNet V2 and MiniFASNet V1SE. Given one frame and a face box, they say whether the face is a live person or a photo or a screen.
Apache-2.0 on the code and on the weights. The terms of the training data are not stated. Upstream does not say under what terms the data the models were trained on may be used. See Licence.
Files
| file | what it is | size |
|---|---|---|
minifasnet_v2_80_fp16.rnpu |
MiniFASNet V2, shown the face box widened 2.7× | 0.9 MB |
minifasnet_v1se_80_fp16.rnpu |
MiniFASNet V1SE, shown the face box widened 4.0× | 0.9 MB |
Each takes an 80×80 uint8 BGR crop and returns a softmax over three
classes. Class 1 is live. rcdl.FaceLiveness cuts both crops the way the
original does: the box widened about its centre, moved inside the frame, and
resized the way cv2.resize resizes. Its score is the mean of the two models'
live probabilities.
Measured
Against the float ONNX models, on the two faces in zidane.jpg with the crops
cut by the same rule, every class probability agrees within 0.004.
| RK3588S | RK3588 | |
|---|---|---|
| a model run | 0.77 ms | 0.75 ms |
| one face scored: both crops, both models | 2.3 ms | 2.2 ms |
| live score on the two reference faces | 0.57, 0.98 | 0.57, 0.98 |
The float models give the same two scores, 0.57 and 0.98. These numbers are
from RCDL 0.3.0 packages on its own NPU runtime, with one NPU core and NPU
driver 0.9.8 (BSP kernels 6.1.118 and 6.1.141), under the performance CPU
governor. Each is a median over nine runs. The boards default to schedutil,
which clocks the CPU down while it waits for the NPU. Under it, tasks that
alternate NPU and CPU work measured up to about four times slower.
Use it
On the board (linux-aarch64), with RCDL 0.3.1 or newer:
conda create -n rcdl -c https://mirrors.ruis.ai/conda -c conda-forge rcdl
conda activate rcdl
curl -LO https://huggingface.co/ruisv/rcdl-minifasnet/resolve/main/minifasnet_v2_80_fp16.rnpu
curl -LO https://huggingface.co/ruisv/rcdl-minifasnet/resolve/main/minifasnet_v1se_80_fp16.rnpu
import cv2, rcdl
live = rcdl.FaceLiveness(rcdl.Engine("minifasnet_v2_80_fp16.rnpu"),
rcdl.Engine("minifasnet_v1se_80_fp16.rnpu"))
img = cv2.imread("zidane.jpg")
# Face boxes (x1, y1, x2, y2) in source pixels, as a face detector returns them.
faces = [(918.27, 102.68, 1049.75, 264.17), (552.26, 261.60, 654.62, 397.87)]
for box in faces:
p_v2, p_v1se = rcdl.liveness_probabilities(live, img, box) # three classes each
score = rcdl.liveness_score(live, img, box) # mean P(live)
print(f"face at x={box[0]:.0f}: P(live) {score:.2f} "
f"(V2 {p_v2[1]:.2f}, V1SE {p_v1se[1]:.2f})")
zidane.jpg is in RCDL's repository under
data/images/. The two
boxes are the faces RetinaFace finds in it. Expect scores of about 0.57 and
0.98.
What to know before deploying
Bring your own face detector. The models need a face box. RCDL's RetinaFace
build is not published here, because its licence has not been checked to the
end. Any detector that gives (x1, y1, x2, y2) in source pixels will do.
The box moves the score. Each model sees the box widened 2.7× or 4.0×, so a different box is a different crop. On the same photo, the boxes a face detector found on the board in RCDL's benchmark scored 0.48 and 0.94, where the reference boxes score 0.57 and 0.98. Set your threshold with the detector you deploy.
One frame, no challenge. It judges a single frame and asks nothing of the person. Where spoofing matters, pair it with a blink or a head turn.
One model alone works. rcdl.FaceLiveness(rcdl.Engine("minifasnet_v2_80_fp16.rnpu"))
scores with V2 only. rcdl.LivenessConfig holds the widening factors
(scale_a 2.7, scale_b 4.0) and the live class.
These files need RCDL's NPU stack. It runs on RK3588 / RK3588S under the
Rockchip BSP kernels' rknpu driver (0.9.8). It has been verified on two boards
and two kernels (6.1.118, 6.1.141). The rocket driver of mainline kernels is
not supported. The stack is marked experimental, and the .rnpu format may
still change.
Licence
Apache-2.0, on the Silent-Face-Anti-Spoofing code and on these weights. Upstream does not state the terms of the data the models were trained on.
RCDL itself is Apache-2.0 and is unrelated to these terms. It is a
general-purpose runtime that loads any .rnpu or .rknn. The licence above
applies to these weights and these compiled files only.
The input contract for every model is in RCDL's
docs/MODELS.md. The
licence status of every model RCDL has verified is in
model_zoo/.
中文说明
简介
这是 MiniFASNet 人脸活体检测模型为 Rockchip RK3588 / RK3588S NPU 编译好的
文件(.rnpu),下载即可加载。它们是 RCDL
自有 NPU 栈的 fp16 构建:由 RCDL 自己的编译器从 ONNX 编译,再由它自己的运行时
直接驱动 rknpu 内核驱动来运行。不需要 librknnrt,也不需要 rknn-toolkit2,只要
板子镜像自带的 rknpu 驱动即可。这两个模型没有 .rknn 版本。整套 NPU 栈的介绍见
docs/OPEN_NPU.zh.md。
上游是 Silent-Face-Anti-Spoofing 的 MiniFASNet V2 和 MiniFASNet V1SE:给定一帧图像和一个人脸框,判断这是真人, 还是照片或屏幕。
代码和权重都是 Apache-2.0。上游没有说明训练数据的使用条款。
文件
minifasnet_v2_80_fp16.rnpu(0.9 MB):MiniFASNet V2,看的是人脸框放大 2.7 倍后的区域。minifasnet_v1se_80_fp16.rnpu(0.9 MB):MiniFASNet V1SE,看的是放大 4.0 倍后的区域。
两个模型的输入都是 80×80 的 uint8 BGR 图像,输出三类 softmax,第 1 类是真人。
rcdl.FaceLiveness 按原项目的规则裁剪:以框中心放大,移回画面内,再按
cv2.resize 的方式缩放。最终分数是两个模型真人概率的平均值。
实测
- 与浮点 ONNX 模型对比(
zidane.jpg中的两张人脸,按同一规则裁剪):每个类别的概率相差都在 0.004 以内。 - 一次模型推理:RK3588S 上 0.77 ms,RK3588 上 0.75 ms。给一张脸打分(两次裁剪、两个模型):2.3 ms / 2.2 ms。
- 两个参考人脸框的真人分数是 0.57 和 0.98,与浮点模型完全一致。
- 测试条件:RCDL 0.3.0 的 conda 包,自有 NPU 运行时,单个 NPU 核心,NPU 驱动 0.9.8(BSP 内核 6.1.118 和 6.1.141),CPU 调频策略为
performance,取九次运行的中位数。板子默认的schedutil会在等待 NPU 时降低 CPU 频率,NPU 与 CPU 交替工作的任务在它下面最多慢到约四倍。
用法
需要 RCDL 0.3.1 或更新版本,从上面的 conda 频道安装。用法见上面的代码:
用 curl 下载两个模型,Python 片段给 zidane.jpg 里的两个人脸框打分,输出应约为
0.57 和 0.98。zidane.jpg 在 RCDL 仓库的 data/images/ 目录下,这两个框就是
RetinaFace 在这张图上找到的人脸。
部署前须知
- 人脸检测器需要自备。 模型需要人脸框。RCDL 的 RetinaFace 构建没有在这里发布,因为它的许可证还没有核实清楚。任何能给出原图像素坐标
(x1, y1, x2, y2)的检测器都可以。 - 框会影响分数。 两个模型看的是放大 2.7 倍和 4.0 倍的框,框不同,裁剪出来的图就不同。同一张照片上,RCDL 基准测试在板上检测出的框得到 0.48 和 0.94,而参考框是 0.57 和 0.98。阈值请用实际部署的检测器来定。
- 只看一帧,不要求用户配合。 防伪要求高的场合,请配合眨眼或转头等动作检测。
- 单个模型也能用。 只传 V2 的 Engine 给
rcdl.FaceLiveness,就只用 V2 打分。rcdl.LivenessConfig可以修改放大倍数(scale_a2.7,scale_b4.0)和真人类别。 - 这些文件只能在 RCDL 的 NPU 栈上运行。 支持 RK3588 / RK3588S,使用 Rockchip BSP 内核的 rknpu 驱动(0.9.8),已在两块板子、两个内核(6.1.118、6.1.141)上验证;主线内核的
rocket驱动不支持。这套栈仍处于实验阶段,.rnpu格式以后还可能改变。
许可证
Silent-Face-Anti-Spoofing 的代码和这些权重都是 Apache-2.0,上游没有说明训练数据
的条款。RCDL 本身是 Apache-2.0,与上述条款无关;上面的许可证只适用于这些权重和
编译后的文件。各模型的输入约定见 RCDL 的
docs/MODELS.md,
RCDL 核实过的所有模型的许可证情况见
model_zoo/。