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, andmodel_precision. See the Models guide.model_nameandmodel_pathcannot be used together.model_pathcan be a local path or anhttp(s)URL.The model must be a quantized TFLite model with the two outputs of a YOLOv8-type segmentation model: see Models.
Dataset ¶
dataset_namemust becoco_is, the dataset loader used to read the images.prediction_pathis the folder that contains the images to predict. Only.jpg,.jpeg, and.pngfiles 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 withclass_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.
rescalingappliesout = scale * in + offsetto the pixel values. Withscale: 1/255.0andoffset: 0, the values are rescaled to the interval [0.0, 1.0].resizing.aspect_ratioset tofitresizes the images to the model input size, and they are distorted if their aspect ratio differs.cropcrops or pads the images to the input size instead. Withfit,resizing.interpolationis required and can bebilinear,nearest,bicubic,area,lanczos3,lanczos5,gaussian, ormitchellcubic(see the TensorFlow documentation oftf.image.resize).color_modeisrgbfor the zoo models.
Postprocessing ¶
confidence_threshis the minimum confidence score for a detection to be kept. A higher value gives fewer detections.IoU_eval_threshis 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.