本文是第一次在Ubuntu 16.04上安装Caffe(CPU Only)的过程。主要参考了以下链接:
官方安装文档:http://caffe.berkeleyvision.org/installation.html
官方Ubuntu安装文档:http://caffe.berkeleyvision.org/install_apt.html
博客:http://www.linuxidc.com/Linux/2016-09/135034.htm
http://www.linuxdiyf.com/linux/23093.html
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 libboost-all-dev
CPU Only的情况下,跳过了CUDA相关的安装;
接下来是BLAS:
sudo apt-get install libatlas-base-dev
使用默认Python来建立pycaffe接口,需要安装:
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
2、下载Caffe源码
没有安装git的话需要先装一下git
sudo apt-get install git
下载Caffe源码
git clone https://github.com/BVLC/caffe.git
如果需要Caffe的Python接口,切换到caffe下的python目录下,输入以下命令下载python依赖库(先安装pip):
sudo apt-get install python-pip
for req in $(cat requirements.txt); do pip install $req; done
3、编译Caffe
到Caffe文件夹中,拷贝一份Makefile.config.example并重命名成Makefile.config,修改该配置文件:
cp Makefile.config.example Makefile.config
使用文本编辑器打开Makefile.config,因为这里没有配置GPU,所以去掉CPU_ONLY := 1前面的注释;
由于Ubuntu16.04文件结构的变化,#Whatever else you find you need goes here.处要改成下面这样:
# 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
设置到这里开始编译,make pycaffe,结果报错,错误和numpy相关,重新打开Makefile.config目录,又查找了一下numpy的安装目录,发现对应不上,需要重新设置,需要把原本如下的内容:
# 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
更改为:
# 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
之后就是编译:
make pycaffe
make all
make test
make runtest
make默认单核运算,如果想加快速度,我这里是4核,可以在每条命令后面加上-j4,如果有报错,建议最好make clean重新开始。
如果所有测试都通过,则说明安装好了。
4、测试
测试Caffe的Python接口,切换到caffe/python文件目录下,记录下来当前路径,输入以下命令:
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
进入python环境,输入:
import caffe
如果没有报错,证明安装成功。
上面的方法,一旦关闭终端或者打开新终端则失效,如果放到配置文件中,可以永久有效果,命令操作如下:
#A.把环境变量路径放到 ~/.bashrc文件中
sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
#B.使环境变量生效
source ~/.bashrc
5、结束语
配置Caffe环境还是要仔细阅读官方文档的步骤,另外配置文件的注释也很详细。装完了才发现了一个更靠谱的教程,在Caffe的Github的Wiki里面,附上链接:
https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide
下一步再入坑GPU相关的安装~
以上内容主要参考https://blog.youkuaiyun.com/muzilinxi90/article/details/53673184博文 ,对于机器上只有python2.7版本应该可以解决安装问题。
由于我的机器事先已经安装tensorflow以及python3.5,一些python依赖包基本都安装在python3.5路径下面,导致后期用默认python2.7导入caffe时报错,以下错误信息供参考
(1)
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
solution:
sudo apt-get install python-matplotlib python-numpy python-pil python-scipy
sudo apt-get install build-essential cython
sudo apt-get install python-skimage
(2) error:
/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
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 8, in <module>
from caffe.proto import caffe_pb2
File "caffe/proto/caffe_pb2.py", line 6, in <module>
from google.protobuf.internal import enum_type_wrapper
ImportError: No module named google.protobuf.internal
solution:sudo apt-get install python-protobuf