SNPE 是 Qualcomm Snapdragon Neural Processing Engine 的简称。
- SNPE 是神经网络在骁龙平台上推理的开发套件,方便开发者在使用高通芯片的设备上加速AI应用。
支持的模型框架:TensorFlow, CAFFE, ONNX, TensorFlowLite
可选择的硬件:CPU,GPU,DSP,HTA,HTP
- SNPE的下载地址在: https://developer.qualcomm.com/software/qualcomm-neural-processing-sdk/tools一个月更新一版,目前最新的版本是Qualcomm Neural Processing SDK for AI v1.59.0 ,注册之后就可以下载了。
这里我用SNPE1.52.0做演示。
安装SNPE
我用一个Ubuntu:18.04 的docker container来开始。
docker run -it -v "/local/mnt/workspace2":"/workspace" --name=tutor --entrypoint /bin/bash ubuntu:18.04
需要Python3.6
root@3e28cb421090:/workspace/tutor# unzip snpe-1.52.0.zip
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# tree -L 2
.
|-- LICENSE.pdf
|-- NOTICE.txt
|-- ReleaseNotes.txt
|-- android
|-- benchmarks
|-- bin
|-- doc
|-- examples
|-- include
|-- lib
|-- models
`-- share
安装过程可以参考文档 <workspace>/snpe-1.52.0.2724/doc/html/setup.html
(1)Python3 设置,如果默认是python3.6 可以跳过此步。
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# apt-get install python3
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# update-alternatives --list python
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# asudo update-alternatives --config python
# 选择2
(2)检测安装系统包
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# source bin/dependencies.sh
如果以上自动检测安装出错,那么需要安装 'python3-dev', 'wget', 'zip','libc++-9-dev'
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# apt-get install python3-dev
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# apt-get install wget
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# apt-get install zip
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# apt-get install libc++-9-dev
(3)检测和安装python依赖包, 需要首先安装pip3
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# apt-get install python-pip3 root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# source bin/check_python_depends.sh
如果以上自动检测和安装出错,那么需要安装 'numpy','sphinx','scipy','matplotlib','scikit-image','protobuf','pyyaml','mako'
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# pip install numpy sphinx scipy matplotlib scikit-image protobuf pyyaml mako
(4)配置环境
这里我们配置SNPE+Tensorflow的环境。
先不需要NDK,所以可以忽略ANDROID_NDK_ROOT,后续需要编译Native C++ Example的时候再设置。
这里主要配置的是SNPE_ROOT,TENSORFLOW_HOME,PATH, LD_LIBRARY_PATH, 和PYTHONPATH
SNPE支持TF1.x 和2.3,如果发现出问题,可能与tf版本有关系,这里我安装tf2.3
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# python -m pip install --upgrade pip
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# pip install tensorflow==2.3.0
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# pip show -f tensorflow | grep Location
Location: /usr/local/lib/python3.6/dist-packages
设置 TensorFlow SNPE 环境
#source bin/envsetup.sh -t $TENSORFLOW_DIR
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# source bin/envsetup.sh -t /usr/local/lib/python3.6/dist-packages/tensorflow/
测试是否配置成功,如果打印出help,则完成配置。
root@3e28cb421090:/workspace/tutor/snpe-1.52.0.2724# snpe-tensorflow-to-dlc -h
使用SNPE 推理 InceptionV3
可以参考<workspace>/snpe-1.52.0.2724/doc/html/tutorial_setup.html
(1)下载 inception_v3,解压。
root@3e28cb421090:/workspace/tutor/inceptionv3# wget https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz
root@3e28cb421090:/workspace/tutor/inceptionv3# tar -zxvf inception_v3_2016_08_28_frozen.pb.tar.gz
root@3e28cb421090:/workspace/tutor/inceptionv3# mv inception_v3_2016_08_28_frozen.pb inception_v3.pb
(2)将inception_v3.pb转化为SNPE支持的DLC模型格式 inception_v3.dlc。
root@3e28cb421090:/workspace/tutor/inceptionv3# snpe-tensorflow-to-dlc -i inception_v3.pb -d input 1,299,299,3 --out_node InceptionV3/Predictions/Reshape_1
2021-08-20 10:21:56,330 - 188 - INFO - INFO_ALL_BUILDING_NETWORK:
==============================================================
Building Network
==============================================================
2021-08-20 10:21:57,445 - 188 - INFO - Resolving static sub-graphs in network...
2021-08-20 10:21:57,563 - 188 - INFO - Resolving static sub-graphs in network, complete.
2021-08-20 10:21:57,963 - 188 - INFO - INFO_DLC_SAVE_LOCATION: Saving model at /workspace/tutor/inceptionv3/inception_v3.dlc
2021-08-20 10:21:58,697 - 188 - INFO - INFO_CONVERSION_SUCCESS: Conversion completed successfully
root@3e28cb421090:/workspace/tutor/inceptionv3# ls -al
total 273516
drwxr-xr-x 2 root root 4096 Aug 20 10:21 .
drwxr-xr-x 4 461960 200 4096 Aug 20 10:12 ..
-rw-r--r-- 1 root root 95639760 Aug 20 10:21 inception_v3.dlc
-rw-r----- 1 270850 5000 95746802 Jan 18 2017 inception_v3.pb
(3)准备input数据和验证脚本
SNPE 提供了inception_v3的测试数据的生成脚本: <workspace>/snpe-1.52.0.2724/doc/html/tutorial_setup.html
Getting Inception v3
运行之后会在<workspace>snpe-1.52.0.2724\models\inception_v3 下面生成数据,我们copy到我们的目录下
# 生成数据
root@3e28cb421090:/workspace/tutor/inceptionv3# python3 $SNPE_ROOT/models/inception_v3/scripts/setup_inceptionv3.py -a . -d
# copy 数据到当前测试目录
root@3e28cb421090:/workspace/tutor/inceptionv3# cp $SNPE_ROOT/models/inception_v3/data . -r
# copy 后处理脚本
root@3e28cb421090:/workspace/tutor/inceptionv3# cp $SNPE_ROOT/models/inception_v3/scripts/show_inceptionv3_classifications.py .
root@3e28cb421090:/workspace/tutor/inceptionv3# ls -al
total 273524
drwxr-xr-x 3 root root 4096 Aug 20 10:34 .
drwxr-xr-x 4 461960 200 4096 Aug 20 10:12 ..
drwxr-xr-x 3 root root 4096 Aug 20 10:33 data
-rw-r----- 1 270850 5000 10479 Jan 17 2017 imagenet_slim_labels.txt
-rw-r--r-- 1 root root 95639760 Aug 20 10:21 inception_v3.dlc
-rw-r----- 1 270850 5000 95746802 Jan 18 2017 inception_v3.pb
-rw-r--r-- 1 root root 88668554 Jan 18 2017 inception_v3_2016_08_28_frozen.pb.tar.gz
-rw-r----- 1 root root 3376 Aug 20 10:34 show_inceptionv3_classifications.py
(4)在当前Ubuntu 环境下用SNPE推理 inception_v3.
root@3e28cb421090:/workspace/tutor/inceptionv3# snpe-net-run --version
SNPE v1.52.0.2724
root@3e28cb421090:/workspace/tutor/inceptionv3# snpe-net-run --container inception_v3.dlc --input_list data/cropped/raw_list.txt
-------------------------------------------------------------------------------
Model String: N/A
SNPE v1.52.0.2724
-------------------------------------------------------------------------------
Processing DNN input(s):
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/notice_sign.raw
Processing DNN input(s):
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/trash_bin.raw
Processing DNN input(s):
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/plastic_cup.raw
Processing DNN input(s):
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/chairs.raw
# 输出在output目录
root@3e28cb421090:/workspace/tutor/inceptionv3# ls output -la
total 72
drwxr-xr-x 6 root root 4096 Aug 20 10:38 .
drwxr-xr-x 4 root root 4096 Aug 20 10:38 ..
drwxr-xr-x 3 root root 4096 Aug 20 10:38 Result_0
drwxr-xr-x 3 root root 4096 Aug 20 10:38 Result_1
drwxr-xr-x 3 root root 4096 Aug 20 10:38 Result_2
drwxr-xr-x 3 root root 4096 Aug 20 10:38 Result_3
lrwxrwxrwx 1 root root 14 Aug 20 10:38 SNPEDiag.log -> SNPEDiag_0.log
-rw-r--r-- 1 root root 46536 Aug 20 10:38 SNPEDiag_0.log
(5)查看推理结果
root@3e28cb421090:/workspace/tutor/inceptionv3# python show_inceptionv3_classifications.py -i data/cropped/raw_list.txt -o output/ -l imagenet_slim_labels.txt
Classification results
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/notice_sign.raw 0.167454 459 brass
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/trash_bin.raw 0.684572 413 ashcan
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/plastic_cup.raw 0.990612 648 measuring cup
/workspace/tutor/snpe-1.52.0.2724/models/inception_v3/data/cropped/chairs.raw 0.382222 832 studio couch
到此为止,我们就完成了
- SNPE的安装
- snpe-tensorflow-to-dlc 模型转化工具的使用
- snpe-net-run 在Ubuntu环境下的inceptionv3模型是推理
参考:
<path_to_snpe_sdk>/snpe-1.52.0.2724/doc/html/index.html
作者:Wenhao