Object Detection STM32 model prediction

The prediction service runs an object detection model on a set of images and draws the detected bounding boxes, class labels, and confidence scores on each image. It accepts a Keras ( .keras), TensorFlow Lite ( .tflite), or ONNX ( .onnx) model, either a pretrained model of the zoo selected by name or your own model file.

1. Configure the YAML file

Use the prediction section of user_config.yaml, or update prediction_config.yaml. The example below uses your own model file:

operation_mode: prediction

model:
   model_type: <model-type>              # st_yoloxn, st_yololcv1, yolov2t, yolov8n, ...
   model_path: <path-to-a-Keras-or-TFlite-or-ONNX-model-file>

dataset:
   class_names: [person]                 # Names of the classes, as there is no dataset to infer them from
   prediction_path: <path-to-the-images-to-predict>

preprocessing:
   rescaling: { scale: 1/127.5, offset: -1 }
   resizing:
      aspect_ratio: fit
      interpolation: nearest
   color_mode: rgb

postprocessing:
   confidence_thresh: 0.5
   NMS_thresh: 0.5
   IoU_eval_thresh: 0.5
   max_detection_boxes: 10
  • model_type is required, also when you use model_path.

  • Instead of model_path, you can select a pretrained model by name, as in prediction_config.yaml: model_name, input_shape, model_precision, and the dataset_name and dataset_subsetname of the pretrained weights. model_name and model_path cannot be used together.

  • preprocessing must be identical to the one used for training the model.

  • class_names is required because no annotated dataset is available to infer the class names. prediction_path is the folder that contains the images to predict.

Run the inference on the STM32N6 target

If you use a quantized TFLite or ONNX model, you can run the inferences with the standard Python interpreter on the PC ( host, the default), with the C code generated by STEdgeAI on the PC ( stedgeai_host), or with the C code generated by STEdgeAI directly on the STM32N6 board ( stedgeai_n6) using the target attribute. See prediction_n6_config.yaml for a complete example. The prediction section looks like this for the N6:

prediction:
   profile: default
   input_type: uint8       # int8 / uint8 / float32
   output_type: float32    # 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

Hydra and MLflow settings

The mlflow and hydra sections must always be present in the YAML configuration file. Every run creates a uniquely named experiment directory based on the date and time of the run:

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

mlflow:
   uri: ./experiments_outputs/mlruns

2. Launch the prediction

If you chose to modify user_config.yaml, run the following command from the object_detection/ folder:

python stm32ai_main.py

If you chose to update prediction_config.yaml, run:

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

The annotated images are saved as <image-name>_predict.jpg in the predictions folder of the experiment directory experiments_outputs/<date-and-time>.