ubuntu16.04 caffe(github源码cpu)+python3.5+opencv3.4.5安装编译

时间:2022-11-12 06:28:46

系统ubuntu16.04 自带python3.5.2

第一 安装依赖包

sudo apt-get install libprotobuf-dev(接口的版本太低)

sudo apt-get install libleveldb-dev

sudo apt-get install libsnappy-dev

sudo apt-get install libopencv-dev(简装)

sudo apt-get install libhdf5-serial-dev

sudo apt-get install protobuf-compiler

sudo apt-get install libgflags-dev

sudo apt-get install libgoogle-glog-dev

sudo apt-get install liblmdb-dev sudo

apt-get install libatlas-base-dev

这些包应该是ubuntu官网提供的,网址https://launchpad.net/ubuntu

 

第二 安装3.6.1版本protobuf

1.libprotobuf-dev 是2.6.1版本的,前面spt-get默认安装后应该已经将

protoc可执行程序添加到环境变量中可用命令查看

$ which protoc

$ protoc --version

$ whereis protoc

2.因为从github的源码安装caffe需要3.6.1版本的protobuf

下载地址https://github.com/protocolbuffers/protobuf/releases

 解压到主目录改文件夹名字为protobuf

3.安装这些依赖包

$ sudo apt-get install autoconf automake libtool curl make g++ unzip;

4.进入protobuf根据文件夹下的configure配置文件进行编译和安装

$ cd protobuf;

$ ./autogen.sh;

$ ./configure

$ make(编译)

$ make check(检查)

$ sudo make install (安装到默认文件夹)

$ sudo ldconfig # refresh shared library cache.;

 

第三 安装opencv3.4.5

1.安装依赖包

$ sudo apt-get install build-essential

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

$ sudo apt-get install python3-dev python3-scipy python3-numpy python3-pandas python3-matplotlib python3-sklearn

2.下载源码

https://github.com/opencv/opencv/releases;

解压后重命名为opencv3

3.编译

$ cd opencv

$ mkdir release;cd release

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D PYTHON3_EXECUTABLE=/usr/bin/python3 \

-D PYTHON_INCLUDE_DIR=/usr/include/python3.5 \ -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \

-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \ -D INSTALL_PYTHON_EXAMPLES=ON \

-D INSTALL_C_EXAMPLES=OFF \ -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \ -D PYTHON_EXECUTABLE=/usr/lib/python3 \

-D BUILD_opencv_xfeatures2d=OFF \ -D BUILD_EXAMPLES=ON ..

nunmy文件夹可能是 /usr/local/lib/python3.5/dist-packages/numpy/core/include

$ make -j8

4.安装配置

安装到默认文件夹

$ sudo make install;

将OpenCV的库添加到路径,从而可以让系统找到;

$ sudo gedit /etc/ld.so.conf.d/opencv.conf;

执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加

/usr/local/lib;

执行如下命令使得刚才的配置路径生效

$ sudo ldconfig;

5.验证

$cd opencv3/samples/cpp/example_cmake;

我们可以看到这个目录里官方已经给出了一个cmake的example我们可以拿来测试下 按顺序执行

$ cmake .

$ make

$ ./opencv_example

即可看到打开了摄像头,在左上角有一个hello opencv 即表示配置成功

 

第四 下载并编译caffe

1.可直接使用Git下载caffe,如果没有Git,先下载 $ sudo apt-get install git

2.下载caffe到主目录 $ git clone https://github.com/BVLC/caffe.git

3.生成Makefile.config文件,这里是将caffe目录下自带的Makefile.config.example文件

复制一份并更名为Makefile.config,命令如下:

$ cd caffe

$ cp Makefile.config.example Makefile.config;

4.修改Makefile.config文件中的配置

编辑Makefile.config文件

$ sudo gedit Makefile.config(gedit没有的话用$ sudo spt-get install gedit 进行安装)

1)设置cpu模式

# cuDNN acceleration switch (uncomment to build with cuDNN).

# USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).

  CPU_ONLY := 1;

2)使用opencv3版本

Uncomment if you're using OpenCV 3

OPENCV_VERSION := 3;

3)计算库选择

# BLAS choice:

# atlas for ATLAS(default)

# mkl for MKL

# open for OpenBlas

BLAS := atlas

4)注释掉python2.7 选择python3.5

# PYTHON_INCLUDE := /usr/include/python2.7 \

# /usr/lib/python2.7/dist-packages/numpy/core/include;

# Uncomment to use Python 3 (default is Python 2)

  PYTHON_LIBRARIES := boost_python3 python3.5m

  PYTHON_INCLUDE := /usr/include/python3.5m \

  /usr/lib/python3/dist-packages/numpy/core/include;

  PYTHON_LIB := /usr/lib;

5)# Whatever else you find you need goes here. 

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/i386-linux-gnu/hdf5/serial /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial;

6) # N.B. both build and distribute dirs are cleared on `make clean`

BUILD_DIR := build

DISTRIBUTE_DIR := distribute

5.修改对c++11的支持

$ sudo gedit makefile;

找到

# Linux ifeq ($(LINUX), 1)

CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

CXX ?= /usr/bin/g++ GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)

添加这两行

CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

6.建立连接

$ ln -s /usr/local/python3/bin/python3.5 /usr/bin/python3

$ sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so

7.编译

编译caffe,make默认单核运算,-j8表示八核,可加快速度,根据自己实际情况修改。

$ make all -j8

$ make test -j8

$ make runtest -j8

5.验证caffe

$ cd caffe

$ build/tools/caffe

出现命令选项基本上成功了

 

第六 安装caffe python3接口

$ sudo apt install python3-pip #安装pip工具或者($ wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py)

$ sudo apt install python3-numpy #解决后面编译无法找到numpy错误。

$ sudo apt-get install gfortran #安装依赖文件

$ sudo pip3 install matplotlib#解决后面import caffe报错

pip3 install python-dateutil --upgrade#解决报错raise ValueError,"Can't create weekday with n == 0" SyntaxError:invalid syntax

$ cd caffe

$ pip3 install -r python/requirements.txt

$ make pycaffe -j8

增加环境变量;

gedit ~/.bashrc

结尾添加一行 export PYTHONPATH=~/caffe/python:$PYTHONPATH;

验证

$ python3

>>>import cv2

>>>import caffe

没报错说明成功(。♥‿♥。)