ubuntu16.04和服务器 caffe 安装

在centos6.X上安装caffe

0.编译安装gcc4.8.5

由于centos6.x中的gcc版本老旧,不支持c++11所以要安装gcc4.8.5,以下是安装教程。参考CentOS 6.4 编译安装 gcc-4.8.0
解压安装包进入目录执行download_prerequisites脚本./contrib/download_prerequisites
新建buildmkdir build
进入build目录执行

../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib(生成Makefile文件)

修改Makefile文件中prefix=安装路径,这里的安装路径是/home/guanjun/caffe_lib/third/gcc-4.8.5
注意本文以下的安装路径都是/home/guanjun/caffe_lib/third下的对应目录

make -j32
make install

安装完成后要将gcc4.8.5中bin目录添加到环境变量(临时创建env_caffe.sh)
在env_caffe.sh中添加

export PATH=/home/guanjun/caffe_lib/third/gcc-4.8.5/bin:$PATH

1.安装Anaconda python 环境

执行安装文件
./Anaconda2-4.2.0-Linux-x86_64.sh
注意在提示的最后的选项选no即不添加到.bashrc
之后同样在env_caffe.sh中添加export PATH=/home/guanjun/anaconda2/bin:$PATH
之后执行下面的命令
source ~/env_caffe.sh
因为编译boost时会用到python环境

2.编译安装boost

解压安装包然后执行

./bootstrap.sh
./b2 install --prefix=安装路径  

参考boost Installation

3.编译安装opencv

解压安装包然后进入安装包执行

mkdir build
cd build
ccmake ../

按照提示加载配置文件(按c)、修改cmake_install_prefix路径为安装路径、
WITH CUDA WITH CUFFT WITH JASPER分别设置为off,按照提示保存退出(按c 按g),然后执行

make -j32
make install

4.编译安装glog

解压然后依次执行

./configure --prefix=安装路径
make -j32
make intstall

5.编译安装gflags

解压然后进入解压文件依次执行

mkdir build
cd build
export CXXFLAGS="-fPIC"
ccmake ../

按c加载配置文件、设置安装路径按c g退出,之后执行

make -j32
make install

6.编译安装lmdb

首先下载lmdb安装包执行git clone https://github.com/LMDB/lmdb
打开lmdb中MakeFile文件、修改安装路径

make -j
make install

7.安装openblas

下载新版OpenBLASgit clone https://github.com/xianyi/OpenBLAS
进入OpenBLAS打开目录中cpuid.h文件在倒数第二行添加#define NO_AVX2 1024然后执行

make -j32
make install PREFIX=安装路径

8.编译安装hdf5

解压、进入文件执行

./configure --prefix=安装路径
make -j32
make install

9.编译安装protobuf

解压、进入文件执行

./configure --prefix=安装路径
make -j32
make install

之后将protobuf添加到环境变量中(env_caffe.sh)export PATH=/home/guanjun/caffe_lib/third/protobuf/bin:$PATH
在编译caffe前确保env_caffe.sh文件如下

export PATH=/home/guanjun/caffe_lib/third/gcc-4.8.5/bin:/home/guanjun/anaconda2/bin:/home/guanjun/caffe_lib/third/protobuf/bin:$PATH
export PYTHONPATH=/home/guanjun/caffe/py-R-FCN/caffe/python:$PYTHONPATH
export LD_LIBRARY_PATH=/home/guanjun/caffe_lib/third_source/leveldb/out-shared:/home/guanjun/anaconda2/lib:/usr/local/cuda/lib64:/home/guanjun/caffe_lib/third/boost/lib:/home/guanjun/caffe_lib/third/hdf5/lib:/home/guanjun/caffe_lib/third/lmdb/lib:/home/guanjun/caffe_lib/third/openblas_v1/lib:/home/guanjun/caffe_lib/third/opencv/lib:/home/guanjun/caffe_lib/third/protobuf/lib:/home/guanjun/caffe_lib/third/glog/lib:/home/guanjun/caffe_lib/third/gflags/lib:/home/guanjun/caffe_lib/third/glibc-2.14/lib:/home/guanjun/caffe_lib/third/gcc-4.8.5/lib64:$LD_LIBRARY_PATH

/home/guanjun/替换成/home/你的用户名/
同时,保证caffe中的Makefile.config和下面的配置文件一样

# Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
D_PATH := /home/guanjun/caffe_lib/third
# 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 lines for compatibility.
#CUDA_ARCH := -gencode arch=compute_20,code=sm_20 
#		-gencode arch=compute_20,code=sm_21 
CUDA_ARCH := -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
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /home/guanjun/caffe_lib/third/openblas_v1/include
BLAS_LIB := /home/guanjun/caffe_lib/third/openblas_v1/lib

# 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/guanjun/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 := $(D_PATH)/protobuf/include
#INCLUDE_DIRS := /data/shiyang/anaconda2/include
INCLUDE_DIRS += $(D_PATH)/hdf5/include
INCLUDE_DIRS += $(D_PATH)/gflags/include
INCLUDE_DIRS += $(D_PATH)/glog/include
INCLUDE_DIRS += $(D_PATH)/opencv/include
INCLUDE_DIRS += $(D_PATH)/boost/include
INCLUDE_DIRS += $(D_PATH)/lmdb/include
INCLUDE_DIRS += $(D_PATH)/glibc-2.14/include
INCLUDE_DIRS += $(D_PATH)/gcc-4.8.5/include
INCLUDE_DIRS += /home/guanjun/caffe_lib/third_source/leveldb/include

LIBRARY_DIRS := $(D_PATH)/protobuf/lib
#LIBRARY_DIRS := /data/shiyang/anaconda2/lib
LIBRARY_DIRS += $(D_PATH)/hdf5/lib
LIBRARY_DIRS += $(D_PATH)/gflags/lib
LIBRARY_DIRS += $(D_PATH)/glog/lib
LIBRARY_DIRS += $(D_PATH)/opencv/lib
LIBRARY_DIRS += $(D_PATH)/boost/lib
LIBRARY_DIRS += $(D_PATH)/lmdb/lib
LIBRARY_DIRS += $(D_PATH)/glibc-2.14/lib
LIBRARY_DIRS += $(D_PATH)/gcc-4.8.5/lib64
LIBRARY_DIRS += /home/guanjun/caffe_lib/third_source/leveldb/out-shared


INCLUDE_DIRS += $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS += $(PYTHON_LIB) /usr/local/lib /usr/lib
# 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

# 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 ?= @

之后执行source ~/env_caffe.sh
进入caffe目录执行

make -j32
make runtest
make pycaffe

将caffe的python添加到环境变量export PYTHONPATH=/home/guanjun/caffe/py-R-FCN/caffe/python:$PYTHONPATH就是env_caffe.sh中的第二行。
新建一个python文件测试import caffe是否可用。


在本地Ubuntu16.04上安装caffe

1.安装cuda

先把错配的显卡驱动清理干净
sudo apt-get --purge remove nvidia-*
https://developer.nvidia.com/cuda-downloads下载对应的deb文件(cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb)
到deb的下载目录下

sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb 
sudo apt-get update 
sudo apt-get install cuda
sudo reboot

参考ubuntu 14.04 现在安装cuda7.5超级简便,惊了

2.安装caffe

安装依赖

sudo apt-get install -y opencl-headers build-essential protobuf-compiler 
    libprotoc-dev libboost-all-dev libleveldb-dev hdf5-tools libhdf5-serial-dev 
    libopencv-core-dev  libopencv-highgui-dev libsnappy-dev 
    libatlas-base-dev cmake libstdc++6-4.8-dbg libgoogle-glog0v5 libgoogle-glog-dev 
    libgflags-dev liblmdb-dev git python-pip gfortran libopencv-dev
sudo apt-get clean

下载caffe并安装caffe python依赖

git clone https://github.com/BVLC/caffe.git
cd caffe
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done

准备Makefile.config,以便它可以ubuntu上构建

cd ../
cp Makefile.config.example Makefile.config

修改Makefile.config如下

## Refer to http://caffe.berkeleyvision.org/installation.html
# 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 := 1
# 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 lines for compatibility.

CUDA_ARCH := -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 := open
# 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/guan/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 /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

# 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 ?= @

注意修改路径。
执行

make all -j
make runtest
make pycaffe

执行echo "export PYTHONPATH=/opt/cat-dogs/repo/caffe/python:$PYTHONPATH" >> ~/.bashrc这句也可以不添加到.bashrc,可以自己写个env_caffe.sh每次用caffe的时候source env_caffe.sh


编译caffe时出现的问题和解决方法(本地ubuntu16.04和服务器centos)

1.编译caffe时出现的错误

错误

.build_release/src/caffe/proto/caffe.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is

解决方法下载新版本的、编译安装

sudo apt-get install autoconf automake libtool
git clone https://github.com/google/protobuf 
./autogen.sh
 ./configure
make
make check
sudo make install

错误

/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory

解决方法

export CPLUS_INCLUDE_PATH=/usr/include/python2.7
make clean
make all -j2

错误

fatal error: caffe/proto/caffe.pb.h: No such file or directory

解决方法

protoc src/caffe/proto/caffe.proto --cpp_out=.
mkdir include/caffe/proto
mv src/caffe/proto/caffe.pb.h include/caffe/proto

2.make runtest出现的错误

错误

.build_release/tools/caffe: error while loading shared libraries: libprotobuf.so.14: cannot open shared object file: No such file or directory
Makefile:526: recipe for target 'runtest' failed

解决方法添加链接路径

export LD_LIBRARY_PATH=/usr/local/lib/

3.import caffe时出现的错误

错误
No module named google.protobuf.internal
解决方法
/home/guan/anaconda2/bin/pip install protobuf

错误

/home/guan/anaconda2/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found

解决方法
conda install libgcc

错误
No module named google.protobuf.internal
解决方法
/home/guan/anaconda2/bin/pip install protobuf

4.runtest出现的错误

错误

src/caffe/test/test_gradient_based_solver.cpp:373: Failure
The difference between expected_updated_weight and solver_updated_weight is 1.7136335372924805e-07, which exceeds error_margin, where
expected_updated_weight evaluates to 9.6857547760009766e-06,
solver_updated_weight evaluates to 9.8571181297302246e-06, and
error_margin evaluates to 1.0000000116860974e-07.
[  FAILED  ] NesterovSolverTest/2.TestNesterovLeastSquaresUpdateWithEverything, where TypeParam = caffe::GPUDevice<float> (6484 ms)

解决方法
执行export CUDA_VISIBLE_DEVICES=0,重新执行测试。
参考runtest出现的问题

原文地址:https://www.cnblogs.com/pk28/p/7761055.html