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 后面添加的完整路径信息

 

cuda9.0+anaconda3.5.1+安装caffe_第1张图片

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

 

    

你可能感兴趣的:(安装环境)