Face Detection STM32 Model Zoo ¶
Face Detection locates human faces in an image and returns one bounding box per face, together with facial keypoints, enabling applications such as access control, user presence detection, and face-based interaction on STM32 devices.
This page takes you from a dataset to an evaluated and quantized model, and points you to benchmarking, prediction, and deployment on an STM32N6 board. Every service is selected with the
operation_mode
attribute of a YAML file and launched with the same command. Face Detection uses TensorFlow-based tooling and provides no training service: you evaluate, quantize, benchmark, and deploy the pretrained models of the zoo or your own models.
Before you start ¶
Set up your Python environment as described in Getting started.
Run all commands from the
face_detection/folder:cd stm32ai-modelzoo-services/face_detectionPretrained models are loaded by name. You can browse them in the ST Model Explorer. The names and input shapes that can be loaded are listed in the Models guide.
How a run works ¶
python stm32ai_main.py --config-path <config-folder> --config-name <config-file>.yaml
Without
--config-pathand--config-name, user_config.yaml is used.Each run creates
experiments_outputs/<date_time>/, which contains the logstm32ai_main.log, quantized models inquantized_models/, and the prediction images inpredictions/.Any YAML value can be overridden on the command line, for example
operation_mode=evaluationordataset.test_path=/path/to/test_set.Use absolute paths for datasets and models.
Every Face Detection configuration needs:
model.model_type:yunetorfacedetect_front, also when you usemodel_path.dataset.class_names: the list of class names, for example[face]. It is not needed forquantization,benchmarking, andchain_qb.A
preprocessingsection (withrescaling, except fordeployment).A
postprocessingsection withconfidence_thresh,NMS_thresh, andIoU_eval_thresh, in every operation mode exceptbenchmarking.max_detection_boxeshas no default and is needed for evaluation, prediction, and deployment. Keep the values of the example files.The
mlflowandhydrasections of the example files:mlflow.uriis required, andhydra.run.dirsets theexperiments_outputs/<date_time>/folder.
Prepare your dataset ¶
Evaluation reads a flat folder that contains, for each image, a
.jpg
file and a
.tfs
label file with the same name:
test_set/
image_1.jpg
image_1.tfs
image_2.jpg
image_2.tfs
Images without a
.tfs
file are ignored. Quantization and prediction read the
.jpg
images only; no label files are needed.
If your annotations are not in this format, convert them first:
Datasets Converter
turns COCO, Pascal VOC, or KITTI annotations into YOLO Darknet
.txt
files,
Datasets Analysis
helps you choose the maximum number of faces per image, and
Dataset create TFS
generates the
.tfs
files. See the
Datasets guide
for the whole workflow.
1. Evaluate a model ¶
You can evaluate a pretrained model from the zoo by name, or your own model file with
model.model_path. The evaluation reports precision, recall, and the average precision (AP) in
stm32ai_main.log. Keep
preprocessing
identical to the one used for training.
config_file_examples/evaluation_yunet_config.yaml
evaluates the pretrained quantized
yunetn
model. Set
dataset.test_path
to your test set and run it:
model:
model_type: yunet
model_name: yunetn
input_shape: (3, 320, 320)
model_precision: w8a8_ptq # float32 evaluates the float model
dataset:
dataset_name: widerface # also selects the pretrained weights
class_names: [face]
test_path: /path/to/test_set # folder of .jpg and .tfs files
python stm32ai_main.py --config-path ./config_file_examples/ --config-name evaluation_yunet_config.yaml
To evaluate your own model, replace the
model
section, because
model_name
and
model_path
cannot be used together:
model:
model_type: yunet
model_path: /path/to/model.onnx # .onnx, .tflite or .keras
For BlazeFace, use
evaluation_blazeface_config.yaml. To run a quantized model on the STM32N6 board instead of the host, set
evaluation.target:
stedgeai_n6; see the
Evaluation guide.
2. Quantize a model ¶
Quantization needs calibration images that are representative of your data and preprocessed the same way as for training. In
config_file_examples/quantization_config.yaml, set
dataset.quantization_path:
model:
model_type: yunet
model_name: yunetn # the float model of the zoo is loaded
input_shape: (3, 320, 320)
dataset:
dataset_name: widerface
class_names: [face]
quantization_path: /path/to/calibration_images # .jpg files, no labels needed
quantization_split: 0.01 # fraction of the images to use
To quantize your own float model, replace
model_name
and
input_shape
with
model_path:
/path/to/float_model.onnx.
python stm32ai_main.py --config-path ./config_file_examples/ --config-name quantization_config.yaml
The quantized model is saved to
experiments_outputs/<date_time>/quantized_models/. If
quantization_path
and
training_path
are empty, the model is quantized with random data: its accuracy is meaningless, but it can be used to estimate the footprints on target. Options are described in the
Quantization guide.
3. Run several steps at once ¶
Chained services run consecutive steps with a single configuration file.
chain_eqe
evaluates a float model, quantizes it, and evaluates the quantized model, so you can compare their accuracy in one run:
python stm32ai_main.py --config-path ./config_file_examples/ --config-name chain_eqe_config.yaml
chain_eqeb_config.yaml adds a benchmark of the quantized model, and chain_qd_n6_config.yaml quantizes and deploys it on an STM32N6 board.
4. Predict, benchmark, and deploy ¶
Prediction draws the detected faces and keypoints on your images and saves a crop of each face. Start from prediction_yunet_config.yaml and follow the Prediction guide.
Benchmarking estimates the footprints and inference time of a model on an STM32 board: see the Benchmarking guide.
Deployment builds and flashes the application on an STM32N6 board: see the Deployment guide.
Operation modes ¶
The
operation_mode
attribute defines the service you want to run. In the names of chained modes,
e
= evaluation,
q
= quantization,
b
= benchmarking,
d
= deployment.
|
operation_mode attribute |
Operations |
|---|---|
|
|
Evaluate the accuracy of a float or quantized model on a test dataset |
|
|
Quantize a float model |
|
|
Detect faces in images using a float or quantized model |
|
|
Benchmark a float or quantized model on an STM32 board |
|
|
Deploy a model on an STM32 board |
|
|
Sequentially: evaluation of a float model, quantization, evaluation of the quantized model |
|
|
Sequentially: quantization of a float model, benchmarking of the quantized model |
|
|
Sequentially: evaluation of a float model, quantization, evaluation of the quantized model, benchmarking of the quantized model |
|
|
Sequentially: quantization of a float model, deployment of the quantized model |
The
model_type
attributes currently supported for Face Detection are:
yunet: YuNet, a lightweight face detection model that predicts a bounding box and 5 keypoints (eyes, nose, mouth corners) per face. The zoo model isyunetnwith a 320x320 input, in ONNX format.facedetect_front: BlazeFace Front, a lightweight model for frontal faces that predicts a bounding box and 6 keypoints per face. The zoo model isblazeface_frontwith a 128x128 input, quantized in TFLite format.
Remember that minimalistic YAML files are available here to play with specific services, and that the pretrained models of the ST Model Explorer are good starting points.