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

  1. Set up your Python environment as described in Getting started.

  2. Run all commands from the face_detection/ folder:

    cd stm32ai-modelzoo-services/face_detection
    
  3. Pretrained 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-path and --config-name, user_config.yaml is used.

  • Each run creates experiments_outputs/<date_time>/, which contains the log stm32ai_main.log, quantized models in quantized_models/, and the prediction images in predictions/.

  • Any YAML value can be overridden on the command line, for example operation_mode=evaluation or dataset.test_path=/path/to/test_set.

  • Use absolute paths for datasets and models.

Every Face Detection configuration needs:

  • model.model_type: yunet or facedetect_front, also when you use model_path.

  • dataset.class_names: the list of class names, for example [face]. It is not needed for quantization, benchmarking, and chain_qb.

  • A preprocessing section (with rescaling, except for deployment).

  • A postprocessing section with confidence_thresh, NMS_thresh, and IoU_eval_thresh, in every operation mode except benchmarking. max_detection_boxes has no default and is needed for evaluation, prediction, and deployment. Keep the values of the example files.

  • The mlflow and hydra sections of the example files: mlflow.uri is required, and hydra.run.dir sets the experiments_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

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

evaluation

Evaluate the accuracy of a float or quantized model on a test dataset

quantization

Quantize a float model

prediction

Detect faces in images using a float or quantized model

benchmarking

Benchmark a float or quantized model on an STM32 board

deployment

Deploy a model on an STM32 board

chain_eqe

Sequentially: evaluation of a float model, quantization, evaluation of the quantized model

chain_qb

Sequentially: quantization of a float model, benchmarking of the quantized model

chain_eqeb

Sequentially: evaluation of a float model, quantization, evaluation of the quantized model, benchmarking of the quantized model

chain_qd

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 is yunetn with 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 is blazeface_front with 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.