最近由於需要,安裝一下
caffe
,由於手邊暫時沒有gpu
,所以在自己筆記本先安裝一下cpu
版本的caffe
。由於caffe是屬於比較老的框架,然後對python 2.7
支持比較好,或者python3.5
以下也是可以的,但是由於我們的python
是3.6,以及tensorflow == 1.6
,因此我們嘗試在python 3.6
的情況下,安裝caffe CPU
版本。
系統版本:
ubutu 16.04
anoconda 4.3.30
python 3.6
opencv 3.2.0
caffe 1.0.0
1. 首先利用anaconda創建虛擬環境
爲了和tensorflow
、pytorch
、theano
等環境區分,我們首先創建一個caffe
環境,使用anaconda創建。conda創建、查看、刪除虛擬環境
1.首先查看當前已有環境:
conda-env list
2.創建caffe虛擬環境
conda create -n CAFFE python=3.6
3.進入環境
source activate CAFFE
小提示,退出環境命令爲
source deactivate
2. 安裝opencv3以及其他各種依賴項
2.1 安裝opencv3
爲了安裝caffe,我們首先先將
opencv3
安裝好
參考我的博客:
ubutu16.04卸載opencv2安裝opencv3
2.2 安裝其他各種依賴項
1.切換到root權限,su
, 依次安裝:
apt-get install libprotobuf-dev
apt-get install libleveldb-dev
apt-get install libsnappy-dev
apt-get install libopencv-dev
apt-get install libhdf5-serial-dev
apt-get install protobuf-compiler
apt-get install --no-install-recommends libboost-all-dev
CPU Only的情況下,跳過了CUDA相關的安裝;
接下來是BLAS:
apt-get install libatlas-base-dev
使用默認Python來建立pycaffe接口,需要安裝:
apt-get install python-dev
一些兼容性依賴庫:
apt-get install libgflags-dev
apt-get install libgoogle-glog-dev
apt-get install liblmdb-dev
3 安裝caffe
參考:
- ubuntu16.04 python3.6 caffe(CPU) 配置記錄
- caffe安裝:基於anaconda3—python3.6, linux, 僅CPU
- Ubuntu16.04安裝Caffe(CPU Only)
- 利用C++ Boost庫將C++項目封裝爲Python模塊
3.1 下載caffe相關包
沒有安裝git的話需要先裝一下git
,同樣也是需要root權限的。
apt-get install git
下載Caffe源碼:
git clone https://github.com/BVLC/caffe.git
如果需要Caffe的Python接口,切換到caffe
下的python
目錄下,輸入以下命令下載python依賴庫(先安裝pip):
apt-get install python-pip
for req in $(cat requirements.txt); do pip install $req; done
小技巧,從這裏可以看出這裏的
requirements.txt
需要安裝的東西還是比較多的,由於下載速度可能會比較慢,所以有些包可能需要下載,然後離線安裝,安裝包的格式就是.whl
和.zip
格式等。
3.2 開始編譯caffe
前面的各種包並不是太難,caffe難裝的原因就是編譯可能會出現各種各樣的錯誤,這裏我們也是一步一步來。
3.2.1 到Caffe文件夾中
cd caffe
拷貝一份Makefile.config.example並重命名成Makefile.config,修改該配置文件:
cp Makefile.config.example Makefile.config
3.2.2 修改Makefile.config
這一步非常麻煩,但是也十分重要。。。
這是我的Makefile.config
,裏面做了一些修改。主要參考的是ubuntu16.04 python3.6 caffe(CPU) 配置記錄,同時也在這個基礎上,做了一些修改,這是依照後面的錯誤來的。
- 添加
HOME := /home/xuchao
,原來沒有寫。 - 添加代碼
LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system hdf5_hl hdf5 m opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
我的Makefile.config
如下:
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 0
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
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
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app
# 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/local/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# HOME path !!!!
HOME := /home/xuchao
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.6m \
$(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
# PYTHON_INCLUDE := /usr/include/python3.6m \
# /usr/lib/python3.6/dist-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
# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib
# Uncomment to support layers written in Python (will link against Python libs)
# WITH_PYTHON_LAYER := 1
# 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
# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib
# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1
# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1
LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system hdf5_hl hdf5 m opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0
# enable pretty build (comment to see full commands)
Q ?= @
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib#
3.2.3 安裝libboost(基於python3.6)的庫
1.首先輸入命令:
sudo ln -s /home/xuchao/anaconda3/bin/python3 /usr/lib/python3.6m(這裏是建立一個軟鏈接,把我本地目錄下的python3.6加到usr/lib/中去。
2.下載boost_1_67_0
wget -O boost_1_67_0.tar.gz http://sourceforge.net/projects/boost/files/boost/1.67.0/boost_1_67_0.tar.gz/download
tar xzvf boost_1_67_0.tar.gz
3.安裝附加依賴庫
sudo apt-get update
sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev
4.編譯boost_1_67_0
cd boost_1_67_0/
./bootstrap.sh --with-libraries=python --with-toolset=gcc
./b2 --with-python include="/home/xuchao/anaconda3/include/python3.6m/"
sudo ./b2 install
/home/xuchao/
可能要換成你自己的目錄名字。
編譯安裝成功後,/usr/local/lib
下會有libboost_python36.so
和libboost_python36.a
,有些應用link時需要的是libboost_python3.so
或者libboost_python3.a
,我們建個軟鏈:
cd /usr/local/lib
sudo ln -s libboost_python-py36.so libboost_python3.so
sudo ln -s libboost_python-py36.a libboost_python3.a
這時,我們要把/usr/local/lib
中,相關文件,建立相對於名稱的軟鏈接到/usr/lib/x86_64-linux-gnu
中。
sudo cp /usr/local/lib/libboost_python36.a /usr/lib/x86_64-linux-gnu/libboost_python_python36.a
sudo cp /usr/local/lib/libboost_python36.so.1.67.0 /usr/lib/x86_64-linux-gnu/libboost_python3.so
3.2.4 編譯caffe
依次輸入以下命令:
sudo make all -j4
sudo make test -j4
sudo make runtest -j4
sudo make pycaffe -j4
make默認單核運算,如果想加快速度,我這裏是4核,可以在每條命令後面加上-j4
,如果有報錯,建議最好make clean
重新開始。
如果所有測試都通過,則說明安裝好了。
4 測試caffe
測試Caffe的Python接口,切換到caffe/python文件目錄下,記錄下來當前路徑,輸入以下命令:
export PYTHONPATH=/home/xuchao/caffe/python:$PYTHONPATH
/home/xuchao
換成你自己的。
進入python環境,輸入:
import caffe
果沒有報錯,證明安裝成功。
上面的方法,一旦關閉終端或者打開新終端則失效,如果放到配置文件中,可以永久有效果,命令操作如下:
#A.把環境變量路徑放到 ~/.bashrc文件中
sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
#B.使環境變量生效
source ~/.bashrc
其他參考:
花了一天的時間,終於把caffe安裝好了。後面安裝gpu版本的caffe再說了。