参考博客:总体 https://www.cnblogs.com/fanwendi2312/p/8438575.html
cuda9.1安装 http://blog.youkuaiyun.com/jonms/article/details/79318566
1,先安装anaconda(python2.7版本)代替系统自带的python:
bash Anaconda2-5.1.0-Linux-x86_64.sh
安装完后添加环境变量
打开~/.bashrc
添加export PATH=/home/xxx/anaconda2/bin:$PATH
source ~/.bashrc
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
安装cudnn时,下载到的文件为 .solitairetheme8 格式文件,解压方式如下:
cp cudnn-9.1-linux-x64-v7.0.solitairetheme8 cudnn-9.1-linux-x64-v7.0.tgz
tar -xvf cudnn-9.1-linux-x64-v7.0.tgz
要注意的是不要把cudnn文件放在u盘里往ubuntu解压,会出现tar: Exiting with failure status due to previous errors这样的错误!最好在ubuntu下解压到本地。
安装cudnn时好多教程都提到要建立软连接或者环境变量,我从官网没看到这样做的理由,解压完直接这样做就行:
1.进入到cudnn的压缩文件所在目录;
2解压cuDNN安装包
$ tar -xzvf cudnn-9.0-linux-x64-v7.tgz
3.复制相应文件到cuda的相应目录,同时给复制到lib64中的文件修改权限
$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
6,安装OpenCV3.4.0(下载source版本的)
先将其解压到home下:
cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j8
sudo make install
7,安装caffe
git clone https://github.com/BVLC/caffe.git
下完cd进caffe
sudo cp Makefile.config.example Makefile.config
a.若使用cudnn,则将
#USE_CUDNN := 1 修改成: USE_CUDNN := 1
b.若使用的opencv版本是3的,则将
#OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3
c.若要使用python来编写layer,则将
#WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1
d. 重要的一项 :
将# Whatever else you find you need goes here.下面的
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为:
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
这是因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径.
e.更重要的
注释掉这一行
#-gencode arch=compute_20,code=sm_20
(5)修改makefile文件
打开makefile文件,做如下修改:
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
(6)修改host_config.h
sudo gedit /usr/local/cuda/include/crt/host_config.h
将
#error-- unsupported GNU version! gcc versions later than 6 are not supported!
改为
//#error-- unsupported GNU version! gcc versions later than 6 are not supported!
(7)复制链接
sudo cp opencv-3.4.0/build/lib/libopencv_core.so.3.4 /usr/local/lib/libopencv_core.so.3.4 && sudo ldconfig make -j8
在caffe/python目录下
for req in $(cat requirements.txt); do pip install $req; done
这一步中我们可以将pip源改成国内源,加速下载,比如可以更改至清华的源,
可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple
具体参考http://blog.youkuaiyun.com/lambert310/article/details/52412059
然后
make pycaffe
最后添加环境变量
sudo gedit ~/.bashrc
tianjia export PYTHONPATH=/home/felix/caffe/python:$PYTHONPATH
shishengxiao source ~/.bashrc
最后
make test -j8
make runtest
打开~/.bashrc
添加export HDF5_DISABLE_VERSION_CHECK=1
source ~/.bashrc后继续make runtest
又出现这个错:
[----------] Global test environment tear-down
[==========] 2139 tests from 285 test cases ran. (322957 ms total)
[ PASSED ] 2138 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] BatchReindexLayerTest/3.TestGradient, where TypeParam = caffe::GPUDevice<double>
1 FAILED TEST
Makefile:533: recipe for target 'runtest' failed
make: *** [runtest] Error 1
解决这个错在这里https://github.com/BVLC/caffe/issues/6164
即将Makefile中的这段
...
# Debugging
ifeq ($(DEBUG), 1)
COMMON_FLAGS += -DDEBUG -g -O0
NVCCFLAGS += -G
else
COMMON_FLAGS += -DNDEBUG -O2
endif
...
改为
...
# Debugging
ifeq ($(DEBUG), 1)
COMMON_FLAGS += -DDEBUG -g -O0
NVCCFLAGS += -G
else
COMMON_FLAGS += -DNDEBUG -O2
NVCCFLAGS += -G
endif
...
然后重新编译caffe
make clean
make all -j8
make -j8 test
make runtest
重新编译的时候记得再编译一下caffe的python接口:make pycaffe
终于成功这么长时间终于搞定,我足足高兴了1分钟,又开始忙新的任务了!