Ubuntu14.04—tencent NCNN运行环境搭建

 

操作系统:Ubuntu 14.04 LTS版本,OpenCV 2.4.10

在Ubuntu14.04上安装NCNN

1、准备工作

1.1 首先安装protobuf

         protobuf是一种轻便高效的结构化数据存储格式。

1.1.1、下载protobuf代码

git clone  https://github.com/protocolbuffers/protobuf

或者:git clone https://github.com/google/protobuf 

说明:

尽量最好不要在github上面下载。原因是,github上面下载的没有configure文件。此时需要先在该文件夹内运行./autogen.sh命令。该命令会从无法访问的网站下载文件,从而导致protobuf文件下面的源文件不完整,进而导致make或者make install无法通过!(如果电脑能fq,就没事。否则就没法下载成功。因而会出现一系列问题,编译报错)。
推荐下载(亲测有效):

https://github.com/protocolbuffers/protobuf/releases/

我下载的是下面的C++的版本:

 

tar  xvf   protobuf-cpp-3.6.1.tar.gz

解压后得到文件目录protobuf-3.6.1

cd   protobuf-3.6.1

---------------------------------------

1.1.2、安装protobuf

如何卸载老版本的protobuf ,参考如下:

http://blog.csdn.net/ahbbshenfeng/article/details/52065676

开始安装,步骤如下:

       sudo apt-get install libprotobuf-dev protobuf-compiler

       sudo apt-get install  autoconf automake libtool curl    

       ./autogen.sh 

说明:若protobuf目录下已经有了configure文件,就不用运行./autogen.sh。

  ./configure --prefix=/usr/local/protobuf

  make

  make check

      sudo make install

 

----------------------------------------------

若编译报错,你可能需要下载或安装如下文件:

1.1.2.1 下载安装google test 
 sudo apt-get install libgtest-dev 
这样会自动把googtest的头文件安装到/usr/include/gtest目录下,而源文件在/usr/src/gtest目录下。

把下载的gtest源代码下面的include/gtest目录拷贝到全局头文件目录,如:

sudo cp  /usr/include/gtest/*   /usr/local/include/  -r

1.1.2.2 下载gmock

gmock由于网络的原因无法下载,需要我们从别的地方下载好然后放到protobuf文件夹中。

git clone https://github.com/paulsapps/gmock-1.7.0

mv  gmock-1.7.0 gmock

-----------------------------------

1.1.3 下面是配置:

vim /etc/profile,添加

  export PATH=$PATH:/usr/local/protobuf/bin/

  export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/

  保存执行,source /etc/profile。

同时在~/.profile中添加上面两行代码,否则会出现登录用户找不到protoc命令。

1.1.4  配置动态链接库

  sudo vi /etc/ld.so.conf,在文件中添加/usr/local/protobuf/lib(注意: 在新行处添加),

然后执行命令: sudo ldconfig

安装配置完之后,使用protoc --version查看版本,如下所示:

$ protoc   --version 
libprotoc 3.6.1

1.1.5  简易安装

或者简单一点的,一次搞定:

./configure
make
make check
sudo make install
sudo ldconfig

 

1.1.6 如何使用protobuf

  .proto文件是protobuf一个重要的文件,它定义了需要序列化数据的结构。使用protobuf的3个步骤是:

1.1.6.1 在.proto文件中定义消息格式

1.1.6.2 用protobuf编译器编译.proto文件

1.1.6.3 用C++/Java等对应的protobuf API来写或者读消息

1.2 安装opencv2.4.10

opencv库在demo运用时会用到,先安装。

NCNN例程序只支持opencv2,不支持opencv3。

1.2.1  编译前期准备工作

1)安装依赖包

主要为build-essential软件包,为编译程序提供必要的软件包的列别信息,这样软件包才知道头文件、库函数的位置。此外,它还会下载依赖的软件包,安装gcc/g++/gdb/make等基本编程工具,最后组成一个开发环境。使用命令为:

sudo apt-get install build-essential

2)安装能够支持图像读写以及视频读写的相关依赖包,使用命令如下:

sudo apt-get install  libgtk2.0-dev libavcodec-dev libavformat-dev  libtiff4-dev  libswscale-dev libjasper-dev

sudo apt-get install qt5-default

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev libdc1394 2.x libjpeg-dev libpng-dev libtiff-dev libjasper-dev
--------------------- 
 

3)安装cmake,用于编译源码,使用命令如下:

sudo apt-get install cmake

注:已经安装了,就跳过这一步。

4)安装pkg-config,它是一个统一接口计算机软件,用于从源码中编译软件时查询已安装的库,使用命令如下:

sudo apt-get  install  pkg-config

5)sudo apt-get update

1.2.2 编译安装过程

1)在官网上下载并解压OpenCV,网址为https://opencv.org/releases.html

下载所需版本,此处,我下的版本为opencv-2.4.10.zip,然后执行如下命令进行解压:

unzip opencv-2.4.10.zip

cd opencv-2.4.10
rm -rf build
mkdir build
cd build

6. cmake编译OpenCV源码,安装所有的lib文件都会被安装到/usr/local目录下
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

或者
cmake -D CUDA_ARCH_BIN=3.2 -D CUDA_ARCH_PTX=3.2 -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D BUILD_TIFF=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

make -j
sudo make install

此时安装完毕,加载环境变量, 
设置动态链接用到的路径,可以执行:

sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

1.2.3 测试是否安装成功

直接在终端敲入命令 
pkg-config --modversion opencv 
显示出版本号2.4.10,说明已经安装成功

1.3 Ubuntu14.04安装CMake3.6.3

准备工作:官网下载cmake-3.6.3.tar.gz(https://cmake.org/download/)

1.解压文件tar -xvf cmake-3.6.3.tar.gz,并修改文件权限chmod -R 777 cmake-3.6.3

2.检测gcc和g++是否安装,如果没有则需安装gcc-g++:sudo apt-get install build-essential(或者直接执行这两条命令sudo apt-get install gcc,sudo apt-get install g++)

3.进入cmake-3.6.3 进入命令 cd cmake-3.6.3

4.执行sudo ./bootstrap 


           5.执行sudo make 


           6.执行 sudo make install 


           7.执行 cmake –version,返回cmake版本信息,则说明安装成功

 

2、开始安装

2.1 下载编译源码

1

 git clone https://github.com/Tencent/ncnn

下载完成后,需要对源码进行编译

 

cd ncnn

mkdir build && cd build

cmake ..

警告信息:

-- CMAKE_INSTALL_PREFIX = /home/tommy/NCNN/ncnn/build/install
-- Could NOT find Protobuf (missing:  Protobuf_INCLUDE_DIR) 
CMake Warning at tools/caffe/CMakeLists.txt:10 (message):
  Protobuf not found, caffe model convert tool won't be built


-- Could NOT find Protobuf (missing:  Protobuf_INCLUDE_DIR) 
CMake Warning at tools/onnx/CMakeLists.txt:12 (message):
  Protobuf not found, onnx model convert tool won't be built


-- Configuring done
-- Generating done
-- Build files have been written to: /home/tommy/NCNN/ncnn/build

解决:

sudo apt-get install libprotobuf-dev protobuf-compiler

警告信息:

-- CMAKE_INSTALL_PREFIX = /home/tommy/NCNN/ncnn/build/install
CMake Warning at /usr/local/share/cmake-3.6/Modules/FindProtobuf.cmake:387 (message):
  Protobuf compiler version 3.2.0 doesn't match library version 2.5.0
Call Stack (most recent call first):
  tools/caffe/CMakeLists.txt:1 (find_package)


CMake Warning at /usr/local/share/cmake-3.6/Modules/FindProtobuf.cmake:387 (message):
  Protobuf compiler version 3.2.0 doesn't match library version 2.5.0
Call Stack (most recent call first):
  tools/onnx/CMakeLists.txt:2 (find_package)


-- Configuring done
-- Generating done
-- Build files have been written to: /home/tommy/NCNN/ncnn/build
解决:

安装protobuf-2.5.0版本

错误报告:

onnx.proto:402:5: Expected "required", "optional", or "repeated".
onnx.proto:402:17: Missing field number.
onnx.proto:430:3: Expected "required", "optional", or "repeated".
onnx.proto:430:15: Missing field number.
make[2]: *** [tools/onnx/onnx.pb.cc] Error 1
[  8%] Built target caffe2ncnn
make[1]: *** [tools/onnx/CMakeFiles/onnx2ncnn.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 94%] Built target ncnn
make: *** [all] Error 2

原因:

新版本又更新了一些参数,查看了下需求,也发现需要用到2.6版本,所以只能重新进行编译protobuf.高的版本。
解决:

安装高版本的protobuf-2.6.1

https://github.com/protocolbuffers/protobuf/releases?after=v3.0.0-alpha-1

make -j

make install

这时在build/tools文件夹下有可执行文件caffe2ncnn和ncnn2mem,其作用分别是将caffe模型转成ncnn模型以及对ncnn模型进行加密。

3. ncnn的demo


  进入ncnn/examples
  自己编写一个Makefile文件

NCNN_DIR = /home/tommy/NCNN/ncnn

OPENCV = /home/tommy/NCNN/opencv-2.4.10

INCPATH =       -I${NCNN_DIR}/build/install/include \
                -I${OPENCV}/modules/objdetect/include \
                -I${OPENCV}/modules/highgui/include \
                -I${OPENCV}/modules/imgproc/include \
                -I${OPENCV}/modules/core/include

LIBS = -lopencv_core -lopencv_highgui -lopencv_imgproc  \
                -fopenmp -pthread

LIBPATH = -L${OPENCV}/lib

squeezenet:squeezenet.cpp
    $(CXX) $(INCPATH) $(LIBPATH) $^ ${NCNN_DIR}/build/install/lib/libncnn.a $(LIBS) -o $@

然后make,即可得到可执行文件squeezenet,
运行

squeezenet test.jpg

即可得到图像的分类结果。



 

猜你喜欢

转载自blog.csdn.net/u014689845/article/details/86692079