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:
evaluation_yunet_config.yaml: the pretrained quantized
yunetnmodel.evaluation_blazeface_config.yaml: the pretrained quantized
blazeface_frontmodel.
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
.jpgimage and a.tfslabel file with the same name for each example. See the Datasets guide to create the.tfsfiles. Images without a.tfsfile are ignored.The evaluation set is chosen in this order:
test_path; elsevalidation_path; else the lastvalidation_splitfraction oftraining_path(default0.2). Settest_path, ortraining_pathwhen you have no separate test set.Set
dataset_name: widerfaceto load a pretrained model by name, or setmodel.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
rescalingappliesout = scale * in + offsetto pixel values in [0, 255]. The YuNet example keeps the [0, 255] range, and the BlazeFace Front example uses{ scale: 1/255, offset: 0 }withcolor_mode: rgb.aspect_ratioisfit,crop, orpadding. Withfit, the images are resized to the model input size and distorted if the aspect ratios differ. Withcroporpadding, they are center-cropped or padded with black pixels. Usefit, as in the examples: thebgrchannel swap is only applied by thefitloader.interpolationis one ofbilinear,nearest,bicubic,area,lanczos3,lanczos5,gaussian, ormitchellcubic.color_modeis one ofrgb,bgr,grayscale, orrgba.
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 use0.001, and the prediction and deployment examples use0.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 (defaultFalse). IfTrue, 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 ¶
generalis optional.gpu_memory_limitlimits the GPU memory TensorFlow may use, in GB, andnum_threads_tflitesets the number of threads of the TFLite interpreter.The
mlflowandhydrasections of the example files must be kept.hydra.run.dircreates a newexperiments_outputs/<date_time>/folder for each run, andmlflow.uriis 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