我安装的是基于ubuntu16.04的cpu版本。
说明一下:因为我先前已经安装了anaconda3.6,所以python版本是3.6,但是发现caffe貌似还没有那么高的对应版本。所以我就切换到了管理员权限,那里的python版本是2.7的,刚好符合caffe版本。如下所示:
lsq@lsq:~$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
lsq@lsq:~$ su
Password:
root@lsq:/home/lsq# python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
1.第一步安装依赖包
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 libgflags-dev
sudo apt-get install libgoogle-glog-dev
sudo apt-get install liblmdb-dev
sudo apt-get install libatlas-base-dev
其实在这里安装依赖的时候,我还遇到过问题,主要是下载不了,报了类似这样的错误http://ppa.launchpad.net/pyside/ppa/ubuntu/dists/xenial/main/binary-amd64/Packages,我都忘了我怎么解决的了。
2.下载caffe
其实我早先安装的时候,已经下载了caffe,不知道在哪里下的,居然不全,害我编译的时候总是出错,所以只好重新下载。
可直接使用Git下载caffe,如果没有Git,请先安装Git,作者新装的Ubuntu16.04没有Git,所以先安装Git,执行如下语句:
sudo apt-get install git
安装结束后下载caffe,执行以下命令:
git clone git://github.com/BVLC/caffe.git
3. 编译caffe
1.进入caffe目录
cd caffe/
2.生成Makefile.config文件,这里是将caffe目录下自带的Makefile.config.example文件复制一份并更名为Makefile.config,命令如下:
cp Makefile.config.example Makefile.config
3.修改Makefile.config文件中的配置
1)编辑Makefile.config文件
sudo gedit Makefile.config
2)去掉CPU_ONLY前面的#号,修改成如下:
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
3)配置引用文件路径(主要是HDF5的路径问题),配置成如下:
# Whatever else you find you need goes here.
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
4)执行编译
sudo make all
sudo make test
sudo make runtest
4.编译python接口
通常为了更好的调用caffe,我们往往需要写代码,caffe具有Python和C++接口,我们较为常用Python接口,这里我将展示如何编译Python接口。
1)安装pip
因为Ubuntu16.04默认安装的是python2.7,没有pip,所以需要先安装pip,命令如下:
sudo apt-get install python-pip
2)安装Python接口依赖库
在caffe根目录下,有个python文件夹,文件夹里面有个requirements.txt,里面有需要的依赖库和版本信息,按照其安装即可,在安装前,需要先安装fortran编辑器(gfrotran),因为安装scipy库时需要它,命令如下:
sudo apt-get install gfortran
cd ~/caffe/python
for req in $(cat requirements.txt); do pip install $req; done
安装结束后,可以执行如下语句验证:
sudo pip install -r requirements.txt
3)将caffe根目录下的python文件夹加入到环境变量
sudo gedit ~/.bashrc
在文件的最后面添加:
export PYTHONPATH=/home/lsq/caffe/python:$PYTHONPATH
如下所示:
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
# . /etc/bash_completion
#fi
export PYTHONPATH=/home/lsq/caffe/python:$PYTHONPATH
注意:这里路径因人而异
4)编译python接口
cd ~/caffe/
make pycaffe
在最后编译python接口时候出现了如下错误:
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: no such file or directory
Makefile:498: recipe for target 'python/caffe/_caffe.so' failed make: *** [python/caffe/_caffe.so] Error
查了一下,是因为numpy安装的不够完整,解决方法如下:
sudo apt-get install python-numpy
5)验证python接口
进行python环境,引入caffe包,如果没有报错则安装成功!
如下所示:
lsq@lsq:~$ su
Password:
root@lsq:/home/lsq# python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>
这样caffe就安装成功了。