[TOC]

概述

QNN的SDK下载

环境搭建

平台依赖

QNN SDK所验证的主机操作系统是Ubuntu 18.04 LTS(Bionic)。

QNN SDK支持持python3

1
2
$ sudo apt-get update
$ sudo apt-get install python3=3.6.9

深度学习框架

This QNN release is verified to work with versions of the ML training frameworks specified below:

编译工具链

QNN SDK允许用户可以使用,以用于不同的后端,如CPU、GPU、HTP和DSP。你可能需要安装适当的交叉编译工具链,以便为特定的后端编译此类包。

相关命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 安装abnconda版本2020.07
conda create -n qnn_env python=3.6.9 创建python环境
conda activate qnn_env


# sdk 目录下
cd qnn-v1.7.2.211027051327_28940/target/x86_64-linux-clang/bin/

# 将QNN SDK的路径添加到环境变量
vim ~/.bashrc
# >>>>> 设置QNN环境变量<<<<<<
export QNN_ROOT=/home/frewen/DevTools/qnn-v1.12.0/target/x86_64-linux-clang/bin
# <<<<< 设置QNN的环境变量 <<<<<<


source ${QNN_ROOT}/check-python-dependency.sh
source ${QNN_ROOT}/check-linux-dependency.sh

# 安装
# 安装固定版本的tensorflow()
conda install tensorflow=1.15.0
pip3 install tensorflow==2.3.0 -i https://pypi.douban.com/simple
pip3 install tflite==2.3.0
# 验证是否安装成功
python -c "import tensorflow"

conda install caffe
python -c "import caffe"

conda install -c conda-forge onnx=1.6.0

pip3 install torch==1.8.1
python -c "import torch"



# download package from github
git clone --recursive https://github.com/apache/incubator-tvm <your_path>
cd <your_path>
git checkout 0f4c0654ef94c2252d0075e
git submodule update --init
conda build --output-folder=conda/pkg conda/recipe
conda install tvm -c ./conda/pkg
conda install -y cmake boost libboost boost-cpp glog gflags protobuf hdfs lmdb libopencv scikit-image


git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd
cp Makefile.config.example Makefile.config // 使用服务器上的/home/baiduiov/tool/QNN/caffe/Makefile.config
make -j8
# Make sure to include $CAFFE_ROOT/python to your PYTHONPATH.
#可以参考 /home/baiduiov/tool/QNN/qnn-v1.7.2.211027051327_28940/target/x86_64-linux-clang/bin/source.sh
make py

source ${QNN_SDK_ROOT}/target/x86_64-linux-clang/bin/envsetup.sh -t &lt;your_tensorflow_installation_path&gt;
for example:/home/baiduiov/anaconda3/pkgs/tensorflow-base-1.15.0-mkl_py36he1670d9_0
source ${QNN_SDK_ROOT}/target/x86_64-linux-clang/bin/envsetup.sh -c &lt;your_caffe_installation_path&gt;
for example:/home/baiduiov/tool/QNN/caffe/python ///ssd caffe
source ${QNN_SDK_ROOT}/target/x86_64-linux-clang/bin/envsetup.sh -o &lt;your_onnx_installation_path&gt;
for example:/home/baiduiov/anaconda3/pkgs/onnx-1.6.0-py36h830a2c2_101

for example:
${QNN_SDK_ROOT}/target/x86_64-linux-clang/bin/qnn-caffe-converter --input_network FaceDetection0428V3Main.prototxt --caffe_bin FaceDetection0428V3Main.caffemodel ----input_list image.txt --output_path FaceDetection0428V3Main.cpp
2021-12-02 19:01:25,593 - 214 - INFO - Skipping quantization, no input_list provided
2021-12-02 19:01:25,594 - 214 - INFO - Saving QNN Model...
2021-12-02 19:01:25,601 - 214 - INFO - Model CPP saved at: FaceDetection0428V3Main.cpp
2021-12-02 19:01:25,601 - 214 - INFO - Model BIN saved at: /home/baiduiov/tool/QNN/qnn-v1.7.2.211027051327_28940/target/x86_64-linux-clang/bin/FaceDetection0428V3Main.bin
2021-12-02 19:01:25,606 - 214 - INFO - Model Network JSON saved at: /home/baiduiov/tool/QNN/qnn-v1.7.2.211027051327_28940/target/x86_64-linux-clang/bin/FaceDetection0428V3Main_net.json
2021-12-02 19:01:25,606 - 214 - INFO - Conversion complete!

当运行:${QNN_SDK_ROOT}/target/x86_64-linux-clang/bin/qnn-model-lib-generator -c FaceDetection0428V3Main.cpp -b FaceDetection0428V3Main.bin -o ./ -t aarch64-qnx
如果出现:/bin/sh: 1: aarch64-unknown-nto-qnx7.1.0-g++: not found
source ~/tool/sdp_toolchain/sdp/qnx710/qnxsdp-env.sh
再运行上述指令

${QNN_SDK_ROOT}/target/x86_64-linux-clang/bin/qnn-net-run \
--backend ${QNN_SDK_ROOT}/target/x86_64-linux-clang/lib/libQnnCpu.so \
--model ${QNN_SDK_ROOT}/examples/Models/InceptionV3/model_libs/x86_64-linux-clang/FaceDetection0428V3Main.so \
--input_list data/cropped/raw_list.txt

初始化ONNX环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## 初始化python环境
conda create -n py36 python=3.6.9

# 启用python的环境
conda activate py36

#安装其他的依赖
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple (numpy-1.19.5)
# 安装onnx
# Prerequisites
# numpy >= 1.16.6
# protobuf >= 3.12.2
# typing-extensions >= 3.6.2.1
# https://onnxruntime.ai/
pip install protobuf -i https://pypi.tuna.tsinghua.edu.cn/simple (protobuf-3.19.6)
pip install onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxruntime -i https://pypi.tuna.tsinghua.edu.cn/simple ( flatbuffers-23.5.26 onnxruntime-1.10.0)
# 安装opencv
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install opencv-python==4.2.0.32 -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install opencv-python==4.8.0.74 -i https://pypi.tuna.tsinghua.edu.cn/simple

1
2
3
4
5
6
7
8
9
(qnn_env) frewen@freweniubuntu:~$ pip install tflite==2.3.0
Collecting tflite==2.3.0
Downloading tflite-2.3.0-py2.py3-none-any.whl (79 kB)
|████████████████████████████████| 79 kB 248 kB/s
Collecting flatbuffers
Using cached flatbuffers-2.0-py2.py3-none-any.whl (26 kB)
Requirement already satisfied: numpy in ./DevTools/anaconda3/envs/qnn_env/lib/python3.6/site-packages (from tflite==2.3.0) (1.16.5)
Installing collected packages: flatbuffers, tflite
Successfully installed flatbuffers-2.0 tflite-2.3.0

问题解决

1
2
3
4
5
6
7
8
9
(qnn_env) frewen@freweniubuntu:~$ source ${QNN_ROOT}/check-linux-dependency.sh
Checking for clang-9:
dpkg-query: 没有找到与 clang-9 相匹配的软件包
clang-9 Found: /usr/local/clang-9.0.0 . Added to PATH variable
Checking for flatbuffers-compiler:
Checking for libflatbuffers-dev: install ok installed
Checking for rename: install ok installed
=============================================================
All Dependency Packages Found

解决方案:https://blog.csdn.net/qiuchangyong/article/details/107658007

1
2
3
# 命令:
sudo apt install clang
# 安装后会在/usr/bin目录下同时存在clang和clang++

ModuleNotFoundError: No module named ‘packaging’

pip install packaging

ModuleNotFoundError: No module named ‘yaml’

pip install pyyaml

No module named ‘pandas’

pip install pandas

1
2
3
4
5
6
mkdir -p obj/x86_64-linux-clang
clang++ -c -std=c++11 -march=x86-64 -O3 -fno-exceptions -fno-rtti -Wno-write-strings -DQNN_API="__attribute__((visibility(\"default\")))" -fPIC -fvisibility=hidden -Ijni/ -I/home/baiduiov/work/inference/qnn/qnn-v2.5.2.230714074102_42159-auto/include jni/DMS20230726.cpp -o obj/x86_64-linux-clang/DMS20230726.o
/bin/sh: 1: clang++: not found
Makefile.linux-x86_64:97: recipe for target 'obj/x86_64-linux-clang/DMS20230726.o' failed
make: *** [obj/x86_64-linux-clang/DMS20230726.o] Error 127

1
2
3
4
解决方法是:

sudo apt-get update
sudo apt-get install clang

前端依赖库

高通QNX系统中集成的SO

1
2
3
4
5
6
7
8
9
10
11
12
13
# find / -name "*Qnn*"  
/ifs/lib64/libQnnCpuFusa.so.1.3
/ifs/lib64/libQnnCpuFusa.so
find: Can't get stat. (/dev/spi3): No such process
find: Cannot recurse into '/dev/sem' - filesystem forms an infinite loop
/mnt/lib64/libQnnCpu.so
/mnt/lib64/libQnnHtp.so
/mnt/lib64/libQnnHtpNetRunExtensions.so
/mnt/lib64/libQnnHtpProfilingReader.so
/mnt/lib64/libQnnHtpV68Stub.so
/mnt/lib64/libQnnSaver.so
/mnt/etc/images/cdsp0/libQnnHtpV68Skel.so
/mnt/etc/images/cdsp1/libQnnHtpV68Skel.so