Ubuntu16.04 在cuda9.0环境下编译caffe

Ubuntu16.04 在cuda9.0环境下编译caffe

安装依赖库

1.安装cuda9.0库 安装教程
2.安装opencv 安装教程
3.安装开发依赖包:

sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev 
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install -y python-dev
sudo apt-get install -y python-numpy python-scipy
sudo apt-get install libopenblas-dev

下载源码

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

修改配置文件

将caffe主目录下面的Makefile.config.example拷贝更名为Makefile.config,打开操作:

cp Makefile.config.example Makefile.config

修改Makefile.config文件:

#USE_CUDNN := 1
#WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

修改成

USE_CUDNN := 1
WITH_PYTHON_LAYER := 1
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

cuda9不支持‘ compute-20 ’,需要修改Makefile.config文件中CUDA_ARCH设置,所以将:

 -gencode arch=compute_20,code=sm_20 \
 -gencode arch=compute_20,code=sm_21

删除就行了。
打开 Makefile文件:

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

更改成:

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

编译

进入到caffe主目录的python文件夹下运行:

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

安装python依赖库
回到caffe主目录运行:

make all -j7
make test -j7
make runtest -j7
make pycaffe -j7

配置环境变量

vim ~/.bashrc

在文件末尾写入caffe-python的安装路径:

export PYTHONPATH=caffe安装路径/caffe/python:$PYTHONPATH

运行:

source ~/.bashrc

测试

$python
>>import caffe

参考:
https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide
http://wgshun.top/2018/06/ubuntu-install-caffe/

你可能感兴趣的:(Ubuntu16.04 在cuda9.0环境下编译caffe)