ubuntu16.04+Cuda8.0+CuDnn v5+OpenCV3.1.0+Matlab2014+Python+Caffe安装

from :  http://blog.csdn.net/u012780572/article/details/52801191

继win10安装caffe后,近日笔者在ubuntu Kylin16.04上开始安装caffe框架,以下为笔者经历。

在参考众多博客后,笔者发现 ubuntu16.04安装caffe 此篇较为具体,笔者在此基础上加以补充。

大概具体步骤如下:

  1. 最新显卡驱动的安装
  2. Cuda8.0的安装
  3. Cudnnv5的安装
  4. BLAS的安装
  5. OpenCV3.1.0的安装
  6. Matlab的安装
  7. anaconda的安装
  8. caffe及相关依赖的安装

一、最新显卡驱动的安装

(1)检查你的电脑有哪些显卡

<code class="hljs lasso has-numbering">lspci <span class="hljs-attribute">-k</span> <span class="hljs-subst">|</span> grep <span class="hljs-attribute">-A</span> <span class="hljs-number">2</span> <span class="hljs-attribute">-i</span> <span class="hljs-string">"VGA"</span>  </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

(2)查看哪一个专有驱动是推荐安装的

<code class="hljs bash has-numbering"><span class="hljs-built_in">sudo</span> ubuntu-drivers devices</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

有个recommended 的那个驱动就是推荐的,也可以上官网查自己显卡型号推荐的驱动

(3)安装驱动
安装驱动有三种方法:(1)run文件;(2)deb文件;(3)第三方安装,各种尝试后发现第三种方法是最简便安全的。

<code class="hljs lasso has-numbering">sudo add<span class="hljs-attribute">-apt</span><span class="hljs-attribute">-repository</span> ppa:graphics<span class="hljs-attribute">-drivers</span>  </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

然后

<code class="hljs lasso has-numbering">sudo apt<span class="hljs-attribute">-get</span> update   
sudo apt<span class="hljs-attribute">-get</span> install nvidia<span class="hljs-subst">-</span><span class="hljs-number">370</span>  
sudo apt<span class="hljs-attribute">-get</span> install mesa<span class="hljs-attribute">-common</span><span class="hljs-attribute">-dev</span>   
sudo apt<span class="hljs-attribute">-get</span> install freeglut3<span class="hljs-attribute">-dev</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li></ul>

nvidia-370是根据我的显卡740M推荐的驱动,显卡驱动型号要改成你匹配的型号,然后重启电脑使驱动生效。

这里值得一提的是,在没有装驱动的时候,通过命令行推荐型号为367,但是装上367后Cuda安装失败,提示驱动版本不匹配,这时再用命令行搜驱动,发现推荐驱动为370,然后cuda安装成功。

重启后测试

<code class="hljs lasso has-numbering">nvidia<span class="hljs-attribute">-smi</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

若界面显示驱动,则OK。

二. Cuda8.0的安装

( 1)从官网下载cuda8.0.run文件 ,进入该页面选择相应版本
( 2)在下载后的路径中,打开终端,启动安装程序

<code class="hljs applescript has-numbering">sh cuda_8<span class="hljs-number">.0</span><span class="hljs-number">.27</span>_linux.<span class="hljs-command">run</span> <span class="hljs-comment">--override</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

启动安装程序,一直按空格到最后,输入accept接受条款
输入n不安装nvidia图像驱动,之前已经安装过了
输入y安装cuda 8.0工具
回车确认cuda默认安装路径:/usr/local/cuda-8.0
输入y用sudo权限运行安装,输入密码
输入y或者n安装或者不安装指向/usr/local/cuda的符号链接
输入y安装CUDA 8.0 Samples,以便后面测试
回车确认CUDA 8.0 Samples默认安装路径:/home/automan(automan是我的用户名),该安装路径测试完可以删除

<code class="hljs asciidoc has-numbering">===========  
<span class="hljs-header">= Summary =  </span>
===========  

Driver:   Not Selected  
Toolkit:  Installed in /usr/local/cuda-7.5  
Samples:  Installed in /usr/local/cuda-7.5, but missing recommended librarie </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>

即安装正确
(3)添加环境变量

<code class="hljs bash has-numbering">    <span class="hljs-built_in">sudo</span> gedit ~/.bashrc  
    <span class="hljs-keyword">export</span> CUDA_HOME=/usr/local/cuda-<span class="hljs-number">8.0</span>    
    <span class="hljs-keyword">export</span> LD_LIBRARY_PATH=/usr/local/cuda-<span class="hljs-number">8.0</span>/lib64:<span class="hljs-variable">$LD_LIBRARY_PATH</span>   
    <span class="hljs-keyword">export</span> PATH=/usr/local/cuda-<span class="hljs-number">8.0</span>/bin:<span class="hljs-variable">$PATH</span>  
    <span class="hljs-built_in">source</span> ~/.bashrc </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

在这里可以补充一下,关于添加环境变量的三种方法。
1.控制台中,不赞成使用这种方法,因为换个shell,你的设置就无效了,因此这种方法仅仅是临时使用,以后要使用的时候又要重新设置,比较麻烦。
这个只针对特定的shell;
PATH=”$PATH:/my_new_path” (关闭shell,会还原PATH)

2.修改/etc/profile文件,如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题。 这里是针对所有的用户的,所有的shell;
vi /etc/profile
在里面加入:
export PATH=”$PATH:/my_new_path”

或者在/etc/bashrc里面作同样的修改。

3.修改.bashrc文件,这种方法更为安全,它可以把使用这些环境变量的权限控制到用户级别,这里是针对某一个特定的用户,如果你需要给某个用户权限使用这些环境变量,你只需要修改其个人用户主目录下的.bashrc文件就可以了。
vi /root/.bashrc
在里面加入:
export PATH=”$PATH:/my_new_path”

后两种方法一般需要重新注销系统才能生效,最后可以通过echo命令测试一下:
echo $PATH

输出已经是新路径了。

(4)查看cuda版本

<code class="hljs brainfuck has-numbering"><span class="hljs-comment">nvcc</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">version</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

然后编译例子

<code class="hljs bash has-numbering"><span class="hljs-built_in">cd</span> ~/NVIDIA_CUDA-<span class="hljs-number">8.0</span>_Samples/  
make -j4 </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

这里会出现gcc、g++版本过高的问题。
两种方法:
a、直接下载低版本的gcc、g++替换之
ubuntu16.04 默认安装的gcc版本为gcc-5.4,(可用gcc –version查看)有时可能需要低版本的,所以我们先安装gcc-4.8.5

<code class="hljs bash has-numbering">    <span class="hljs-built_in">sudo</span> apt-get install -y gcc-<span class="hljs-number">4.8</span> g++-<span class="hljs-number">4.8</span>    
    <span class="hljs-built_in">sudo</span> update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-<span class="hljs-number">4.8</span> <span class="hljs-number">20</span>   
    <span class="hljs-built_in">sudo</span> update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-<span class="hljs-number">4.8</span> <span class="hljs-number">20</span>   
    <span class="hljs-built_in">cd</span> /usr/bin    
    <span class="hljs-built_in">sudo</span> cp gcc gcc_backup   
    <span class="hljs-built_in">sudo</span> cp g++ g++_backup   
    <span class="hljs-built_in">sudo</span> rm gcc g++   
    <span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span> gcc-<span class="hljs-number">4.8</span> gcc   
    <span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span> g++-<span class="hljs-number">4.8</span> g++  </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>

b、修改源码,使其支持更高版本的gcc和g++

<code class="hljs bash has-numbering"><span class="hljs-built_in">cd</span> /usr/local/cuda/include/ <span class="hljs-comment">#进入到头文件目录   </span>
<span class="hljs-built_in">sudo</span> gedit host_config.h <span class="hljs-comment">#编辑头文件 </span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

Ctrl + F 搜索GNUC,找到一段类似于 “if GNUC>5 || (GNUC == 5 &&GNUC_MINOR> 3)”
,就是最大支持gcc-5.3,可自己取消该限制。建议改为使其支持gcc-5.4,或者直接把两个5改成6
(5)进入例子

<code class="hljs bash has-numbering"><span class="hljs-built_in">cd</span> ~/NVIDIA_CUDA-<span class="hljs-number">8.0</span>_Samples/bin/x86_64/linux/release  
./deviceQuery </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

Result = PASS 说明cuda已经安装成功!
若fail,很有可能就是驱动不够新,

sudo ubuntu-drivers devices

装上recommend的驱动即可。

三. Cudnnv5的安装

在NVIDIA官网下载cudnn v5.0 for cuda8.0rc:cudnn-8.0-Linux-x64-v5.0-ga.tgz
安装过程很简单,直接解压拷贝就可以了。得到的文件是 .h 和 .so 文件。所以,直接把他们拷贝到 /usr/local/cuda/include 和 /usr/local/cuda/lib64/ 下就好了。

<code class="hljs avrasm has-numbering">tar xvzf cudnn-<span class="hljs-number">8.0</span>-linux-x64-v5<span class="hljs-number">.0</span>-ga<span class="hljs-preprocessor">.tgz</span>   
sudo <span class="hljs-keyword">cp</span> cudnn<span class="hljs-preprocessor">.h</span> /usr/local/cuda/include   <span class="hljs-preprocessor">#复制头文件</span>
sudo <span class="hljs-keyword">cp</span> lib* /usr/local/cuda/lib64   <span class="hljs-preprocessor">#复制动态链接库,lib.*等同与*.*等同于libcudnn.*</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li></ul>

建立软链接

<code class="hljs bash has-numbering">    <span class="hljs-built_in">cd</span> /usr/local/cuda/lib64/   
    <span class="hljs-built_in">sudo</span> rm -rf libcudnn.so libcudnn.so.<span class="hljs-number">5</span>   <span class="hljs-comment">#删除原有动态文件</span>
    <span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span> libcudnn.so.<span class="hljs-number">5.0</span>.<span class="hljs-number">5</span> libcudnn.so.<span class="hljs-number">5</span>  <span class="hljs-comment">#生成软衔接 </span>
    <span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span> libcudnn.so.<span class="hljs-number">5</span> libcudnn.so  <span class="hljs-comment">#生成软链接</span>
    <span class="hljs-built_in">sudo</span> ldconfig <span class="hljs-comment">#更新链接库</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

四. BLAS的安装

BLAS,即基础线性代数子程序库,里面拥有大量已经编写好的关于线性代数运算的程序,是一个应用程序接口的标准。caffe官网上推荐了三种实现:ATLAS, MKL, or OpenBLAS。其中atlas可以直接通过命令行安装.在此,采用intel的MKL库。intel官网下载MKL ,该链接进入后,申请学生版本的Parallel Studio XE Cluster Edition,这里注册要学校的邮箱拿去注册,然后邮箱会收到一封里面含有license的邮件,license在安装时会用到。文件比较大,下载完成后切换进文件所在目录解压缩该文件。

<code class="hljs bash has-numbering"><span class="hljs-built_in">cd</span> ~/Downloads/  
tar zxvf parallel_studio_xe_2017.tgz   <span class="hljs-comment">#解压下载文件  </span>
chmod <span class="hljs-number">777</span> parallel_studio_xe_2017 -R   <span class="hljs-comment">#获取文件权限  </span>
<span class="hljs-built_in">cd</span> parallel_studio_xe_2017/  
<span class="hljs-built_in">sudo</span> ./install_GUI.sh</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

安装完成后需要添加library路径, 创建/etc/ld.so.conf.d/intel_mkl.conf文件, 在文件中添加内容

<code class="hljs avrasm has-numbering">sudo gedit /etc/<span class="hljs-keyword">ld</span><span class="hljs-preprocessor">.so</span><span class="hljs-preprocessor">.conf</span><span class="hljs-preprocessor">.d</span>/intel_mkl<span class="hljs-preprocessor">.conf</span>  

/opt/intel/lib/intel64  
/opt/intel/mkl/lib/intel64

sudo ldconfig</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>

五. OpenCV3.1.0的安装

这里问题会比较多,要注意

首先安装一些必要的依赖库:

<code class="hljs lasso has-numbering">    sudo apt<span class="hljs-attribute">-get</span> install build<span class="hljs-attribute">-essential</span>  
    sudo apt<span class="hljs-attribute">-get</span> install cmake git libgtk2<span class="hljs-number">.0</span><span class="hljs-attribute">-dev</span> pkg<span class="hljs-attribute">-config</span> libavcodec<span class="hljs-attribute">-dev</span> libavformat<span class="hljs-attribute">-dev</span> libswscale<span class="hljs-attribute">-dev</span>  
    sudo apt<span class="hljs-attribute">-get</span> install python<span class="hljs-attribute">-dev</span> python<span class="hljs-attribute">-numpy</span> libtbb2 libtbb<span class="hljs-attribute">-dev</span> libjpeg<span class="hljs-attribute">-dev</span> libpng<span class="hljs-attribute">-dev</span> libtiff<span class="hljs-attribute">-dev</span> libjasper<span class="hljs-attribute">-dev</span> libdc1394<span class="hljs-subst">-</span><span class="hljs-number">22</span><span class="hljs-attribute">-dev</span>  </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li></ul>

下载OpenCV3.1.0并解压缩,然后进入解压缩后的文件,创建build文件。

<code class="hljs mathematica has-numbering">cd build  
cmake -<span class="hljs-keyword">D</span> CMAKE_BUILD_TYPE=<span class="hljs-keyword">Release</span> -<span class="hljs-keyword">D</span> CMAKE_INSTALL_PREFIX=/usr/local ..</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

期间可能会卡在ippcv的下载中,Ctrl + c中断后,可以到 ippicv_linux_20151201.tgz 下载。将下载的文件替换掉 OpenCV-3.1.0/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b下的同名文件。

<code class="hljs mathematica has-numbering">cmake -<span class="hljs-keyword">D</span> CMAKE_BUILD_TYPE=<span class="hljs-keyword">Release</span> -<span class="hljs-keyword">D</span> CMAKE_INSTALL_PREFIX=/usr/local ..  
make -j4       #四核运算  
sudo make install  </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li></ul>

但是make到82%出现错误,graphcuts.cpp文件中许多变量没有声明,google之后发现是因为opencv3.1还不支持cuda8.0,但是有个同学已经对其进行修改。
请参考:https://github.com/opencv/opencv/pull/6510/commits/10896129b39655e19e4e7c529153cb5c2191a1db

六. Matlab的安装

在网盘上下载安装包及Crack破解文件之后 matlab2014 ,解压两个压缩文件(最后一步解压时也会将part2一起合并解压):

<code class="hljs avrasm has-numbering">cd matlab2014/  
unrar <span class="hljs-built_in">x</span> MATHWORKS_R2014A<span class="hljs-preprocessor">.part</span>1<span class="hljs-preprocessor">.rar</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

然后在命令行cd进入matlab2014目录,输入:

<code class="hljs bash has-numbering"><span class="hljs-built_in">sudo</span> ./install </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

1、选择“不联网安装”:“Use a File Installation Key”;
2、当出现密钥时,输入20个数字12345-67890-12345-67890即可;
3、安装路径默认为:/usr/local/MATLAB/R2014a,然后选择自己需要安装的工具(默认全选);符号链接也为默认:/usr/local/bin。
4、需要激活时选择不要联网激活”Activate manually without the Internet”,运用Crack目录下的“license_405329_R2014a.lic”文件作为激活文件。
5、安装完成之后,还要将Crack/linux目录下的libmwservices.so文件拷贝到/usr/local/MATLAB/R2014a/bin/glnxa64,即:

<code class="hljs bash has-numbering"><span class="hljs-built_in">cd</span> ~/matlab2014/Crack/Linux/  
<span class="hljs-built_in">sudo</span> cp libmwservices.so /usr/local/MATLAB/R2014a/bin/glnxa64/  </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

为了避免每次都用root权限打开matlab,通过’sudo chown automan -R matlab’改变权限。(取消此操作)
~/.bashrc中添加 export PATH=/usr/local/MATLAB/R2014a/bin:$PATH最终直接在终端输入’matlab’就可以打开Matlab了,至此完成了Matlab在Ubuntu下的安装和破解。

七. anaconda的安装

(后面没用到,可跳过)

选择的是anaconda linux64 2.7版本python2.7 。下载完成之后,最好也要进行md5sum的检验。完成之后,cd进入下载文件所在的目录

<code class="hljs lasso has-numbering">bash Anaconda2<span class="hljs-subst">-</span><span class="hljs-number">4.2</span><span class="hljs-number">.0</span><span class="hljs-attribute">-Linux</span><span class="hljs-attribute">-x86_64</span><span class="hljs-built_in">.</span>sh  </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

安装完成之后,在~/.bashrc文件末尾添加Anaconda的库文件(注意“=”两边不要有空格)
PS: 之后

<code class="hljs bash has-numbering"><span class="hljs-built_in">sudo</span> gedit ~/.bashrc      
<span class="hljs-keyword">export</span> PATH=<span class="hljs-string">"/home/automan/anaconda2/bin:<span class="hljs-variable">$PATH</span>"</span>  
<span class="hljs-keyword">export</span> LD_LIBRARY_PATH=<span class="hljs-string">"/home/automan/anaconda2/lib:<span class="hljs-variable">$LD_LIBRARY_PATH</span>"</span>
<span class="hljs-built_in">source</span> ~/.bashrc</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li></ul>

重启电脑之后,在命令行输入:ipython,即可看到Python的版本。

八. caffe及相关依赖的安装

(1)首先安装各种依赖

<code class="hljs lasso has-numbering">sudo apt<span class="hljs-attribute">-get</span> update  
sudo apt<span class="hljs-attribute">-get</span> upgrade  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> build<span class="hljs-attribute">-essential</span> cmake git pkg<span class="hljs-attribute">-config</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> libprotobuf<span class="hljs-attribute">-dev</span> libleveldb<span class="hljs-attribute">-dev</span> libsnappy<span class="hljs-attribute">-dev</span> libhdf5<span class="hljs-attribute">-serial</span><span class="hljs-attribute">-dev</span> protobuf<span class="hljs-attribute">-compiler</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> libatlas<span class="hljs-attribute">-base</span><span class="hljs-attribute">-dev</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> <span class="hljs-subst">--</span>no<span class="hljs-attribute">-install</span><span class="hljs-attribute">-recommends</span> libboost<span class="hljs-attribute">-all</span><span class="hljs-attribute">-dev</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> libgflags<span class="hljs-attribute">-dev</span> libgoogle<span class="hljs-attribute">-glog</span><span class="hljs-attribute">-dev</span> liblmdb<span class="hljs-attribute">-dev</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> python<span class="hljs-attribute">-pip</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> python<span class="hljs-attribute">-dev</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> python<span class="hljs-attribute">-numpy</span> python<span class="hljs-attribute">-scipy</span>  
sudo apt<span class="hljs-attribute">-get</span> install <span class="hljs-attribute">-y</span> libopencv<span class="hljs-attribute">-dev</span> </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul>

(2)下载caffe,并安装配置

<code class="hljs avrasm has-numbering">cd ~  
git clone https://github<span class="hljs-preprocessor">.com</span>/BVLC/caffe<span class="hljs-preprocessor">.git</span> 
cd caffe    //打开到刚刚git下来的caffe  
<span class="hljs-keyword">cp</span> Makefile<span class="hljs-preprocessor">.config</span><span class="hljs-preprocessor">.example</span> Makefile<span class="hljs-preprocessor">.config</span> //make指令只能make Makefile<span class="hljs-preprocessor">.config</span>  
sudo gedit Makefile<span class="hljs-preprocessor">.config</span>      //打开Makefile<span class="hljs-preprocessor">.config</span>文件</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

(3)修改配置。此时不建议增加MATLAB,Python接口,仅做如下操作

<code class="hljs makefile has-numbering">1)USE_OPENCV,USE_LEVELDB,USE_LMDB三项取消注释,且0改成1,改为如下
<span class="hljs-comment"># uncomment to disable IO dependencies and corresponding data layers</span>
 USE_OPENCV := 1
 USE_LEVELDB := 1
 USE_LMDB := 1
2) OPENCV_VERSION := 3    <span class="hljs-comment">#取消注释符号#,应用opencv3 </span>
<span class="hljs-constant">USE_CUDNN</span> := 1 #取消该句注释
<span class="hljs-constant">BLAS</span> := mkl
<span class="hljs-constant">MATLAB_DIR</span> := /usr/local/MATLAB/R2014a
<span class="hljs-constant">WITH_PYTHON_LAYER</span> := 1 #取消注释  
3)
 <span class="hljs-comment"># Whatever else you find you need goes here.</span>
<span class="hljs-constant">INCLUDE_DIRS</span> := <span class="hljs-variable">$(PYTHON_INCLUDE)</span> /usr/local/include
<span class="hljs-constant">LIBRARY_DIRS</span> := <span class="hljs-variable">$(PYTHON_LIB)</span> /usr/local/lib /usr/lib
        修改为
<span class="hljs-comment"># Whatever else you find you need goes here.</span>
<span class="hljs-constant">INCLUDE_DIRS</span> := <span class="hljs-variable">$(PYTHON_INCLUDE)</span> /usr/local/include /usr/include/hdf5/serial
<span class="hljs-constant">LIBRARY_DIRS</span> := <span class="hljs-variable">$(PYTHON_LIB)</span> /usr/local/lib /usr/lib /usr/lib/i386-linux-gnu/hdf5/serial
4)</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul>

打开makefile文件,

<code class="hljs lasso has-numbering">将
NVCCFLAGS <span class="hljs-subst">+=-</span>ccbin<span class="hljs-subst">=</span>$(CXX) <span class="hljs-attribute">-Xcompiler</span><span class="hljs-attribute">-fPIC</span> $(COMMON_FLAGS)
替换
NVCCFLAGS <span class="hljs-subst">+=</span> <span class="hljs-attribute">-D_FORCE_INLINES</span> <span class="hljs-attribute">-ccbin</span><span class="hljs-subst">=</span>$(CXX) <span class="hljs-attribute">-Xcompiler</span> <span class="hljs-attribute">-fPIC</span> $(COMMON_FLAGS)</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li></ul>

(4)然后编译 make all -j4

下面错误可能会出现

(1).build_release/lib/libcaffe.so: undefined reference to ‘google::protobuf::io::CodedOutputStream::WriteVarint64ToArray(unsigned long long, unsigned char*)’
原因:apt-get install 的安装命令导致安装的protobuf
的gcc编译器版本低,所以需要卸载当前的libprotobuf-dev 和protobuf-compiler,重新安装protobuf-2.5.0.
1.卸载当前libprotobuf-dev 和protobuf-compiler

<code class="hljs lasso has-numbering">sudo apt<span class="hljs-attribute">-get</span> autoremove libprotobuf<span class="hljs-attribute">-dev</span> protobuf<span class="hljs-attribute">-compiler</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>

2.重新让系统恢复默认的gcc-5

<code class="hljs bash has-numbering"><span class="hljs-built_in">cd</span> /usr/bin
<span class="hljs-built_in">sudo</span> rm gcc
<span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span> gcc-<span class="hljs-number">5</span> gcc
<span class="hljs-built_in">sudo</span> rm g++
<span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span>  g++-<span class="hljs-number">5</span> g++</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

3.protobuf-2.5.0安装
下载protobuf-2.5.0,下载地址:http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz
安装过程参考:http://blog.csdn.net/hjimce/article/details/51999566
我的有点不同的地方是:路径不同,安装完操作如下

<code class="hljs bash has-numbering"><span class="hljs-built_in">sudo</span> gedit /etc/profile
<span class="hljs-keyword">export</span> PATH=<span class="hljs-variable">$PATH</span>:/usr/local/bin/
<span class="hljs-keyword">export</span> PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
<span class="hljs-built_in">source</span> /etc/profile
<span class="hljs-built_in">sudo</span> gedit /etc/ld.so.conf
/usr/local/lib
<span class="hljs-built_in">sudo</span> ldconfig
</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul>

安装完后,测试protoc –version
(2)fatal error: caffe/proto/caffe.pb.h: No such file or directory
重新链接CUDNN
(3)undefined reference to `leveld 这时需重新安装leveldb
首先可以从https://github.com/google/leveldb.git下载leveldb,然后cd到leveldb目录中,执行make。过一会,就可以在目录下看到静态链接库libleveldb.a和动态链接库libleveldb.so.1.19. 如果不用动态链接库的话,安装已经完成了。但是如果要用动态链接库,则还需要把头文件以及动态链接库拷贝到系统路径里面,具体如下:

  1. 把include/leveldb目录拷贝到/usr/include

    sudo cp -r include/leveldb /usr/include

  2. 把动态链接库文件拷贝到/usr/lib下,再按当前目录下的形式,创建两个软连接。

<code class="hljs bash has-numbering"><span class="hljs-built_in">cd</span> out-shared

<span class="hljs-built_in">sudo</span> cp libleveldb.so.<span class="hljs-number">1.18</span> /usr/lib

<span class="hljs-built_in">cd</span> /usr/lib

<span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span> libleveldb.so.<span class="hljs-number">1.18</span> libleveldb.so.<span class="hljs-number">1</span>

<span class="hljs-built_in">sudo</span> ln <span class="hljs-operator">-s</span> libleveldb.so.<span class="hljs-number">1</span> libleveldb.so

ldconfig</code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul>

最后要执行ldconfig命令,将动态链接库加到缓存中,这样系统才能真正使用这个动态链接库。

(4)/usr/bin/ld: cannot find -lcudnn
collect2: error: ld returned 1 exit status
make: * [.build_release/lib/libcaffe.so] Error 1

解决方法很简单,只要赋予 others 可读(写)权限即可:

sudo chmod 755 libcudnn.so.X.Y.ZZ

如果编译没有问题,可进行

<code class="hljs lasso has-numbering">make test <span class="hljs-attribute">-j4</span>  
make runtest <span class="hljs-attribute">-j4</span> </code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

此时当提示CPU错误时,如下
[ FAILED ] AdamSolverTest/0.TestSnapshotShare, where TypeParam = caffe::CPUDevice
如果使用了Intel MKL作为BLAS,可能是Intel MKL的浮点数计算功能没有设置正确。使用如下命令:
export MKL_CBWR=AUTO
然后重新make runtest
若使用第二步中的命令完全不起作用时,建议使用Atlas替换Intel MKL。
重新编译make runtest即可。

(5)接下来编译matcaffe ,pycaffe
笔者发现,当gcc,g++版本由5.4降到4.7时,caffe的编译会出错,显示protobuf和leveldb有问题,当版本升回5.4,编译成功。在编译caffe的时候,make all一定要在高版本的gcc下
之前cuda8.0最高支持5.3时,建议改文件,不要降版本,否则caffe编译会失败。

虽然MATLAB2014a支持gcc的最高版本为4.7x,但是在caffe目录下make matcaffe,用gcc-5也成功编译!然后make mattest
报错
undefined symbol:
_ZNK6google8protobuf7MessageGETxxxxxx(后面的信息不记得了)

在终端输入:

ldd ~/work/caffe/matlab/+caffe/private/caffe_.mexa64

在matlab窗口输入:

!ldd ~/work/caffe/matlab/+caffe/private/caffe_.mexa64

查看matlab的依赖库和caffe的不同之处,发现是libprotobuf.so.8存在问题。

然后关闭MATLAB,在终端输入

<code class="hljs lasso has-numbering">sudo rm <span class="hljs-attribute">-f</span> /usr/<span class="hljs-built_in">local</span>/MATLAB/R2014a/bin/glnxa64/libprotobuf<span class="hljs-built_in">.</span>so<span class="hljs-number">.8</span>
sudo ln <span class="hljs-attribute">-sf</span> /usr/<span class="hljs-built_in">local</span>/lib/libprotobuf<span class="hljs-built_in">.</span>so<span class="hljs-number">.8</span> /usr/<span class="hljs-built_in">local</span>/MATLAB/R2014a/bin/glnxa64/libprotobuf<span class="hljs-built_in">.</span>so<span class="hljs-number">.8</span></code><ul class="pre-numbering" style="opacity: 0.842947;"><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>

再次make mattest, 成功~

更多matcaffe的bug参见(http://blog.csdn.net/sinat_29089097/article/details/52702246)

接下来make pycaffe,成功
然后打开Python,import caffe报错,在~/.bashrc 加上
export PYTHONPATH=/home/automan/work/caffe/python:$PYTHONPATH
再次import ,成功。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值