1. 程式人生 > >docker中cuda9.2+cudnn7+caffe

docker中cuda9.2+cudnn7+caffe

萬事開頭難, 所以儘量找別人開好的頭, 現在nvidia官方給的都cuda9.2了, 開源的gpu-caffe基本還是cuda8.0, 經過綜合考慮, 還是在docker上面重新搭建, 這樣不影響本地主機的環境又方便以後的移植部署.

     1. Pull docker image. (docker的安裝請自行百度下.)

# sudo docker pull nvidia/cuda:9.2-cudnn7-devel-ubuntu16.04 

     2. Git clone caffe source. (Git的安裝我是用 apt安裝的)

# cd workspace && git clone

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

    3. 啟動docker 映象, 記得一定要用nvidia-docker命令,這個不是docker自帶的, 安裝請百度下.

# sudo nvidia-docker run -it -v #HOME/workspace:/var/workspace --name cuda-caffe nvidia/cuda:9.2-cudnn7-devel-ubuntu16.04 /bin/bash

Ps:啟動過後, 以後進入docker的bash需要執行

# sudo nvidia-docker exec -it cuda-caffe /bin/bash

或者

# sudo docker exec -it cuda-caffe /bin/bash

  4. 準備caffe環境, 參考的caffe官網 installation, 其實這裡說得很清楚了, 我這裡也是做一個搬運工.

         a. Cuda, 官方說需要cuda7以上版本, 以前的版本也是可以的, 但是不保證沒得小問題. 當然我們docker image直接是cuda9.2,所以不考慮這個問題.

         b. Blas, 好像是一個數學的計算庫, 包括矩陣計算啥的, 有MKL, ATLAS, OpenBLAS等都可以, 預設支援MKL, 這個選擇不同配置的makefile-config. 我看OpenBLAS親切, 於是就上的OpenBLAS

# apt install libopenblas-dev

         c. Boost, 這個是c++的一個封裝庫, 很強大, 有很多模組. 如果不考慮pycaffe啥的, 可以直接用apt安裝(# apt install libboost-all-dev), 但是我這裡用的anaconda安裝的python3.6, 考慮到apt安裝的預設python3.5版本不相容, 所以下載的boost重新編譯的.

Boost沒有立即git最新版本, 是以前下載的(1.66.0), 但和最新版本應該不超過三個月, 相差不大, 所以最新版本(1.68.0)應該也沒有很大的問題, 靈活處理, 歡迎交流.

下載,編譯與安裝:

# tar xf boost_1_66_0.tar.gz

# cd boost_1_66_0

# ./bootstrap.sh --with-libraries=python --with-toolset=gcc

# ./b2 cflags='-fPIC' cxxflags='-fPIC' --with-python include="/root/anaconda3/include/python3.6m/" # 這裡的include根據自己的python環境進行修改即可

# ./b2 install

         d. Protobuf, gflags, hdf5, glog. 前面三個在Ubuntu上可以用apt進行安裝, 最後一個要git原始碼安裝.

# apt install libprotobuf-dev libgflags-dev libhdf5-dev

# cd glog

# ./autogen.sh && ./configure && make && make install #如果沒有安裝autogen工具, 百度安裝下即可

         e. Opencv 是現在霸屏似得影象處理工具, caffe官方推薦版本要大於2.4, 當然3.0以後也是沒得問題的. 當然不知道怎麼的, opencv3.3以後的版本沒有c-api的, 需要注意下. 我這裡不糾結直接用apt進行安裝.

# apt install libopencv-dev

Ps: 當然這樣安裝是有壞處的, 首先版本不能控制, 這個完全看官方維護, 我安裝完後查看了一下是 2.4.9.1 . 滿足官方的要求, 我就沒有糾結, 當然我們公司專案用的是opencv 3.2.0 . 那都是後話.

         f. Io 庫, 官方需要 lmdb 和leveldb. 直接apt安裝

# apt install liblmdb-dev libleveldb-dev

         g. Cudnn, 官方建議v6以上, 我們docker image自帶v7. 所以這裡不做修改.

5. 修改makefile.config

改的細節挺多的, 我這裡直接把改好的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

# 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

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

CUDA_ARCH := -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

BLAS := 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 := /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)/anaconda

# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \

# $(ANACONDA_HOME)/include/python2.7 \

# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

PYTHON_LIBRARIES := boost_python3 python3.6m

ANACONDA_HOME := $(HOME)/anaconda3

PYTHON_INCLUDE := $(ANACONDA_HOME)/include \

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

 $(ANACONDA_HOME)/lib/python3.6/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/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 ?= @

6. 編譯caffe與測試

# cd $HOME/caffe

# make all -j8

# make pycaffe

# make test

7. Python 環境新增 caffe

a. 在.bashrc中新增一下內容

export PYTHONPATH="/var/source/caffe/python:$PYTHONPATH"

export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/hdf5/serial:/usr/local/lib:$LD_LIBRARY_PATH"

b. 更新環境

# source $HOME/.bashrc

8. 總結

總體來看, 沒有修改caffe程式碼或者庫, 唯一麻煩的就是配置下Makefile.config檔案. 哈哈, 大功告成, 到這裡基本搞定了, 但部落格總有遺漏的地方, 歡迎大家積極交流.