Semantic Segmentation STM32 model evaluation

Our evaluation service is a comprehensive tool that enables users to assess the accuracy of their TensorFlow Lite (.tflite), Keras (.keras), or ONNX (.onnx) semantic segmentation model. By uploading their model and an evaluation set (validation or test), users can quickly and easily estimate the performance of their model and generate various metrics.

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 semantic segmentation 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 TensorFlow evaluation_config.yaml, the PyTorch configuration exemple STDC or the BiSeNet. The registered PyTorch evaluation wrapper uses the ONNX evaluator.

Alternatively, you can follow the tutorial below, which shows how to evaluate your pre-trained semantic segmentation model using our evaluation service.

1.1 Set the model and the operation mode

You can either update the default user_config.yaml for your model and dataset, or start from an evaluation example: TensorFlow, PyTorch with STDC, or PyTorch with BiSeNet. In either case, set operation_mode to evaluation and configure the model and dataset sections for your use case.

model:
   model_type: deeplab
   model_path: <path-to-a-Keras-or-TFlite-model-or-ONNX-file> 
operation_mode: evaluation

The path to the model and its type are provided in the model section.

evaluation:
   target: host # host, stedgeai_host, or 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.

For TensorFlow evaluation on the host target with a .tflite model, set general.num_threads_tflite to choose the number of threads used by the TensorFlow Lite interpreter (default: 1):

general:
   num_threads_tflite: 1

This setting has no effect for Keras or ONNX models, or when evaluation runs through a generated-code or device target.

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:
   dataset_name: coco_voc
   dataset_subsetname: person
   class_names: [ "background", "person" ]
   test_images_path: ./datasets/person_COCO2017_VOC2012/JPEGImages
   test_annotations_path: ./datasets/person_COCO2017_VOC2012/SegmentationClassAug
   test_files_path: ./datasets/person_COCO2017_VOC2012/ImageSets/Segmentation/val.txt

The state machine below describes the rules to follow when handling dataset paths for the evaluation.

plot

The class_names attribute specifies the classes in the dataset. This information must be provided in the YAML file. If class_names is absent, use classes_file_path to point to a text file containing the class names.

The service first seeks a test set specification defined by test_images_path, test_annotations_path, and test_files_path parameters. If no test set is specified, the data loader checks if a validation set is specified either by the parameters val_images_path, val_annotations_path, val_files_path or alternatively by train_images_path, train_annotations_path, val_files_path. In the latter, it has to be verified that no images in the val_files_path list of names belong to the set of images used for training, otherwise, there will be an over-estimation of accuracy and IoU.

To finish, in case there is no validation set or test set properly specified, the available data under the train_images_path directory is randomly 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_tf 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:

   train_images_path: ./datasets/person_COCO2017_VOC2012/JPEGImages
   train_annotations_path: ./datasets/person_COCO2017_VOC2012/SegmentationClassAug
   train_files_path: ./datasets/person_COCO2017_VOC2012/ImageSets/Segmentation/trainaug.txt  
   validation_split: 0.20
   

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: bilinear
   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 interpolation must be one of {“bilinear”, “nearest”, “bicubic”, “area”, “lanczos3”, “lanczos5”, “gaussian”, “mitchellcubic”}.

  • The value of aspect_ratio must be either “fit” or “crop”. If you set it to “fit”, the resized images will be distorted if their original aspect ratio is not the same as in the resizing size. If you set it to “crop”, images will be cropped as necessary to preserve the aspect ratio.

The color_mode attribute must be one of “ grayscale ”, “ rgb ” or “ rgba ”.

1.4 Hydra and MLflow settings

The mlflow section is required. A user-supplied hydra section is optional; when provided, it can configure the directory and naming pattern for experiment outputs. Hydra still creates an output directory for each run. The example below uses a timestamp so each run has a unique directory.

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 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 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 service reports pixel accuracy, average IoU, and mean IoU. The background class contributes to these metrics when it is present in the ground-truth masks; pixels labeled with the ignore label are excluded from mean IoU.

The accuracy is the proportion of pixels whose predicted class matches the ground-truth class. The evaluator computes it over each unit it processes—which may be a batch or an individual image, depending on the model format and target—and averages those values across the evaluation set.

Intersection over Union (IoU) measures the overlap between predicted and ground-truth pixels for a class: the intersection divided by the union. Average IoU averages the per-class IoU values for classes present in each evaluated image or batch, then averages those values across the evaluation set. Mean IoU is computed from a confusion matrix accumulated over the evaluation set, by calculating IoU for each class and averaging the class values.

All three metrics are displayed on screen and saved in stm32ai_main.log under /experiments_outputs/. They are also logged to MLflow.