Image Classification STM32 model zoo ¶
Image Classification assigns a label to an entire image, enabling applications such as object recognition, quality inspection, and scene understanding 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
image_classification/folder:cd stm32ai-modelzoo-services/image_classificationPretrained models are loaded by name from the STM32 model zoo. Their location is set by
CHECKPOINTS_USED_STORAGEin common/utils/models_utils.py; by default, astm32ai-modelzooclone is expected next tostm32ai-modelzoo-services.
Choose your framework ¶
|
|
TensorFlow |
PyTorch |
|---|---|---|
|
Example configurations |
||
|
Full user configuration |
||
|
Model names |
|
End in
|
|
|
|
|
|
Input preprocessing |
|
|
|
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.
The available models, their accuracy, and their footprints are listed in the Models guide.
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 insaved_models/, 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.
Prepare your dataset ¶
TensorFlow: put the images in one sub-folder per class. Folder names are used as class names.
dataset_root/
class_a/
image_1.jpg
class_b/
image_2.jpg
If
validation_path
is empty,
validation_split
(default
0.2) of
training_path
is used for validation. If
test_path
is empty, the validation set is used for evaluation.
PyTorch:
use the ImageNet layout and set
dataset.data_dir
to its parent folder:
data_dir/
imagenet/
train/<class folders>
val/<class folders>
For your own dataset, set
dataset_name:
custom
and point
training_path
and
validation_path
to two class-folder trees like the TensorFlow one above.
Further details on supported datasets are available in
Datasets guide.
1. Train a model ¶
TensorFlow ¶
Edit these keys in config_file_examples/training_config.yaml:
model:
model_name: mobilenetv2_a035
input_shape: (224, 224, 3)
pretrained: True # start from ImageNet weights (transfer learning)
dataset:
dataset_name: tf_flowers
training_path: /path/to/flower_photos # one sub-folder per class
validation_split: 0.2
# Remove classes_file_path to take class names from the folder names,
# or point it to a text file with one class name per line.
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 200 epochs with early stopping; add
training.epochs=2
to the command for a quick check.
PyTorch ¶
Edit these keys in config_file_examples_pt/training_config.yaml:
model:
framework: torch
model_name: mobilenetv4small_pt
input_shape: [3, 224, 224]
pretrained: True
pretrained_dataset_name: imagenet
dataset:
dataset_name: imagenet
data_dir: /path/to/data_dir # must contain imagenet/train and imagenet/val
num_classes: 1000
For a custom dataset, replace the
dataset
keys above with:
dataset:
dataset_name: custom
training_path: /path/to/train
validation_path: /path/to/val
num_classes: <number-of-classes>
classes_file_path: '' # take class names from the folder names
# Single GPU
CUDA_VISIBLE_DEVICES=0 python stm32ai_main.py --config-path ./config_file_examples_pt/ --config-name training_config.yaml
# Multiple GPUs: batch_size is applied per GPU
torchrun --nproc_per_node=4 stm32ai_main.py --config-path ./config_file_examples_pt/ --config-name training_config.yaml
The
.pth.tar
checkpoints and the exported
.onnx
model are saved in
experiments_outputs/<date_time>/saved_models/. The example runs only 2 epochs; increase
training.epochs
for a real training.
More options, such as data augmentation, learning-rate schedules, and layer freezing, 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. Keep
preprocessing
identical to the one used for training.
TensorFlow ¶
config_file_examples/evaluation_config.yaml
evaluates the pretrained
mobilenetv2_a035
Food-101 model. Set
dataset.test_path
to your Food-101 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_path: /path/to/experiments_outputs/<date_time>/saved_models/best_model.keras # or .tflite / .onnx
dataset:
dataset_name: tf_flowers
test_path: /path/to/test_set # one sub-folder per class
# Use the same classes_file_path or class names as for training.
python stm32ai_main.py --config-path ./config_file_examples/ --config-name evaluation_config.yaml
PyTorch ¶
config_file_examples_pt/evaluation_config.yaml
evaluates the pretrained
mobilenetv2_w035_pt
ImageNet model. Set
dataset.test_path
or
dataset.data_dir
and run it. To evaluate your own model, set:
model:
framework: torch
model_path: /path/to/experiments_outputs/<date_time>/saved_models/<model>.onnx
python stm32ai_main.py --config-path ./config_file_examples_pt/ --config-name evaluation_config.yaml
Accuracy and the confusion matrix are written to
experiments_outputs/<date_time>/. 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.
TensorFlow ¶
In
config_file_examples/quantization_config.yaml, set
dataset.quantization_path
to your calibration images. To quantize your own model, replace
model_name
and
input_shape
with:
model:
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/quantization_config.yaml, set
dataset.quantization_path
to your calibration images.
quantization_split:
0.01
uses 1% of them. To quantize your own model, set:
model:
framework: torch
model_path: /path/to/experiments_outputs/<date_time>/saved_models/<model>.onnx
python stm32ai_main.py --config-path ./config_file_examples_pt/ --config-name quantization_config.yaml
The
Onnx_quantizer
saves a QDQ
.onnx
model to
experiments_outputs/<date_time>/quantized_models/.
If no calibration data is provided, 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/ --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.
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 classification models in the model zoo (BYOD) or your own model (BYOM) |
|
|
Evaluate the accuracy of a float or quantized model on a test or validation dataset |
|
|
Quantize a float model |
|
|
Predict the classes some images belong to 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.