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

  1. Set up your Python environment as described in Getting started.

  2. Run all commands from the image_classification/ folder:

    cd stm32ai-modelzoo-services/image_classification
    
  3. Pretrained models are loaded by name from the STM32 model zoo. Their location is set by CHECKPOINTS_USED_STORAGE in common/utils/models_utils.py; by default, a stm32ai-modelzoo clone is expected next to stm32ai-modelzoo-services.

Choose your framework

TensorFlow

PyTorch

Example configurations

config_file_examples/

config_file_examples_pt/

Full user configuration

user_config.yaml

user_config_pt.yaml

Model names

mobilenetv2_a035, …

End in _pt: mobilenetv2_w035_pt, …

model.input_shape

(height, width, channels), e.g. (224, 224, 3)

[channels, height, width], e.g. [3, 224, 224]

Input preprocessing

preprocessing.rescaling ( scale, offset)

preprocessing.normalization ( mean, std)

Trained model

.keras

.pth.tar checkpoint and .onnx export

Default quantized model

.tflite ( TFlite_converter)

QDQ .onnx ( Onnx_quantizer)

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-path and --config-name, user_config.yaml is used.

  • Each run creates experiments_outputs/<date_time>/, which contains the log stm32ai_main.log, trained models in saved_models/, and quantized models in quantized_models/.

  • Any YAML value can be overridden on the command line, for example training.epochs=2 or operation_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

training

Train a model from the variety of classification models in the model zoo (BYOD) or your own model (BYOM)

evaluation

Evaluate the accuracy of a float or quantized model on a test or validation dataset

quantization

Quantize a float model

prediction

Predict the classes some images belong to using a float or quantized model

benchmarking

Benchmark a float or quantized model on an STM32 board

deployment

Deploy a model on an STM32 board

chain_tqeb

Sequentially: training, quantization of trained model, evaluation of quantized model, benchmarking of quantized model

chain_tqe

Sequentially: training, quantization of trained model, evaluation of quantized model

chain_eqe

Sequentially: evaluation of a float model, quantization, evaluation of the quantized model

chain_qb

Sequentially: quantization of a float model, benchmarking of quantized model

chain_eqeb

Sequentially: evaluation of a float model, quantization, evaluation of quantized model, benchmarking of quantized model

chain_qd

Sequentially: quantization of a float model, deployment of quantized model

See the Benchmarking, Prediction, and Deployment guides for the other services.