在ubuntu16.04下编译安装了py-faster-rcnn。

主要步骤包括:安装cuda/cudnn,换apt源,装开源显卡驱动,装caffe依赖的apt包和python包,下载py-faster-rcnn代码,编译代码。注意一点:不要用cuda安装包自带的显卡驱动,装好cuda后用apt-get装源里的最新驱动,否则很可能黑屏

py-faster-rcnn代码默认使用cudnn3,本文安装的是cudnn5,是通过修改微量py-faster-rcnn所依赖的caffe代码做到的。

本文提供了步骤0~17,其中0~14作为基础配置存在,也可用于其他开源代码库的配置参考。也包括不识别网卡的问题。

具体步骤如下:

0 下载cuda/cudnn

cuda用于调用显卡加速计算,py-faster-rcnn训练自己数据只能gpu模式因此cuda必须装;cudnn也用于加速可以不装但推荐安装。cudnn的版本要和cuda版本匹配,比如(cuda7.5, cudnn5.0)或(cuda8.0, cudnn5.1)的组合。强烈建议使用.run格式的cuda安装包。
请在windows平台上,到nvidia官网去下载,开始下载后换迅雷下载以加速。

这里以cuda7.5为例进行安装

1 编辑开机启动参数

这一步可选。有些台式机装好ubuntu重启后会黑屏,此时考虑本步骤。
开机后grub界面按e, 在"linux"开头行行尾single后添加参数nomodeset,注意空格分隔各个参数;按F10使用临时修改参数启动ubuntu

2 下载cuda和cudnn

用U盘或者移动硬盘,拷贝从windows上迅雷下载好的cuda和cudnn到HOME目录下。HOME目录就是例如/home/chris或者/home/rain这样的目录,是你打开“ubuntu的文件夹”后默认进入的目录

3 拉黑nouveau

拉黑nouveau这个显卡驱动,需要编辑配置文件并添加配置参数:按Ctrl+Alt+T打开终端,输入以下命令(#开头的内容是注释不会被执行):

sudo gedit /etc/modprobe.d/blacklist.conf   # 用gedit编辑器打开配置文件

在文件末尾追加如下内容:

blacklist nouveau

4 安装cuda

安装cuda的.run文件。需要在tty终端下、lightdm进程关闭的情况下安装,并且使用override模式、不安装cuda自带的显卡驱动

先按Ctrl+Alt+F1切换到tty1;然后执行以下命令:

sudo init 3    #关闭切换过来之前的图形界面
sudo service lightdm stop  # 关闭lightdm进程
sudo sh ./cuda_7.5.18_linux.run --override    # 安装cuda的run文件,注意要有--override参数表示强制安装。  cuda的版本号根据情况改变

接下来会提示很多内容,首先是按q然后选择accept,然后会问是否安装cuda安装包带的显卡驱动,当然是不装!(跳出的提示中选择N),后续的各种提示都选'yes'或者默认值(比如cuda的样例程序安装位置等)

稍等片刻安装好cuda后,会提示要设定PATH环境变量和ldconfig的配置项。因为现在还是在tty1下面,不能用图形界面的东西,此时只能用vi和nano这两个很难用的编辑器,不妨先切回图形界面然后用gedit来设定

5 切换回图形界面

sudo init 5
service lightdm start    # 忘了是否需要这一步了,执行即可,就算报错也没有关系

然后按Ctrl+Alt+F7切换过去。

或者不用上面两个命令,用startx命令直接切换过去。

6 添加cuda用到环境变量和环境配置文件

开启终端(Ctrl+Alt+T),输入如下命令:

sudo gedit /etc/profile   # 打开配置文件

在文件末尾追加这一行内容并保存关闭文件:

export PATH=$PATH:/usr/local/cuda/bin       # 这一行的cuda-7.5这个版本根据你的cuda版本而改变。这一行在系统环境变量PATH上追加了cuda的bin目录使得nvcc等命令能被在任意目录下调用

在终端输入如下命令:

sudo gedit /etc/ld.so.conf.d/cuda.conf    # 新建一个ldconfig的配置文件,是为了后续用gcc/g++编译的时候能够链接到cuda的库文件

输入如下内容:

/usr/local/cuda/lib64

更新ldconfig:

sudo ldconfig   # 可选,其实不用做的

这里可能遇到的一个问题是,cuda已经装好了,但是编译cuda的例程时失败,提示cuda的库链接不上。这在我的笔记本电脑上遇到过,关掉secure boot就好了,会有一个蓝框的提示。需要按照提示输入两次密码,然后重启,再输入密码(至少8位)
重启时候输入密码,会弹出一个蓝色的小框,选择change secure boot state,然后按照提示的位输入密码,比如提示第7位,那就输入先前设定的密码中的第7位。

7 重启ubuntu系统

8 换apt源

要安装开源nvidia显卡驱动了,是用apt-get命令来安装,但是apt-get的默认源网速太慢,换一个离你地理位置近的源:
以浙大源为例(也可以用阿里源),用gedit打开配置文件并填入配置:

sudo gedit /etc/apt/sources.list    # 打开配置文件

清空内容,并复制粘贴如下内容:(或者手动访问http://mirrors.zju.edu.cn/,手动选定系统版本手动生成)

deb http://mirrors.zju.edu.cn/ubuntu xenial main universe restricted multiverse
deb http://mirrors.zju.edu.cn/ubuntu xenial-security main universe restricted multiverse
deb http://mirrors.zju.edu.cn/ubuntu xenial-updates main universe restricted multiverse
deb http://mirrors.zju.edu.cn/ubuntu xenial-backports main universe restricted multiverse
deb-src http://mirrors.zju.edu.cn/ubuntu xenial main universe restricted multiverse
deb-src http://mirrors.zju.edu.cn/ubuntu xenial-security main universe restricted multiverse
deb-src http://mirrors.zju.edu.cn/ubuntu xenial-updates main universe restricted multiverse
deb-src http://mirrors.zju.edu.cn/ubuntu xenial-backports main universe restricted multiverse

更新源:

sudo apt-get update

这里可能遇到的坑是,有线网和无线网都无法连接,用ifconfig只显示lo这个本地回环的伪网卡。这可能是你的网卡过于奇葩了,比如我遇到msi(微星)主板+killer(杀手)E2400的网卡,ubuntu并不认它。通过手动编译安装ndiswrapper并使用了一个tp-link的TL-wn726n外置无线网卡,得以上网。然后出现重启ubuntu后再次无法发现任何网卡,重启进入windows连接网络成功后重启进入ubuntu又好了。后续更新了内核,再次多次重启ubuntu都可以上网。很奇怪。

ndiswrapper下载地址
ndiswrapper原理是,加载网卡的windows驱动中的.inf等文件中的信息,关于其原理和使用详细步骤的可以参考这一篇博客
另外还有这个英文讨论帖,用hack的方式把Killer E2400的驱动模块打到内核中,没有尝试过,不确定是否可行。

9 安装nvidia开源显卡驱动

先找到需要的显卡驱动版本,在终端输入:

cd /usr/local/cuda/samples
grep "nvidia-3" -r ./

根据命令执行结果,找到需要的nvidia显卡驱动版本号,比如我找到的是352,那就下载:

sudo apt-get install nvidia-352

10 安装cudnn

将下载下来的cudnn-7.0-Linux-x64-v4.0-prod.tgz 解压(先前已经放到/home/chris目录下了,打开文件夹用鼠标解压就可以),解压后得到名为cuda的文件夹,只不过里面只包含includelib目录,我们要做的就是把这两个目录放到cuda的安装目录(默认是/usr/local/cuda)。不过因为默认的cuda安装目录需要sudo权限,直接用鼠标操作比较麻烦,不如命令行干脆,因此用命令行来操作。

在打开的终端中输入下面命令:

cd ~/cuda    # 切换到cuda所在目录。  ~表示HOME目录,比如我的是/home/chris
sudo cp cudnn.h /usr/local/cuda/include/
cd ~/cuda/lib64
sudo cp lib* /usr/local/cuda/lib64/   # 把几个.so文件和其他文件拷贝过去

继续更新文件链接:这是因为/usr/local/cuda/lib64目录,是先前步骤6配置过的/etc/ld.so.conf.d/cuda.conf目录中填入的路径,这个conf文件会被系统加载,表示以后编译代码时链接库会考虑从这个目录搜索库文件,也就是作为“辅助库目录”而存在。库目录要求,“大名”相同的文件(后面小版本不同而前缀相同),只能有一个普通文件存在而其他的作为软链接。那么继续更新软链接:

cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.5    # 这里的版本号请换成你实际的版本号
sudo ln -s libcudnn.so.5.0.18 libcudnn.so.5
sudo ln -s libcudnn.so.5 libcudnn.so 

11 装一些用于提升开发效率的apt包(可选,个人推荐):

sudo apt-get install vim zsh openssh-server

安装zsh的主题插件oh-my-zsh(可选,个人推荐):

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

12 装caffe依赖的apt包:

安装caffe和py-faster-rcnn所依赖的apt包(我用zsh+ohmyzsh,bash的话请去掉包含的"\")

sudo apt-get install -y build-essential cmake git pkg-config libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev python-dev python-pip python-numpy python-scipy libhdf5-\* libgflags-dev python-opencv python-skimage python-yaml git
sudo apt-get install -y --no-install-recommends libboost-all-dev

13 安装pip包

先配pip源,比如阿里云源(或者豆瓣的也行):

在终端用gedit打开~/.pip/pip.conf文件:

gedit ~/.pip/pip.conf

然后填入如下内容并保存、关闭gedit:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

然后用pip下载后续用到的python包(需要sudo权限因为要写入/usr/local等系统目录):

sudo pip install protobuf cython easydict jupyter

14 git下载代码

先配置FQ工具,比如ss。 然后为git配置sock5代理(这个步骤可选,目的是加速git下载):

在终端输入如下命令:

git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

配置好后,用git下载代码python版faster-rcnn代码(只下载caffe也是OK的,依赖项都配置好了)

mkdir -p ~/work && cd ~/work
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn

15 编译代码

先编译lib目录下的代码

cd ~/work/py-faster-rcnn
cd lib
make

其次编译依赖的特定版本的caffe。
需要修改配置文件Makefile.config和Makefile,以及源码的更新

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 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 \
        -gencode arch=compute_50,code=sm_50 \
        -gencode arch=compute_50,code=compute_50

# 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)/anaconda
# 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 /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

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:
找到第397行,NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)修改为NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) (添加了-D_FORCE_INLINES参数

更新用到的caffe:原版py-faster-rcnn依赖的caffe比较老,不支持cudnnv5,那就更新下对应的源码。

用最新caffe源码的以下文件替换掉faster rcnn 的对应文件
include/caffe/layers/cudnn_relu_layer.hpp, src/caffe/layers/cudnn_relu_layer.cpp, src/caffe/layers/cudnn_relu_layer.cu

include/caffe/layers/cudnn_sigmoid_layer.hpp, src/caffe/layers/cudnn_sigmoid_layer.cpp, src/caffe/layers/cudnn_sigmoid_layer.cu

include/caffe/layers/cudnn_tanh_layer.hpp, src/caffe/layers/cudnn_tanh_layer.cpp, src/caffe/layers/cudnn_tanh_layer.cu

用caffe源码中的这个文件替换掉faster rcnn 对应文件
include/caffe/util/cudnn.hpp

将 faster rcnn 中的 src/caffe/layers/cudnn_conv_layer.cu 文件中的所有
cudnnConvolutionBackwardData_v3 函数名替换为 cudnnConvolutionBackwardData
cudnnConvolutionBackwardFilter_v3函数名替换为 cudnnConvolutionBackwardFilter

16 其他补充

尝试用anaconda提供的python替代系统的python,不过编译起来没有成功。装anaconda主要是能避免自己手动装一些python包,不过其实借助apt-get和pip,也都能很快装好的。
然后
开启一个notebook:

jupyter-notebook

然后通过浏览器访问http://localhost:8888的形式(具体端口看你的命令提示),就可以使用notebook了。

另:如果编译caffe-fast-rcnn失败,但是依赖的环境和我提到的一致,那么不妨重新从git获取一份caffe的代码:

cd ~/work/py-faster-rcnn
rm -rf caffe-fast-rcnn
git submodule update --init --recursive

然后重启电脑,更新src和include目录的代码,以及Makefile与Makefile.config,再编译,就可以了。

17 参考

http://blog.csdn.net/g0m3e/article/details/51420565

ubuntu16.04配置py-faster-rcnn的更多相关文章

  1. caffe学习一:ubuntu16.04下跑Faster R-CNN demo (基于caffe). (亲测有效,记录经历两天的吐血经历)

    兜兜转转,兜兜转转; 一次有一次,这次终于把Faster R-CNN 跑通了. 重要提示1:在开始跑Faster R-CNN之前一定要搞清楚用的是Python2 还是Python3. 不然你会无限次陷 ...

  2. py faster rcnn+ 1080Ti+cudnn5.0

    看了py-faster-rcnn上的issue,原来大家都遇到各种问题. 我要好好琢磨一下,看看到底怎么样才能更好地把GPU卡发挥出来.最近真是和GPU卡较上劲了. 上午解决了g++的问题不是. 然后 ...

  3. Ubuntu16.04配置TOMCAT8

    基于虚拟机Ubuntu16.04配置Tomcat过程 一.安装JDK 首先要确定好要安装的jdk和tomcat版本能对的上,具体如图所示: 版本选择是Jdk1.8,首先上官网http://www.or ...

  4. ubuntu16.04配置java环境(重启后不会失效)

    ubuntu16.04配置java环境(重启后不会失效) 1.jdk的安装包(.tar.gz)拷贝到/opt目录下 mv jdk-8u144-linux-x64.tar.gz /opt 2.解压文件 ...

  5. Ubuntu16.04配置apache+php+mysql

    命令行配置apache input sudo apt-get install apache2 done! 命令行配置mysql 参见: MySQL install and setting 命令行配置p ...

  6. ubuntu16.04配置记录

    新开一篇随笔记录ubuntu16.04配置中遇到的坑 1.安装Bumblebee(大黄蜂) Bumblebee是一款双显卡驱动,可以关闭独显,有效控制笔记本发热 第一步:安装我们的主角Bumblebe ...

  7. py-faster-rcnn + opencv3.0.0 + ubuntu16.04配置(CPU模式)

    最近开始做行人检测,因此开始接触faster-rcnn,这里贴上配置教程(亲测可行),不过是基于cpu的,蓝瘦... 参考博客:http://www.tuicool.com/articles/nYJr ...

  8. 【.net core 跨平台】第一步 在Ubuntu16.04 配置.net core环境

    本次使用VMware10.0.4工具安装Ubuntu16.04系统并配置.net core环境   Ubuntu 16.04 desktop下载地址:http://releases.ubuntu.co ...

  9. Ubuntu16.04配置静态IP地址

    ubuntu如何设置静态IP? 设置静态IP 1.编辑/etc/network/interfaces文件: # This file describes the network interfaces a ...

随机推荐

  1. 4.5 .net core下直接执行SQL语句并生成DataTable

    .net core可以执行SQL语句,但是只能生成强类型的返回结果.例如var blogs = context.Blogs.FromSql("SELECT * FROM dbo.Blogs& ...

  2. 20个不可思议的 WebGL 示例和演示

    WebGL 是一项在网页浏览器呈现3D画面的技术,有别于过去需要安装浏览器插件,通过 WebGL 的技术,只需要编写网页代码即可实现3D图像的展示.WebGL 可以为 Canvas 提供硬件3D加速渲 ...

  3. Java关键字final、static

    一.final根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效率. final ...

  4. 转载---javascript 定时器总结

    转载:http://www.jb51.net/article/40193.htm JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延 ...

  5. Git从码云Clone代码到本地

    Git从码云或者Github 克隆代码到本地 1.下载安装Git,傻瓜式下一步下一步即可... 2.配置Git: 2.1.选择你要clone到本地的路径:右键--->$ Git Bash Her ...

  6. swift学习笔记2——函数、闭包

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  7. JavaEE开发基础

    1 JavaEE简介 Java平台有三个版本,分别是JavaSE(Java Platform, Standard Edition),JavaEE(Java Platform, Enterprise E ...

  8. 读<<领域驱动设计-软件核心复杂性应对之道>>有感

    道可道,非常道. 名可名,非常名. 无名天地之始,有名万物之母. ---老子 关于标题 好久没写东西了,动笔的动机是看完了一本书,想写点总结性的东西,一是为了回顾一下梳理知识点,二是为了日后遗忘时能有 ...

  9. Hibernate 系列 05 - Session 类

    引导目录: Hibernate 系列教程 目录 前言: Session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库的存取都与Session息息相关. 就如同在编写JDBC时需要关 ...

  10. Jdk与Tomcat配置与安装

    一.jdk的安装与配置 先下载Tomcat与jdk的压缩包:在usr/local/src目录下下载,下载方法:wget+链接 (tar.gz) 解压tomcat与jdk的压缩包: tar –zvxf ...