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目錄執行html

../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下的對應目錄python

make -j32
make install

安裝完成後要將gcc4.8.5中bin目錄添加到環境變量(臨時建立env_caffe.sh)
在env_caffe.sh中添加linux

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環境c++

2.編譯安裝boost

解壓安裝包而後執行git

./bootstrap.sh
./b2 install --prefix=安裝路徑

參考boost Installationgithub

3.編譯安裝opencv

解壓安裝包而後進入安裝包執行shell

mkdir build
cd build
ccmake ../

按照提示加載配置文件(按c)、修改cmake_install_prefix路徑爲安裝路徑、
WITH CUDA WITH CUFFT WITH JASPER分別設置爲off,按照提示保存退出(按c 按g),而後執行bootstrap

make -j32
make install

4.編譯安裝glog

解壓而後依次執行ubuntu

./configure --prefix=安裝路徑
make -j32
make intstall

5.編譯安裝gflags

解壓而後進入解壓文件依次執行centos

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出現的問題

相關文章
相關標籤/搜索