caffe ubuntu安装全过程 GPU CPU均可参考

来源:互联网 发布:java 彩票关注源码 编辑:程序博客网 时间:2024/06/07 05:25

基本常时命令

sudo uname --m 查看自己运行ubuntu是32位还是64sudo uname --s 显示内核名字s sudo uname --r 显示内核版本 sudo uname --n 显示网络主机名 sudo uname --p 显示cpu  sudo uname -a 查看Ubuntu版本号

安装过程中参考博客:
http://www.2cto.com/os/201607/528798.html
http://blog.csdn.net/u012177034/article/details/52102676
http://blog.csdn.net/wopawn/article/details/52302164
开始安装英伟达驱动(命令行方式)
先确认下Nouveau是已经被干掉,使用命令: lsmod | grep nouveau
安装Nvidia显卡的官方驱动和系统自带的nouveau驱动冲突。

1)把驱动加入黑名单中: /etc/modprobe.d/blacklist.conf  在后面加入:blacklist nouveauoptions nouveau modeset=0不要sudo reboot,不然进不来图形化界面只能命令行了。z确认下Nouveau是已经被干掉,使用命令: lsmod | grep nouveau
(3)重启系统至init 3(文本模式),也可先进入图形桌面再运行init 3进入文本模式,再安装下载的驱动就无问题,安装命令示例
GTX1080显卡驱动367.35安装 gtx1070台式机测试成功
sudo apt-get update##更新源 sudo add-apt-repository ppa:graphics-drivers/ppa 按回车继续 sudo apt-get update sudo apt-get install nvidia-367 sudo apt-get install mesa-common-dev sudo apt-get install freeglut3-dev 

以上装完后重启电脑,此时驱动安装完毕,系统没有卡顿现象
GTX1050ti 的显卡:
装驱动前关闭 x server

sudo /etc/init.d/lightdm stop 或sudo service lightdm stopsudo /etc/init.d/lightdm status 或sudo service lightdm statussudo apt-get update##更新源 sudo add-apt-repository ppa:graphics-drivers/ppasudo apt-get update sudo apt-get install nvidia-378 sudo apt-get install mesa-common-dev sudo apt-get install freeglut3-dev

安装后打开X server

sudo service lightdm start/restart//如果装完驱动发现进不了桌面了,可以将装的驱动删除(    sudo dpkg -l|grep nvidia(查询)    sudoapt-getremove --purge nvidia*(删除))显示机器上的显卡信息:lspci  | grep -i vga查看更详细的信息:lspci -v

开始安装英伟达驱动(非命令行方式)
如果这种方式安装显卡后进不了图形化界面的话,试试下面这种方法:

首先,点击system setting,进入下图所示的菜单。 Ubuntu system setting点击software & updates ubuntu software把Download from改成Main server,然后点击close。 这个时候,系统会问你要不要reload,点击reload等待加载完毕即可。再次进入system setting,点击Details system details

因为我已经安装好了,这个时候Graphics看到的是GeForce GTX 860M/PCIe/SSE2
如果你没有安装NVIDIA驱动,这个时候看到的应该是独显的信息。
当然,这个时候你的右下角按钮显示的也不是System Up-To-Date。点击更新按钮,更新一下系统文件。然后重启。

上一步的重启完成之后,点击system setting – >software & updates – > addtional drivers 。然后点击Using NVIDIA binary driver -version 367.57,再点击右下角Apply Changes。additional drivers

之后就是等待,等安装完成后重启电脑。如果安装正常,就可以在System Detail中看到如下界面了(Graphics 变成了)。

system detail运行nvidia-smi可以看到更详细的信息。 nvidia-smi

到此,NVIDIA驱动安装完毕。
输入指令查看是否安装成功 nvidia-smi
安装过程如下:

如果没有GPU支持的话就可以直接跳到第10步。
安装CUDA 8.0
cuda下载地址:https://developer.nvidia.com/cuda-downloads
验证过程省略,按照官方文档自己操作吧(遇到问题首先要看官方文档啊,血泪教训)
安装CUDA有两种方法,
离线.run安装:从官网下载对应版本的.run安装包安装,安装过程挺复杂,尝试过几次没成功,遂放弃。
在离线.deb安装:deb安装分离线和在线,我都尝试过都安装成功了,官网下载地址
安装之前请先进行md5校验,确保下载的安装包完整
1.下载cuda_8.0.27_linux.run和cudnn-8.0-linux-x64-v5.1.tgz
这里我提供了百度网盘,这两个文件我先在win10下下载好,并用u盘拷贝到ubuntu的下载目录下
2.安装cuda8.0
终端cd 下载/

sh cuda_8.0.27_linux.run --override 

启动安装程序,一直按空格到最后,输入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,以便后面测试Installing the CUDA Toolkit in /usr/local/cuda-8.0 …Missing recommended library: libXi.soMissing recommended library: libXmu.so Installing the CUDA Samples in /home/zhou …Copying samples to /home/zhou/NVIDIA_CUDA-8.0_Samples now…
Finished copying samples. ============ Summary ============Driver: Not SelectedToolkit: Installed in /usr/local/cuda-8.0Samples: Installed in /home/zhou, but missing recommended libraries Please make sure that- PATH includes /usr/local/cuda-8.0/bin- LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA. ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.To install the driver using this installer, run the following command, replacing with the name of this run file:sudo .run -silent -driver Logfile is /tmp/cuda_install_2961.log

安装完成,但是缺少一些库。
安装sudo apt-get install libxmu-dev libxi-dev
安装完成。
4) 设置环境变量
在终端输入这两句:

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

然后修改文件中环境变量设置

sudo vi /etc/profile

输入上面export的两句,保存,退出。

sudo ldconfig //环境变量立即生效

5) 验证安装是否完成

nvidia-smi nvcc –V显示: nvcc -V nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2016NVIDIA Corporation Built on Wed_May__4_21:01:56_CDT_2016 Cudacompilation tools, release 8.0, V8.0.26 

6) 测试cuda的samples

sudo apt-get install gccsudo apt-get install g++

安装cudnn v5.1
cudnn下载:https://developer.nvidia.com/rdp/cudnn-download
终端输入

cd 下载/ tar zxvf cudnn-8.0-linux-x64-v5.1.tgz 

解压在下载目录下产生一个cuda目录
复制头文件 安装cuDNN比较简单,解压后把相应的文件拷贝到对应的CUDA目录下即可:

tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz

显示以下信息:

*cuda/include/cudnn.hcuda/lib64/libcudnn.socuda/lib64/libcudnn.so.5cuda/lib64/libcudnn.so.5.0.5cuda/lib64/libcudnn_static.a*

继续执行以下指令:

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/sudo chmod a+r /usr/local/cuda/include/cudnn.hsudo chmod a+r /usr/local/cuda/lib64/libcudnn*

8) 验证一下;

cd /NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuerymake

然后deviceQuery就生成了./deviceQuery 就可以了。
cuda的samples 里面有个deviceQuery 运行之后会显示信息,最后一行出行pass说明成功啦

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 1050 TiResult = PASS

安装开发所需的依赖包

sudo apt-get install build-essential vim cmake git libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler sudo apt-get install --no-install-recommends sudo apt-get install libatlas-base-dev sudo apt-get install python-dev 

编译Caffe
下载caffe:

git clone https://github.com/bvlc/caffe.git cd caffe/ mv Makefile.config.example Makefile.config

配置CPU版本时
修改Makefile.config文件,打开CPU_ONLY选项,保存

## 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  # 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 \  # 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  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 := 0  # enable pretty build (comment to see full commands)  Q ?= @  保存退出配置GPU版本时:根据个人情况修改文件:a.若使用cudnn,则将#USE_CUDNN := 1修改成: USE_CUDNN := 1b.若使用的opencv版本是3的,则将#OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3c.若要使用Python来编写layer,则将#WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1

安装caffe的Python接口及python相关库
安装完该接口后,就能够在python中,通过import caffe方式来使用caffe,如此可以用到python的一些强大的库用来显示和计算,如matplotlib,sklearn等,这是命令行式caffe所难以达到的效果。
Cd caffe/python

1)安装python包安装工具pip sudo apt-get install python-dev python-pip2)sudo apt-get install python-opencv  sudo pip install pyzmq tornado jinja2 jsonschema jupyter (3)安装python包依赖,在requirements.txt文件里 sudo apt-get install gfortran libatlas-dev libblas-dev 

此步应先执行,否则下一步requirement里的scipy会安装失败
安装requirement里的python依赖包

for req in $(cat requirements.txt);do sudo pip install $req; done 

(4)python包都安装无误后,编译pycaffe

Cd .. Make pycaffe –j8 

此时即可在python里import caffe
编译

make all -j4  make test  make runtest  

遇到错误&&解决方法:
error while loading shared libraries: libcudart.so.8.0: cannot open shared object file: No such file or directory

sudo cp /usr/local/cuda-8.0/lib64/libcudart.so.8.0.44 /usr/local/lib/libcudart.so.8.0 && sudo ldconfigsudo cp /usr/local/cuda-8.0/lib64/libcublas.so.8.0.45 /usr/local/lib/libcublas.so.8.0 && sudo ldconfigsudo cp /usr/local/cuda-8.0/lib64/libcurand.so.8.0.44 /usr/local/lib/libcurand.so.8.0 && sudo ldconfigsudo cp /usr/local/cuda-8.0/lib64/libcudnn.so.5 /usr/local/lib/libcudnn.so.5 && sudo ldconfig

编译Python wrapper错误总结

make  pycaffe  

遇到错误:
make pycaffe出现错误
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: no such file or directory
Makefile:498: recipe for target ‘python/caffe/_caffe.so’ failed make: * [python/caffe/_caffe.so] Error 1
原因
python-numpy没有安装到位
解决方法
输入命令

sudo apt-get install python-numpy

错误提示:
building ‘dfftpack’ library
error: library dfftpack has Fortran sources but no Fortran compiler found
也就是没有安装gfortran编译器

sudo apt-get install gfortran  

ImportError: No module named packaging.version

wget https://bootstrap.pypa.io/get-pip.pysudo python get-pip.pypip install setuptools 

安装MATLAB
MATLAB安装步骤见我之后的笔记。
caffe中配置matlab:
修改Makefile.config文件,将MATLAB目录添加进来

修改Makefile.config,加上这一句:MATLAB_DIR := /opt//MATLAB/R2014a之后make matcaffe

生成了 matlab/+caffe/private/caffe_.mex64,可以直接被MATLAB调用。
Ubuntu14.04自带的gcc版本是4.8,MATLAB2014a支持的最高版本为4.7x。因此,需要安装gcc4.7,并给gcc降级
否则会出现错误:
error: no matching function for call to ‘remove_if(std::vector

 sudo apt-getupdate sudo apt-get install gcc-4.7 g++-4.7 g++-4.7-multilib gcc-4.7-multilib

在终端执行以下系统gcc降级命令(其中数字越大,表示优先级越高):

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.7 100 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 100 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.7 100sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.8 50

验证gcc-4.7是否安装并成为系统的默认版本:

gcc -vmake -j4makematcaffe -j4以上4表示采用4核。验证gcc-4.7是否安装并成为系统的默认版本: gcc -v

/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:208:4: error: no matching function for call to ‘remove_if(std::vector

1 0
原创粉丝点击