高通骁龙snpe 编译配置运行

文章目录

  • 硬件准备
  • 安装
    • 1 编译caffe
      • 准备
      • 修改编译选项
      • 编译运行
    • 2 安装python3.5+tensorflow
      • 安装miniconda
      • 更换conda源
      • 安装python3.5虚拟环境
    • 3 配置snpe的环境
  • 运行
    • 1. 导出cld模型
    • 2 基准测试
  • caffe错误总结(愿所有的坑因填坑人而平坦)
      • **错误信息**:`gflags/gflags.h: No such file or directory`
      • **错误信息**:`glog.h: No such file or directory`
      • **错误信息**:`cublas_v2.h: No such file or directory`
      • **错误信息**:fatal error: cblas.h: No such file or directory
      • **错误信息**: lmdb.h no such file or directory
      • **错误信息** hdf5.h: No such file or directory
      • 错误 :`undefined reference to `cv::imread(cv::String const&, int)'`
      • 错误 cannot find -lopencv_imgcodecs
      • **错误信息** :找不到 #include

转载请注明出处:https://blog.csdn.net/qq_27262241/article/details/110930325
I’ll make a Snpe Docker Image to fuck the shit compile process.

硬件准备

  • 笔记本电脑: 安装ubuntu16.04
  • 手机:搭载高通骁龙处理器
  • usb线

安装

不踩坑指南: 要先装caffe, 再装python虚拟环境 最后装 TensorFlow,否则protocol会冲突

1 编译caffe

参考链接

准备

#下载
git clone https://github.com/BVLC/caffe.git
# 安装依赖
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev

还要安装:


apt-get install libgflags-dev 
sudo apt-get install libgoogle-glog-dev

修改编译选项

cd <caffe目录>
cp Makefile.config.example Makefile.config

在caffe目录下,修改 Makefile.config

#给INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include添加   /usr/include/hdf5/serial
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial

#给 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 添加  /usr/lib/x86_64-linux-gnu/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial


#中把 CPU_ONLY=1行取消注释。
CPU_ONLY=1

修改Makefile

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
# 改为
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

编译运行

make all # 这一步骤会出现很多错误,参考文末,caffe错误总结
make test 
make runtest
make distribute
make pycaffe

2 安装python3.5+tensorflow

安装miniconda

先下载 ubuntu64位 https://repo.anaconda.com/miniconda/Miniconda2-latest-Linux-x86_64.sh

然后运行 bash *.sh

更换conda源

更换清华源

show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

安装python3.5虚拟环境

conda create -n py35 python=3.5

激活虚拟环境

conda activate 
conda deactivate 

安装tensorflow

# 先激活虚拟环境
conda install tensorflow

3 配置snpe的环境

SDK 下载链接

https://developer.qualcomm.com/software/qualcomm-neural-processing-sdk

#解压
unzip -X snpe-X.Y.Z.zip
# 配置环境变量
source snpe-X.Y.Z/bin/dependencies.sh

# 检查python依赖项是否齐全,如有错误,请使用conda install *进行安装
source snpe-X.Y.Z/bin/check_python_depends.sh

设置环境变量

# NDK
export ANDROID_NDK_ROOT=<path_to_ndk>
# 指向Caffe 编译目录
source bin/envsetup.sh -c <CAFFE_DIR>
# 指向tensorflow conda虚拟环境所在的目录即可。
source bin/envsetup.sh -t <TENSORFLOW_DIR>

运行

1. 导出cld模型

首先下载 inception v3模型

inception_v3_2016_08_28_frozen.pb.tar.gz 
- https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz

转换模型

python $SNPE_ROOT/models/inception_v3/scripts/setup_inceptionv3.py -a ~/tmpdir
#参数解释
#	-a Assets:模型目录
#	-d 加上 -d表示当前还没下载模型,需要运行时下载
#	-r runtime,运行平台: cpu、gpu、dsp、aip、all

转换后在./dlc目录下获得 dlc模型

2 基准测试

首先配置运行文件 config.json

{
    "Name":"",
    "HostRootPath": "<运行结果保存在哪个Path to copy result on host>",
    "HostResultsDir":"",
    "DevicePath":"",
    "Devices":[""],
    "Runs":<No. of runs运行次数>,

    "Model": {
        "Name": "",
        "Dlc": "",
        "UDO": ""
        "Data": [
            ""
        ],
        "InputList": ""
        "RandomInput": <Pass number no. of random images eg: 10
                        NOTE: If using this option, then "DATA" and "InputList" should not be passed in config file>
    },

    "Runtimes":[<list of runtimes supported runtimes: "CPU","GPU","GPU_s","GPU_FP16","DSP","AIP","AIP_ACT16">],
    "Measurements": [<Measurement type: "timing","mem">],
    "CpuFallback": <Set to true to enable CPU fallback>,
    "UBFloatOutput": <Set to true to override the userbuffer output used for inference, and the output type is float>,
    "PerfProfile": <Choose performance profile from the following options: balanced, default, sustained_high_performance, high_performance, power_saver, system_settings>,
    "ProfilingLevel": <Choose profiling level from the following options: basic, moderate, detailed, off>,
    "BufferTypes": <Choose Buffer Types from the following options: float, ub_float, ub_tf8, ub_tf16. All buffer types except ub_tf16 are considered by default, if BufferTypes is not specified.
                    NOTE: ub_tf8 is only supported on DSP,AIP,AIP_ACT16 runtime and for other runtimes it gets ignored,
                          ub_tf16 is only supported on AIP,AIP_ACT16 runtime and for other runtimes it gets ignored.
                          For AIP_ACT16 runtime, [float, ub_float, ub_tf16] are considered by default if BufferTypes is not specified.>
}

然后运行

# 先要设置 环境变量 SNPE NDK tensorflow
export ANDROID_NDK_ROOT=/opt/android-ndk-r17c
cd /mnt/c/Users/sc1812/Documents/ov_workspace/snpe_ws/snpe-1.43.0.2307/
source bin/envsetup.sh -c ~/snpe_ws/caffe
source bin/envsetup.sh -t /home/sc/miniconda2/envs/py35


cd $SNPE_ROOT/benchmarks
python3 snpe_bench.py -c alexnet_sample.json

WSL系统配置adb,在~/.bashrc中添加

alias adb='adb.exe'
alias fastboot='fastboot.exe'

caffe错误总结(愿所有的坑因填坑人而平坦)

错误信息gflags/gflags.h: No such file or directory

安装:

apt-get install libgflags-dev
来自  

错误信息glog.h: No such file or directory

glog:

sudo apt-get install libgoogle-glog-dev

错误信息cublas_v2.h: No such file or directory

在MakeFile.config中把 CPU_ONLY=1行取消注释。

错误信息:fatal error: cblas.h: No such file or directory

sudo apt-get install libopenblas-dev

错误信息: lmdb.h no such file or directory

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

错误信息 hdf5.h: No such file or directory

修改 Makefile.config

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
改为
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

修改 Makefile

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改为
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

错误 :undefined reference tocv::imread(cv::String const&, int)’`

错误信息:

CXX/LD -o .build_release/tools/caffe.bin
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'

解决方法
在 Makefile.config中取消OPENCV_VERSION=3这一行的注释, 确保你的系统是opencv3

#取消注释,设opencv3
OPENCV_VERSION=3

I think you may be using OpenCV 3. You need to set the OPENCV_VERSION variable in Makefile.config.
来自 https://github.com/BVLC/caffe/issues/3700

错误 cannot find -lopencv_imgcodecs

注释掉 Makefile.config中的

#OPENCV_VERSION =3 

这一行。和前面的错误不一样,因为前面我的系统装的是opencv3又换了个电脑装的是opencv2

错误信息 :找不到 #include

sudo apt-get install python-numpy

参考

资源汇总

官方文档:https://developer.qualcomm.com/docs/snpe/android_tutorial.html

CSDN文档:

https://bss.csdn.net/m/zone/qualcomm2016/resource_detail?id=1592

基准测试:

https://developer.qualcomm.com/docs/snpe/benchmarking.html

视频教程

https://edu.csdn.net/course/play/28096?spm=1002.2001.3001.4157

SNPE量化加速

https://zhuanlan.zhihu.com/p/74326567

SDK 下载链接

https://developer.qualcomm.com/software/qualcomm-neural-processing-sdk

你可能感兴趣的:(Android打怪升级之路,深度学习)