ubuntu16.04系统下配置caffe的GPU环境,训练mobileNet-SSD并在EAIDK-310嵌入式上推理


前言

如果没有安装好ubuntu系统或者系统崩溃需要重装的可以参考上一篇博客:ubuntu+win10双系统安装


一、NVIDIA显卡驱动及KUDA的安装

1.安装gpu驱动

这里推荐一个简单的安装方法,打开菜单,找到软件和更新,在这里插入图片描述
找到附加驱动,这里会自动推荐适合GPU的驱动,在这里插入图片描述
选择使用NVIDIA驱动,点击右下角的应用更改,系统会自动安装显卡驱动并重启。
若此方法失败,则可参考网上安装gpu驱动的教程,教程有很多,这里不再赘述。
验证是否成功安装:nvidia-smi

nvidia-smi

显示如下图则说明安装成功,注意版本,我这里是Driver Version: 384.130在这里插入图片描述

2.安装cuda和cudnn

版本问题
cada: cuda_8.0.44_linux.run
cudnn:cudnn-8.0-linux-x64-v5.1.tgz

  1. 如没有使用此版本以及上面的显卡驱动版本,不保证能配置成功。本人经过长达两周的时间摸索此对应的版本。推荐CUDA官网cudnn的安装下载,若有需要,可参考百度云。
  2. 至于安装教程,网上很多,这里推荐一篇教程
  3. 查看是否安装成功:nvcc -V在这里插入图片描述

二、ubuntu安装GPU版caffe

(一) 安装依赖库:

1.安装指令如下:

sudo apt-get install libprotobuf-dev //(16.04默认版本2.6.1,不能升级)
sudo apt-get install libleveldb-dev
sudo apt-get install libsnappy-dev
sudo apt-get install libopencv-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install python-dev
sudo apt-get install libgflags-dev
sudo apt-get install libgoogle-glog-dev
sudo apt-get install liblmdb-dev

2.特别注意
本人安装时python版本是Ubuntu16.04自带的Python2.7,如果需要其他python版本,建议参考后面第三节anaconda虚拟环境下的安装。
安装完成后确认python版本,直接输入python:

python

在这里插入图片描述
这里我只有用opencv2.4.9.1才编译成功,及Ubuntu16.04默认安装的版本。
安装完成后确认opencv版本,接着输入:

import cv2
cv2.__version__

opencv版本

(二) 下载源码并切换到 ssd 分支

1.下载码源
在主目录下(我的是/home/xxx,xxx为用户名)打开命令窗口,依次输入:
下载github上的caffe项目:

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

若后面编译不成功,也可以下载这里编译好的caffe。
切换到caffe文件夹:

cd caffe

2.切换到 ssd 分支

git checkout ssd

3.安装python依赖库
切换到 caffe 下的 python 目录下,下载 python 依赖库(先安装 pip)

cd python
sudo apt-get install python-pip
for req in $(catpython requirements.txt); do pip install $req; done

(三) 修改配置文件

(针对python2.7,opencv2.4.9.1d的环境修改)

1.修改Makefile.config文件

先将Makefile.config.example复制并重命名为Makefile.config

复制指令如下:

cp Makefile.config.example Makefile.config

然后修改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 lines after *_35 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_61,code=sm_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/local/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)/anaconda2
# 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_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

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

2.修改Makefile文件

(这里代码有点多,避免一行行的去查找修改,这里直接给出修改好的Makefile文件。)

(四) 编译及测试caffe

  1. 在caffe目录下编译:
make pycaffe -j8
make all -j8
make test -j8
make runtest -j8

时间较长,请耐心等待,-j8是指多核同时编译,可节省时间,根据自己电脑设置-j**。出现pass则编译成功。

  1. 测试:
    添加环境变量,注意caffe的路径:
cd caffe/python
export PYTHONPATH=~/caffe/python:$PYTHONPATH

进入 python 环境,输入:import caffe 如果没有报错,证明安装成功。若编译通过但是导入失败,则make pycaffe一下。

  1. 导入环境变量:
sudo echo export PYTHONPATH=“~/caffe/python" >>~/.bashrc
source ~/.bashrc #使环境变量生效

或者:

echo "export CAFFE_ROOT=~/caffe" >> ~/.bashrc
echo "export PYTHONPATH=${CAFFE_ROOT}/python:$PYTHONPATH" >> ~/.bashrc
source ~/.bashrc

如出错参考链接

重新打开一个终端窗口,输入python,再输入caffe,不出错则添加环境变量成功。

三、anaconda3环境下安装gpu版caffe

(一) ubuntu系统安装anaconda3

(因为这里主要介绍anaconda3下的caffe环境配置,至于anaconda3的安装,不做过多的赘述。)

1.下载及安装

下载:anaconda安装包下载,推荐官网,或者百度云,提取码:zu0i。

安装:网上很多教程,这里推荐一篇

是否安装成功:安装完成后需要添加环境变量,查看环境变量终端输入:

sudo gedit ~/.bashrc

打开.bashrc后看最后是否有如下字样代码

## >>> conda initialize >>>
## !! Contents within this block are managed by 'conda init' !!

__conda_setup="$('/home/×××/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/×××/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/×××/anaconda3/etc/profile.d/conda.sh"
    else
       export PATH="/home/×××/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
##    <<< conda initialize <<<

若没有则复制进去,注意×××是自己的用户名。
安装完成后,终端命令行前出现默认环境(base),输入conda不报错说明安装成功。

2.创建虚拟环境

  1. 创建名为caffepython2.7的虚拟环境并切换至该环境:
conda create -n caffe python=2.7
conda activate caffe

(二) 虚拟环境下安装caffe

  1. 安装依赖文件如第二节所示,但是我遇到的问题是sudo apt-get安装的opencv库在conda虚拟环境下无法调用,重新编译安装的opencv库与我想安装的caffe环境又有冲突,导致caffe编译失败。
    这里给出我的解决方法:
    usr/lib/python2.7/dist-packages目录下的cv.py,cv.pyccv2.x86_64-linux-gnu.so三个文件复制到anaconda3/envs/caffe/lib/python2.7/dist-packages文件夹下,并将cv2.x86_64-linux-gnu.so改名为cv2.so
    在这里插入图片描述
    在这里插入图片描述
    至此,caffe下的cv2版本才符合要求,如图所示:
    在这里插入图片描述

  2. 下载码源(与前面一致)
    在主目录下(我的是/home/xxx/my_code,xxx为用户名)打开命令窗口,依次输入:
    下载github上的caffe项目:

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

    切换到caffe文件夹:

    cd caffe
    
  3. 切换到 ssd 分支

git checkout ssd
  1. 安装python依赖库
    切换到 caffe 下的 python 目录下,下载 python 依赖库(先安装 pip)
cd python
sudo apt-get install python-pip
for req in $(catpython requirements.txt); do pip install $req; done
  1. 修改配置文件
    首先是Makefile.config文件,将python的路径改成anaconda下虚拟环境的python,其他的不变。
    修改后如下:
## 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 lines after *_35 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_61,code=sm_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/local/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/lf/anaconda3/envs/caffe
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_LIB := $(ANACONDA_HOME)/lib
LINKFLAGS := -Wl,-rpath,$(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

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

其次是Makefile文件,代码比较多,避免一行行的去查找修改,这里直接给出修改好的Makefile文件。)

剩下的编译过程也是完全一样,请参考前面部分。

至此,anconda下的caffe环境就安装好了。

四、caffe环境下训练MobileNet—SSD

本人安装ubuntu下的caffe环境主要是利用自己的数据集训练MobileNetSSD model,这部分可以参考链接
最后,将训练的模型.caffemodel模型文件转化成.tmfile文件,在eaidk-310平台上做推理,这部分内容可以参考下一篇博客。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值