编译caffe(python3.6,OpenCV3.4)
安装依赖包
编译caffe首先要安装一大堆的依赖包:
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
sudo apt-get install git cmake build-essential
安装CUDA和cuDNN
不是本次博客的重点,略去。
安装OpenCV3.4
注意,这个不是python上的OpenCV哦!如果没装这个,在编译caffe的过程中会报以下错误:
/usr/bin/ld: cannot find -lopencv_imgcodecs
sudo apt install git
git clone https://github.com/jayrambhia/Install-OpenCV
cd Install-OpenCV/Ubuntu/
chmod +x *
./opencv_latest.sh
漫长的等待·····编译过程中终端花花绿绿的日志真是好看~
安装Boost Python
从官网下好源码(最新版本,否则在编译caffe的时候会报一个关于CUDA的参数过时了)
unzip boost_1_67_0.zip
cd boost_1_67_0
./bootstrap.sh --with-python=/usr/bin/python3 --with-python-version=3.6 --with-python-root=/home/xxx/anaconda3/lib/python3.6 --prefix=/usr/local
sudo ./b2 install -a --with=all
sudo ldconfig
--with-python-root
指定成自己使用的python就好
漫长的编译过程,有警告?无视掉。
安装caffe
git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd
需要切换到分支ssd,如果是从https://github.com/BVLC/caffe.git 检出的,那么切换到intel分支。
进入 caffe ,将 Makefile.config.example 文件复制一份并更名为 Makefile.config
cp Makefile.config.example Makefile.config
修改的参数:
1. 应用cuDNN
将
#USE_CUDNN := 1
修改成:
USE_CUDNN := 1
- opencv 版本
将
#OPENCV_VERSION := 3
修改为:
OPENCV_VERSION := 3
- 应用python接口
将
#WITH_PYTHON_LAYER := 1
修改为
WITH_PYTHON_LAYER := 1
- 修改 python 路径
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
- 指定anaconda中的python(使用了anaconda的话)
注意:要把原本的python2.7注释掉
# 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/xxx/anaconda3/
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.6m \
$(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
- 设定使用
python3
的boost
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
注意,这里放开了PYTHON_LIBRARIES
,将它设定成python3
的boost
, 但没放开PYTHON_INCLUDE
注释,因为我使用了anaconda
里的PYTHON_INCLUDE
。
假如你使用py3
调用了py2.7
的boost
的话,就会出现这样的错误内容:
undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE
- 根据CUDA版本修改计算架构
注释里说,CUDA>=9.0的话,把compute_20到21的注释掉,否则,会出现以下错误:
unsupported gpu architecture 'compute_20'
# 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
现在开始编译pycaffe
在caffe
目录下,进入python
目录,里面有个requirements.txt
文件:
Cython>=0.19.2
numpy>=1.7.1
scipy>=0.13.2
scikit-image>=0.9.3
matplotlib>=1.3.1
ipython>=3.0.0
h5py>=2.2.0
leveldb>=0.191
networkx>=1.8.1
nose>=1.3.0
pandas>=0.12.0
python-dateutil>=1.4,<2
protobuf>=2.5.0
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.0
six>=1.1.0
需要把python-dateutil>=1.4,<2
去掉,因为它会装上1.5版本的python-dateutil
,而安装matplotlib
需要2.1版本,我们手动安装上高版本的python-dateutil
先。
执行:
for req in $(cat requirements.txt); do pip install $req; done
安装好这些依赖后,继续回到caffe根目录
执行:
make pycaffe
出现报错:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
将环境变量CPLUS_INCLUDE_PATH
指定到自己的python3.6m上
export CPLUS_INCLUDE_PATH=/home/xxx/anaconda3/include/python3.6m
继续编译,发现报错:
/usr/bin/ld: cannot find -lboost_python3
在/usr/lib/x86_64-linux-gnu
中找到libboost_python-py35.so
, 执行:
sudo ln -s libboost_python-py35.so libboost_python3.so
即可使其被Caffe找到.
继续编译,发现报错:
/usr/bin/ld: cannot find -lpython3.5m
解决办法同上:
sudo ln -s libpython3.5m.so.1 libpython3.5m.so
继续编译,成功:
.build_release/examples/cpp_classification/classification.bin CXX/LD
-o python/caffe/_caffe.so python/caffe/_caffe.cpp touch python/caffe/proto/init.py PROTOC (python)
src/caffe/proto/caffe.proto
现在已经编译通过了。
当再一次执行make pycaffe
时,显示:
make: Nothing to be done for ‘pycaffe’.
证明是没有问题的。
现在进入python环境,看看能不能导入caffe模块:
>>> import sys
>>> caffe_root = '/path/to/caffe/'
>>> sys.path.insert(0, caffe_root + 'python')
>>> import caffe
>>> caffe.__version__
'1.0.0'
>>>
说明编译出的caffe能够正常使用了,如果出现以下错误:
ImportError: dynamic module does not define module export function (PyInit__caffe)
说明用来编译caffe的python版本跟当前使用的python版本不一致。
到此完成了pycaffe
的编译。