基于Ubuntu 14.04和CUDA 7.0为基础的caffe环境配置

时间:2022-12-12 00:21:23

一:注意:

以下教程是Ubuntu 14.04 64bit和CUDA 7.0为基础的。

 

二:说明:

1:caffe下载地址

https://github.com/BVLC/caffe

2:caffe环境配置官方文档

http://caffe.berkeleyvision.org/installation.html

3:cuda配置地址

http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/index.html#axzz3xBRqHocK

 

三:环境配置

这个是网上搜索到的配置过程:

http://www.linuxidc.com/Linux/2015-07/120449.htm

但是我按照这个教程安装,没有成功。

所以我按照官方给出的配置步骤,链接在“说明”中,下面我详细介绍一下我的配置过程。

 

1. 安装开发依赖包

sudo apt-get install build-essential

sudo apt-get install vim cmake git

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

 

2. 安装Caffe:

1. 预检查

2.1. Verify You Have a CUDA-Capable GPU

To verify that your GPU is CUDA-capable, go to your distribution's equivalent of System Properties, or, from the command line, 输入命令:

$ lspci | grep -i nvidia

2.2. Verify You Have a Supported Version of Linux

The CUDA Development Tools are only supported on some specific distributions of Linux. These are listed in the CUDA Toolkit release notes.

To determine which distribution and release number you're running, 输入命令:

$ uname -m && cat /etc/*release

2.3. Verify the System Has gcc Installed

The gcc compiler is required for development using the CUDA Toolkit. It is not required for running CUDA applications. It is generally installed as part of the Linux installation, and in most cases the version of gcc installed with a supported version of Linux will work correctly.

To verify the version of gcc installed on your system, 输入命令:

$ gcc --version

 

Install repository meta-data

Note: When using a proxy server with aptitude, ensure that wget is set up to use the same proxy settings before installing the cuda-repo package.

·  $ sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb

 

Update the Apt repository cache

$ sudo apt-get update

 

Install CUDA

$ sudo apt-get install cuda

 

2.4. Additional Package Manager Capabilities

2.4.1. Available Packages

cat /var/lib/apt/lists/*cuda*Packages | grep "Package:"

2.4.2. Package Upgrades

The cuda package points to the latest stable release of the CUDA Toolkit. When a new version is available, use the following commands to upgrade the toolkit and driver:

sudo apt-get install cuda 

 

The cuda-drivers package points to the latest driver release available in the CUDA repository. When a new version is available, use the following commands to upgrade the driver: 

sudo apt-get install cuda-drivers

 

 

3安装sample(验证cuda是否成功)

cuda-install-samples-7.0.sh /home/guokai/cuda

 

If you installed the driver, verify that the correct version of it is loaded. If you did not install the driver, or are using an operating system where the driver is not loaded via a kernel module, such as L4T, skip this step.

When the driver is loaded, the driver version can be found by executing the command

 

$ cat /proc/driver/nvidia/version

nvcc -V i

 

The NVIDIA CUDA Toolkit includes sample programs in source form. You should compile them by changing to ~/NVIDIA_CUDA-7.0_Samples and typing makeThe resulting binaries will be placed under ~/NVIDIA_CUDA-7.0_Samples/bin.

 

4:cuda环境配置


4.1. Environment Setup

The PATH variable needs to include /usr/local/cuda-7.0/bin

The LD_LIBRARY_PATH variable needs to contain /usr/local/cuda-7.0/lib64 on a 64-bit system, and /usr/local/cuda-7.0/lib on a 32-bit system

 

To change the environment variables for 64-bit operating systems:

$ export PATH=/usr/local/cuda-7.0/bin:$PATH 

$ export LD_LIBRARY_PATH=/usr/local/cuda-7.0/lib64:$LD_LIBRARY_PATH

 

4.2.Running the Binaries

After compilation, find and run deviceQuery under ~/NVIDIA_CUDA-7.0_Samples. If the CUDA software is installed and configured correctly, the output for deviceQuery should look similar to that shown in

 

4.3.Running the Binaries

After compilation, find and run deviceQuery under ~/NVIDIA_CUDA-7.0_Samples. If the CUDA software is installed and configured correctly, the output for deviceQuery should look similar to that shown in 

  Figure 1. Valid Results from deviceQuery CUDA Sample

基于Ubuntu 14.04和CUDA 7.0为基础的caffe环境配置

The exact appearance and the output lines might be different on your system. The important outcomes are that a device was found (the first highlighted line), that the device matches the one on your system (the second highlighted line), and that the test passed (the final highlighted line).

If a CUDA-capable device and the CUDA Driver are installed but deviceQuery reports that no CUDA-capable devices are present, this likely means that the /dev/nvidia* files are missing or have the wrong permissions.

On systems where SELinux is enabled, you might need to temporarily disable this security feature to run deviceQuery. To do this, type:

$ setenforce 0

from the command line as the superuser.

Running the bandwidthTest program ensures that the system and the CUDA-capable device are able to communicate correctly. Its output is shown in Figure 2.

Figure 2. Valid Results from bandwidthTest CUDA Sample


基于Ubuntu 14.04和CUDA 7.0为基础的caffe环境配置

Note that the measurements for your CUDA-capable device description will vary from system to system. The important point is that you obtain measurements, and that the second-to-last line (in Figure 2) confirms that all necessary tests passed.

Should the tests not pass, make sure you have a CUDA-capable NVIDIA GPU on your system and make sure it is properly installed.

If you run into difficulties with the link step (such as libraries not being found), consult the Linux Release Notes found in the doc folder in the CUDA Samples directory.

 

4.4. Optional Actions

Other options are not necessary to use the CUDA Toolkit, but are available to provide additional features.

 

4.4.1. Install Third-party Libraries

Some CUDA samples use third-party libraries which may not be installed by default on your system. These samples attempt to detect any required libraries when building. If a library is not detected, it waives itself and warns you which library is missing. To build and run these samples, you must install the missing libraries.

 

4.4.2. Install the source code for cuda-gdb

The cuda-gdb source is automatically installed with the runfile installation method.

To obtain a copy of the source code for cuda-gdb using the RPM and Debian installation methods, the cuda-gdb-src package must be installed.

The source code is installed as a tarball in the /usr/local/cuda-7.0/extras directory.

 

5:安装Boost:

apt-get install mpi-default-dev  #安装mpi库  

apt-get install libicu-dev     #支持正则表达式的UNICODE字符集

 

apt-get install python-dev     #需要python的话  

apt-get install libbz2-dev     #如果编译出现错误:bzlib.h: No such file or directory  

 

上述函数库装好之后,就可以编译boost库了。解压boost_1_49_0.tar.bz2,得到/boost_1_49_0,将当前工作目录切换到此文件夹下。

打开解压后的文件夹,里面有个bootstrap.sh的脚本文件,运行这个脚本

./bootstrap.sh

./b2 install  

建立一个test.cpp文件

 

1. #include<iostream>  

2. #include<boost/bind.hpp>  

3. using namespace std;  

4. using namespace boost;  

5. int fun(int x,int y){return x+y;}  

6. int main(){  

7.     int m=1;int n=2;  

8.     cout<<boost::bind(fun,_1,_2)(m,n)<<endl;  

9.     return 0;  

10. }  

g++ test.cpp -o test  

 

./test


6. 安装openBLAS 或 Intel MKL 或 Atlas

安装BLAS:

sudo apt-get install libatlas-base-dev

 

7. 安装OpenCV

依赖包安装

Ubuntu下安装软件或多或少都需要安装一些依赖包(软件)。opencv也不例外。

(1)build-essential 软件包,为编译程序提供必需软件包的列表信息,这样软件包才知道头文件、库函数在哪里。还会下载依赖的软件包,安装gcc/g++/gdb/make 等基本编程工具,最后才组成一个开发环境。

sudo apt-get install build-essential

(2)下面依赖关系主要是为了能够支持读写图片以及视频等。

sudo apt-get install  libgtk2.0-dev libavcodec-dev libavformat-dev  libtiff4-dev  libswscale-dev libjasper-dev

(3)安装cmake,可以官网上下载源码编译,也可以在百度网盘下载2.8.12版本,当然可以直接使用apt-get方法。

sudo apt-get install cmake

(4)安装pkg-config,它是一个提供从源代码中编译软件时查询已安装的库时使用的统一接口的计算机软件。

sudo apt-get install pkg-config

编译安装过程

(1)使用cmake来检测编译环境以及生成makefile以及其他project信息,在使用cmake过程可以设置一些参数。

在本目录下使用cmake,注意后面的点表示在当前目录下寻找CMakeLists.txt。

cmake .

也可以在可以在另外建一个文件夹,最常见的就是子目录。当然在构建的时候也可以配置一些参数:

cd opencv-2.4.9

mkdir release

cd release

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_ARCH_BIN=3.2 ..

(2)使用make编译

make

(3)使用make install 安装

sudo make install

(4)在/etc/ld.so.conf.d 目录下增加一个conf文件(可以命名为 opencv.conf),同样添加 /usr/local/lib

 

使用下面的动态库管理命令ldconfig,让opencv的相关链接库被系统共享

sudo ldconfig -v

(5)完成了动态库的共享,还不能使用opencv来编程,还需要为程序指定openvc的头文件位置。这里使用pkg-config命令来完成。首先在 /etc/profile 中添加

export  PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

pkg-config 维护着一个关于opencv的配置文件,这个文件(opencv.pc)在目录 /usr/local/lib/pkgconfig,这个文件记录着opencv的一些动态库信息,还有头文件信息通过pkg-config 命令可以列出关于opencv的配置信息:

 

测试

(1)在解压的opencv目录下找到 samples/c/build_all.sh,运行该文件

./build_all.sh

(2)找其中一个来运行看一下效果,例如 find_obj程序:

到Github上下载已经写好的安装脚本:https://github.com/jayrambhia/Install-OpenCV

下载该脚本,进入Ubuntu/2.4 目录, 给所有shell脚本加上可执行权限

chmod +x *.sh

然后安装最新版本 (当前为2.4.9)

sudo ./opencv2_4_9.sh

脚本会自动安装依赖项,下载安装包,编译并安装OpenCV。整个过程大概半小时左右。

 

8:安装Caffe所需要的Python环境

(1)安装anaconda包

在此下载最新的安装包, 用默认设置安装在用户目录下。

./Anaconda-2.2.0-Linux-x86_64.sh

方法:进入文件/etc.profile进行编辑加入

export PATH=/root/anaconda/bin:$PATH

source /etc/profile

(2)安装python依赖库

打开新的终端, 用which python和which pip确定使用的是anaconda提供的python环境:配置/etc/profile

然后进入caffe_root/python, 执行下列命令

for req in $(cat requirements.txt); do sudo pip install $req; done 声明环境变量

(3)修正Anaconda存在的bug

加入在编译或者运行caffe时遇到这样的错误

/usr/lib/x86_64-linux-gnu/libx264.so.142:undefined reference to' 

那么请删除掉anaconda/lib中的libm.*. 参考this issue

(4)添加Anaconda Library Path

这里需要注意,在运行Caffe时,可能会报一些找不到libxxx.so的错误,而用 locate libxxx.so命令发现已经安装在anaconda中,这时首先想到的是在/etc/ld.so.conf.d/ 下面将 your_anaconda_path/lib 加入 LD_LIBRARY_PATH中。 但是这样做可能导致登出后无法再进入桌面!!!原因(猜测)可能是anaconda的lib中有些内容于系统自带的lib产生冲突。

正确的做法是:为了不让系统在启动时就将anaconda/lib加入系统库目录,可以在用户自己的~/.bashrc 中添加library path, 比如我就在最后添加了两行

# add library path LD_LIBRARY_PATH=your_anaconda_path/lib:$LD_LIBRARY_PATHexport LD_LIBRARY_PATH