Face Detection STM32 model evaluation

The evaluation service measures the accuracy of a Face Detection model on a labeled test set. It accepts a Keras ( .keras, .h5), TensorFlow Lite ( .tflite), or ONNX ( .onnx) model, either a pretrained model of the zoo selected by name or your own model file, and reports the precision, recall, and average precision (AP) of the detected faces.

1. Configure the YAML file

Start from user_config.yaml or from one of the example files:

1.1 Set the model and the operation mode

Set operation_mode to evaluation. Select a pretrained model of the zoo by name, as in the example files, or your own model file with model_path. model_name and model_path cannot be used together, and model_type is required in both cases.

operation_mode: evaluation

model:
  model_type: yunet
  model_name: yunetn
  input_shape: (3, 320, 320)
  model_precision: w8a8_ptq      # float32 evaluates the float model

To evaluate your own model, replace model_name, input_shape, and model_precision with model_path:

model:
  model_type: yunet
  model_path: <path-to-a-Keras-or-TFlite-or-ONNX-model-file>

The names and shapes of the pretrained models, and the formats accepted for each family, are listed in the Models guide. On the host, .tflite models are evaluated for facedetect_front only: use an ONNX model for yunet.

1.2 Prepare the dataset

The dataset section describes the test set:

dataset:
  dataset_name: widerface          # also selects the pretrained weights
  class_names: [face]              # or classes_file_path: a text file with one class name per line
  test_path: <test-set-directory>  # folder of .jpg images and .tfs label files
  • The test set is a flat folder that contains a .jpg image and a .tfs label file with the same name for each example. See the Datasets guide to create the .tfs files. Images without a .tfs file are ignored.

  • The evaluation set is chosen in this order: test_path; else validation_path; else the last validation_split fraction of training_path (default 0.2). Set test_path, or training_path when you have no separate test set.

  • Set dataset_name: widerface to load a pretrained model by name, or set model.pretrained_dataset_name.

1.3 Apply preprocessing

The images must be preprocessed exactly as they were for the training of the model:

preprocessing:
  rescaling: { scale: 1, offset: 0 }
  resizing:
    aspect_ratio: fit
    interpolation: bilinear
  color_mode: bgr
  • rescaling applies out = scale * in + offset to pixel values in [0, 255]. The YuNet example keeps the [0, 255] range, and the BlazeFace Front example uses { scale: 1/255, offset: 0 } with color_mode: rgb.

  • aspect_ratio is fit, crop, or padding. With fit, the images are resized to the model input size and distorted if the aspect ratios differ. With crop or padding, they are center-cropped or padded with black pixels. Use fit, as in the examples: the bgr channel swap is only applied by the fit loader.

  • interpolation is one of bilinear, nearest, bicubic, area, lanczos3, lanczos5, gaussian, or mitchellcubic.

  • color_mode is one of rgb, bgr, grayscale, or rgba.

1.4 Apply post-processing

postprocessing:
  confidence_thresh: 0.001
  NMS_thresh: 0.5
  IoU_eval_thresh: 0.5
  plot_metrics: True
  max_detection_boxes: 100
  • confidence_thresh: a float between 0.0 and 1.0, the score threshold to filter detections. The evaluation examples use 0.001, and the prediction and deployment examples use 0.5.

  • NMS_thresh: a float between 0.0 and 1.0, the non-maximum suppression threshold used to reduce overlapping boxes.

  • IoU_eval_thresh: a float between 0.0 and 1.0, the IoU threshold used to count a detection as a true or false positive.

  • max_detection_boxes: an integer, the maximum number of boxes kept per image. It has no default value.

  • plot_metrics: a boolean (default False). If True, the precision versus recall curves are saved.

1.5 Evaluate on the STM32N6 target

For a quantized model, the evaluation.target attribute selects where the inferences run: host (default) uses the Python interpreters on the PC, stedgeai_host uses the C code generated by STEdgeAI on the PC, and stedgeai_n6 uses the C code generated by STEdgeAI on an STM32N6 board. The YAML of evaluation_n6_yunet_config.yaml contains:

evaluation:
  profile: default
  input_type: uint8       # int8 / uint8 / float32
  output_type: int8       # int8 / uint8 / float32
  input_chpos: chlast     # chlast / chfirst
  output_chpos: chlast    # chlast / chfirst
  target: stedgeai_n6     # host, stedgeai_host, stedgeai_n6

tools:
  stedgeai:
    path_to_stedgeai: C:/ST/STEdgeAI/<x.y>/Utilities/windows/stedgeai.exe

The BlazeFace Front example, evaluation_n6_blazeface_config.yaml, uses float32 for input_type and output_type. Install and configure the tools as described in How to evaluate a model on an STM32N6 target.

1.6 Other sections

  • general is optional. gpu_memory_limit limits the GPU memory TensorFlow may use, in GB, and num_threads_tflite sets the number of threads of the TFLite interpreter.

  • The mlflow and hydra sections of the example files must be kept. hydra.run.dir creates a new experiments_outputs/<date_time>/ folder for each run, and mlflow.uri is the folder where MLflow stores its files:

    mlflow:
      uri: ./experiments_outputs/mlruns
    
    hydra:
      run:
        dir: ./experiments_outputs/${now:%Y_%m_%d_%H_%M_%S}
    

2. Evaluate your model

Run the command from the face_detection/ folder. To use user_config.yaml, which is set for prediction, override the operation mode and add the test set:

python stm32ai_main.py operation_mode=evaluation dataset.test_path=/path/to/test_set

To use an example file, run:

python stm32ai_main.py --config-path ./config_file_examples/ --config-name evaluation_yunet_config.yaml

To compare the accuracy of the float and quantized models in one run, use chain_eqe with chain_eqe_config.yaml. To also benchmark the quantized model, use chain_eqeb (see the Benchmarking guide).

python stm32ai_main.py --config-path ./config_file_examples/ --config-name chain_eqe_config.yaml

3. Visualize the evaluation results

The mean precision, mean recall, and mean average precision (mAP) of the float or quantized model are reported in the log file stm32ai_main.log in experiments_outputs/<date_time>/, in the lines <float|quantized>_model_mpre, _mrec, and _map. The log of the console also gives the precision, recall, and AP per class.

If postprocessing.plot_metrics is True, the precision versus recall curves are saved as PNG files in experiments_outputs/<date_time>/precision_vs_recall_curves/<model-file-name>/.

The metrics are also logged in MLflow. To browse them, run the following command from the experiments_outputs folder:

mlflow ui