Ubuntu16.04+cuda9.0+cudnn7.4.1+opencv3.4.0+anaconda3(python3.6)+caffe

本文介绍如何在Ubuntu16.04上配置Caffe深度学习框架,包括安装基本环境、依赖库,配置及编译Caffe等步骤。特别针对CUDA9.0、cuDNN7.4.1和OpenCV3.4.0版本进行了详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ubuntu16.04+cuda9.0+cudnn7.4.1+opencv3.4.0+anaconda3(python3.6)+caffe

 

0.基本环境配置

操作系统:Ubuntu16.04

英伟达驱动:nvidia384.130(安装)

CUDA版本:cuda9.0(安装)

cnDNN版本:cudnn7.4.1(安装)

OpenCV版本:opencv3.4.0(安装)

Anaconda版本:anaconda3(python3.6)(安装)

安装cuda要注意nvidia版本!

1.基本依赖库安装

终端输入:

sudo apt install caffe-cuda

安装依赖:


 
  1. sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler

  2. sudo apt-get install --no-install-recommends libboost-all-dev

  3. sudo apt-get install libatlas-base-dev

  4. sudo apt-get install libhdf5-serial-dev

  5. sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

2.配置caffe

2.1clone caffe源码

本文caffe安装目录为:

usr/local/

cd 进入安装目录并git:

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

2.2配置Makefile.config文件

进入caffe文件夹,复制更改Makefile.config文件:


 
  1. cd /usr/local/caffe

  2. sudo cp Makefile.config.example Makefile.config

  3. sudo gedit Makefile.config

更改配置如下:


 
  1. ## Refer to http://caffe.berkeleyvision.org/installation.html

  2. # Contributions simplifying and improving our build system are welcome!

  3.  
  4. # cuDNN acceleration switch (uncomment to build with cuDNN).

  5. USE_CUDNN := 1

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

  8. # CPU_ONLY := 1

  9.  
  10. # uncomment to disable IO dependencies and corresponding data layers

  11. # USE_OPENCV := 0

  12. # USE_LEVELDB := 0

  13. # USE_LMDB := 0

  14. # This code is taken from https://github.com/sh1r0/caffe-android-lib

  15. # USE_HDF5 := 0

  16.  
  17. # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)

  18. # You should not set this flag if you will be reading LMDBs with any

  19. # possibility of simultaneous read and write

  20. # ALLOW_LMDB_NOLOCK := 1

  21.  
  22. # Uncomment if you're using OpenCV 3

  23. OPENCV_VERSION := 3

  24.  
  25. # To customize your choice of compiler, uncomment and set the following.

  26. # N.B. the default for Linux is g++ and the default for OSX is clang++

  27. # CUSTOM_CXX := g++

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

  30. CUDA_DIR := /usr/local/cuda

  31. # On Ubuntu 14.04, if cuda tools are installed via

  32. # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:

  33. # CUDA_DIR := /usr

  34.  
  35. # CUDA architecture setting: going with all of them.

  36. # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.

  37. # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.

  38. # For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.

  39. CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \

  40. -gencode arch=compute_35,code=sm_35 \

  41. -gencode arch=compute_50,code=sm_50 \

  42. -gencode arch=compute_52,code=sm_52 \

  43. -gencode arch=compute_60,code=sm_60 \

  44. -gencode arch=compute_61,code=sm_61 \

  45. -gencode arch=compute_61,code=compute_61

  46.  
  47. # BLAS choice:

  48. # atlas for ATLAS (default)

  49. # mkl for MKL

  50. # open for OpenBlas

  51. # BLAS := atlas

  52. BLAS := open

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

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

  55. # (which should work)!

  56. # BLAS_INCLUDE := /path/to/your/blas

  57. # BLAS_LIB := /path/to/your/blas

  58.  
  59. # Homebrew puts openblas in a directory that is not on the standard search path

  60. # BLAS_INCLUDE := $(shell brew --prefix openblas)/include

  61. # BLAS_LIB := $(shell brew --prefix openblas)/lib

  62.  
  63. # This is required only if you will compile the matlab interface.

  64. # MATLAB directory should contain the mex binary in /bin.

  65. # MATLAB_DIR := /usr/local

  66. # MATLAB_DIR := /Applications/MATLAB_R2012b.app

  67.  
  68. # NOTE: this is required only if you will compile the python interface.

  69. # We need to be able to find Python.h and numpy/arrayobject.h.

  70. # PYTHON_INCLUDE := /usr/include/python2.7 \

  71. # /usr/lib/python2.7/dist-packages/numpy/core/include

  72. # Anaconda Python distribution is quite popular. Include path:

  73. # Verify anaconda location, sometimes it's in root.

  74. ANACONDA_HOME := $(HOME)/anaconda3

  75. PYTHON_INCLUDE := $(ANACONDA_HOME)/include \

  76. $(ANACONDA_HOME)/include/python3.6m \

  77. $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include \

  78.  
  79. # Uncomment to use Python 3 (default is Python 2)

  80. PYTHON_LIBRARIES := boost_python-py35 python3.6m

  81. #这一段可删除#(注意:check the boost_python version library in /usr/lib/x86_64-linux-gnu/. For example, if there exists libboost_python-py34.so in this directory, uncomment and modify the line of PYTHON_LIBRARIES as PYTHON_LIBRARIES := boost_python-py34 python3.6m,本文用boost_python-py35)

  82.  
  83. # PYTHON_INCLUDE := /usr/include/python3.5m \

  84. # /usr/lib/python3.5/dist-packages/numpy/core/include

  85.  
  86. # We need to be able to find libpythonX.X.so or .dylib.

  87. # PYTHON_LIB := /usr/lib

  88. PYTHON_LIB := $(ANACONDA_HOME)/lib

  89.  
  90. # Homebrew installs numpy in a non standard path (keg only)

  91. # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include

  92. # PYTHON_LIB += $(shell brew --prefix numpy)/lib

  93.  
  94. # Uncomment to support layers written in Python (will link against Python libs)

  95. WITH_PYTHON_LAYER := 1

  96.  
  97. # Whatever else you find you need goes here.

  98. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/

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

  100.  
  101. # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies

  102. # INCLUDE_DIRS += $(shell brew --prefix)/include

  103. # LIBRARY_DIRS += $(shell brew --prefix)/lib

  104.  
  105. # NCCL acceleration switch (uncomment to build with NCCL)

  106. # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)

  107. # USE_NCCL := 1

  108.  
  109. # Uncomment to use `pkg-config` to specify OpenCV library paths.

  110. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)

  111. # USE_PKG_CONFIG := 1

  112.  
  113. # N.B. both build and distribute dirs are cleared on `make clean`

  114. BUILD_DIR := build

  115. DISTRIBUTE_DIR := distribute

  116.  
  117. # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171

  118. # DEBUG := 1

  119.  
  120. # The ID of the GPU that 'make runtest' will use to run unit tests.

  121. TEST_GPUID := 0

  122.  
  123. # enable pretty build (comment to see full commands)

  124. Q ?= @

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

2.3. 配置Makefile文件

进入caffe文件夹中,并执行。终端输入:


 
  1. cd /usr/local/caffe

  2. sudo gedit Makefile

修改如下:(可以ctrl+f进行搜索)


 
  1. PYTHON_LIBRARIES ?= boost_python python2.7

  2. 修改为:

  3. PYTHON_LIBRARIES ?= boost_python-py35 python3.6m


 
  1. NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

  2. 修改为:

  3. NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)


 
  1. 将:

  2. LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5

  3. 改为:

  4. LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

2.4编译

cd到caffe目录,并编译


 
  1. cd /usr/local/caffe

  2. sudo make clean

  3. sudo make all -j12

在终端执行:

sudo make test -j12

成功之后输入:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{path to your anaconda}/anaconda3/lib:{path to your anaconda}/anaconda3/pkgs/python-3.6.1-2/lib

运行测试:

sudo make runtest -j12

成功:

成功之后会显示PASSED,继续进入caffe下python目录,修改requirements.txt里为python-dateutil>=2.0:


 
  1. cd /usr/local/caffe/python

  2. sudo gedit requirements.txt

修改成功后在当前python目录执行执行:

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

最后对pycaffe配置:


 
  1. cd /usr/local/caffe

  2. sudo make pycaffe

成功:

3.测试caffe

3.1更改环境变量

sudo gedit /etc/profile

加入:export PYTHONPATH={path to your caffe}/caffe/python/:$PYTHONPATH

本文中为:export PYTHONPATH=/usr/local/caffe/python/:$PYTHONPATH

source一下使之生效:

source /etc/profile

3.2测试


 
  1. python

  2. import caffe

成功:

4.Error

1.cannot find -lboost_python-py36

按照2.2和2.3的配置文件进行修改,ubuntu自带的boost_1.58.0没有对python3.6m进行编译,所以在/usr/lib/x86_64-linux-gnu/. 不存在 libboost_python-py36.so ,只能将Makefile.config中改为:PYTHON_LIBRARIES := boost_python-py35 python3.6m

2.Warning!***HDF5 library version mismatched error *** python pandas...

系统的HDF5与Anaconda中冲突,解决方法:

conda install -c anaconda hdf5=1.8.15

3.略...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值