安装Caffe-Master(GPU和CPU)

温馨提示

由于caffe的作者已经停止更新,并推荐我们使用pytorch,该文章将于2020年01月01日正式删除。

安装简介

如果你没有NVIDIA独立显卡,你可以直接跳过环境配置的前三条,并在最后安装Caffe前,对Makefile.config中关于前三条的选项(GPU/CUDA/cudnn)进行注释,然后取消CPU的注释即可

安装前的环境配置

  • 安装显卡驱动
  • 安装cuda-8.0
  • 安装cuDNN-8.0-v5.0
  • 安装MKL
  • 安装Anaconda3
  • 安装MATHWORKS_R2018B
  • 安装OpenCV-4.0.0

依赖包安装

conda install GraphViz
pip install protobuf
pip install pydot
  • System>=Ubuntu 16.04的看这里

sudo apt install libatlas-base-dev #根据需求安装,使用OpenBlas或MKL的用户可以跳过
sudo apt install --no-install-recommends libboost-all-dev
sudo apt install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
  • System>=Ubuntu 17.04并且喜欢偷懒的看这里

sudo apt install caffe-cpu  #编译caffe时只能用CPU_ONLY模式
sudo apt install caffe-cuda #编译caffe时可启用USE_CUDNN/CUDA_DIR/CUDA_ARCH
#以上两行命令任选其一

下载压缩文件

官方最新下载地址:GitHub地址

如果官网最新的caffe资源无法支持该网页的配置方案,我这里准备了可用的旧版本。点击此处下载旧版

准备压缩文件

将下载好的压缩包提取到当前用户根目录下,并打开该文件夹

修改配置文件

找到Makefile.config.example,并重命名为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 := 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-8.0
# 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_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 := mkl
# 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/R2014a
# 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)/anaconda
# 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)
ANACONDA_HOME  := $(HOME)/Anaconda3
PYTHON_LIBRARIES := boost_python-py35 python3.6m
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		  $(ANACONDA_HOME)/include/python3.6m \
		  $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
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 ?= @

为安装matcaffe接口做准备

A:修改Makefile,在该文件411行附近,插入以下命令

CXXFLAGS += -std=c++11

开始安装

cd caffe-master
make all -j
make matcaffe     #若最后一行出现“MEX 已成功完成”,则代表matcaffe接口编译成功
make pycaffe

安装测试

make mattest
make pytest

如果出现以下提示则证明调用matlab接口成功,否则请看系统调整进行修复

Totals:
   7 Passed, 0 Failed, 0 Incomplete.
   0.31512 seconds testing time.

如果出现以下提示则证明调用python接口成功,否则请看系统调整进行修复

Ran 51 tests in 13.732s
OK (skipped=8)

注意,若出现"(skipped=8)"字眼,说明在安装前,没有开启"WITH_PYTHON_LAYER",若开启之后,这个提示不应该出现

部署pycaffe

A:复制Python接口

在当前用户根目录下打开终端,输入以下命令即可

cp -r caffe-master/python/caffe Anaconda3/lib/python3.6/site-packages

B:添加环境变量

在当前用户根目录下,找到.bashrc文件并打开,在最后添加下面两行文本,保存退出即可

# added by python caffe interface
export LD_LIBRARY_PATH="$HOME/caffe-master/build/lib:$LD_LIBRARY_PATH"

C:运行测试

重启终端后,进入python环境,输入以下命令,如果能正常加载caffe,则部署成功

安装Caffe-Master(GPU和CPU)_第1张图片

系统调整

根据提示,缺了哪个就执行哪个命令即可

sudo ln -sf /usr/lib/x86_64-linux-gnu/libstdc++.so.6    /usr/local/MATLAB/R2014a/bin/glnxa64
sudo ln -sf /usr/local/cuda-8.0/lib64/libcublas.so.8.0  /usr/local/MATLAB/R2014a/bin/glnxa64
sudo ln -sf /usr/local/cuda-8.0/lib64/libcudart.so.8.0  /usr/local/MATLAB/R2014a/bin/glnxa64
sudo ln -sf /usr/local/cuda-8.0/lib64/libcudnn.so.5     /usr/local/MATLAB/R2014a/bin/glnxa64

部分文章摘录,感谢以下的网友提供的解决方案

http://blog.csdn.net/houchaoqun_xmu/article/details/72822199

以下链接提供不存在链接使用的解决办法

http://blog.sina.com.cn/s/blog_721a75e50102wfig.htm

以下链接提供Python3的兼容问题解决思路

http://blog.sina.com.cn/s/blog_63cdc3630102wea5.html

以下链接用于解决Python3依赖项

https://stackoverflow.com/questions/35177262/importerror-no-module-named-pydot-unable-to-import-pydot

以下链接主要为匹配Anaconda3(Python3.6.1)的boost

http://blog.csdn.net/songyu0120/article/details/77895373

如果本篇文章能让你成功安装该套配置,请顺手点个赞,毕竟这篇文章我重装了N次系统和工具才完成的。。

你可能感兴趣的:(安装Caffe-Master(GPU和CPU))