天天看点

Ubuntu14.04+cuda7.5+caffe+OpenCV2.4.9+cudnn7.5+Anaconda2安装配置

系统出问题不得不重装系统,所有caffe相关也都需重装,在此做个记录。

环境:Ubuntu14.04/台式机集成显卡+独立显卡gtx960

准备好的文件:

Ubuntu14.04+cuda7.5+caffe+OpenCV2.4.9+cudnn7.5+Anaconda2安装配置

1.安装cuda7.5

a)禁用nouveau

$ cd /etc/modprobe.d/
$ sudo geidit blacklist.conf
           

在/etc/modprobe.d/blacklist.conf最后一行加上:

blacklist nouveau
options nouveau modeset=0
           

b)关闭GUI:

$ sudo service lightdm stop
           

c) Alt + ctrl +F1进入命令行

d) 更改CUDA Toolkit执行权限:

切换到cuda安装文件的路径

$ sudo chmod +x cuda_7._linux.run
           

e) 执行CUDA安装文件

f) 切换回GUI:

$ sudo service lightdm start
           

g) 成功切回GUI后,添加环境变量PATH:

$ sudo gedit /etc/profile
           

h) 在/etc/profile文件最后添加

export PATH=/usr/local/cuda-7.5/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH 
           

i) 然后source一下,使之生效

$ source /etc/profile   
           

j) 检查上述的环境变量是否设置成功。

终端中输入 :

$ env

在输出的环境变量中检查有无上述 h) 中设置的变量,如果有则代表设置成功。

k) 再然后就是添加共享库变量:

$ cd /etc/ld.so.conf.d/
$ touch cuda.conf
$ sudo gedit cuda.conf
           

在 /etc/ld.so.conf.d/加入文件 cuda.conf, 内容如下

/usr/local/cuda-/lib64
           

执行下列命令使之立刻生效

$ sudo ldconfig
           

2.检验cuda是否安装成功

a) 检查 NVIDIA Driver是否安装成功

终端输入 :

$ cat /proc/driver/nvidia/version 
           

会输出NVIDIA Driver的版本号

b) 检查 CUDA Toolkit是否安装成功

终端输入 :

会输出CUDA的版本信息

c) 尝试编译cuda提供的例子

切换到例子存放的路径,默认路径是

~/NVIDIA_CUDA-7.5_Samples

(即

/home/XX/ NVIDIA_CUDA-7.5_Samples

然后终端输入:

$ make
           

如果出现错误的话,则会立即报错停止,否则会开始进入编译阶段。

我的第一次运行时出现了报错,提示的错误信息是系统中没有g++

然后在终端运行 :

$ sudo apt-get install g++

安装完g++后,再make就正常了 。整个编译的时间持续比较长,耐心等待。

d) 运行编译生成的二进制文件。

编译后的二进制文件 默认存放在

~/NVIDIA_CUDA-7.5_Samples/bin/x86_64/linux/release

中。

切换路径 :

终端输入 :

$ ./deviceQuery

若正确显示安装的cuda7.5即GPU信息,则表示安装成功。

3.安装依赖库

包含了boost opencv leveldb protobuf 等:

BLAS安装:

4.Caffe安装

a) 安装git

$ sudo add-apt-repository ppa:git-core/ppa
           

中间暂停时,按回车键Enter继续安装。

$ sudo apt-get update
           
$ sudo apt-get install git     
           

安装下载完成后,可以使用下面的命令行,确认git的版本:

b) 下载caffe(注意下载的位置,下载到

/home/just

):

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

Cd进home/just/caffe,然后编译caffe:

$ cd ~/caffe
$ sudo cp Makefile.config.example Makefile.config
$ sudo make all
$ sudo make test  
$ sudo make runtest  
           

5.安装OpenCV

a) 下载OpenCV

b) 解压到任意目录

$ unzip opencv-..zip
           

c) 进入源码目录,创建release目录

$ cd opencv-.
           
$ mkdir release  
           

d) 可以看到在OpenCV目录下,有个CMakeLists.txt文件,需要事先安装一些软件

e) 进入release目录,安装OpenCV是所有的文件都会被放到这个release目录下

$ cd release  
           

f) cmake编译OpenCV源码,安装所有的lib文件都会被安装到/usr/local目录下

注:这里-D的作用是设置编译的选项,如选择release模式, 输出的目录,不编译cuda模块,编译openMP与QT模块,这里的QT模式带有很强的可视化效果,推荐。但需要在第一步中加上 libqt4-dev 安装QT环境

g) 安装

$ sudo make install    
           

6.测试 OpenCV

(1) Create a directory DisplayImagefor test project:

$ mkdir DisplayImage  
$ cd DisplayImage  
           

(2) Create DisplayImage.cpp and edit it: gedit DisplayImage.cpp

Then edit the DisplayImage.cpp:

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
  if ( argc !=  )
  {
    printf("usage: DisplayImage.out <Image_Path>\n");
    return -;
  }
  Mat image;
  image = imread( argv[],  );
  if ( !image.data )
  {
    printf("No image data \n");
    return -;
  }
  namedWindow("Display Image", WINDOW_AUTOSIZE );
  imshow("Display Image", image);
  waitKey();
  return ;
}
           

(3) Create a CMake file:

$ gedit CMakeLists.txt  
           

Then edit the CMakeLists.txt:

cmake_minimum_required(VERSION )  
project(DisplayImage)  
find_package(OpenCV REQUIRED)  
add_executable(DisplayImage DisplayImage.cpp)  
target_link_libraries(DisplayImage ${OpenCV_LIBS})  
           

(4) Generate the executable file:

$ cmake .  
           
$ make  
           

(5) Execute it:

$ ./DisplayImage lena.jpg  
           

成功则显示图像。

7.cudnn

6.cudnn

1.进官网下载文件(cudnn-7.5-linux-x64-v5.0-ga.tgz)

在终端中切换到文件所在文件夹,输入下面指令:

$ sudo tar xvf cudnn--linux-x64-v5.-ga.tgz
$ cd cuda/include
$ sudo cp *.h /usr/local/include/
$ cd ../lib64
$ sudo cp lib* /usr/local/lib/
$ cd /usr/local/lib
$ sudo chmod +r libcudnn.so.. 
$ /usr/local/lib$ sudo ln -sf libcudnn.so.. libcudnn.so.
$ sudo ln -sf libcudnn.so. libcudnn.so^C
$ sudo ldconfig    
           

然后切换到caffe根目录下,将Makefile.config中的USE_CUDNN行前的#去掉:

$ cd ~/caffe
$ sudo gedit Makefile.config
           

将Makefile.config中的USE_CUDNN行前的#去掉。

保存后重新编译:

$ cd ~/caffe
$ sudo make clean
$ sudo make all
$ sudo make test  
$ sudo make runtest 
           

遇到过error:

.build_release/tools/caffe: error while loading shared libraries: libcudart.so.7.0: cannot open shared object file: No such file or directory

解决办法

sudo ldconfig /usr/local/cuda-/lib64
           

8.配置pycaffe

  1. 安装anaconda2

    去官网下载,下载2.7版本(Anaconda2-4.2.0-Linux-x86_64.sh)。

bash Anaconda2--Linux-x86_64.sh    
           

安装的时候,最后一定要选yes。

安装完毕打开

home/just/.bashrc

发现,它自动在最后添加了:

#Add by anaconda2 4.2.0 installer

Export PATH=”/home/just/anaconda2/bin:$PATH”

修改Makefile.config:

cd ~/caffe
sudo gedit 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 := 

# 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/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 \
        $(ANACONDA_HOME)/lib/python2/site-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 := 

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

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

# enable pretty build (comment to see full commands)
Q ?= @
           

开始编译:

$ cd ~/caffe/
$ sudo make clean 
$ sudo make all -j8
$ sudo make test -j8
$ sudo make runtest -j8
$ make pycaffe -j8
           

编译runtest的时候,遇到这样的错误:

.build_release/test/test_all.testbin: error while loading shared libraries: libhdf5.so.10: cannot open shared object file: No such file or directory

这是因为 libhdf5.so的版本问题,你可以进入

/usr/lib/x86_64-linux-gnu

看一下,你的libhdf5.so.x中的那个x是多少,比如我的是libhdf5.so.7

因此可以执行下面几行代码解决:

# cd /usr/lib/x86_64-linux-gnu
# sudo ln -s libhdf5.so.7 libhdf5.so.10
# sudo ln -s libhdf5_hl.so.7 libhdf5_hl.so.10
# sudo ldconfig
           

最终查看python接口是否编译成功:

进入python环境,进行import操作

$  python
>>> import caffe
           

如果没有提示错误,则编译成功。

报错:no module named caffe

解决方法:将anacanda与caffe头文件进行链接。

打开.bashrc:

$ cd ~
$ sudo gedit .bashrc
           

添加:

export PYTHONPATH=”/home/just/caffe/python:$PYTHONPATH”

在进入python环境,import caffe,

报错:ImportError:No module named google.protobuf.internal

解决方法:

$ pip install protobuf
$ /home/just/anaconda2/bin/pip install protobuf
           

尽情的:

$ python   
>>Import caffe
           
$ Ipython 
>>Import caffe
           

由于安装了anaconda,jupyter notebook就已经自动安装好,可以:

$ jupyter notebook
           

就会在浏览器中打开notebook。