前提
这个Caffe的安装与配置是在阿里云轻量应用服务器Ubuntu16.04的环境下实操的
1. 使软件包列表最新
$ sudo apt-get update
注:
- 如果发现之后有文件无法下载的情况,那就换个更新源吧,下面给出一个更新源参考地址
https://www.cnblogs.com/bovenson/p/5752213.html
换好之后再执行$ sudo apt-get update
一下
2. 安装依赖库
$ sudo apt-get install libprotobuf-dev
$ sudo apt-get install libleveldb-dev
$ sudo apt-get install libsnappy-dev
$ sudo apt-get install libopencv-dev
$ sudo apt-get install libhdf5-serial-dev
$ sudo apt-get install protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev
$ sudo apt-get install libatlas-base-dev
$ sudo apt-get install python-dev
$ sudo apt-get install libgflags-dev
$ sudo apt-get install libgoogle-glog-dev
$ sudo apt-get install liblmdb-dev
3. 下载Caffe
$ sudo apt-get install git
$ sudo git clone https://github.com/BVLC/caffe.git
4. 下载python依赖库
$ sudo apt-get install python-pip
for req in $(cat requirements.txt); do pip install $req; done
注:
在~/caffe/python/这个目录下,执行下载python依赖库的命令
当
~/caffe/python
路径不存在时,请用绝对路径替换
5. 修改Caffe配置参数
$ sudo cp Makefile.config.example Makefile.config
$ sudo vi Makefile.config
# CPU_ONLY:=1
替换成
CPU_ONLY:=1
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/hdf5/serial
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
替换成
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include
6. 编译
$ sudo make pycaffe
$ sudo make all
$ sudo make test
$ sudo make runtest
注:
- 执行
$ sudo make pycaffe
有错误提示:
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
compilation terminated.
Makefile:517: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1
原因:
缺失python下的numpy文件
解决办法:
$ sudo apt-get install python-numpy
$ sudo make pycaffe -j16
再输入$ sudo make pycaffe
7. 设置环境变量
$ sudo vi ~/.bashrc
在最后一行输入
export PYTHONPATH=~/caffe/python
然后
$ source ~/.bashrc
注:
当
~/caffe/python
路径不存在时,请用绝对路径替换-
执行
$ source ~/.bashrc
有错误提示:
原因:这是由于大多数Linux产品安装,以及默认使用bash shell,而Zsh是一个Linux用户很少使用的shell,小部分默认会用到它
解决方法:
$ source ~/.zshrc
8. 执行
$ python
$ import caffe
附上一张执行成功的图
注:
- 执行
$ import caffe
有错误提示:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "caffe/init.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "caffe/pycaffe.py", line 15, in <module>
import caffe.io
File "caffe/io.py", line 2, in <module>
import skimage.io
ImportError: No module named skimage.io
原因:
缺少python下的skimage文件
解决方法:
$ sudo apt-get install python-skimage
- 执行
$ import caffe
有警告提示:
/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building
原因:已经存在Matplotlib 文件
解决方法:
下次再$ import caffe
就不会出现警告了
参考
https://blog.csdn.net/muzilinxi90/article/details/53673184
https://blog.csdn.net/sinat_31515333/article/details/66975301
https://chunml.github.io/ChunML.github.io/project/Installing-Caffe-CPU-Only/