Ubuntu14.04 安装Caffe(仅CPU)
目录:
前言:
-
按照安装Caffe时,要装Cuda,按照, 命令行输入:$ lspci | grep -i nvidia,发现没有nvidia显卡,所以只能暂时放弃Gpu,改用Cpu了。
GPU版本
-
详见:
正文:
安装依赖库(一):
$ 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
安装BLAS:
$ sudo apt-get install libatlas-base-dev
-
可以安装OpenBLAS 或 MKL,以提升CPU性能,但是要修改caffe中Makefile文件…
安装python
-
我的Ubuntu14.04自带 可使用pycaffe接口
安装matlab
-
详见: 如不使用matcaffe接口,可以不装
安装opencv
-
详见: 注:opencv必须安装,且版本为>=2.4或3.0
安装依赖库(二):
$ sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
下载Caffe:
$ cd ~ $ git clone git://github.com/BVLC/caffe.git
如果安装的是opencv3.0:
a、修改Makefile,在
LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system hdf5_hl hdf5 m opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
处加入后面的opencv_imgcodecs,因为opencv3.0.0把imread相关函数放到imgcodecs.lib中了(原来是imgproc.lib)
b、修改caffe/examples/cpp_classification/classification.cpp文件,加入:
#include <opencv2/imgproc/types_c.h> #include <opencv2/objdetect/objdetect_c.h>
否则会出现”CV_BGR2GRAY”的错误
编译Caffe:
$ cd ~/caffe $ cp Makefile.config.example Makefile.config # 修改Makefile.config文件:去掉CPU_ONLY:= 1的注释 $ make all $ make test $ make runtest
配置pycaffe:
a、安装依赖库:
$ sudo apt-get install python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags Cython ipython $ sudo apt-get install protobuf-c-compiler protobuf-compiler
b、编译:
$ cd ~/caffe $ make pycaffe
c、添加~/caffe/python到$PYTHONPATH:
$ sudo gedit /etc/profile # 末尾添加: export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH # 用完整路径,不要用~ $ source /etc/profile # 使之生效
d、测试是否可以引用:
$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import caffe >>>
配置matcaffe:
a、gcc降级(Ubuntu14.04自带的gcc版本是4.8,MATLAB2014a支持的最高版本为4.7x。因此,需要安装gcc4.7,并给gcc降级): 详见
sudo apt-get install gcc-4.7 g++-4.7 g++-4.7-multilib gcc-4.7-multilib sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.7 100 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 100 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50 sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.7 100 sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.8 50 # 验证gcc默认版本: $ gcc -v
b、编译:
$ cd ~/caffe # 修改Makefile.config文件,MATLAB_DIR := /usr/local/MATLAB/R2014a $ make matcaffe
c、添加工作空间:
$ sudo matlab -nodesktop -nosplash >>> addpath ~/caffe/matlab >>> savepath
即可