Last edited 2 weeks ago

Image classification

Applicable for STM32MP13x lines, STM32MP15x lines, STM32MP25x lines

This article explains how to use the stai_mpu API for image classification applications supporting OpenVX [1], TensorFlow Lite[2], ONNX [3] or Coral [4] back-ends.

1. Description[edit source]

The image classification neural network model allows identification of the subject represented by an image. It classifies an image into various classes.

Image classification application

The application demonstrates a computer vision use case for image classification where frames are grabbed from a camera input (/dev/videox) and analyzed by a neural network model interpreted by OpenVX, TFLite or ONNX framework.
A Gstreamer pipeline is used to stream camera frames (using v4l2src), to display a preview (using gtkwaylandsink) and to execute neural network inference (using appsink).
The result of the inference is displayed in the preview. The overlay is done using GtkWidget with cairo.

The models used with this application are the MobileNet v1 downloaded from the TensorFlow Lite Hub[5] and the MobileNet v2 downloaded from the ST model zoo[6]

Info white.png Information
On STM32MP2 series' boards More info green.png the MobileNet v2 used for this application, is a TensorFlow Lite per-tensor asymmetric quantized model, which is accelerated using the neural processing unit (NPU). Then, the model has been converted to NBG format using ST Edge AI tool. For more information about this tool, refer to the dedicated article.

2. Installation[edit source]

2.1. Install from the OpenSTLinux AI package repository[edit source]

Warning white.png Warning
The software package is provided AS IS, and by downloading it, you agree to be bound to the terms of the software license agreement (SLA0048). The detailed content licenses can be found here.

After having configured the AI OpenSTLinux package you can install X-LINUX-AI components for image classification application:

2.1.1. Install on STM32MP2x board[edit source]

The OpenVX application will be installed to take advantage of the neural processing unit (NPU) and graphics processing unit (GPU).

  • To install this application, please use the following command:
 x-linux-ai -i stai-mpu-image-classification-cpp-ovx
Warning DB.png Important
You can install the Python version of this application by installing this package: stai-mpu-image-classification-python-ovx


  • Then, restart the demo launcher:
 systemctl restart weston-graphical-session.service

2.1.2. Install on STM32MP1x board[edit source]

The TFLite application will be installed with the XNNPACK delegate to accelerate the neural network inference on the CPU.


  • To install this application, please use the following command:
 x-linux-ai -i stai-mpu-image-classification-cpp-tflite
Warning DB.png Important
You can install the Python version of this application by installing this package: stai-mpu-image-classification-python-tflite


  • Then, restart the demo launcher:
 systemctl restart weston-graphical-session.service

2.2. Source code location[edit source]

  • in the OpenSTLinux Distribution with X-LINUX-AI Expansion Package:
<Distribution Package installation directory>/layers/meta-st/meta-st-x-linux-ai/recipes-samples/image-classification/files/stai_mpu
  • on GitHub:
recipes-samples/image-classification/files/stai_mpu

2.3. Regenerate the package from OpenSTLinux Distribution (optional)[edit source]

Using the OpenSTLinux Distribution, you are able to rebuild the application.

Info white.png Information
If not already installed, the X-LINUX-AI OpenSTLinux Distribution need to be installed by following this link


  • Set up the build environment:
 cd <Distribution Package installation directory>
 source layers/meta-st/scripts/envsetup.sh
Warning.png Be careful to select the right board in the script to setup the correct environment
  • Rebuild the application on STM32MP2x:
 bitbake stai-mpu-image-classification-cpp-ovx -c compile

The generated binary is available here:

<Distribution Package installation directory>/<build directory>/tmp-glibc/work/cortexa35-ostl-linux/stai-mpu-image-classification-cpp/5.0.0-r0/stai-mpu-image-classification-cpp-5.0.0/stai_mpu
Warning DB.png Important
You can generate the Python version of this application: stai-mpu-image-classification-python-ovx


  • Rebuild the application on STM32MP1x:

For more information about building the application for STM32MP1x, please expand this section.

 bitbake stai-mpu-image-classification-cpp-tflite -c compile

The generated binary is available here:

<Distribution Package installation directory>/<build directory>/tmp-glibc/work/cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi/stai-mpu-image-classification-cpp/5.0.0-r0/stai-mpu-image-classification-cpp-5.0.0/stai_mpu
Warning DB.png Important
You can generate the Python version of this application: stai-mpu-image-classification-python-tflite

3. How to use the application[edit source]

3.1. Launching via the demo launcher[edit source]

You can click on the icon to run either the C/C++ or Python application. By default the OpenVX applications will be installed on STM32MP2x, and the TFLite applications will be installed on STM32MP1x.

Demo launcher

3.2. Executing with the command line[edit source]

The image classification C++ & Python applications are located in the userfs partition:

/usr/local/x-linux-ai/image-classification/stai_mpu_image_classification
/usr/local/x-linux-ai/image-classification/stai_mpu_image_classification.py

It accepts the following input parameters:

  • In C/C++ application:
 
Usage: stai_mpu_image_classification -m <model .tflite> -l <label .txt file>

-m --model_file <.tflite file path>:  .tflite model to be executed
-l --label_file <label file path>:    name of file containing labels
-i --image <directory path>:          image directory with image to be classified
-v --video_device <n>:                video device is automatically detected but can be set (example video0)
--frame_width  <val>:                 width of the camera frame (default is 640)
--frame_height <val>:                 height of the camera frame (default is 480)
--framerate <val>:                    framerate of the camera (default is 15fps)
--input_mean <val>:                   model input mean (default is 127.5)
--input_std  <val>:                   model input standard deviation (default is 127.5)
--dual_camera_pipeline                Use dual camera post-processing pipeline, one dedicated for the NN and the other dedicated for the display 
--verbose:                            enable verbose mode
--validation:                         enable the validation mode
--val_run:                            set the number of draws in the validation mode
--help:                               show this help
  • In Python application:
usage: stai_mpu_image_classification.py [-h] [-i IMAGE] [-v VIDEO_DEVICE]
                                        [--frame_width FRAME_WIDTH]
                                        [--frame_height FRAME_HEIGHT] [--framerate FRAMERATE]
                                        [-m MODEL_FILE] [-l LABEL_FILE]
                                        [--input_mean INPUT_MEAN] [--input_std INPUT_STD]
                                        [--validation] [--val_run VAL_RUN]
                                        [--num_threads NUM_THREADS]

options:
  -h, --help            show this help message and exit
  -i IMAGE, --image IMAGE
                        image directory with image to be classified
  -v VIDEO_DEVICE, --video_device VIDEO_DEVICE
                        video device ex: video0
  --frame_width FRAME_WIDTH
                        width of the camera frame (default is 640)
  --frame_height FRAME_HEIGHT
                        height of the camera frame (default is 480)
  --framerate FRAMERATE
                        framerate of the camera (default is 15fps)
  -m MODEL_FILE, --model_file MODEL_FILE
                        .tflite model to be executed
  -l LABEL_FILE, --label_file LABEL_FILE
                        name of file containing labels
  --input_mean INPUT_MEAN
                        input mean
  --input_std INPUT_STD
                        input standard deviation
  --validation          enable the validation mode
  --val_run VAL_RUN     set the number of draws in the validation mode
  --num_threads NUM_THREADS
                        Select the number of threads used by tflite interpreter to run inference

4. Testing with MobileNet[edit source]

4.1. Test with MobileNetV2 on STM32MP2x[edit source]

The model used for testing is the mobilenet_v2_1.0_224_int8_per_tensor.nb

Info white.png Information
The different objects the neural network is able to classify are listed in the labels.txt file located in the target:

/usr/local/x-linux-ai/image-classification/models/mobilenet/labels_imagenet_2012.txt

To ease launching of the application, two shell scripts are available for both C/C++ and Python applications on the board:

  • For C/C++ application:
    • launch image classification based on camera frame inputs:
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification.sh
    • launch image classification based on the pictures located in /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification_testdata.sh
  • For Python application:
    • launch image classification based on camera frame inputs:
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification.sh
    • launch image classification based on the pictures located in /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification_testdata.sh
Warning DB.png Important
Note that you need to populate the testdata directory with your own data sets. The pictures are then randomly read from the testdata directory

4.2. Test with MobileNetV1 on STM32MP1x[edit source]

The model used for testing is the mobilenet_v1_0.5_128_quant.tflite downloaded from Tensorflow Lite Hub[5].

Info white.png Information
The different objects the neural network is able to classify are listed in the labels.txt file located in the target:

/usr/local/x-linux-ai/image-classification/models/mobilenet/labels_imagenet.txt

To ease launching of the application, two shell scripts are available for both C/C++ and Python applications on the board:

  • For C/C++ application:
    • launch image classification based on camera frame inputs:
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification.sh
    • launch image classification based on the pictures located in /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification_testdata.sh
  • For Python application:
    • launch image classification based on camera frame inputs:
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification.sh
    • launch image classification based on the pictures located in /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification_testdata.sh
Warning DB.png Important
Note that you need to populate the testdata directory with your own data sets. The pictures are then randomly read from the testdata directory

5. Going further[edit source]

5.1. Install all image classification packages[edit source]

Warning white.png Warning
The software package is provided AS IS, and by downloading it, you agree to be bound to the terms of the software license agreement (SLA0048). The detailed content licenses can be found here.

After having configured the AI OpenSTLinux package you can install directly all the X-LINUX-AI packages related to image classification by using the following command::

 x-linux-ai -i stai-mpu-image-classification-*

Then restart the demo launcher:

 systemctl restart weston-graphical-session.service

5.2. Launching via the demo launcher[edit source]

Now, it's possible to launch all the image classification application on your board by clicking on it in the demo launcher. The Python & C/C++ application are now available using TFLite, ONNX and Coral backend. Moreover, on STM32MP2x the OpenVX backend is also available.

Demo launcher

5.3. Launching via the command line interface (CLI)[edit source]

5.3.1. CLI on STM32MP2x[edit source]

The model used for testing is the MobilenetV2.

Info white.png Information
The different objects the neural network is able to classify are listed in the labels.txt file located in the target:

/usr/local/x-linux-ai/image-classification/models/mobilenet/labels_imagenet_2012.txt

The two shell scripts described before offers the possibility to select the framework directly between TFlite, ONNX, OpenVX and Coral. To be able to run the application using all supported frameworks, the models for each frameworks must be available in the /usr/local/x-linux-ai/image-classification/models/mobilenet/ directory. Then, you will need to specify the framework as an argument of the launch scripts as follow.

  • For C/C++ application:
    • Run image classification based on camera input with the chosen framework. Available framework options are: tflite, onnx, nbg, coral.
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification.sh nbg
    • Run image classification based on picture located in the /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory with the chosen framework. Available framework options are: tflite, onnx, nbg, coral.
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification_testdata.sh nbg
  • For Python application:
    • Run image classification based on camera input with the chosen framework. Available framework options are: tflite, onnx, nbg, coral.
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification.sh nbg
    • Run image classification based on picture located in the /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory with the chosen framework. Available framework options are: tflite, onnx, nbg, coral.
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification_testdata.sh nbg
Warning DB.png Important
Note that you need to populate the testdata directory with your own data sets. The pictures are then randomly read from the testdata directory

5.3.2. CLI on STM32MP1x[edit source]

The model used for testing is the MobilenetV1.

Info white.png Information
The different objects the neural network is able to classify are listed in the labels.txt file located in the target:

/usr/local/x-linux-ai/image-classification/models/mobilenet/labels_imagenet.txt

The two shell scripts described before offers the possibility to select the framework directly between TFlite, ONNX and Coral. To be able to run the application using all supported frameworks, the models for each frameworks must be available in the /usr/local/x-linux-ai/image-classification/models/mobilenet/ directory. Then, you will need to specify the framework as an argument of the launch scripts as follow.

  • For C/C++ application:
    • Run image classification based on camera input with the chosen framework. Available framework options are: tflite, onnx, coral.
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification.sh tflite
    • Run image classification based on picture located in the /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory with the chosen framework. Available framework options are: tflite, onnx, coral.
    /usr/local/x-linux-ai/image-classification/launch_bin_image_classification_testdata.sh tflite
  • For Python application:
    • Run image classification based on camera input with the chosen framework. Available framework options are: tflite, onnx, coral.
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification.sh tflite
    • Run image classification based on picture located in the /usr/local/demo-ai/image-classification/models/mobilenet/testdata directory with the chosen framework. Available framework options are: tflite, onnx, coral.
    /usr/local/x-linux-ai/image-classification/launch_python_image_classification_testdata.sh tflite

6. References[edit source]