上一篇:ncnn填坑记录一:安装protobuf、cmake和opencv
下一篇:ncnn填坑记录三:在pc端运行examples
1.下载ncnn代码
github上下载太慢,可换码云下载
git clone https://github.com/Tencent/ncnn.git
或
git clone https://gitee.com/Tencent/ncnn.git
2.安装依赖项
git
g++
cmake
protocol buffer (protobuf) headers files and protobuf compiler
vulkan header files and loader library
glslang
(optional) opencv # For building examples
建议提前安装好,不然后面很容易报错。
3.安装
cd ncnn
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_VULKAN=ON -DNCNN_SYSTEM_GLSLANG=ON -DNCNN_BUILD_EXAMPLES=ON -DOpenCV_DIR=/usr/share/OpenCV .. # 开始没加cv地址
make -j$(nproc)
4.报错
1.cmake
时,会报错
-- Target arch: x86
CMake Warning at CMakeLists.txt:193 (message):
GLSLANG_TARGET_DIR must be defined! NCNN_SYSTEM_GLSLANG will be turned off.
CMake Error at CMakeLists.txt:218 (message):
The submodules were not downloaded! Please update submodules with "git
submodule update --init" and try again.
-- Configuring incomplete, errors occurred!
See also "/home/lgy/tools/ncnn/build/CMakeFiles/CMakeOutput.log".
根据提示操作即可
git submodule update --init
后来才发现官方教程里面,下载完成后就有这一步。
2.未安装Vulkan时报错
(base) lgy@lgy:~$ wget https://sdk.lunarg.com/sdk/download/1.2.154.0/linux/vulkansdk-linux-x86_64-1.2.154.0.tar.gz?Human=true -O vulkansdk-linux-x86_64-1.2.154.0.tar.gz
--2021-05-13 15:12:12-- https://sdk.lunarg.com/sdk/download/1.2.154.0/linux/vulkansdk-linux-x86_64-1.2.154.0.tar.gz?Human=true
正在解析主机 sdk.lunarg.com (sdk.lunarg.com)... 104.26.1.199, 172.67.73.90, 104.26.0.199, ...
正在连接 sdk.lunarg.com (sdk.lunarg.com)|104.26.1.199|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 404 Not Found
2021-05-13 15:12:14 错误 404:Not Found。
cmake时将-DNCNN_VULKAN=ON
改为OFF,正常编译完成,后续可以使用ncnn在pc上推理。
但不建议这样做,因为后面在android上使用时,官方代码默认的有GPU,不会改代码的话,可能会造成一些奇奇怪怪的错误。
安装Vulkan
下载地址:https://vulkan.lunarg.com/sdk/home
export VULKAN_SDK=/home/lgy/tools/vulkan-1.2.176.1
编译前不执行这一步的话,后面也会提示你执行的。
3.cmake时未指定opencv地址报错
(base) lgy@lgy:~/tools/ncnn/build$ cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_VULKAN=ON -DNCNN_SYSTEM_GLSLANG=ON -DNCNN_BUILD_EXAMPLES=ON ..
-- CMAKE_INSTALL_PREFIX = /home/lgy/tools/ncnn/build/install
-- NCNN_VERSION_STRING = 1.0.20210526
-- Target arch: x86
CMake Warning at CMakeLists.txt:197 (message):
GLSLANG_TARGET_DIR must be defined! NCNN_SYSTEM_GLSLANG will be turned off.
-- OpenCV library: /home/lgy/anaconda3
-- version: 3.4.2
-- libraries: opencv_core;opencv_highgui;opencv_imgproc;opencv_imgcodecs;opencv_videoio
-- include path: /home/lgy/anaconda3/include;/home/lgy/anaconda3/include/opencv
CMake Warning at /usr/local/share/cmake-3.19/Modules/FindProtobuf.cmake:502 (message):
Protobuf compiler version 3.14.0 doesn't match library version 3.17.0
Call Stack (most recent call first):
tools/caffe/CMakeLists.txt:2 (find_package)
CMake Warning at /usr/local/share/cmake-3.19/Modules/FindProtobuf.cmake:502 (message):
Protobuf compiler version 3.14.0 doesn't match library version 3.17.0
Call Stack (most recent call first):
tools/onnx/CMakeLists.txt:2 (find_package)
-- Configuring done
CMake Warning at examples/CMakeLists.txt:2 (add_executable):
Cannot generate a safe runtime search path for target mobilenetssd because
files in some directories may conflict with libraries in implicit
directories:
runtime library [libgomp.so.1] in /usr/lib/gcc/x86_64-linux-gnu/5 may be hidden by files in:
/home/lgy/anaconda3/lib
Some of these libraries may not be found correctly.
Call Stack (most recent call first):
examples/CMakeLists.txt:47 (ncnn_add_example)
针对libgomp.so.1
没有在/usr/lib/gcc/x86_64-linux-gnu/5
中找到,反而在/home/lgy/anaconda3/lib
找到,网上查了不少资料,建议改PATH地址,然而改了并没用,最后直接修改/home/lgy/anaconda3/lib
下该文件名,等编译完成后再改回来。
针对Protobuf compiler version 3.14.0 doesn't match library version 3.17.0
,从github上下载3.17版本protoc,然后安装好后
(base) lgy@lgy:~/tools/protobuf$ protoc --version
libprotoc 3.14.0
发现本地有多个protoc版本,而系统默认的不是安装的版本
(base) lgy@lgy:~/tools/protobuf$ whereis protoc
protoc: /usr/bin/protoc /usr/local/bin/protoc /home/lgy/anaconda3/bin/protoc /usr/share/man/man1/protoc.1.gz
(base) lgy@lgy:~/tools/protobuf$ which protoc
/home/lgy/anaconda3/bin/protoc
除了系统默认版本是在anaconda3下,linux自带版本是在/usr/bin下,而安装版本默认在/usr/local/bin下,解决方法为,将/usr/local/bin/protoc复制到/home/lgy/anaconda3/bin/文件夹内,原来的可以改个名备份。
cmake时调用了anaconda的opencv库,会导致后面推理时报错
cv2.error: OpenCV(3.4.2) /tmp/build/80754af9/opencv-suite_1535558553474/work/modules/highgui/src/window.cpp:632: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
事实上cmake时已经提示了
-- OpenCV library: /home/lgy/anaconda3
-- version: 3.4.2
-- libraries: opencv_core;opencv_highgui;opencv_imgproc;opencv_imgcodecs;opencv_videoio
-- include path: /home/lgy/anaconda3/include;/home/lgy/anaconda3/include/opencv
显然应该调用我们安装的opencv库才对,导致我以为opencv编译出了问题,卸载安装好几次,也没解决问题,后来还用了apt安装opencv
sudo apt-get install libopencv-dev python-opencv # 建议还是编译安装,apt安装的版本太低了
解决方法为,cmake时指定opencv地址
cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_VULKAN=ON -DNCNN_SYSTEM_GLSLANG=ON -DNCNN_BUILD_EXAMPLES=ON -DOpenCV_DIR=/usr/share/OpenCV ..