cuda9.0+anaconda3.5.1+安裝caffe

默認安裝好cuda9.0+cudnn7.1.3,下面介紹用anaconda3(Python3.6.4)安裝caffe。

0、安裝依賴的包

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

1、OpenCV3.4.1

a) 下載opencv3.4

(最好安裝3.4.0)

進入官網 :http://opencv.org/releases.html , 選擇 3.4.1 版本的 sources , 下載 opencv-3.1.0.zip,在用戶主目錄(/home/lxh)下解壓。

b) 編譯

進入opencv解壓後的目錄下

$: cd opencv-3.4.1/

$: mkdir build                           # 創建編譯的文件目錄

$: cd build

$: cmake -D CMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..

$:  sudo make -j8                                #編譯

c) 安裝

$: sudo make install

(中間會下載--IPPICV: Download: ippicv_2017u3_lnx_intel64_general_20170822.tgz)

pkg-config --modversion opencv 查看版本信息

 

2、caffe

2.1 下載caffe源碼

 

進入主目錄,git下載源碼;或者直接下載caffe-master.zip, 解壓重命名爲caffe。
$: cd ~
$: git clone https://github.com/BVLC/caffe.git
2.2 配置Makefile.config  

 

 sudo cp Makefile.config.example Makefile.config

(1) 應用 cudnn,將 #USE_CUDNN := 1

修改成: 

USE_CUDNN := 1

(2) 應用opencv版本,將#OPENCV_VERSION := 3 

修改爲: 
OPENCV_VERSION := 3
(3) 使用python接口,將
#WITH_PYTHON_LAYER := 1 
修改爲:

WITH_PYTHON_LAYER := 1

(4) 修改python路徑
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

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial   

(5) cuda_arch:去掉前兩行 20 和 21信息,cuda9.0不支持。

(6) 修改anaconda 信息

去掉ANACONDA——HOME 註釋#,改爲實際路徑

ANACONDA_HOME := $(HOME)/anaconda3

(7) 註釋去掉:# PYTHON_INCLUDE := /usr/include/python2.7 

將其改爲:

PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
  $(ANACONDA_HOME)/include/python3.6m \
  $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include \

                  /usr/include/python2.7

(8) 修改 PYTHON_LIB, 將其的註釋掉,改爲anaconda的lib

# PYTHON_LIB := /usr/lib

PYTHON_LIB := $(ANACONDA_HOME)/lib

(9) 修改PYTHON_LIBRARIES

PYTHON_LIBRARIES := boost_python3 python3.6m 

(注意:同時,建立軟鏈接,在另一個終端輸入

sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so.1.58.0 /usr/local/lib/libboost_python3.so

(libboost_python-py35.so**依實際名稱爲準df)

否則會出現問題

/usr/bin/ld: cannot find -lboost_python3

collect2: error: ld returned 1 exit status
Makefile:572: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1
make: *** Waiting for unfinished jobs....
參考:https://blog.csdn.net/lien0906/article/details/51784191

)  

(10) 在末尾添加:

LINKFLAGS := -Wl,-rpath,$(ANACONDA_HOME)/lib

(11) 退出保存,具體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
# 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)/anaconda3
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
  $(ANACONDA_HOME)/include/python2.7 \
  $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
  /usr/include/python2.7


# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
  $(ANACONDA_HOME)/include/python3.6m \
  $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include \
  /usr/include/python2.7

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

LINKFLAGS := -Wl,-rpath,$(ANACONDA_HOME)/lib

----------------------------------------------------------------------------------------------------------------------------------

2.3 配置Makefile

$: sudo gedit Makefile
(1)將:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替換爲:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) 
(2)將:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改爲:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial  

2.4 編譯運行

(1) make clean && make all -j64    

(注:-j64爲線程並行參數,根據自己電腦實際內核數調整,出現問題見2.6.1)

(2) make runtest -j64

顯示 passed,爲通過測試。

(出現問題見2.6.2,其中(1)和(4)基本會出現)

(3) 生成python接口

make pycaffe

(4) 測試:

$ python 

>>> import caffe

>>> dir(caffe)

打印信息。

(若使用CPU,則只需將Makefile.config中 :1、USE_CUDNN := 1註釋掉;2:取消掉註釋CPU_ONLY := 1)

問題:出現類似信息:No module named caffe*,

a) 需要再~/.bashrc中添加pythonpath

export PYTHONPATH=/home/ai/caffe/python:$PATHONPATH

source ~/.bashrc

import caffe

b) 若還不行的話,

import sys

sys.path.append("/home/ai/caffe/python")

sys.path.append("/home/ai/caffe/python/caffe")

import caffe 

如果可以的話,說明系統可能找不到caffe的python接口,將caffe的python接口加入~/.bashrc

export PATH="/home/ai/caffe/python:$PATH"

以後只要,python --- import caffe 就可以。

最後,~/.bashrc 後面添加的完整路徑信息

 

caffeMake配置鏈接:https://pan.baidu.com/s/1f5I3yd1ItopbDr9FJiWvMw 密碼:60an

(參考:https://blog.csdn.net/striker_v/article/details/51596628)

(參考:https://blog.csdn.net/Dark_Miro/article/details/78355292?locationNum=9&fps=1)

2.5 LENET測試
 

(1)下載mnist數據

 

         cd ~/caffe
         sh data/mnist/get_mnist.sh

 

         sh examples/mnist/create_mnist.sh

 

 

(2)GPU與CPU選擇
         運行時,如果有GPU,則不需要修改配置文件;
         CPU則需要修改配置文件lenet_solver.prototxt
         vi examples/mnist/lenet_solver.prototxt,將最後一行的solver_mode:GPU改爲solver_mode:CPU。

 

 

(3)運行: sh examples/mnist/train_lenet.sh

2.6 遇到的錯誤信息

2.6.1 make all -j64 階段

(1)/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
解決:
make clean
export CPLUS_INCLUDE_PATH=/usr/include/python2.7

make all -j8

(2)/usr/bin/ld: cannot find -lboost_python3

 

collect2: error: ld returned 1 exit status
Makefile:572: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1
make: *** Waiting for unfinished jobs....

 

解決:ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so.1.58.0 /usr/local/lib/libboost_python3.so

(3)~/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
answer:
add to Makefile.config at last line

LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib

(4)libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
answer:
https://github.com/BVLC/caffe/issues/1276
Makefile:
 LIBRARIES += glog gflags protobuf leveldb snappy \
  lmdb boost_system hdf5_hl hdf5 m \
  opencv_core opencv_highgui opencv_imgproc opencv_imgcodec

 

2.6.2 make runtest -j64階段

(1) HDF5 header 與version 不匹配問題:

conda remove hdf5,再 make runtest -j64;

若還不行,卸載 pip uninstall h5py,  重新安裝 pip intall h5py,再make runtest -j64。

(2).build_release/tools/caffe
.build_release/tools/caffe: error while loading shared libraries: libcudart.so.9.0: cannot open shared object file: No such file or directory
Makefile:532: recipe for target 'runtest' failed

make: *** [runtest] Error 127

解決:試試不要用sudo make runtest

或者(有效)

answer:
(參考:https://blog.csdn.net/tangwenbo124/article/details/56847897)
sudo cp /usr/local/cuda-9.0/lib64/libcudart.so.9.0 /usr/local/lib/libcudart.so.9.0 && sudo ldconfig
sudo cp /usr/local/cuda-9.0/lib64/libcublas.so.9.0 /usr/local/lib/libcublas.so.9.0 && sudo ldconfig

sudo cp /usr/local/cuda-9.0/lib64/libcurand.so.9.0 /usr/local/lib/libcurand.so.9.0 && sudo ldconfig

(3).build_release/tools/caffe: error while loading shared libraries: libcudnn.so.7: cannot open shared object file: No such file or directory

Makefile:532: recipe for target 'runtest' failed

解決:不要用sudo 

或者(不建議,應該是權限問題):https://zhidao.baidu.com/question/716710727370554125.html
修改文件權限並創建新軟連接
sudo chmod u=rwx,g=rx,o=rx libcudnn.so.7.1.35
sudo ln -s libcudnn.so.7.1.3 libcudnn.so.7

sudo ln -s libcudnn.so.7 libcudnn.so

 

(4) error while loading shared libraries: libopencv_core.so.3.2: cannot open share

    cd ~

    sudo vim /etc/ld.so.conf

    在末尾添加:/usr/local/lib

    sudo ldconfig

參考:https://blog.csdn.net/qq_36362060/article/details/78501231?locationNum=6&fps=1

 

    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章