【1】Ubuntu16.04+caffe-GPU安装

一.必要依赖包安装

sudo apt-get install build-essential
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install libblas-dev liblapack-dev libatlas-base-dev gfortran python-numpy

caffe依赖Boost、LMDB、LevelDB、glog、gflags、OpenCV、BLAS等库,其中 Boost最为庞大,功能也多,包含智能指针,函数对象,事件处理等;OpenCV专注于计算机视觉;LMDB、LevelDB是两个高性能的内存数据库;BLAS最小,主要为了完成矩阵运算;glog完成日志功能;gflags处理命令行参数解析。

二.安装pip和easy-install

cd
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
sudo python ez_setup.py --insecure
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py

三. 安装caffe及Python依赖

1).安装git

sudo apt-get install git

2).获取caffe源码(可cd到指定文件夹下下载):

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

3).在caffe根目录python文件夹下安装依赖项,目的是为了编译pycaffe接口用;

sudo apt-get install python-pip
sudo for req in $(cat "requirements.txt"); do pip install -i https://pypi.tuna.tsinghua.edu.cn/simple $req; done

四.编译caffe

打开终端

cd caffe
cp Makefile.config.example Makefile.config
vim Makefile.config

在打开的Makefile.config文件中做如下修改;

1).将USE_CUDNN := 1取消注释,表示使用CUDNN加速库,如果你不想使用它可以保持注释不变;
2).INCLUDE_DIRS := $(PYTHON_INCLUDE)/usr/local/include空格后然后添加/usr/include/hdf5/serial /usr/local/cuda-8.0/include/(添加cuda的include路径);

3).LIBRARY_DIRS:=$(PYTHON_LIB)/usr/local/lib /usr/lib   空格后添加 /usr/local/cuda-8.0/lib64/(添加cuda的lib路径);

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

# 以下 := 0表示禁用   := 1表示使用

# 可选择使用cudnn加速库,如果不需要使用,可以注释它
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1

# 如果机器不支持GPU,取消注释选择使用cpu
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

# 取消注释以禁用IO依赖项和相应的数据层(一般不做变动)
# 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

# 如果使用的是opencv3,取消注释
# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3

# 自定义的编译器类型,linux系统默认的是g++(不做变动)
# 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++

# 当选择使用GPU时,需要指定包含bin/和lib/的cuda安装路径,默认是/usr/local/cuda
#	这里我使用的机器做出了调整
# CUDA directory contains bin/ and lib/ directories that we need.
# CUDA_DIR := /usr/local/cuda
CUDA_DIR := /public/software/cuda-9.0

# 在Ubuntu 14.04,如果是用下面的命令安装的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架构的设置,如果cuda版本小于6.0 ,50及以后的都要注释
#	版本小于8.0 ,61要注释;大于等于9.0,20,21(第一行)需要注释
#	这里我采用的9.0,所以注释第一行
# 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_20,code=sm_20 \
		-gencode arch=compute_20,code=sm_21 \
		-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

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

# 基础线性代数子程序库,默认是atlas
# 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

# 如果需要编译matlab接口,需要指定以下路径
# 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

# 如果需要编译python接口,默认是系统下的2.7版本,需要指定以下路径
# 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/lib64/python2.7/site-packages/numpy/core/include

# 如果使用Anaconda下的路径,注释上面系统指定的python路径,取消下面四行,
#	找到ANACONDA_HOME的路径,并准确替换
# 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

# 如果需要使用python3,取消下面三行注释
# 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

# 取消注释以支持用Python编写的层
# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1

# 这里需要加入两个include和一个lib
# 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/hdf5/serial /public/software/cuda-9.0/include/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib  /public/software/cuda-9.0/lib64/

# 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

# 运行测试的GPU的ID将用于运行单元测试。
# 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 ?= @

接着就开始编译caffe

make all -j16   (16线程,线程越多速度越快)
make test -j16
make runtest -j16
make pycaffe  -j16  # 编译pycaffe接口

一般在编译make all 的时候可能会出错,后面出错的几率不大。

cd caffe/python
python 
import caffe 

没有报错,编译就成功了。

下面是我在另外一台机器上使用Anaconda2 python环境安装caffe的环境变量配置。

输入命令:vim ~/.bashrc ,在文件末尾添加下面三行内容

export PATH=/home/bxx-yll/anaconda2/bin:$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/bxx-yll/anaconda2/lib
export PYTHONPATH=/home/bxx-yll/caffe/python:$PYTHONPATH

输入命令: source ~/.bashrc 使得当前修改生效。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值