The Qualcomm® Neural Processing SDK for artificial intelligence (AI) (the SDK) allows developers to convert neural network models trained in Caffe/Caffe2, ONNX, or TensorFlow and run them optimally on Snapdragon® mobile platforms.
Below, we show you how to set up the SDK and use it to build your first working app.
The following dependencies are required to use the SDK:
Set up the Qualcomm Neural Processing SDK for AI
This step allows the SDK to work with the Caffe, TensorFlow, ONNX, and TensorFlow Lite frameworks via Python APIs. Follow the steps below to set up the SDK on Ubuntu 18.04:
- Download the latest version of the SDK.
- Unpack the SDK’s .zip file to an appropriate location (e.g., ~/snpe-sdk).
- Install the SDK’s dependencies:
sudo apt-get install python3-dev python3-matplotlib python3-numpy python3-protobuf python3-scipy python3-skimage python3-sphinx wget zip
- Verify that all dependencies are installed:
- Verify that the Python dependencies are installed:
- Initialize the Qualcomm Neural Processing SDK environment:
Note: set ANDROID_NDK_ROOT to the location of your Android Studio installation.
- Set up your SDK’s environment based on your chosen ML framework:
Environment Setup for Caffe
Navigate to $SNPE_ROOT and run the following script to set up the SDK’s environment for Caffe:
source bin/envsetup.sh -c $CAFFE_DIRwhere $CAFFE_DIR is the path to the Caffe installation.
The script may show warnings about a mismatched Caffe SHA. The SHA that the script checks for is the SHA that the SDK has been tested with. It is ok to use a different version of Caffe, including a version built from source, so this warning can be ignored.
Environment Setup for Caffe2
Navigate to $SNPE_ROOT and run the following script to set up the SDK’s environment for Caffe2:
source bin/envsetup.sh -f $CAFFE2_DIRwhere $CAFFE2_DIR is the path to the Caffe2 installation.
Environment Setup for TensorFlow
Navigate to $SNPE_ROOT and run the following script to set up the SDK’s environment for TensorFlow:
source bin/envsetup.sh -t $TENSORFLOW_DIRwhere $TENSORFLOW_DIR is the path to the TensorFlow installation.
Environment Setup for ONNX
Navigate to $SNPE_ROOT and run the following script to set up the SDK’s environment for ONNX:
source bin/envsetup.sh -o $ONNX_DIRwhere $ONNX_DIR is the path to the ONNX installation.
Environment Setup for TensorFlow Lite
Navigate to $SNPE_ROOT and run the following script to set up the SDK’s environment for TensorFlow Lite:
The initialization will do the following:
- Update $SNPE_ROOT, $PATH, $LD_LIBRARY_PATH, $PYTHONPATH, $CAFFE_DIR, $CAFFE2_DIR, $TENSORFLOW_DIR, or $ONNX_DIR
- Copy the Android NDK libgnustl_shared.so library locally
- Update the Android AAR archive
Download ML Models and Convert them to .dlc Format
The Qualcomm Neural Processing SDK for AI does not bundle any model files but contains scripts to download some popular publicly-available models and convert them to Qualcomm Technoloiges’ proprietary Deep Learning Container (.dlc) format. Follow the steps below to use these scripts:
- Run the following commands to download and convert a pre-trained AlexNet example in Caffe format:
cd $SNPE_ROOTTip: take a look at the setup_alexnet.py script, which performs the conversion to DLC. You will likely perform the same operations for your Caffe model conversion.
python ./models/alexnet/scripts/setup_alexnet.py -a ./temp-assets-cache -d
- Run the following commands to download and convert a pre-trained inception_v3 example in TensorFlow format:
cd $SNPE_ROOTTip: take a look at the setup_inceptionv3.py script, which also performs quantization on the model to reduce its size by almost 75% (91MB down to 23MB).
python ./models/inception_v3/scripts/setup_inceptionv3.py -a ./temp-assets-cache -d
Build the SDK’s Example Android App
The Qualcomm Neural Processing SDK for AI includes an example Android app that combines the Snapdragon NPE runtime (provided by the /android/snpe-release.aar Android library) and the DLC model generated by the Caffe AlexNet example described above.
Follow the steps below to build this example app:
- Run the following commands to prepare the app by copying the runtime and the model:
cp ../../../android/snpe-release.aar ./app/libs # copies the NPE runtime library
bash ./setup_models.sh # packages the Alexnet example (DLC, labels, imputs) as an Android resource file
- Choose one of the following options to build the APK:
Option A: Build the APK in Android Studio:
- Launch Android Studio.
- Open the project in ~/snpe-sdk/examples/android/image-classifiers.
- Accept the Android Studio suggestions to upgrade the build system components, if offered.
- Click Run app button to build and run the APK.
Option B: Execute the following command to build the APK from the command line:
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386
Note: The command above will likely require ANDROID_HOME and JAVA_HOME to be set to the location of the Android SDK and the JRE/JDK in your system.
Snapdragon and Qualcomm Neural Processing SDK are products of Qualcomm Technologies, Inc. and/or its subsidiaries.