Ubuntu 16.04 安装 Caffe

来源:互联网 发布:广州java编程培训 编辑:程序博客网 时间:2024/05/20 23:35

前言

Caffe的安装大体分为5个步骤:参考地址

  • 安装 CUDA 和 cuDNN
  • 安装软件 Python 和 Matlab
  • 安装依赖
  • 编译 Caffe,pycaffe 和 matcaffe
  • 路径设置

安装 CUDA 和 cuDNN

安装 CUDA 参考地址 下载地址

  • Verify You Have a CUDA-Capable GPU: lspci | grep -i nvidia
  • Verify You Have a Supported Version of Linux: uname -m && cat /etc/*release
  • Verify the System Has gcc Installed: gcc --version
  • Verify the System has the Correct Kernel Headers and Development Packages Installed: uname -r,sudo apt-get install linux-headers-$(uname -r)
  • Create a file at /etc/modprobe.d/blacklist-nouveau.conf with the following contents:
    blacklist nouveau
    options nouveau modeset=0
  • Regenerate the kernel initramfs: sudo update-initramfs -u
  • Reboot into text mode (runlevel 3): systemctl set-default runlevel3.target
  • 重启reboot
  • 确认nouveau被屏蔽:lsmod | grep nouveau,无输出则被屏蔽
  • 安装:sh cuda_8.0.61_375.26_linux.run,examples路径设置为:/usr/local/cuda-8.0
  • 进入图像界面 systemctl set-default runlevel5.target
  • 重启:reboot
  • 查看显卡:cat /proc/driver/nvidia/version
  • 编译示例:cd /usr/local/cuda-8.0/NVIDIA_CUDA-8.0_Samples/
    make
  • 添加PATH路径:vim /etc/profile
    #CUDA
    export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
    . /etc/profile
  • 添加动态链接库:vim /etc/ld.so.conf
    #CUDA
    /usr/local/cuda-8.0/lib64
  • 编译例子 cd /usr/local/cuda-8.0/NVIDIA_CUDA-8.0_Samples
    make
  • 检验安装结果:cd 1_Utilities/deviceQuery
    ./deviceQuery

    若出现下列结果,则安装成功
./deviceQuery Starting...CUDA Device Query (Runtime API) version (CUDART static linking)Detected 1 CUDA Capable device(s)Device 0: "GeForce GT 740"  CUDA Driver Version / Runtime Version          8.0 / 8.0  CUDA Capability Major/Minor version number:    3.0  Total amount of global memory:                 974 MBytes (1021706240 bytes)  ( 2) Multiprocessors, (192) CUDA Cores/MP:     384 CUDA Cores  GPU Max Clock rate:                            993 MHz (0.99 GHz)  Memory Clock rate:                             2500 Mhz  Memory Bus Width:                              128-bit  L2 Cache Size:                                 262144 bytes  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers  Total amount of constant memory:               65536 bytes  Total amount of shared memory per block:       49152 bytes  Total number of registers available per block: 65536  Warp size:                                     32  Maximum number of threads per multiprocessor:  2048  Maximum number of threads per block:           1024  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)  Maximum memory pitch:                          2147483647 bytes  Texture alignment:                             512 bytes  Concurrent copy and kernel execution:          Yes with 1 copy engine(s)  Run time limit on kernels:                     Yes  Integrated GPU sharing Host Memory:            No  Support host page-locked memory mapping:       Yes  Alignment requirement for Surfaces:            Yes  Device has ECC support:                        Disabled  Device supports Unified Addressing (UVA):      Yes  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0  Compute Mode:     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GT 740Result = PASS

安装 cuDNN

下载地址

  • 解压下载文件
    tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz
  • 拷贝文件到相应路径
    sudo cp cuda/include/cudnn.h /usr/local/cuda-8.0/include
    sudo cp cuda/lib64/libcudnn* /usr/local/cuda-8.0/lib64

安装软件 Python 和 Matlab

安装 Python

Ubuntu 16.04 自带Python2和Python3想用哪个版本都可以,这里就不介绍Python的安装了

安装Matlab

  • 拷贝安装文件到硬盘
  • 修改安装文件权限chmod -R +777 R2015b_glnxa64
  • 执行安装程序cd R2015_glnxa64 sudo ./install
  • 选择不联网安装,序列号为09806-07443-53955-64350-21751-41297,若安装Matlab Production Server,使用40236-45817-26714-51426-39281
  • 拷贝破解文件sudo cp R2015b/bin/glnxa64/* /usr/local/MATLAB/R2015b/bin/glnxa64/
  • 激活cd /usr/local/MATLAB/R2015b/binsudo ./matlab,选择standalone进行激活
  • 创建快捷方式, sudo vim /usr/share/applications/matlab.desktop输入下列内容:
[Desktop Entry]Encoding=UTF-8Name=Matlab R2015bExec=/usr/local/MATLAB/R2015b/bin/matlab -desktopIcon=/usr/local/MATLAB/R2015b/toolbox/shared/dastudio/resources/MatlabIcon.pngTerminal=falseType=ApplicationCategories=Development;IDE;Name[zh_CN]=Matlab R2015b
  • 注意刚安装完运行时可能遇到两个问题:
    1.启动时终端显示matlab is selecting software opengl rendering,运行imshow() 会提示错误,原因是没安装显卡驱动,所以要最好先安装CUDA,再安装Matlab
    2.用快捷方式启动Matlab时,画面卡住不动,而且在终端不用root权限运行matlab时会出错,解决办法:
    sudo chmod a+w -R /home/sibo/.matlab

安装依赖

General dependencie

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev

BLAS

sudo apt-get install libatlas-base-dev

Python headers

apt-get install python3-dev

编译 Caffe,pycaffe 和 matcaffe

编译Caffe

  • 解压文件
    cd /usr/local
    unzip /media/sibo/UUI/软件/caffe-master.zip
  • 修改Python依赖包
    cd caffe-master/python
    vim requirements.txt

    内容修改为
    Cython
    numpy==1.11.0
    scipy
    scikit-image
    matplotlib
    ipython
    h5py
    leveldb
    networkx
    nose
    pandas
    python-dateutil
    protobuf
    python-gflags
    pyyaml
    Pillow
    six
  • 安装Python包
    sudo apt-get install python3-pip
    pip3 install --upgrade pip
    for i in $(cat requirements.txt); do sudo pip3 install $i; done
  • 复制配置文件cp Makefile.config.example Makefile.config
  • 修改配置文件
USE_CUDNN := 1CUDA_DIR := /usr/local/cuda-8.0BLAS := atlasMATLAB_DIR := /usr/local/MATLAB/R2015bPYTHON_LIBRARIES := boost_python3 python3.5mPYTHON_INCLUDE := /usr/include/python3.5m \                 /usr/local/lib/python3.5/dist-packages/numpy/core/includePYTHON_LIB := /usr/lib/python3.5/config-3.5m-x86_64-linux-gnuWITH_PYTHON_LAYER := 1INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/includeLIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/libBUILD_DIR := buildDISTRIBUTE_DIR := distributeTEST_GPUID := 0
  • 编译make all
    出现错误:fatal error: gflags/gflags.h: 没有那个文件或目录
    解决办法:apt-get install libgflags-dev
    出现错误:fatal error: glog/logging.h: 没有那个文件或目录
    解决办法:apt-get install libgoogle-glog-dev
    出现错误:fatal error: hdf5.h: 没有那个文件或目录
    解决办法:vim Makefile.config 添加
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include \
    /usr/include/hdf5/serial

    出现错误:fatal error: lmdb.h: 没有那个文件或目录
    解决办法:apt-get install liblmdb-dev
    出现错误:/usr/bin/ld: 找不到 -lhdf5_hl
    /usr/bin/ld: 找不到 -lhdf5
    /usr/bin/ld: 找不到 -lboost_python3

    解决办法:vim Makefile.config 添加
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib \
    /usr/lib/x86_64-linux-gnu/hdf5/serial \
    /usr/lib/x86_64-linux-gnu

    cd /usr/lib/x86_64-linux-gnu/
    ln -s libboost_python-py35.so libboost_python3.so
  • make test, make runtest
  • make pycaffe
  • 修改python测试命令
    vim /usr/local/caffe-master/Makefile

    pytest: pycd python; python -m unittest discover -s caffe/test修改为pytest: pycd python; python3 -m unittest discover -s caffe/testmake pytest
  • make matcaffe
    make mattest
    出现错误:Invalid MEX-file '/usr/local/caffe-master/matlab/+caffe/private/caffe_.mexa64':
    /usr/local/MATLAB/R2015b/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version
    GLIBCXX_3.4.20’
    not found `
    解决办法:
cd /usr/local/MATLAB/R2015b/sys/os/glnxa64/mv libstdc++.so.6 libstdc++.so.6.bakln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 libstdc++.so.6

出现错误:caffe_.mexa64: undefined symbol:
_ZN2cv8imencodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE

原因是caffe在编译的时候用的是系统的opencv库,而Matlab软件用的是自己的opencv库,所以有冲突
解决办法:参考地址

root@sibo-All-Series:/usr/local/caffe-master# cd /usr/local/MATLAB/R2015b/bin/glnxa64/root@sibo-All-Series:/usr/local/MATLAB/R2015b/bin/glnxa64# mv libopencv_imgproc.so.2.4 libopencv_imgproc.so.2.4.bakroot@sibo-All-Series:/usr/local/MATLAB/R2015b/bin/glnxa64# mv libopencv_highgui.so.2.4 libopencv_highgui.so.2.4.bakroot@sibo-All-Series:/usr/local/MATLAB/R2015b/bin/glnxa64# mv libopencv_core.so.2.4 libopencv_core.so.2.4.bakroot@sibo-All-Series:/usr/local/MATLAB/R2015b/bin/glnxa64# ln -s /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9 libopencv_core.so.2.4root@sibo-All-Series:/usr/local/MATLAB/R2015b/bin/glnxa64# ln -s /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9 libopencv_highgui.so.2.4root@sibo-All-Series:/usr/local/MATLAB/R2015b/bin/glnxa64# ln -s /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.9 libopencv_imgproc.so.2.4

设置路径

  • 对于Python

    vim /home/sibo/.bashrc
    # caffe
    export PYTHONPATH=/usr/local/caffe-master/python${PYTHONPATH:+:${PYTHONPATH}}
    . /home/sibo/.bashrc
  • 对于Matlab
    编写程序前将添加路径
    addpath /usr/local/caffe-master/matlab
0 0
原创粉丝点击