ubuntu16安装caffe,配置python,matlab接口

安装编译caffe真是巨多的坑,记录下自己安装的过程

个人建议安装好anaconda后先安装caffe,再安装tensorflow-gpu,tensorflow-gpu会有protobuf依赖,之前看到有博客上说这可能会自己安装的起冲突

安装依赖项

sudo apt-get install build-essential
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
sudo apt-get install libatlas-base-dev
sudo apt-get install python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

在github上下载caffe:

sudo apt-get install git#安装git

git clone https://github.com/BVLC/caffe.git

到下载的caffe目录下

将Makefile.config.example的内容复制到Makefile.config并打开该文件(make命令只能make *.config文件)
sudo cp Makefile.config.example Makefile.config#复制文件

sudo gedit 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 := 1
# USE_LEVELDB := 0
# USE_LMDB := 0
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 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.
# For CUDA >= 9.0, comment the *_20 and *_21 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_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
 MATLAB_DIR := /usr/local/MATLAB/R2017b
# 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)/anaconda3
 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		 $(ANACONDA_HOME)/include/python3.6m \
		 $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
 PYTHON_LIBRARIES := boost_python3 python3.6m
 PYTHON_INCLUDE := /usr/include/python3.6m \
                 /usr/lib/python3.6/dist-packages/numpy/core/include \
                  /home/liu/anaconda3/include/python3.6m

# We need to be able to find libpythonX.X.so or .dylib.
#PYTHON_LIB := /usr/lib
 PYTHON_LIB := $(ANACONDA_HOME)/lib \
               $(ANACONDA_HOME)/pkgs/python-3.6.5-hc3d631a_2/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
#LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include /usr/lib/x86_64-linux-gnu/hdf5/serial/include
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 ?= @

修改makefile

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m 
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

也可用cmake安装:在caffe目录下

mkdir build && cd build && cmake-gui ..

在跳出来的cmake-gui界面中调整配置:Browse Source,选择caffe目录,Browse Build选择build目录,再点击左下角的configure按钮,yes(选择建立build目录),选择默认的Unix makefiles和use default native compilers 点击finish

里面没列出的可add entry

下图是设置CMAKE_CXX_FLAGS: -std=c++11

示例如下

USE_CUDNN ON
USE_OPENCV ON
Build_python ON
Build_python_layer ON

BLAS atlas
CMAKE_CXX_FLGAS -std=c++11#这个是为了防止产生下面的error1

CMAKE_INSTALL_PREFIX /home/liu/caffe/build/insta

配置结果如下

******************* Caffe Configuration Summary *******************
General:
  Version           :   1.0.0
  Git               :   1.0-132-g99bd997-dirty
  System            :   Linux
  C++ compiler      :   /usr/bin/c++
  Release CXX flags :   -O3 -DNDEBUG -fPIC -Wall -std=c++11 -Wno-sign-compare -Wno-uninitialized
  Debug CXX flags   :   -g -fPIC -Wall -std=c++11 -Wno-sign-compare -Wno-uninitialized
  Build type        :   Release

  BUILD_SHARED_LIBS :   ON
  BUILD_python      :   ON
  BUILD_matlab      :   OFF
  BUILD_docs        :   ON
  CPU_ONLY          :   OFF
  USE_OPENCV        :   ON
  USE_LEVELDB       :   ON
  USE_LMDB          :   ON
  USE_NCCL          :   OFF
  ALLOW_LMDB_NOLOCK :   OFF
  USE_HDF5          :   ON

Dependencies:
  BLAS              :   Yes (Atlas)
  Boost             :   Yes (ver. 1.58)
  glog              :   Yes
  gflags            :   Yes
  protobuf          :   Yes (ver. 3.6.1)
  lmdb              :   Yes (ver. 0.9.17)
  LevelDB           :   Yes (ver. 1.18)
  Snappy            :   Yes (ver. ..)
  OpenCV            :   Yes (ver. 3.4.5)
  CUDA              :   Yes (ver. 9.2)

NVIDIA CUDA:
  Target GPU(s)     :   Auto
  GPU arch(s)       :   sm_35
  cuDNN             :   Yes (ver. 7.4.2)

Python:
  Interpreter       :   /usr/bin/python2.7 (ver. 2.7.12)
  Libraries         :   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
  NumPy             :   /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.0)

Documentaion:
  Doxygen           :   No
  config_file       :   

Install:
  Install path      :   /home/liu/下载/caffe/build/install

Configuring done

再执行下面的命令即可

make clean
make -j8 
make pycaffe

注意,这种方法默认的是找到系统自带的python2.7,目前我不知怎么配置到anaconda,如果不需要配置到anaconda上可以用这种方法。参考链接caffe官网cmake方法编译Ubuntu16.04 安装 CUDA、CUDNN、OpenCV 并用 Anaconda 配置 Tensorflow 和 Caffe 详细过程

 

踩的坑:

error1.

In file included from /usr/include/c++/5/atomic:38:0,
                 from /usr/local/include/google/protobuf/io/coded_stream.h:113,
                 from .build_release/src/caffe/proto/caffe.pb.h:23,
                 from ./include/caffe/util/cudnn.hpp:8,
                 from ./include/caffe/util/device_alternate.hpp:40,
                 from ./include/caffe/common.hpp:19,
                 from ./include/caffe/util/math_functions.hpp:9,
                 from src/caffe/solvers/nesterov_solver.cu:1:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \

解决方案:1.在CMakelists.txt中加入下面的代码(这个方法对我没用)

#将下面的内容添加到CMakeLists.txt当中
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
        message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()


#或将CMakeLists.txt中下面的代码段
if(UNIX OR APPLE)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall")
endif()if()
改为
if(UNIX OR APPLE)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -std=c++11")
endif()if()

#还有可能需要改makefile文件:在对应的行后加上-std=c++11
CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) -std=c++11
LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

                 2.可能是因为有两个.so和.a libs(我也不知道啥意思)

                    这种情况输入下面的代码后重新编译即可

make clean
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
sudo ldconfig

最后,这个问题主要参考了c++11支持解决方案

 

error2. ./include/caffe/util/cudnn.hpp:8:34: fatal error: caffe/proto/caffe.pb.h: 没有那个文件或目录

输入下面的命令即可

sudo pip install protobuf --upgrade -i http://pypi.douban.com/simple 
sudo pip install pillow --upgrade -i http://pypi.douban.com/simple

error3. /usr/bin/ld: 找不到 -lboost_python3
            collect2: error: ld returned 1 exit status
            Makefile:582: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
            make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1

            解决方法:

cd /usr/lib/x86_64-linux-gnu

sudo ln -s libboost_python-py35.so libboost_python3.so

error4. 

//home/liu/anaconda3/lib/libpng16.so.16��//home/liu/anaconda3/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
�对‘inflateValidate@ZLIB_1.2.9’未定义的引用
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
//home/liu/anaconda3/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
//home/liu/anaconda3/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
//home/liu/anaconda3/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
//home/liu/anaconda3/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
//home/liu/anaconda3//home/liu/anaconda3/lib/libpng16.so.16:

解决方法:在Makefile.config中加入下面

LINKFLAGS := -Wl,-rpath,/home/liu/anaconda3/lib

重新编译即成功

 

/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: 没有那个文件或目录

原因:

python路径都改为了anaconda的路径,但是CPLUS的path没有设置,系统中我也没把aneconda设置成默认的python导致的.

解决方法:

make clean

export CPLUS_INCLUDE_PATH=/home/liu/anaconda3/include/python3.6m

make all -j8

运行

run runtest -j8

error5: 

[ RUN      ] AdaGradSolverTest/0.TestLeastSquaresUpdateWithEverythingAccumShare
Warning! ***HDF5 library version mismatched error***
The HDF5 header files used to compile this application do not match
the version used by the HDF5 library to which this application is linked.
Data corruption or segmentation faults may occur if the application continues.
This can happen when an application was compiled by one version of HDF5 but
linked with a different version of static or shared HDF5 library.
You should recompile the application or check your shared library related
settings such as 'LD_LIBRARY_PATH'.
You can, at your own risk, disable this warning by setting the environment
variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.
Setting it to 2 or higher will suppress the warning messages totally.
Headers are 1.8.16, library is 1.10.2

查找解决方案

conda install hdf5=1.8.16

 

下面是配置python和matlab的接口

make pycaffe

make matcaffe

编译报错解决方法

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值