本文主要参考大牛的博客https://www.cnblogs.com/xuanxufeng/p/6016945.html 然后填了一些自己安装过程中遇到的坑。
1.安装依赖
2.下载Caffe 使用Git直接下载Caffe非常简单,或者去https://github.com/BVLC/caffe下载。由于我习惯去github上找代码,所以就直接去下载的源码。
使用git下载caffe到家目录,下载完成后,会在家目录下的下载里找到caffe文件夹,切换到caffe文件夹中。
$ cd ~
3.编译Caffe (1)创建Makefile.comfig文件
cp Makefile.config.example Makefile.config
(2)修改配置文件Makefile.config
- style="font-size:14px;"># CPU-only switch (uncomment to build without GPU support).
- CPU_ONLY := 1 #一定需要打开。
- # Uncomment if you're using OpenCV 3
- # OPENCV_VERSION := 3 #用的是2.4版本不需要打开。
- # To customize your choice of compiler, uncomment and set the following.
- # N.B. the default for Linux is g++ and the default for OSX is clang++
- # CUSTOM_CXX := g++ #已经更新到至少4.8,选择默认的就好。
- # CUDA directory contains bin/ and lib/ directories that we need. #这是GPU用到的,注释掉就可以。
- #CUDA_DIR := /usr/local/cuda
- # On Ubuntu 14.04, if cuda tools are installed via
- # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
- # CUDA_DIR := /usr
- # CUDA architecture setting: going with all of them.
- # For CUDA < 6.0, comment the *_50 lines for compatibility. #这些部分全部注释,和我们cpu家族没有关系。
- #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_50,code=compute_50
- # BLAS choice:
- # atlas for ATLAS (default)
- # mkl for MKL
- # open for OpenBlas
- BLAS := atlas #如果安装mkl并且设置好路径的可以试一下mkl,不过一般会出错,找不到lmkl文件,建议采用atlas,简单易行。
- # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
- # Leave commented to accept the defaults for your choice of BLAS
- # (which should work)!
- # BLAS_INCLUDE := /path/to/your/blas
- # BLAS_LIB := /path/to/your/blas
- # Homebrew puts openblas in a directory that is not on the standard search path
- # BLAS_INCLUDE := $(shell brew --prefix openblas)/include
- # BLAS_LIB := $(shell brew --prefix openblas)/lib
- # This is required only if you will compile the matlab interface. #python很流行,matlab暂时没安装,也就用不到了。
- # MATLAB directory should contain the mex binary in /bin.
- # MATLAB_DIR := /usr/local
- # MATLAB_DIR := /Applications/MATLAB_R2012b.app
- # 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 \ #因为下安装的时候采用的时2.7版本的python,所以这里打开2.7版本的,但是要确保python安装在系统默认路径下。
- /usr/lib/python2.7/dist-packages/numpy/core/include
- # Anaconda Python distribution is quite popular. Include path:
- # Verify anaconda location, sometimes it's in root.
- # ANACONDA_HOME := $(HOME)/anaconda2
- # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
- $(ANACONDA_HOME)/include/python2.7 \
- $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
- # Uncomment to use Python 3 (default is Python 2)
- # PYTHON_LIBRARIES := boost_python3 python3.5m
- # PYTHON_INCLUDE := /usr/include/python3.5m \
- # /usr/lib/python3.5/dist-packages/numpy/core/include
- # We need to be able to find libpythonX.X.so or .dylib.
- PYTHON_LIB := /usr/lib
- # PYTHON_LIB := $(ANACONDA_HOME)/lib
- # Homebrew installs numpy in a non standard path (keg only)
- # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
- # PYTHON_LIB += $(shell brew --prefix numpy)/lib
- # Uncomment to support layers written in Python (will link against Python libs)
- # WITH_PYTHON_LAYER := 1
- # Whatever else you find you need goes here.
- INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial #增加hdf5路径
- LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
- # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
- # INCLUDE_DIRS += $(shell brew --prefix)/include
- # LIBRARY_DIRS += $(shell brew --prefix)/lib
- # Uncomment to use `pkg-config` to specify OpenCV library paths.
- # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
- # USE_PKG_CONFIG := 1
- BUILD_DIR := build #编译分配用到的路径,打开即可。
- DISTRIBUTE_DIR := distribute
- # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
- # DEBUG := 1 #挑错模式,喜欢debug的朋友不妨试试。
(3)编译 Caffe
- make pycaffe -jX #为了提高编译速度,这里的x设成自己的cpu核数
- make all -jX
- make test -jX
- make runtest -jX #注意,这里也可直接运行测试,make runtest
export LC_ALL="C"
4.编译Python接口 Caffe拥有python\C++\shell接口,在Caffe使用python特别方便,在实例中都有接口的说明。
- 确保pip已经安装
sudo apt-get install python-pip
- 执行安装依赖
在caffe根目录的python文件夹下,有一个requirements.txt的清单文件,上面列出了需要的依赖库,按照这个清单安装就可以了。
在安装scipy库的时候,需要fortran编译器(gfortran),如果没有这个编译器就会报错,因此,我们可以先安装一下。
首先回到caffe的根目录,然后执行安装代码:
安装完成以后,再次回到caffe根目录我们可以执行:
sudo pip install -r python/requirements.txt
就会看到,安装成功的,都会显示Requirement already satisfied, 没有安装成功的,会继续安装。
- 运行python结构
如果没有报错,说明caffe安装全部完成
注意:如果import caffe提示找不到caffe,需要将 PYTHONPATH加入.bashrc中:
- vim ~/.bashrc
- 在文件末端添加下面两句
- #set PYTHONPATH 设置caffe下的路径。否则caffe也找不到。
- export PYTHONPATH="/home/tom/caffe/python:$PYTHONPATH" 标红的地方换成自己的caffe路径
source ~/.bashrc
5.在Mnist运行Lenet
- 获取数据源
- 因为是CPU运行,所以修改在examples文件下的Mnist下的lenet_solver.prototxt中的solver_mode:CPU
solver_mode: CPU
- 训练模型
./examples/mnist/train_lenet.sh
6.最后,我的文章是基于各位前辈大神们的文章,虽然按他们的过程走我都报错了,但是最终还是帮助我安装成功。为了表示对别人成果的尊重,这里留下大神们的参考链接:
http://www.linuxidc.com/Linux/2016-09/135034.htm
http://blog.csdn.net/u010402483/article/details/51506616
http://www.cnblogs.com/denny402/p/5679037.html
http://blog.csdn.net/u012029332/article/details/51098248
http://blog.csdn.net/mandagod/article/details/52337434