windows/ubuntu 系统下 caffe安装流程

Win10 下caffe安装(仅cpu、python接口)

参考博客:https://www.jianshu.com/p/16c872da684d

提前需要安装的程序:
windows/ubuntu 系统下 caffe安装流程_第1张图片

git checkout windows:
windows/ubuntu 系统下 caffe安装流程_第2张图片
修改scripts\build_win.cmd
windows/ubuntu 系统下 caffe安装流程_第3张图片
执行scripts\build_win.cmd:
windows/ubuntu 系统下 caffe安装流程_第4张图片

conda config --add channels conda-forge
conda config --add channels willyd
conda install --yes cmake ninja numpy scipy protobuf==3.1.0 six scikit-image pyyaml pydotplus graphviz
把caffe目录下的python\caffe整个文件夹,复制到anaconda的安装路径下Lib\site-packages中

windows/ubuntu 系统下 caffe安装流程_第5张图片

linux下caffe安装

参考博文:https://blog.csdn.net/yhaolpz/article/details/71375762
参考博文:https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html
参考博文: https://www.cnblogs.com/freeweb/p/5794447.html

1 安装依赖包
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 libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install git cmake build-essential
2 禁用 nouveau
sudo vim /etc/modprobe.d/blacklist-nouveau.conf
打开后发现该文件中没有任何内容,写入:blacklist nouveau option nouveau modeset=0 
sudo update-initramfs -u
3 配置环境变量
sudo gedit ~/.bashrc 
打开后在文件最后加入以下两行内容:
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH 
4 下载 CUDA
5 安装CUDA
验证cuda安装成功与否
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
6 安装 cudnn

https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html

下载完成后解压,得到一个 cudn 文件夹,该文件夹下include 和 lib64 两个文件夹,命令行进入 cudn/include 路径下,然后进行以下操作:

sudo cp cudnn.h /usr/local/cuda/include/ #复制头文件
然后命令行进入 cudn/lib64 路径下,运行以下命令:
sudo cp lib* /usr/local/cuda/lib64/ #复制动态链接库
cd /usr/local/cuda/lib64/sudo rm -rf libcudnn.so libcudnn.so.5 # 删除原有动态文件
#(cuda/lib64路径为安装cuda的路径)
sudo ln -s libcudnn.so.5.1.10 libcudnn.so.5 #生成软衔接
sudo ln -s libcudnn.so.5 libcudnn.so #生成软链接
安装完成后可用 nvcc -V 命令验证是否安装成功:

在这里插入图片描述

7 安装和编译oepncv
// 软件源更新
sudo apt-get update 
// 升级本地所有安装包,最新系统可以不升级,版本过高反而需要降级才能安装
sudo apt-get upgrade
// 安装build-essential、cmake、git和pkg-config
sudo apt-get install build-essential cmake git pkg-config 
// 安装jpeg格式图像工具包
sudo apt-get install libjpeg8-dev 
// 安装tif格式图像工具包
sudo apt-get install libtiff5-dev 
// 安装JPEG-2000图像工具包
sudo apt-get install libjasper-dev 
//安装视频I/O包(注意最后一个包的数字“4”后面是“L”):
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
//安装gtk2.0
sudo apt-get install libgtk2.0-dev
//优化函数包:
sudo apt-get install libatlas-base-dev gfortran
// 下载OpenCV
wget -O opencv-3.4.1.zip https://github.com/Itseez/opencv/archive/3.4.1.zip 
// 解压OpenCV
unzip opencv-3.4.1.zip
// 下载OpenCV_contrib库:
wget -O opencv_contrib-3.4.1.zip https://github.com/Itseez/opencv_contrib/archive/3.4.1.zip
// 解压OpenCV_contrib库:
unzip opencv_contrib-3.4.1.zip

// 打开源码文件夹,这里以我修改文章时最新的3.4.1为例
cd opencv-3.4.1
// 新建release文件夹
mkdir release
// 进入release文件夹
cd release
/** CMAKE_BUILD_TYPE是编译方式
* CMAKE_INSTALL_PREFIX是安装目录
* OPENCV_EXTRA_MODULES_PATH是加载额外模块   !!!!!!!!!!!注意!!!!!!!!!此处路径不要写错
* INSTALL_PYTHON_EXAMPLES是安装官方python例程
* BUILD_EXAMPLES是编译例程(这两个可以不加,不加编译稍微快一点点,想要C语言的例程的话,在最后一行前加参数INSTALL_C_EXAMPLES=ON \)
**/

sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D BUILD_EXAMPLES=ON ..
// 编译,以管理员身份,否则容易出错
sudo make -j8
// 安装
sudo make install
// 更新动态链接库
sudo ldconfig
安装完成后通过查看 opencv 版本验证是否安装成功:
pkg-config --modversion opencv 

安装过程时间可能会有点长10~30分钟左右,编译完成之后OpenCV就安装到了指定的/usr/local下面的一些目录中,库文件就安装到了/usr/local/lib下,Python的模块安装位置是:/usr/local/lib/python2.7/site-packages,执行 ls。
/usr/local/lib/python2.7/site-packages/ 可以看到两个文件,一个是cv2.so一个是cv.py

这两个文件在刚才opencv-2.4.13编译的目录下面lib/下也存在着两个python模块文件,但是不幸的是现在并不能直接使用,原因是我们操作系统python依赖包的位置是/usr/lib/python2.7下,所以刚才如果编译到/usr下是直接可以用的,不过这个也不重要,只要我们将这两个文件复制到正确的目录下,那么就能正常使用OpenCV的功能了,执行: cp /usr/local/lib/python2.7/site-packages/cv /usr/lib/python2.7/site-packages/* 复制过去之后进入python交互式界面执行 import cv2 没有报错则安装成功。

8 安装caffe
git clone https://github.com/BVLC/caffe.git
sudo cp Makefile.config.example Makefile.config
sudo vim Makefile.config  # 以下为我的Makefile.config文件

CUDA9.0 + anaconda2 + opencv3.4.1

## 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-9.0
CUDA_DIR := /home/username/cuda-9.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.
# 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_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)/python/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)
# 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

# 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 ?= @

然后修改 /username/cuda-9.0/include/host_config.h 文件 :

 将
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改为
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

OK ,可以开始编译了,在 caffe 目录下执行 :

make all -j8
sudo make runtest -j8

windows/ubuntu 系统下 caffe安装流程_第6张图片

9 安装 pycaffe notebook 接口环境

首先编译 pycaffe :

cd caffe
sudo make pycaffe -j8
手动配置caffe到系统静态链接库:
export LD_LIBRARY_PATH=/path_to_your_caffe/build/lib/:$LD_LIBRARY_PATH
完了之后再source保存一下

验证一下是否可以在 python 中导入 caffe 包:
python
在这里插入图片描述

你可能感兴趣的:(caffe)