1、安装依赖包
安装后续步骤或环境必需的依赖包,依次输入以下命令:
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
2、安装 caffe
首先在你要安装的路径下 clone :
git clone https://github.com/BVLC/caffe.git
如果下载网速慢,可以先下载zip文件,再解压。进入 caffe ,将 Makefile.config.example 文件复制一份并更名为 Makefile.config ,也可以在 caffe 目录下直接调用以下命令完成复制操作 :
sudo cp Makefile.config.example Makefile.config
复制一份的原因是编译 caffe 时需要的是 Makefile.config 文件,而Makefile.config.example 只是caffe 给出的配置文件例子,不能用来编译 caffe。
然后修改 Makefile.config 文件,在 caffe 目录下打开该文件:
sudo gedit Makefile.config
修改 Makefile.config 文件内容:
USE_CUDNN := 1
OPENCV_VERSION := 3
WITH_PYTHON_LAYER := 1
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
然后修改 caffe 目录下的 Makefile 文件:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
然后修改 /usr/local/cuda/include/host_config.h 文件 :(9.0没有)
将
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改为
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
OK ,可以开始编译了,在 caffe 目录下执行 :
make all -j8
这是如果之前的配置或安装出错,那么编译就会出现各种各样的问题,所以前面的步骤一定要细心。
//记得要选择自己的cuda版本
nvcc fatal : Unsupported gpu architecture 'compute_20'
在Makefile.config文件中根据自己CUDA的版本注释不同的行。
#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
编译成功后可运行测试:
sudo make runtest -j8
如果测试pass,则表示 caffe 已经成功安装
3、配置 pycaffe
首先编译 pycaffe :
sudo make pycaffe -j8
以下是我编译 pycaffe 时出现的错误:
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: 没有那个文件或目录
解决方法:
sudo apt-get install python-numpy
此外也可能是由于 Makefile.config 文件中 python 路径设置错误出现的错误,可根据上一步检查一下,也可能出现别的错误,百度谷歌之~
编译 pycaffe 成功后,将路径加入到环境变量中
sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
source ~/.bashrc
验证一下是否可以在 python 中导入 caffe 包,首先进入 python 环境:
python
然后导入 caffe :
>>> import caffe
若不报错则表示 caffe 的 python 接口已正确编译,但是应该不会那么顺利,以下是我导入 caffe 时出现的错误:
错误1:
ImportError: No module named skimage.io
解决:
pip install -U scikit-image
错误1:
ImportError: dynamic module does not define module export function (PyInit__caffe)
解决:
caffe只适用于python2.
基本上就可以使用了。
~~~~~~~~~~~~~~~~over~~~~~~~~~~~~~~~~~~~~~~~~~~~