Image Classification Prediction ¶
Our prediction service is a simple and efficient tool that allows users to upload their TensorFlow Lite (.tflite) or Keras (.keras) model and a set of images for prediction. The service then uses the model to predict the class of each image in the set. This can be particularly useful for anyone working with image classification tasks and looking for a quick and easy way to generate predictions. Our prediction service is designed to be user-friendly and accessible, making it an ideal solution for both beginners and experts alike.
1. Configure the YAML file ¶
To use the prediction service, users must fill in the
prediction
section of the
user_config.yaml
file like the
prediction_config.yaml
or as shown in the example below.
Select the model of your choice after having compared the model accuracy and performances on various targets:
ST Edge AI Model Explorer.
Download model files package which contains example YAML that can help filling following sections in particular
model
section.
general:
model:
model_name: <model-name>
input_shape: <model-input-shape>
pretrained: True
pretrained_dataset_name: <pretraining-dataset>
model_precision: <model-precision> example : w8a8_ptq
#model_path: <path-to-a-Keras-or-TFlite-model-file> # Path to the model to use to make predictions
operation_mode: prediction
dataset:
dataset_name: tf_flowers
class_names: [daisy, dandelion, roses, sunflowers, tulips] # Names of the classes
prediction_path: ./datasets/flower_predict # Path to the directory containing the images to predict
preprocessing:
rescaling:
scale: 1/127.5
offset: -1
resizing:
interpolation: nearest
aspect_ratio: False
color_mode: rgb
mlflow:
uri: ./experiments_outputs/mlruns
hydra:
run:
dir: ./experiments_outputs/${now:%Y_%m_%d_%H_%M_%S}
In the
model
section, users can provide the path to their model file using the
model_path
attribute. This can be either a Keras model file with a ‘.keras’ filename extension (float model), a ‘.onnx’ model or a TensorFlow Lite model file with a ‘.tflite’ filename extension (quantized model). Otherwise users must define the parameter in the
model
section as above. Please also refer to
models
for more details on supported models and
model
section parameters.
The
dataset
section requires users to provide the names of the classes using the
class_names
or the
classes_file_path
the attribute pointing to a text file containing the class names, as there is no dataset available to infer them.
prediction_path
is the path to the directory containing the images to predict must also be provided here. Check
datasets
to have more details on the supported datasets.
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. Hereafter, an example is provided for the N6:
prediction:
profile: default
input_type: uint8 # int8 / uint8 / float32
output_type: int8 # int8 / uint8 / float32
input_chpos: chlast # chlast / chfirst
output_chpos: chlast # chlast / chfirst
target: stedgeai_n6 # host, stedgeai_host, stedgeai_n6
Finally, once all of these sections have been filled in, users can run the prediction service to generate predictions for their set of images.
Hydra and MLflow Settings ¶
The
mlflow
and
hydra
sections must always be present in the YAML configuration file. The
hydra
section can be used to specify the name of the directory where experiment directories are saved and/or the pattern used to name experiment directories. With the YAML code below, every time you run the Model Zoo, an experiment directory is created that contains all the directories and files created during the run. The names of experiment directories are all unique as they are based on the date and time of the run.
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. Launch the Prediction ¶
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 prediction_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 prediction_config.yaml