ubuntu+python2.7+opencv3+caffe(gpu)

1、安装显卡驱动

2、安装依赖库(gpu依赖库和caffe依赖库)

sudo apt-get install vim python-pip git

sudo apt-get install build-essential libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler

sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev

sudo apt-get install  libboost-all-dev

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

sudo apt-get install freeglut3-devbuild-essentiallibx11-devlibxmu-devlibxi-devlibgl1-mesa-glxlibglu1-mesalibglu1-mesa-dev

3、安装opencv

sudo apt install libopencv-dev python-opencv

4、安装cuda(我装的是9.1)

进入到有cuda安装文件的目录,执行安装文件(确定该目录下有cuda_x.x.x_linux.run文件)

sudo sh ./cuda_x.x.x_linux.run

到安装nvidia驱动确认的时候选择n(如果之前已经安装过了)

5、安装cudnn(只是复制文件加链接,不需要编译安装的过程)

下载对应cuda版本的cudnn

解压出cuda文件夹,在解压出的cuda文件夹目录下

sudo cp lib64/lib* /usr/local/cuda/lib64/   

sudo cp include/cudnn.h /usr/local/cuda/include/

cd /usr/local/cuda/lib64/ 

sudo chmod +r libcudnn.so.x.x.x

sudo ln -sf libcudnn.so.x.x.x libcudnn.so.x 

sudo ln -sf libcudnn.so.x libcudnn.so 

sudo ldconfig

然后修改环境变量

sudo vim /etc/profile

 添加环境变量如下:

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

export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

source /etc/profile

6、安装caffe

下载caffe

git clone https://github.com/BVLC/caffe.git

进入caffe目录下

sudo cp Makefile.config.example Makefile.config

修改 Makefile.config 文件

sudo vim Makefile.config

(1)CUDA_ARCH设置,因为安装的是9.1,所以删除前两行

-gencode arch=compute_20,code=sm_20\ 

-gencodearch=compute_20,code=sm_21\ 

(2)应用cudnn

#USE_CUDNN := 1

to

USE_CUDNN := 1

(3)应用opencv3

#OPENCV_VERSION := 3

to 

OPENCV_VERSION := 3

(4)使用python接口

#WITH_PYTHON_LAYER := 1

to

WITH_PYTHON_LAYER := 1

(5)修改python路径

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

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

to

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 /usr/lib/x86_64-linux-gnu/hdf5/serial

然后修改caffe目录下的Makefile文件

(1)

NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

to

NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

(2)

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5

to

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

然后修改/usr/local/cuda/include/crt/host_config.h文件

#error-- unsupported GNU version! gcc versions later than x.x are not supported!

to

//#error-- unsupported GNU version! gcc versions later than x.x are not supported!

7、编译

caffe目录下执行:

make all

make test

make runtest

注意:

在make runtest可能会出现如下错误:

Makefile:532: recipe for target 'runtest' failed

make: *** [runtest] Error 1

解决办法:修改caffe目录下Makefile文件如下,之后分别make clean,make all,make test,make runntest

# Debugging

ifeq ($(DEBUG), 1)

        COMMON_FLAGS += -DDEBUG -g -O0

        NVCCFLAGS += -G

else

        COMMON_FLAGS += -DNDEBUG -O2

endif

to

# Debugging

ifeq ($(DEBUG), 1)

        COMMON_FLAGS += -DDEBUG -g -O0

        NVCCFLAGS += -G

else

        COMMON_FLAGS += -DNDEBUG -O2

        NVCCFLAGS += -G

endif

8、安装pycaffe(caffe的Python接口)

进入目录caffe/python,然后终端执行:

for req in $(cat requirements.txt) --user; do pip install $req; done

然后回到caffe目录,终端执行:

make pycaffe

配置caffe中的python环境:

vim ~/.bashrc

加入以下内容:

export PYTHONPATH=/caffe所在路径/caffe/python:$PYTHONPATH

注意:

for req in $(cat requirements.txt) --user; do pip install $req; done

这句命令如果不加 --user有的会没有权限。在执行过程中也可能会发现缺模块,但是根据提示pip install安装就行。最新版的pip会出现错误如下:

Import Error:cannot import name main

解决方法如下:

sudo vim /usr/bin/pip

之后

from pip import main

if__name__ =='__main__':

    sys.exit(main())

to

from pip import__main__

if__name__ =='__main__':

    sys.exit(__main__._main())

你可能感兴趣的:(ubuntu+python2.7+opencv3+caffe(gpu))