Getting Started

This guide demonstrates how to get started with the Qualcomm® Snapdragon™ Neural Processing Engine (NPE) SDK. Starting from a clean Ubuntu installation, this tutorial is designed to provide you with the steps to install the dependencies, setup the SDK tools, download and prepare some example neural network models, and finally build the example Android APP that you can use for your solutions that use artificial intelligence (AI).

Longer form documentation about this process is available in the SDK documentation contained in the  /doc/html folder; open index.html to begin learning.

Legal Disclaimer:Your use of this guide, including but not limited to the instructions, steps, and commands below, is subject to the Website Terms of Use and may contain references to third party or open source software that is not distributed by Qualcomm Technologies, Inc. or its affiliates. You are solely responsible for the installation, configuration, and use of any such third party or open source software and compliance with any applicable licenses. 

System Requirements

We recommended performing the following operations on a dedicated machine, to better understand the SDK dependencies:

  1. Install Ubuntu 14.04 (recommended), for example on a virtual machine.
  2. Install the latest Android Studio.
  3. Install the latest Android SDK, from Android Studio or stand-alone.
  4. Install the latest Android NDK, from the Android Studio SDK Manager or stand-alone.
  5. Install Caffe (installation instructions, git revision d8f79537 recommended with this SDK).
  6. # this will build Caffe (and the pycaffe bindings) from source - see the official instructions for more information

    sudo apt-get install cmake git libatlas-base-dev libboost-all-dev libgflags-dev libgoogle-glog-dev libhdf5-serial-dev libleveldb-dev liblmdb-dev libopencv-dev libprotobuf-dev libsnappy-dev protobuf-compiler python-dev python-numpy

    git clone https://github.com/BVLC/caffe.git ~/caffe; cd ~/caffe; git reset --hard d8f79537

    mkdir build; cd build; cmake ..; make all -j4; make install

  7. Optional: install TensorFlow (installation instructions, version 1.0 recommended).
  8. # this will download and install TensorFlow in a virtual environment - see the official instructions for more information

    sudo apt-get install python-pip python-dev python-virtualenv

    mkdir ~/tensorflow; virtualenv --system-site-packages ~/tensorflow; source ~/tensorflow/bin/activate

    pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.0-cp27-none-linux_x86_64.whl

Setup the SDK

This step allows the Snapdragon NPE SDK to communicate to the Caffe and Tensorflow frameworks via the python APIs. To setup the SDK on Ubuntu 14.04, proceed as follows:

  1. Make sure you have installed the Android NDK, Caffe (here assumed in ~/caffe) and optionally TensorFlow (here assumed in ~/tensorflow) before proceeding.
  2. Download the latest Snapdragon NPE SDK.
  3. Unpack the .zip file to an appropriate location (here assumed in the ~/snpe-sdk folder).
  4. Install missing system packages:
  5. # install a few more SDK dependencies, then perform a comprehensive check

    sudo apt-get install python-dev python-matplotlib python-numpy python-protobuf python-scipy python-skimage python-sphinx wget zip

    source ~/snpe-sdk/bin/dependencies.sh # verifies that all dependencies are installed

    source ~/snpe-sdk/bin/check_python_depends.sh # verifies that the python dependencies are installed

  6. Initialize Snapdragon NPE SDK environment on the current console; in the future, repeat this for every new console:
  7. # initialize the environment on the current console

    cd ~/snpe-sdk/

    export ANDROID_NDK_ROOT=~/Android/Sdk/ndk-bundle # default location for Android Studio, replace with yours

    source ./bin/envsetup.sh -c ~/caffe

    source ./bin/envsetup.sh -t ~/tensorflow # optional for this guide

The initialization will set or update $SNPE_ROOT, $PATH, $LD_LIBRARY_PATH, $PYTHONPATH, $CAFFE_HOME, $TENSORFLOW_HOME, and in addition it will copy the Android NDK libgnustl_shared.so library locally, and update the Android AAR archive.

Download the ML Models and convert them to .DLC

The Snapdragon NPE SDK does not bundle model files which are publicly available, but contains scripts to download some popular models and convert them to the Deep Learning Container ("DLC") format. Scripts are located in the /models folder, which will also contains the DLC models.

  • Download and convert a pre-trained Alexnet example in Caffe format:
  • cd $SNPE_ROOT
    python ./models/alexnet/scripts/setup_alexnet.py -a ./temp-assets-cache -d

    Tip: 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 models conversion.

  • Optional: download and convert a pre-trained "inception_v3" example in Tensorflow format:
  • cd $SNPE_ROOT
    python ./models/inception_v3/scripts/setup_inceptionv3.py -a ./temp-assets-cache -d

    Tip: take a look at the setup_inceptionv3.py script, which also performs quantization on the model, for a 75% size reduction (91MB → 23MB).

Build the Example Android APP

The Android APP 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.

  1. Prepare the APP by copying the runtime and the model
  2. cd $SNPE_ROOT/examples/android/image-classifiers
    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

  3. Option A: build the Android APK from Android studio:
    1. Launch Android Studio.
    2. Open the project in the ~/snpe-sdk/examples/android/image-classifiers folder.
    3. Accept the Android Studio suggestions to upgrade build system components, if offered.
    4. Press the "Run app" button to build and run the APK.
  4. Option B: build the Android APK from the Command line:
  5. sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1
    libbz2-1.0:i386 # Android SDK build dependencies on ubuntu
    ./gradlew assembleDebug # build the APK

    The command above will likely require ANDROID_HOME and JAVA_HOME to be set to location of the Android SDK and the JRE/JDK in your system.

What's Next?

Congratulations, you just made you first App using the Snapdragon NPE SDK. It's time to get started creating your own AI solutions!

App using the Snapdragon NPE SDK

The source code of the example Android APP demonstrates how to use the SDK correctly. ClassifyImageTask.java is a good starting point to start learning. API documentation, tutorials, and architectural details are available in the documentation bundled with the SDK. Point your browser to /doc/html/index.html to start learning. Answers to frequent questions can be found in the Forums, where you can also talk "APIs" with our experts.