- Last edited 3 months ago ago
X-LINUX-AI - image classification using TensorFlow Lite C++ API
This article explains how to experiment with TensorFlow Lite applications for image classification based on MobileNet v1 model using the TensorFLow Lite C++ API.
- 1 Description
- 2 Installation
- 3 How to use the application
- 4 References
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 the TensorFlow Lite framework.
A Gstreamer pipeline is used to stream camera frames (using v4l2src), to display a preview (using waylandsink) 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.
This combination is quite simple and efficient in terms of CPU overhead.
The model used with this application is the MobileNet v1 downloaded from the Tensorflow Lite Hub.
2.1 Install from the OpenSTLinux AI package repository
After having configured the AI OpenSTLinux package you can install X-LINUX-AI components for this application:
apt-get install tflite-cv-apps-image-classification-c++
Then restart the demo launcher:
systemctl restart weston-launch
2.2 Source code location
- in the Openembedded OpenSTLinux Distribution with X-LINUX-AI Expansion Package:
- <Distribution Package installation directory>/layers/meta-st/meta-st-stm32mpu-ai/recipes-samples/tflite-cv-apps/files/image-classification/src
- on GitHub:
2.3 Regenerate the package from OpenSTLinux Distribution (optional)
Using the Openembedded OpenSTLinux Distribution, you are able to rebuild the application.
- Set up the build environment:
cd <Distribution Package installation directory> source layers/meta-st/scripts/envsetup.sh
- Rebuild the application:
bitbake tflite-cv-apps-image-classification-c++ -c compile
The generated binary is available here:
<Distribution Package installation directory>/<build directory>/tmp-glibc/work/cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi/tflite-cv-apps-image-classification-c++/3.0.0-r0/tflite-cv-apps-image-classification-c++-3.0.0/image-classification/src
3 How to use the application
3.1 Launching via the demo launcher
3.2 Executing with the command line
The label_tfl_gst_gtk C/C++ application is located in the userfs partition:
It accepts the following input parameters:
Usage: ./label_tfl_gst_gtk -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) --crop: if set, the nn input image is cropped (with the expected nn aspect ratio) before being resized, else the nn input image is only resized to the nn input size (could cause picture deformation). --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) --verbose: enable verbose mode --validation: enable the validation mode -t --threshold <val>: threshold of accuracy above which the boxes are displayed (default 0.60) --help: show this help
3.3 Testing with MobileNet V1
The model used for testing is the mobilenet_v1_0.5_128_quant.tflite downloaded from Tensorflow Lite Hub
To ease launching of the application, two shell scripts are available:
- launch image classification based on camera frame inputs
- launch image classification based on the pictures located in /usr/local/demo-ai/computer-vision/models/mobilenet/testdata directory