1. 首先安裝一些依賴庫
$ 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-pip python-dev python-numpy python-scipy
$ sudo apt-get install -y libopencv-dev
2.下載caffe.git 到download目錄
$ git clone https://github.com/BVLC/caffe.git
$ cd ~/download
$ cp -r ./caffe ~/
3.進入 caffe 中復制配置文件并重命名
$ cd ~/caffe
$ sudo cp Makefile.config.example Makefile.config
4.修改配置文件
$ sudo vim Makefile.config
根據(jù)個人需要進行修改
a.若使用 cudnn,則將#USE_CUDNN := 1
修改成:USE_CUDNN := 1
注意:GPU運算能力(GPU Compute Capability )3.0以上才支持CUDNN
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
e.還要更改
USE_OPENCV: = 1
USE_LEVELDB: = 1
USE_LMDB: = 1
5. 修改 makefile 文件
打開 Makefile 文件,在 415 行,將:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替換為:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
6.修改 host_config.h 文件
sudo vim /usr/local/cuda/include/host_config.h
將其中的第 119 行注釋掉:
#error-- unsupported GNU version! gcc versions later than 5 are not supported!
改為:
//#error-- unsupported GNU version! gcc versions later than 5 are not supported!
7.執(zhí)行編譯和測試命令
在~/ caffe/目錄下
$ make all -j4
$ make test -j4
$ make runtest -j4
問題:
Makefile:532: recipe for target 'runtest' failed
make: *** [runtest] 已放棄 (core dumped)
這個問題就是买猖,電腦不支持cudnn,再前面的Makefile.config里面去掉CUDNN就可以了。
另外,
編譯過程中出現(xiàn)警告:
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
解決:修改Makefile.config文件
將
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
中的
-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
去掉即可串述。
重新clean再編譯一邊
$ make clean
$ make all -j4
$ make test -j4
$ make runtest -j4
[----------] 2 tests from BilinearFillerTest/1, where TypeParam = double
[ RUN ] BilinearFillerTest/1.TestFillEven
[ OK ] BilinearFillerTest/1.TestFillEven (5 ms)
[ RUN ] BilinearFillerTest/1.TestFillOdd
[ OK ] BilinearFillerTest/1.TestFillOdd (8 ms)
[----------] 2 tests from BilinearFillerTest/1 (13 ms total)
[----------] 1 test from SolverTest/1, where TypeParam = caffe::CPUDevice<double>
[ RUN ] SolverTest/1.TestInitTrainTestNets
[ OK ] SolverTest/1.TestInitTrainTestNets (1 ms)
[----------] 1 test from SolverTest/1 (1 ms total)
[----------] Global test environment tear-down
[==========] 2063 tests from 271 test cases ran. (605597 ms total)
[ PASSED ] 2063 tests.
如上面所示,說明編譯成功了怔锌。
8. 配置 pycaffe 接口
在 caffe 根目錄的 python 文件夾下,有一個 requirements.txt 的清單文件,上面列出了需要的依賴庫,按照這個清單安裝就可以了程剥。在安裝 scipy 庫的時候,需要先安裝 fortran 編譯器
(gfortran),如果沒有這個編譯器就會報錯,因此,我們可以先安裝一下。
a.首先回到 caffe 的根目錄,然后執(zhí)行安裝代碼:
$ sudo apt-get install gfortran
$cd ./python
b.在終端輸入下面一段即可
for req in $(cat requirements.txt); do pip install $req; done
錯誤:
IOError: [Errno 13] 權限不夠: '/home/fc/anaconda2/lib/python2.7/site-packages/leveldb.so'
換root用戶重新執(zhí)行上面的命令
c.安裝完成以后,再次回到 caffe 根目錄我們可以執(zhí)行:
$ cd ~/caffe
$ sudo pip install -r python/requirements.txt
問題:
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-Io7ETq/ipython/
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
$ sudo python -m pip install --upgrade --force pip
$ sudo pip install setuptools==33.1.1
9.編譯 python 接口
$ sudo make pycaffe
10. 配置環(huán)境變量,以便 python 調(diào)用
$sudo vim ~/.bashrc
加入
export PYTHONPATH=/home/fc/caffe/python:$PYTHONPATH
$ source ~/.bashrc