安装Caffe

在ubuntu 16.04上安裝caffe,支持python接口,並且選擇:

  • python2.7, Anaconda
  • cuda8, cudnn6
  • no opencv

這裏假設已經提前安裝好cuda8, cudnn6和anaconda。

參考過程

主要是:

Step 1: 安裝依賴庫

1
2
3
4
5
sudo apt-get install libleveldb-dev libsnappy-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev

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

注意,相對於caffe的安裝建議,這裏少了libprotobuf-dev, libopencv-dev, libhdf5-serial-dev。這是因爲(1)本文不想安裝opencv,(2)本文選擇protobuf和hdf5在anaconda裏面安裝。(當然你也可以使用apt-get的版本,安裝這些)。另外,protobuf-compiler可能也不需要。

1
選擇在anaconda裏提前安裝protobuf的原因是:如果anaconda後續安裝了protobuf,並且在caffe的Makefile.config中指定使用anaconda的虛擬環境中的python,那麼,如果這裏apt-get install了protobuf,則存在兩個protobuf版本,後續編譯時可能會報錯,例如:error incompatible with your Protocol Buffer headers(https://github.com/BVLC/caffe/issues/5645)。因而,本文選擇只使用anaconda的版本,並且後續libprotobuf.so也使用anaconda中的。

Step 2: 創建python虛擬環境以及必要庫

創建一個caffe-py27環境,安裝caffe/python/requirements.txt的指定的庫。注意,這裏安裝了protobuf庫。

1
2
3
conda create --name caffe-py27 python=2.7
source activate caffe-py27
conda install cython numpy scipy scikit-image matplotlib ipython h5py leveldb networkx nose pandas python-dateutil protobuf python-gflags pyyaml pillow six

Step 3: 配置caffe的Makefile.config文件

使用cudnn、不啓用opencv、使用anaconda的caffe-py27環境、啓用python_layer支持。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1

# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 0

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
#PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /home/maiqi/yalong/software/anaconda3/envs/caffe-py27
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
#PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1

Step 4: 編譯

1
make all -j4

如果出現跟protobuf相關的錯誤,檢查是否有多個protobuf的版本(https://github.com/BVLC/caffe/issues/5645)。注意,更改环境或库后,别忘记make clean,以及已经在caffe-py27的虚拟python环境中。

1
make test -j4

將$(ANACONDA_HOME)/lib(例如/home/maiqi/yalong/software/anaconda3/envs/caffe-py27/lib)加入到LD_LIBRARY_PATH中。否則,在make runtest時可能提示找不到libprotofo.so。因而,可以將export LD_LIBRARY_PATH=/home/maiqi/yalong/software/anaconda3/envs/caffe-py27/lib:$LD_LIBRARY_PATH 加入到~/.bashrc,並且source ~/.bashrc

1
make runtest -j4

Step 5: caffe python

執行make pycaffe,然後設定PYTHONPATH,例如添加export PYTHONPATH=/home/maiqi/yalong/library/caffe/python:PYTHONPATH到~/.bashrc,並且source ~/.bashrc

驗證,在caffe-py27的python環境下,import caffe; print(caffe.__version__)

總結

有幾個注意的地方:

  1. protobuf和hd5f這些多個版本產生混亂的情況,如果出現,那麼解決掉其他的,保留一個
  2. 如果找不到protobuf.so,但確實存在,那麼需要更新一些cache或指定路徑,參考https://www.cnblogs.com/Anker/p/3209876.html
  3. 首次編譯caffe,盡量減少復雜情況,比如optional的可以先不裝,以及選擇默認的庫的版本