转载:安装Caffe-Master(GPU和CPU)

版权声明:本文转自博主 代码小哥原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文原链接: https://blog.csdn.net/huang826336127/article/details/77148504

安装简介

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

安装前的环境配置

依赖包安装

conda install GraphViz
pip install protobuf
pip install pydot

 
 
  • 1
  • 2
  • 3
  • 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

 
 
  • 1
  • 2
  • 3
  • System>=Ubuntu 17.04并且喜欢偷懒的看这里

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

 
 
  • 1
  • 2
  • 3

下载压缩文件

官方最新下载地址: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).
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 ?= @


 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

为安装matcaffe接口做准备

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

CXXFLAGS += -std=c++11

 
 
  • 1

开始安装

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

 
 
  • 1
  • 2
  • 3
  • 4

安装测试

make mattest
make pytest

 
 
  • 1
  • 2

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

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

 
 
  • 1
  • 2
  • 3

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

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

 
 
  • 1
  • 2

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

部署pycaffe

A:复制Python接口

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

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

 
 
  • 1

B:添加环境变量

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

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

 
 
  • 1
  • 2

C:运行测试

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

python中的caffe加载测试图

系统调整

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

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

 
 
  • 1
  • 2
  • 3
  • 4

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

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次系统和工具才完成的。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值