Registered User mNo edit summary Tag: 2017 source edit |
Registered User mNo edit summary Tag: 2017 source edit |
||
Line 25: | Line 25: | ||
===Install from the OpenSTLinux AI package repository=== | ===Install from the OpenSTLinux AI package repository=== | ||
{{Warning|{{SoftwareLicenseAgreement | distribution=X-LINUX-AI}}}} | {{Warning|{{SoftwareLicenseAgreement | distribution=X-LINUX-AI}}}} | ||
After having [[X-LINUX-AI_Starter_package# | After having [[X-LINUX-AI_Starter_package#Install from the OpenSTLinux AI package repository|configured the AI OpenSTLinux package]] you can install X-LINUX-AI components for image classification application: | ||
<div class="mw-collapsible mw-expanded"> | <div class="mw-collapsible mw-expanded"> | ||
Line 223: | Line 223: | ||
===Install all image classification packages=== | ===Install all image classification packages=== | ||
{{Warning|{{SoftwareLicenseAgreement | distribution=X-LINUX-AI}}}} | {{Warning|{{SoftwareLicenseAgreement | distribution=X-LINUX-AI}}}} | ||
After having [[X-LINUX-AI_Starter_package# | After having [[X-LINUX-AI_Starter_package#Install_the_X-LINUX-AI_tool|configured the AI OpenSTLinux package]] you can install directly all the X-LINUX-AI packages related to image classification by using the following command:: | ||
{{Board$}} x-linux-ai -i stai-mpu-image-classification-* | {{Board$}} x-linux-ai -i stai-mpu-image-classification-* | ||
Latest revision as of 10:31, 20 August 2024
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
The image classification neural network model allows identification of the subject represented by an image. It classifies an image into various classes.
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]
![]() |
On STM32MP2 series' boards ![]() |
2. Installation
2.1. Install from the OpenSTLinux AI package repository
After having configured the AI OpenSTLinux package you can install X-LINUX-AI components for image classification application:
2.1.2. Install on STM32MP1x board
2.2. Source code location
- 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:
2.3. Regenerate the package from OpenSTLinux Distribution (optional)
Using the OpenSTLinux Distribution, you are able to rebuild the application.
![]() |
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.shBe careful to select the right board in the script to setup the correct environment
- Rebuild the application on STM32MP1x:
For more information about building the application for STM32MP1x, please expand this section.
3. How to use the application
3.1. Launching via the demo launcher
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.
3.2. Executing with the command line
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:
4. Testing with MobileNet
4.1. Test with MobileNetV2 on STM32MP2x
The model used for testing is the mobilenet_v2_1.0_224_int8_per_tensor.nb
![]() |
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
4.2. Test with MobileNetV1 on STM32MP1x
5. Going further
5.1. Install all image classification packages
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
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.
5.3. Launching via the command line interface (CLI)
5.3.1. CLI on STM32MP2x
The model used for testing is the MobilenetV2.
![]() |
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