Back to All
Project

ViT (Vision Transformer) Model Compilation and Integration on Qualcomm® Robotics RB5

In today's digital world, the demand for smart applications capable of analysing and understanding images is rapidly growing. At the forefront of this transformation is the Vision Transformer (ViT) model, an innovative approach to image classification. ViT models utilize a self-attention mechanism that allows them to capture global context and intricate patterns across an entire image.

Objective

Deploy the Vision Transformer (ViT) model on Qualcomm Robotics RB5 capable of classifying pictures from the ImageNet dataset.

 

 

Equipment Description
Qualcomm Robotics RB5 Qualcomm Robotics RB5 Development Kit
USB to Micro USB cable  For serial console interface , to view logs
USB to USB Type C cable For connecting the USB3.0 Type C port to the board and flashing images, adb and fastboot
Power adapter 12 V with 2500 mA required by the 96Boards specification

Introduction:

ViT models have demonstrated exceptional performance in a variety of domains. In autonomous driving, ViT models enhance the ability of vehicles to recognize and react to their surroundings with greater accuracy and speed. In the field of medical imaging, ViT models assist in the early detection and diagnosis of diseases by analysing complex medical images more effectively than traditional methods. Moreover, in security and surveillance, ViT models improve the accuracy of identifying and tracking objects or individuals in real-time.

  • Model Type: Image classification
  • Checkpoint: Trained on ImageNet
  • Input Resolution: 224x224 pixels
  • Number of Parameters: 86.6 million
  • Model Size: 330 MB

Vision Transformer (ViT) Qualcomm® Neural Network Model Inference on Qualcomm Robotics RB5

Prerequisites:

  1. Ubuntu 20.04
  2. Conda Environment
  3. Python3.8
  4. ONNX
  5. Qualcomm® AI Engine Direct SDK – Qualcomm Neural Network_SDK 2.19.2.240210
  6. Qualcomm Robotics RB5

 

Steps to Execute:

Step 1: Install Conda on Ubuntu and create py38 environment. Install the necessary packages.

Step 2: Install the required dependencies for Qualcomm Neural Network SDK. Check this link for Qualcomm Neural Network setup.

Audio

Step 3: Download ViT ONNX model from Qualcomm AI Hub website for Qualcomm Neural Network model conversion.

Model Conversion:

Step 1: Qualcomm Neural Network Op Package Generation.

This model has been trained with Gelu activation function, but Gelu Op support was not provided in QNN_SDK-2.19.2.240210. So we created custom op package for Gelu. Refer this link to create Gelu op package for CPU Runtime.

1. Custom Op XML Config Creation

$ qnn-op-package-generator -p 
<QNN_SDK_ROOT>/examples/QNN/OpPackageGenerator/GeluOpPackageCpu.xml -o 
<QNN_SDK_ROOT>/examples/QNN/OpPackageGenerator/Gelu 
#THIS CAN BE ANY PATH
Audio

2. The above command will create GeluOpPackage.

3. We need to write Gelu.cpp script & save it in src/ops folder.

Note:

a. Gelu.cpp script has been provided in the examples section for reference.

b. Below lines highlight the key additional components in the GELU package that are not present in the ReLU package.

a. REGISTER_PACKAGE_OP(Gelu)
b. extern "C" QNN_API Qnn_ErrorHandle_t 
    GeluOpPackageInterfaceProvider(QnnOpPackage_Interface_t* interface)
Audio

d. Compiling for CPU on aarch64-ubuntu, run the following commands

$ export CXX=<path-to-clang++>/clang++
$ export ANDROID_NDK_ROOT=<path-to-ndk-build>
$ cd ${QNN_SDK_ROOT}/examples/Gelu/GeluOpPackage && make aarch64-ubuntu

Note:

a. Write the script Makefile.aarch64-ubuntu if it is not provided with Qualcomm Neural Network-SDK for aarch64-ubuntu. Script has been provided in the examples section for reference.

b. Save it in ./OpPackageGenerator/Gelu/GeluOpPackage/makefiles/Makefile.aarch64-ubuntu. For more information refer this link

Audio
Audio
Audio

Step 2: Qualcomm Neural Network model conversion.

$ qnn-onnx-converter --input_network ./vit.onnx --output_path ./vit.cpp --op_package_config /{QNN_SDK_ROOT}/examples/Gelu/GeluOpPackage/config/GeluOpPackageCpu.xml

Note:

a. Since Gelu Op support was not provided in Qualcomm Neural Network_SDK-2.19.2.240210, we need to create GeluOpPackageCpu.xml file by taking Reference from ReluOpPackageCpu.xml file provided in  Qualcomm Neural Network_SDK-2.19.2.240210.

b. Reference script is provided in Examples section.

Audio
Audio

Step 3: Once the model is converted, we need to build with qnn-model-lib-generator for aarch64-ubuntu-gcc7.5 target.

$ qnn-model-lib-generator -c <MODEL_PATH>vit.cpp -b <MODEL_PATH>vit.bin -t aarch64-ubuntu-gcc7.5 -o <OUTPUT_DIR>/model_libs/
Audio
Audio

Note: After executing above script, libvit.so file will be saved in <OUTPUT_DIR>/model_libs/libvit.so

Deploying the ViT model on Qualcomm Robotics for CPU runtime.

Step 1: To connect with Qualcomm Robotics RB5, use either adb tool or ssh. Create directory in the home directory of Qualcomm Robotics RB5 board.

$ ssh root@<ip_address>
$ mkdir <dir_name>
$ exit

Step 2: Copy the AI model and Qualcomm Neural Network dependency file to the Qualcomm Robotics RB5 board from host system.

$ scp ${QNN_SDK_ROOT}/lib/aarch64-ubuntu-gcc7.5 root@<ip_address>:/<dir_name>
$ scp ${QNN_SDK_ROOT}/bin/x86-64-linux-clang/envsetup.sh root@<ip_address>:/<dir_name>
$ scp ${QNN_SDK_ROOT}/bin/aarch64-ubuntu-gcc7.5/qnn-net-run root@<ip_address>:/<dir_name>
$ scp ${QNN_SDK_ROOT}/lib/hexagon/unsigned/ root@<ip_address>:/<dir_name>
$ scp vit/model_libs/aarch64-ubuntu-gcc7.5/*.so root@<ip_address>:/<dir_name>
$ scp vit/images/*.raw root@<ip_address>:/<dir_name>
$ scp vit/image_list.txt root@<ip_address>:/<dir_name>
$ scp {QNN_SDK_ROOT}/examples/opt/QNN/OpPackageGenerator/Gelu/CPU/GeluOpPackage/libs/ aarch64-ubuntu-gcc7.5/libGeluOpPackage.so root@<ip_address>:/<dir_name/CPU/>
$ scp {QNN_SDK_ROOT}/examples/opt/QNN/OpPackageGenerator/Gelu/DSP/GeluOpPackage/build/libGeluOpPackage.so root@<ip_address>:/<dir_name/DSP/>

Step 3: Connecting to Qualcomm Robotics RB5.

$ ssh root@<ip_address>
$ cd <dir>
  • For CPU runtime specify the libQnnCpu.so as a backend to get output. Run the following commands.
$ qnn-net-run --backend ./libQnnCpu.so --model libMobileNet-v3-Large.so --input_list image.txt --op_packages ./libGeluOpPackage.so:GeluPackageInterfaceProvider --output_dir output/
Audio

Step 4: Checking Output on Qualcomm Robotics RB5.

  • To view results, run the following command.
$ python3 show_vit_classifications.py -i image_list.txt -o output/ 
-l imagenet_slim_labels.txt
Audio

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries ("Qualcomm"). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.

Project Authors
Capgemini
Pinisetti Vijaya GaneshAssociate l – Engineer
Ankitha MAssociate l – Engineer
Avdhesh ChouhanProfessional II

Sign up for the Developer Newsletter.

Get software and hardware tool resources to help optimize your development delivered to your inbox weekly.

Qualcomm relentlessly innovates to deliver intelligent computing everywhere, helping the world tackle some of its most important challenges. Our leading-edge AI, high performance, low-power computing, and unrivaled connectivity deliver proven solutions that transform major industries. At Qualcomm, we are engineering human progress.

Stay connected

Get the latest Qualcomm and industry information delivered to your inbox.

Subscribe
Manage your subscription

© Qualcomm Technologies, Inc. and/or its affiliated companies.

Snapdragon and Qualcomm branded products are products of Qualcomm Technologies, Inc. and/or its subsidiaries. Qualcomm patented technologies are licensed by Qualcomm Incorporated.

Note: Certain services and materials may require you to accept additional terms and conditions before accessing or using those items.

References to "Qualcomm" may mean Qualcomm Incorporated, or subsidiaries or business units within the Qualcomm corporate structure, as applicable.

Qualcomm Incorporated includes our licensing business, QTL, and the vast majority of our patent portfolio. Qualcomm Technologies, Inc., a subsidiary of Qualcomm Incorporated, operates, along with its subsidiaries, substantially all of our engineering, research and development functions, and substantially all of our products and services businesses, including our QCT semiconductor business.

Materials that are as of a specific date, including but not limited to press releases, presentations, blog posts and webcasts, may have been superseded by subsequent events or disclosures.

Nothing in these materials is an offer to sell or license any of the services or materials referenced herein.