Instance Segmentation Prediction

The prediction service runs an instance segmentation model on a set of images and saves each image annotated with the mask, bounding box, class index, and confidence score of every detected object. It runs on the PC with a quantized TensorFlow Lite ( .tflite) model, either a pretrained model of the zoo selected by name or your own model file.

1. Configure the YAML file

Use user_config.yaml with operation_mode: prediction, or update prediction_config.yaml. The example below uses your own model file:

operation_mode: prediction

model:
  model_type: yolov8n_seg
  model_path: <path-to-a-quantized-tflite-model>

dataset:
  dataset_name: coco_is
  classes_file_path: ./datasets/coco_classes.txt
  prediction_path: <path-to-the-images-to-predict>

preprocessing:
  rescaling:
    scale: 1/255.0
    offset: 0
  resizing:
    interpolation: bilinear
    aspect_ratio: fit
  color_mode: rgb

postprocessing:
  confidence_thresh: 0.5
  IoU_eval_thresh: 0.5

mlflow:
  uri: ./experiments_outputs/mlruns

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

Model

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

  • model_path can be a local path or an http(s) URL.

  • The model must be a quantized TFLite model with the two outputs of a YOLOv8-type segmentation model: see Models.

Dataset

  • dataset_name must be coco_is, the dataset loader used to read the images.

  • prediction_path is the folder that contains the images to predict. Only .jpg, .jpeg, and .png files directly in this folder are used.

  • Provide the class names with classes_file_path, a text file with one class name per line (coco_classes.txt for COCO), or with class_names. One of the two is required.

Preprocessing

preprocessing is required and must be identical to the one used for the model. The pretrained models of the zoo use the values of the example.

  • rescaling applies out = scale * in + offset to the pixel values. With scale: 1/255.0 and offset: 0, the values are rescaled to the interval [0.0, 1.0].

  • resizing.aspect_ratio set to fit resizes the images to the model input size, and they are distorted if their aspect ratio differs. crop crops or pads the images to the input size instead. With fit, resizing.interpolation is required and can be bilinear, nearest, bicubic, area, lanczos3, lanczos5, gaussian, or mitchellcubic (see the TensorFlow documentation of tf.image.resize).

  • color_mode is rgb for the zoo models.

Postprocessing

  • confidence_thresh is the minimum confidence score for a detection to be kept. A higher value gives fewer detections.

  • IoU_eval_thresh is the IoU threshold of the non-maximum suppression (NMS) that removes overlapping boxes. A lower value gives fewer detections.

NMS_thresh and max_detection_boxes are not used by the prediction service. They are used by the deployment service.

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.

2. Launch the prediction

Run the following commands from the instance_segmentation/ folder. If you chose to modify user_config.yaml, run:

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

3. Visualize the results

The annotated images are saved with the name of the input image in the predictions folder of the experiment directory experiments_outputs/<date-and-time>. Images without any detection are skipped, and a message is printed.

experiments_outputs
├── mlruns                      MLflow files
└── <date-and-time>
    ├── predictions             annotated images
    ├── stm32ai_main.log
    └── .hydra                  Hydra files

To browse the MLflow runs, run mlflow ui from the experiments_outputs directory. See MLflow for more information.