1. 程式人生 > >搭建caffe環境及訓練模型

搭建caffe環境及訓練模型

visio macosx gpu .sh 編寫 共享庫 image ins bash

Mac OS搭建caffe環境

一、相關軟件安裝

1、 安裝CUDA8

(1) 安裝xcode及命令行

(2) 安裝CUDA的dmg包

(3) 配置環境變量並source ~/.bash_profile

export CUDA_ROOT=/Developer/NVIDIA/CUDA-8.0

export PATH=$CUDA_ROOT/bin${PATH:+:${PATH}}

export DYLD_LIBRARY_PATH=$CUDA_ROOT/lib${DYLD_LIBRARY_PATH:+:${DYLD_LIBRARY_PATH}}

export LD_LIBRARY_PATH=$CUDA_ROOT/lib:$LD_LIBRARY_PATH

(4) 執行nvcc –V驗證安裝

2、 安裝必要的包

brew install –vd snappy leveldb gflags glog szip lmdb

brew tap homebrew/science

brew install hdf5 opencv

3、 brew edit opencv並修改兩行

-DPYTHON_LIBRARY=#{py_prefix}/lib/libpython2.7.dylib

-DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7

4、 安裝protobuf和boost-python

brew install --build-from-source --with-python –vd protobuf

brew install --build-from-source -vd boost boost-python

5、下載NVIDIA Mac drivers

二、caffe安裝

1、下載caffe

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

2、打開Makefile.config, 配置BLAS

BLAS_INCLUDE := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers

BLAS_LIB := /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A

去掉CPU_ONLY的註釋

去掉USE_LEVELDB := 0的註釋

修改PYTHON_INCLUDE的目錄為python2.7的目錄

三、編譯caffe

make all

make test

make runtest

make pycaffe

make pytest

make distribute

會有一些warning, 需要安裝什麽包就安裝什麽包

1)“dot” not found in path問題需要安裝graphviz,不用pip用brew安裝

2) make pytest出現了如下問題:

註釋掉/python/caffe/proto/caffe_pb2.py 所有 “syntax = proto2 “

3)需要進入python文件夾執行python才能import caffe

4)安裝scikit-image庫

5)pip不好使,就pip install –U pip

Centos 7安裝Caffe

一、安裝CUDA

wget https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda-repo-rhel7-8-0-local-ga2-8.0.61-1.x86_64-rpm

rpm -i cuda-repo-rhel7-8-0-local-ga2-8.0.61-1.x86_64-rpm

yum clean all

yum install cuda

設置CUDA環境變量:

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:/usr/local/cuda-8.0/extras/CUPTI/lib64:$LD_LIBRARY_PATH(指定共享庫,編譯opencv、cudasample等)

export CUDA_HOME=/usr/local/cuda-8.0/(指定CUDA安裝路徑)

export PATH=/usr/local/cuda-8.0/bin:$PATH(在任意環境下調用cuda可執行文件)

更新運行庫緩存:

ldconfig

檢驗CUDA是否安裝成功:

nvcc --version

GPU支持:

安裝Nvidia驅動:

根據GPU型號從相應網站下載驅動,例如使用NVIDIA Tesla M60,從NVIDIA網站選擇對應的型號和操作系統,CUDA Toolkit版本,下載驅動文件,如NVIDIA-Linux-x86_64-375.66.run,運行驅動文件,根據提示安裝:

sh NVIDIA-Linux-x86_64-375.66.run

通過NVIDIA命令工具nvidia-smi檢驗GPU情況:

nvidia-smi

安裝cuDNN:

從NVIDIA網站下載cuDNN 安裝包,根據GPU及CUDA版本選擇對應cuDNN版本,下載cuDNN v5.1 for CUDA8.0,解壓拷貝到CUDA安裝目錄

cp include/* /usr/local/cuda/include

cp lib64/* /usr/local/cuda/lib64

二、yum安裝依賴包

yum install protobuf-devel leveldb-devel snappy-devel opencv-devel boost-devel hdf5-devel gflags-devel glog-devel lmdb-devel atlas-devel

n cd /usr/lib64/atlas

n sudo ln -sv libsatlas.so.3.10 libcblas.so

n sudo ln -sv libsatlas.so.3.10 libatlas.so

(如果有需要,可安裝Openblas或者MKL作為atlas的備用)

二、非yum手動安裝依賴包

安裝 gflags:

git clone https://github.com/gflags/gflags

cd gflags

mkdir build && cd build

export CXXFLAGS="-fPIC" && cmake .. -DCMAKE_INSTALL_PREFIX=/data2/shixi_wuhao3/local -DBUILD_SHARED_LIBS=ON

make VERBOSE=1

sudo make install

安裝 glog:

git clone https://github.com/google/glog

cd glog

./configure

make

automake --add-missing

sudo make install

安裝 lmdb:

git clone https://github.com/LMDB/lmdb

cd lmdb/libraries/liblmdb

make

sudo make install

安裝 hdf5:

wget https://support.hdfgroup.org/ftp/HDF5/current18/src/hdf5-1.8.18.tar.gz

tar -xvf hdf5-1.8.18.tar.gz

cd hdf5-1.8.18

./configure --prefix=/usr/local

make

sudo make install

安裝protobuf

tar -xvf protobuf

cd protobuf

./configure --prefix=/usr/local/protobuf

make

make check

make install

安裝boost

wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

tar -zxvf boost_1_59_0.tar.gz

cd boost_1_59_0

./bootstrap.sh --prefix=path/to/install

./b2 install

這會把boost安裝到path/to/install,若不指定,默認將安裝到/usr/local目錄,但需要權限。

安裝opencv

leveldb要和snappy一起安裝

三、下載caffe

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

四、安裝Python依賴包(註意python2和python3的)

for req in $(cat caffe/python/requirements.txt); do pip install $req; done

五、編輯Makefile.config 文件,根據情況修改配置:

BLAS := atlas

BLAS_INCLUDE := /usr/include/atlas

BLAS_LIB := /usr/lib64/atlas

PYTHON_INCLUDE := /usr/local/include/python2.7 \

/usr/local/lib/python2.7/site-packages/numpy/core/include

PYTHON_LIB := /usr/lib64

USE_CUDNN := 1(使用cuDNN)

(去掉CPU_ONLY的註釋——只用CPU)

(去掉USE_LEVELDB := 0的註釋——使用LevelDB)

根據情況確定是否修改Makefile.cofig中的相關位置路徑

## 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

"CuDNN是NVIDIA專門針對Deep Learning框架設計的一套GPU計算加速庫,用於實現高性能的並行計算,在有GPU並且安裝CuDNN的情況下可以打開即將註釋去掉。"

# CPU-only switch (uncomment to build without GPU support).

#CPU_ONLY := 1

"表示是否用GPU,如果只有CPU這裏要打開"

# uncomment to disable IO dependencies and corresponding data layers

USE_OPENCV := 1

"因為要用到OpenCV庫所以要打開,下面這兩個選項表示是選擇Caffe的數據管理第三方庫,兩者都不打開 Caffe默認用的是LMDB,這兩者均是嵌入式數據庫管理系統編程庫。"

# 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

"當需要讀取LMDB文件時可以取消註釋,默認不打開。"

# Uncomment if you‘re using OpenCV 3

OPENCV_VERSION := 2.4.10 (我的2.4.5)

"用pkg-config --modversion opencv命令查看opencv版本"

# 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++

"linux系統默認使用g++編譯器,OSX則是clang++。"

# CUDA directory contains bin/ and lib/ directories that we need.

CUDA_DIR := /usr/local/cuda

"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 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_50,code=compute_50

"這些參數需要根據GPU的計算能力

(http://blog.csdn.net/jiajunlee/article/details/52067962)來進行設置,6.0以下的版本不支持×_50的計算能力。"

# BLAS choice:

# atlas for ATLAS (default)

# mkl for MKL

# open for OpenBlas

BLAS := open

"如果用的是ATLAS計算庫則賦值atlas,MKL計算庫則用mkl賦值,OpenBlas則賦值open。"

# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.

# Leave commented to accept the defaults for your choice of BLAS

# (which should work)!

BLAS_INCLUDE := /usr/local/OpenBlas/include

BLAS_LIB := /usr/local/OpenBlas/lib

"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

"matlab安裝庫的目錄"

# 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

"python安裝目錄"

# 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)

# 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庫位置"

# 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

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# 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

# 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

"所用的GPU的ID編號"

# enable pretty build (comment to see full commands)

Q ?= @

六、編譯及測試:

make all

make test

make runtest

測試成功,結果如下:

編譯caffe的python環境及測試:

make pycaffe

make pytest

這樣進入python交互行import caffe成功即可

http://caffe.berkeleyvision.org/installation.html

pip安裝future庫

安裝最新版的cudnn(美團有鏈接,可能要註冊)

cmake要設置CPU參數OFF, cmake -DUSE_CUDA=OFF ..

git要加參數recursive

make install時要加sudo

在site-package目錄下編寫.pth來使得caffe在任何目錄下都可以import

搭建caffe環境及訓練模型