在ubuntu 下,使用CPU模式安装caffe,在这个过程中,其实没有什么太多难点,主要就是各种依赖包的安装,而且还有版本的问题,由于caffe编写的过程中,用了许多第三方库,而且都是和某一个版本对应的,因此安装的过程是需要注意这一点的。
1、首先把opencv安装了:
可参考一下几篇文章:
http://blog.csdn.net/surgewong/article/details/39078251#
http://blog.csdn.net/a1429331875/article/details/31539129
http://blog.csdn.net/tina_ttl/article/details/52745807
具体安装过程:
opencv的github源文件:https://github.com/opencv/opencv,下载master分支,或者2.4版本分支,git clone到本地,然后安装opencv最关键的是安装各种依赖包:
mkdir build,进入cmake ..,make -j8,,make install等即可:如下所示
安装完成后,便要做一些收尾工作:
至此完成了opencv的安装。
2、完成caffe的安装:
在可能的情况下,安装anaconda2或者anaconda3,这里采用anaconda2,已有默认~/.bashrc中路径的导入要优先搜索anaconda2(切记)
需要装一些依赖库:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
然后修改Makefile.config文件:cpu-only关键的就需要改几个地方:
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
这里安装的主要是opencv3,(github下面master分支),因此是opencv3的版本,可以进入到/usr/local/lib下面查看opencv的版本
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
注释掉python的路径,替换成anaconda的路径:
# 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
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib
# 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
修改之后,便可以mkdir build, cd build, cmake .., make all -j8即完成编译链接。
然后修改python的路径:
在这个过程中,发现关键是opencv和google protobuf的安装版本对应问题:
make all -j8编译链接到81%的时候,总是出错,说protobuf找不到,有冲突,主要是因为apt-get 安装了protobuf,conda install 也安装了protobuf,这样版本不一致。
解决方案是:使用conda list protobuf查看当前conda 下安装了几个版本。然后使用conda remove protobuf来删除掉。现在只留apt-get install 下的protobuf,然后安装,在安装完之后,import caffe的话,会出现没有protobuf的问题,此时再conda install protobuf来重新安装python版的protobuf,这样就可以import caffe了。
一些问题和解决方案:
python caffe报错:No module named google.protobuf.internal: http://blog.csdn.net/wuzuyu365/article/details/52431062
caffe支持pycaffe的安装:ImportError: No module named google.protobuf.internal : http://blog.csdn.net/liyaohhh/article/details/50876777
ImportError: /home/kzl/anaconda2/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found : http://blog.csdn.net/lwgkzl/article/details/77658269
ubuntu14.04安装caffe-master 报错:./include/caffe/util/mkl_alternate.hpp:11:19:fatal error:cblas.h: No such file or directory #include<cblas.h>: http://blog.csdn.net/feelingjun/article/details/68512659
在安装的过程中,发现google::protobuf总是在链接的过程中出现问题,报错内容是
Caffe compile error: undefined reference to `google::protobuf #4558,原因是conda中安装了libprotobuf和protobuf,与系统使用apt-get install方式安装的版本冲突,果断采用conda list protobuf和conda remove protobuf来删除,最终编译通过。
protobuf的安装:http://blog.csdn.net/hailong0715/article/details/52057873
安装caffe的方法:
http://blog.csdn.net/fromlimbo/article/details/51038749
http://www.linuxidc.com/Linux/2016-09/135034.htmX
https://chunml.github.io/ChunML.github.io/project/Installing-Caffe-CPU-Only/
(1) MKL,ATLAS的问题;http://blog.csdn.net/feelingjun/article/details/68512659
(2) GLIBCXX_3.4.21' not found: http://blog.csdn.net/lwgkzl/article/details/77658269
(3) ImportError: No module named google.protobuf.internal: http://blog.csdn.net/liyaohhh/article/details/50876777, http://blog.csdn.net/wuzuyu365/article/details/52431062
(4) python can't import _caffe module: https://github.com/BVLC/caffe/issues/263
(5) recipe for target '.build_release/tools/extract_features.bin' failed: 发现编译链接之后,缺少文件,重新编译一下
(6) Error: 'make all' 'make test' #2348: https://github.com/BVLC/caffe/issues/2348
(7) build failed in "make -j8 && make pycaffe" step #72: https://github.com/rbgirshick/fast-rcnn/issues/72
(8) Google protobuf in Linux: https://stackoverflow.com/questions/2456664/google-protobuf-in-linux, https://github.com/BVLC/caffe/issues/3046
(9) protobuf安装: http://www.cnblogs.com/luoxn28/p/5303517.html
(10) Caffe compile error: undefined reference to `google::protobuf #4558: https://github.com/BVLC/caffe/issues/4558
Caffe样例测试:
Makefile.config
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# 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++
# CUDA directory contains bin/ and lib/ directories that we need.
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 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
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_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := 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.
# 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 \
# /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
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
# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1
# 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
# N.B. both build and distribute dirs are cleared on `make clean`
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
# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0
# enable pretty build (comment to see full commands)
Q ?= @