Object Detection STM32 model evaluation ¶
Our evaluation service is a comprehensive tool that enables users to assess the accuracy of their TensorFlow Lite (.tflite) or Keras (.keras) object detection model. By uploading their model and a validation set, users can quickly and easily evaluate the performance of their model and generate various metrics, such as mAP.
The evaluation service is designed to be fast, efficient, and accurate, making it an essential tool for anyone looking to evaluate the performance of their object detection model.
1. Configure the YAML file ¶
To use this service and achieve your goals, you can use the user_config.yaml or directly update the evaluation_config.yaml file and use it. This file provides an example of how to configure the evaluation service to meet your specific needs.
Alternatively, you can follow the tutorial below, which shows how to evaluate your pre-trained object detection model using our evaluation service.
1.1 Set the model and the operation mode ¶
operation_mode
should be set to evaluation. You can evaluate a pretrained model from the zoo by name, as in
evaluation_config.yaml, or your own model file with
model_path.
model_name
and
model_path
cannot be used together.
model:
model_type: yolov2t
model_name: yolov2t
input_shape: (192, 192, 3)
model_precision: float32 # use w8a8_ptq to evaluate the quantized version
operation_mode: evaluation
In this example, the pretrained ST Yolo V2 Tiny model is selected by
model_name. To evaluate your own model, replace
model_name,
input_shape
and
model_precision
with
model_path, for example:
model:
model_type: yolov2t
model_path: <path-to-a-Keras-or-TFlite-or-ONNX-model-file>
evaluation:
target: host # host, stedgeai_host, stedgeai_n6
In the ‘evaluation’ section, if users are using a quantized TFLITE or ONNX model, they can decide to do the inferences with the classic python interpreters (host -> by default), with the C code generated by stedgeai on the PC (stedgeai_host), or with the C code generated by stedgeai on the N6 board directly (stedgeai_n6) using the
target
attribute.
1.2 Prepare the dataset ¶
Information about the dataset you want to use for evaluation is provided in the
dataset
section of the configuration file, as shown in the YAML code below.
dataset:
format: coco # Dataset format. Required, accepts [coco, voc, darknet_yolo]
dataset_name: coco # Dataset name. Required, accepts [custom_dataset, coco, voc, darknet_yolo]
class_names: [person] # Class names. Required, list of the supported classes.
test_images_path: <test-set-root-directory> # Path to the root directory of the test set images.
test_annotations_path: <test-set-root-directory> # Path to the JSON file of the test set annotations.
format: Defines the annotation format of your dataset. This must match the format of your dataset annotations.
coco: COCO dataset format (JSON annotations)voc: Pascal VOC XML annotation formatdarknet_yolo: YOLO Darknet text file annotations
dataset_name: Specifies the dataset you are using. This can be a well-known dataset like coco, voc, or a custom_dataset if you have your own data and it follows the logic below:
|
Dataset Name |
Allowed Formats |
Description |
|---|---|---|
|
|
|
Native COCO format |
|
|
|
Native Pascal VOC format |
|
|
|
Native Darknet YOLO format |
|
|
|
Your own dataset, in any of the native formats above; the
|
In this example, we are using the
coco
dataset in the
coco
format and the path to the test set is provided in the
test_images_path
and
test_annotations_path
parameters to evaluate the model’s performance.
The state machine below describes the rules to follow when handling dataset paths for the evaluation.
When working with a dataset for the first time, we suggest setting the
check_image_files
attribute to True. This will enable the system to load each image file and identify any corrupt, unsupported, or non-image files. The path to any problematic files will be reported.
In cases where there is no validation set path or test set provided to evaluate the model trained using the training service, the available data under the
train_images_path
and
train_annotations_path
directories is split into two to create a training set and a validation set. By default, 80% of the data is used for training and the remaining 20% is used for the validation set in the evaluation service.
If you want to use a different split ratio, you need to specify the percentage to be used for the validation set in the
validation_split
parameter (to ensure consistency in the
training
and evaluation process, you must specify the same validation_split parameter value in both the training and evaluation services), as shown in the YAML example below:
dataset:
format: coco
dataset_name: coco
class_names: [person]
test_images_path: ./datasets/COCO_2017_person/val_2017
test_annotations_path: ./datasets/COCO_2017_person/annotations/person_val2017.json
1.3 Apply preprocessing ¶
The images from the dataset need to be preprocessed before they are presented to the network for evaluation. This includes rescaling and resizing. In particular, they need to be rescaled exactly as they were at the training step. This is illustrated in the YAML code below:
preprocessing:
rescaling: { scale: 1/127.5, offset: -1 }
resizing:
aspect_ratio: "fit"
interpolation: nearest
color_mode: rgb
In this example, the pixels of the input images read in the dataset are in the interval [0, 255], that is UINT8. If you set
scale
to 1./255 and
offset
to 0, they will be rescaled to the interval [0.0, 1.0]. If you set
scale
to 1/127.5 and
offset
to -1, they will be rescaled to the interval [-1.0, 1.0].
The
resizing
attribute specifies the image resizing methods you want to use:
The value of
interpolationmust be one of {“bilinear”, “nearest”, “bicubic”, “area”, “lanczos3”, “lanczos5”, “gaussian”, “mitchellcubic”}.The value of
aspect_ratiomust be one of “fit”, “crop” or “padding”. If you set it to “fit”, the resized images will be distorted if their original aspect ratio is not the same as the resizing size. With “crop” or “padding”, the image is center-cropped or padded with black pixels instead. Use the same value as in training.
The
color_mode
attribute must be one of “
grayscale
”, “
rgb
” or “
rgba
”.
When you define the preprocessing parameter in the configuration file, the annotation file for the object detection dataset will be automatically modified during preprocessing to ensure that it is aligned with the preprocessed images. This typically involves updating the bounding box coordinates to reflect any resizing or cropping that was performed during preprocessing. This automatic modification of the annotations file is an important step in preparing the dataset for object detection, as it ensures that the annotations accurately reflect the preprocessed images and enables the model to learn from the annotated data.
1.4 Apply post-processing ¶
Apply post-processing by modifying the postprocessing parameters in user_config.yaml as follows:
confidence_thresh- A float between 0.0 and 1.0, the score threshold to filter detections.NMS_thresh- A float between 0.0 and 1.0, NMS threshold to filter and reduce overlapped boxes.IoU_eval_thresh- A float between 0.0 and 1.0, IoU threshold to calculate TP and FP.plot_metrics- A boolean (defaultFalse). IfTrue, the precision versus recall curves are plotted.max_detection_boxes- An integer, the maximum number of bounding boxes retained after post-processing.
1.5 Hydra and MLflow Settings and MLflow Settings ¶
The
mlflow
and
hydra
sections must always be present in the YAML configuration file. The
hydra
section can be used to specify the name of the directory where experiment directories are saved and/or the pattern used to name experiment directories. With the YAML code below, every time you run the Model Zoo, an experiment directory is created that contains all the directories and files created during the run. The names of experiment directories are all unique as they are based on the date and time of the run.
hydra:
run:
dir: ./experiments_outputs/${now:%Y_%m_%d_%H_%M_%S}
The
mlflow
section is used to specify the location and name of the directory where MLflow files are saved, as shown below:
mlflow:
uri: ./experiments_outputs/mlruns
2. Evaluate your model ¶
If you chose to modify the user_config.yaml, you can evaluate the model by running the following command from the UC folder:
python stm32ai_main.py
If you chose to update the evaluation_config.yaml and use it, then run the following command from the UC folder:
python stm32ai_main.py --config-path ./config_file_examples/ --config-name evaluation_config.yaml
In case you want to evaluate the accuracy of the quantized model and then benchmark it, you can either launch the evaluation operation mode followed by the benchmark service that describes in detail how to proceed, or you can use chained services like launching the chain_eqeb example with the command below:
python stm32ai_main.py --config-path ./config_file_examples/ --config-name chain_eqeb_config.yaml
3. Visualize the evaluation results ¶
The mean average precision (mAP) of the float/quantized model is reported in the log file
stm32ai_main.log
under
experiments_outputs/<date-and-time>. If
postprocessing.plot_metrics
is
True, the precision versus recall curves are also saved as one PNG file per class under this directory.