Object Detection STM32 Model Zoo ¶
Object Detection locates and classifies multiple objects within an image by drawing bounding boxes around them, enabling applications such as people counting, obstacle avoidance, and industrial inspection on STM32 devices.
This page takes you from a dataset to a trained, quantized, and evaluated model with as few configuration changes as possible. Every service is selected with the
operation_mode
attribute of a YAML file and launched with the same command.
Before you start ¶
Set up your Python environment as described in Getting started.
Run all commands from the
object_detection/folder:cd stm32ai-modelzoo-services/object_detectionPretrained models are loaded by name. You can browse them in the ST Edge AI Model Explorer.
Choose your framework ¶
|
|
TensorFlow |
PyTorch |
|---|---|---|
|
Example configurations |
config_file_examples_pt/ssd/ and config_file_examples_pt/yolod/st_yolodv2_pt/ |
|
|
Full user configuration |
||
|
Trainable
|
|
|
|
Model names |
|
End in
|
|
|
|
|
|
Dataset formats |
|
|
|
Trained model |
|
|
|
Default quantized model |
|
QDQ
|
For PyTorch, always set
model.framework:
torch. Without it, an
.onnx
model_path
is processed with the TensorFlow pipeline.
Ultralytics models (
yolov8n,
yolov11n,
yolo26n,
yolov5u) are not trained here: use them for evaluation, quantization, and deployment with
model_path, as in
evaluation_yolo26n_config.yaml.
The available models, their accuracy, and their footprints are listed in the TensorFlow and PyTorch model guides.
How a run works ¶
python stm32ai_main.py --config-path <config-folder> --config-name <config-file>.yaml
Without
--config-pathand--config-name,user_config.yamlis used.Each run creates
experiments_outputs/<date_time>/, which contains the logstm32ai_main.log, trained models in thegeneral.saved_models_dirfolder, and quantized models inquantized_models/.Any YAML value can be overridden on the command line, for example
training.epochs=2oroperation_mode=evaluation.Use absolute paths for datasets and models.
Every object detection configuration needs:
model.model_type: the model family, also when you usemodel_path.dataset.format: for training and evaluation, the annotation format of your files (coco,voc, ordarknet_yolo).dataset.class_names: the list of class names, for example[person].A
postprocessingsection withconfidence_thresh,NMS_thresh, andIoU_eval_thresh. Keep the values of the example files.
Prepare your dataset ¶
Darknet YOLO
(TensorFlow): one
.txt
label file per image, with one
class_id
x_center
y_center
width
height
line per box, normalized to [0, 1]. Images and labels can be in the same folder or in two folders:
my_dataset/
train/
images/ image_1.jpg, image_2.jpg, ...
annotations/ image_1.txt, image_2.txt, ...
test/
images/
annotations/
COCO
(TensorFlow and PyTorch): an image folder and one annotation JSON file per split, for example
train2017/
and
annotations/instances_train2017.json.
Pascal VOC: the standard
VOCdevkit
layout, with
JPEGImages/,
Annotations/, and
ImageSets/Main/*.txt
split files.
If no validation set is given,
validation_split
(default
0.2) of the training set is used for validation. If no test set is given, the validation set is used for evaluation.
The annotations are read directly in their original format; no conversion step is needed. See How to prepare my own dataset and the Datasets guide for all options.
1. Train a model ¶
TensorFlow ¶
Edit these keys in config_file_examples/training_config.yaml:
model:
model_type: st_yoloxn
model_name: st_yoloxn_d033_w025
input_shape: (256, 256, 3)
pretrained: True # add to start from the zoo weights; omit to train from scratch
dataset:
format: darknet_yolo
class_names: [person]
train_images_path: /path/to/my_dataset/train/images
train_annotations_path: /path/to/my_dataset/train/annotations
val_images_path: /path/to/my_dataset/test/images # or remove both val_* keys
val_annotations_path: /path/to/my_dataset/test/annotations # to use validation_split
dataset_name:
coco
and
dataset_subsetname:
person
select the pretrained weights, here
st_yoloxn_d033_w025
trained on COCO person; the closest available input size is used. The weights are loaded only if your number of classes matches; otherwise, training starts from random weights with a warning.
python stm32ai_main.py --config-path ./config_file_examples/ --config-name training_config.yaml
The trained model is saved as
experiments_outputs/<date_time>/saved_models/best_model.keras. The example trains for up to 400 epochs with early stopping; add
training.epochs=2
to the command for a quick check.
PyTorch ¶
Edit these keys in config_file_examples_pt/yolod/st_yolodv2_pt/training_config_coco_person.yaml:
model:
framework: torch
model_type: st_yolod
model_name: st_yolodv2milli_actrelu_pt
pretrained: True
pretrained_dataset_name: coco_person
input_shape: [3, 640, 640]
num_classes: 1 # must match the length of class_names
dataset:
format: coco
dataset_name: coco
class_names: [person]
train_images_path: /path/to/coco/train2017
train_annotations_path: /path/to/coco/annotations/instances_train2017_person.json
val_images_path: /path/to/coco/val2017
val_annotations_path: /path/to/coco/annotations/instances_val2017_person.json
For your own dataset, convert it to COCO format and set
class_names,
num_classes, and the four paths. SSD models are trained the same way with
config_file_examples_pt/ssd/.
# Single GPU
CUDA_VISIBLE_DEVICES=0 python stm32ai_main.py --config-path ./config_file_examples_pt/yolod/st_yolodv2_pt/ --config-name training_config_coco_person.yaml
# Multiple GPUs
torchrun --standalone --nproc_per_node=4 stm32ai_main.py --config-path ./config_file_examples_pt/yolod/st_yolodv2_pt/ --config-name training_config_coco_person.yaml
The
.pth
checkpoints and the exported
.onnx
model are saved in
experiments_outputs/<date_time>/<general.saved_models_dir>/. The example trains for 300 epochs; reduce
training.epochs
for a first experiment.
More options, such as data augmentation, learning-rate schedules, and resuming a training, are described in the TensorFlow and PyTorch training guides.
2. 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 the mean average precision (mAP) in
stm32ai_main.log. Keep
preprocessing
identical to the one used for training.
TensorFlow ¶
config_file_examples/evaluation_config.yaml
evaluates the pretrained
yolov2t
COCO person model. Set
test_images_path
and
test_annotations_path
to your test set and run it. Use
model_precision:
w8a8_ptq
to evaluate the quantized version.
To evaluate your own model, replace the
model
section, because
model_name
and
model_path
cannot be used together:
model:
model_type: st_yoloxn
model_path: /path/to/experiments_outputs/<date_time>/saved_models/best_model.keras # or .tflite / .onnx
preprocessing:
rescaling: { scale: 1/255, offset: 0 } # same as in training_config.yaml; the example uses yolov2t values
python stm32ai_main.py --config-path ./config_file_examples/ --config-name evaluation_config.yaml
PyTorch ¶
config_file_examples_pt/yolod/st_yolodv2_pt/evaluation_config.yaml
evaluates the pretrained, quantized
st_yolodv2milli_actrelu_pt
COCO model. Set
test_images_path
and
test_annotations_path
to the COCO
val2017
set and run it. To evaluate your own model, add
model_path
and match
input_shape,
num_classes, and
class_names
to your training:
model:
framework: torch
model_type: st_yolod
model_path: /path/to/experiments_outputs/<date_time>/<saved_models_dir>/<model>.onnx
python stm32ai_main.py --config-path ./config_file_examples_pt/yolod/st_yolodv2_pt/ --config-name evaluation_config.yaml
To run a quantized model on the STM32N6 board instead of the host, set
evaluation.target:
stedgeai_n6; see the
Evaluation guide.
3. Quantize a model ¶
Quantization needs calibration images that are representative of your data and preprocessed the same way as for training.
dataset.quantization_path
is a folder of images: no annotations are needed. If it is empty, the training images are used.
TensorFlow ¶
In
config_file_examples/quantization_config.yaml, set
dataset.quantization_path. To quantize your own model, replace the
model
section with:
model:
model_type: st_yoloxn
model_path: /path/to/experiments_outputs/<date_time>/saved_models/best_model.keras
python stm32ai_main.py --config-path ./config_file_examples/ --config-name quantization_config.yaml
The
TFlite_converter
quantizer saves an 8-bit
.tflite
model to
experiments_outputs/<date_time>/quantized_models/.
PyTorch ¶
In
config_file_examples_pt/yolod/st_yolodv2_pt/quantization_config.yaml, set
dataset.quantization_path.
quantization_split:
0.01
uses 1% of the images. To quantize your own model, add:
model:
framework: torch
model_type: st_yolod
model_path: /path/to/experiments_outputs/<date_time>/<saved_models_dir>/<model>.onnx
python stm32ai_main.py --config-path ./config_file_examples_pt/yolod/st_yolodv2_pt/ --config-name quantization_config.yaml
The
onnx_quantizer
saves a QDQ
.onnx
model to
experiments_outputs/<date_time>/quantized_models/.
If no calibration images are available, the model is quantized with random data. Its accuracy is meaningless, but it can be used to estimate the footprints on target. Mixed precision, Qmn, and AdaQuant are described in the Quantization guide.
4. Run several steps at once ¶
Chained services run consecutive steps with a single configuration file. For example,
chain_tqe
trains, quantizes, and evaluates a model:
python stm32ai_main.py --config-path ./config_file_examples/ --config-name chain_tqe_config.yaml # TensorFlow
python stm32ai_main.py --config-path ./config_file_examples_pt/yolod/st_yolodv2_pt/ --config-name chain_tqe_config.yaml # PyTorch
chain_eqe
evaluates a float model, quantizes it, and evaluates the quantized model, so you can compare their accuracy in one run. To fine-tune a model from the zoo on your own data, see
How to fine-tune a model on my own dataset.
Operation modes ¶
The
operation_mode
attribute defines the service you want to run. In the names of chained modes,
t
= training,
e
= evaluation,
q
= quantization,
b
= benchmarking,
d
= deployment.
|
operation_mode attribute |
Operations |
|---|---|
|
|
Train a model from the variety of object detection models in the model zoo (BYOD) or your own model with the same model type (BYOM) |
|
|
Evaluate the accuracy of a float or quantized model on a test or validation dataset |
|
|
Quantize a float model |
|
|
Detect objects in images using a float or quantized model |
|
|
Benchmark a float or quantized model on an STM32 board |
|
|
Deploy a model on an STM32 board |
|
|
Sequentially: training, quantization of trained model, evaluation of quantized model, benchmarking of quantized model |
|
|
Sequentially: training, quantization of trained model, evaluation of quantized model |
|
|
Sequentially: evaluation of a float model, quantization, evaluation of the quantized model |
|
|
Sequentially: quantization of a float model, benchmarking of quantized model |
|
|
Sequentially: evaluation of a float model, quantization, evaluation of quantized model, benchmarking of quantized model |
|
|
Sequentially: quantization of a float model, deployment of quantized model |
See the Benchmarking, Prediction, and Deployment guides for the other services.