SNPE开发[1]: GetStart
前言
硬件:创通联达以高通QCS610作为SOC的开发板TurboX C610
SDK:Qualcomm Neural Processing SDK for AI v1.59.0
docs:Reference Guide
骁龙神经处理引擎(SnapDragon Neural Processing Engine)是高通用于加速神经网络的运行时推理框架。
Setup
必备条件
- 目前SNPE SDK开发环境仅支持Ubuntu-18.04;
- 支持Caffe,Caffe2,ONNX,PyTorch,TensorFlow,TFLite;
- Python3.6
- Android NDK(可选):用于构建SDK附带的Cpp示例
- Android SDK(可选):用于构建 SDK附带的APK
安装docker-ubuntu18.04
docker pull ubuntu:18.04 |
安装python3.6
换apt-get源
cp /etc/apt/sources.list /etc/apt/sources.list.bak |
删除原内容,添加以下内容,保存
#阿里云源 |
更新apt-get并安装python3.6
apt-get update |
安装SDK
apt-get install unzip |
检查依赖python包安装情况,自行安装缺失的包:
source snpe-1.59.0.3230/bin/check_python_depends.sh |
这时发现python默认版本被指定为python2.7(可能是安装python-pip时安装了python2):
python --verison |
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3633000 Feb 27 2021 /usr/bin/python2.7
lrwxrwxrwx 1 root root 9 Oct 25 2018 /usr/bin/python3 -> python3.6
-rwxr-xr-x 2 root root 4526456 Dec 8 21:08 /usr/bin/python3.6
可以看到/usr/bin/python
软链接指向了python2.7
文件夹,可以手动删掉软链接并重新指定。这里尝试使用update-alternatives
来管理版本:
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 |
再次查看软链接:
ls /usr/bin/python -l |
lrwxrwxrwx 1 root root 24 Feb 18 06:26 /usr/bin/python -> /etc/alternatives/python
发现软链接指向了/etc/alternatives/python
,再看下这是个啥:
ls /etc/alternatives/python -l |
lrwxrwxrwx 1 root root 18 Feb 18 06:30 /etc/alternatives/python -> /usr/bin/python3.6
哦,update-alternatives
管理版本的机制就是增加了一个/etc/alternatives/python
软链接(中间层)来实现的。那这时pip
命令的版本怎么对应python版本呢?以pip -V
打印版本信息为准,实测当切到python3.6时,无论pip
,pip2
,pip3
都是对应python3.6的包管理,而切到python2.7时,pip
和pip2
都对应python2.7的包管理,pip3
对应python3.6的包管理。
升级pip版本,并换源:
pip install -U pip |
安装依赖包:
pip install numpy |
docker内adb连接
首先确保主机可以通过usb-adb连接到板子,这时就可以用界面投影工具scrcpy.exe进入的Android系统UI界面,在UI内操作连接与主机同一局域网的WIFI,并记下板子的IP地址10.0.49.7
。
在docker容器内尝试ping板子的IP:
ping 10.0.49.7 |
可以ping通的话,在主机端以TCP/IP方式重启ADB服务:
adb tcpip 5555 |
在docker内安装adb工具,并通过网络连接adb:
apt-get install android-tools-adb |
* daemon not running; starting now at tcp:5037
* daemon started successfully
connected to 10.0.49.7:5555···
List of devices attached
10.0.49.7:5555 device
安装Android NDK
拷贝下载好的android-ndk-r20b-linux-x86_64.zip
到docker中,解压到想要存放的位置:
unzip android-ndk-r20b-linux-x86_64.zip |
配置环境变量
配置SNPE SDK和Android NDK的环境变量:
vim ~/.bashrc |
在文件最后添加如下内容:
# SNPE SDK |
执行source ~/.bashrc
使其生效
安装Caffe并配置环境
apt-get install caffe-cpu |
安装ONNX并配置环境
pip install onnx |
编译示例程序
编译运行在ARM Android上的示例程序
cd $SNPE_ROOT/examples/NativeCpp/SampleCode |
这会编译armeabi-v7a和arm64-v8a两个版本的可执行文件,分别为:
- $SNPE_ROOT/examples/NativeCpp/SampleCode/obj/local/armeabi-v7a/snpe-sample
- $SNPE_ROOT/examples/NativeCpp/SampleCode/obj/local/arm64-v8a/snpe-sample
执行示例程序
拷贝可执行程序及依赖库到板子上:
export SNPE_TARGET_ARCH=aarch64-android-clang6.0 |
准备alexnet模型和数据:
cd $SNPE_ROOT/models/alexnet/scripts |
发现下载不动,只好手动下载:
下载完拷贝到$SNPE_ROOT/models/alexnet/tmpdir
文件夹下,然后执行:
python setup_alexnet.py -a ../tmpdir |
拷贝模型及数据到板子上:
cd $SNPE_ROOT/models/alexnet |
配置板子上的环境变量:
adb shell |
执行程序:
cd /data/local/tmp/alexnet |
结果pull到本地(docker):
cd $SNPE_ROOT/models/alexnet/ |
调用python脚本分析结果:
python scripts/show_alexnet_classifications.py -i data/target_raw_list.txt \ |
会有以下结果输出:
Classification results
cropped/trash_bin.raw 0.950433 412 ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin
cropped/chairs.raw 0.363969 831 studio couch, day bed
cropped/notice_sign.raw 0.666179 458 brass, memorial tablet, plaque
cropped/plastic_cup.raw 0.720699 647 measuring cup
查看模型信息
snpe-dlc-info -i xxx.dlc |
SNPE SDK跑ONNX模型
- 执行如下代码,判断环境是否配置正确:
snpe-onnx-to-dlc -h |
- 下载VGG模型:
cd $SNPE_ROOT/models/VGG/onnx |
- 下载测试图片和标签文件:
cd $SNPE_ROOT/models/VGG/data |
- 对图片进行预处理(1. resize到
256x256
;2. 中心裁切到224x224
;3. 归一化;4. 保持为raw文件):
cd $SNPE_ROOT/models/VGG/ |
- 模型转换:
cd $SNPE_ROOT/models/VGG |
有如下打印:
WARNING - WARNING_GEMM: GEMM operation is not supported in the general case, attempting to interpret as FC
WARNING - WARNING_GEMM: GEMM operation is not supported in the general case, attempting to interpret as FC
WARNING - WARNING_GEMM: GEMM operation is not supported in the general case, attempting to interpret as FC
INFO - INFO_DLC_SAVE_LOCATION: Saving model at vgg16.dlc
INFO - INFO_CONVERSION_SUCCESS: Conversion completed successfully
与alexnet的例子类似,上述步骤2~5也可以用封装好的脚本$SNPE_ROOT/models/VGG/scripts/setup_VGG.py
完成。
- 查看模型信息:
snpe-dlc-info -i vgg16.dlc |
- 模型推理:
cd $SNPE_ROOT/models/VGG |
- 查看推理结果:
cd $SNPE_ROOT/models/VGG/ |
有如下打印:
Classification results
probability=0.351833 ; class=n02123045 tabby, tabby cat
probability=0.315166 ; class=n02123159 tiger cat
probability=0.313086 ; class=n02124075 Egyptian cat
probability=0.012995 ; class=n02127052 lynx, catamount
probability=0.003528 ; class=n02129604 tiger, Panthera tigris