• ubuntu16.04_cuda9.0_opencv3.4_cudnn_v7_caffe


    安装显卡驱动

    Ubuntu 16.04 自带 nouveau显卡驱动,这个自带的驱动是不能用于CUDA的,需要卸载重装。假如你已经重装过显卡驱动则可跳过这一步。没有重装过的按照如下步骤:
    首先得禁用Ubuntu自带的显卡驱动nouveau,只有在禁用掉 nouveau 后才能顺利安装 NVIDIA 显卡驱动,禁用方法就是在 /etc/modprobe.d/blacklist-nouveau.conf 文件中添加一条禁用命令,首先需要打开该文件,通过以下命令打开:

    sudo vi/etc/modprobe.d/blacklist-nouveau.conf
    

    打开后发现该文件中没有任何内容,写入:

    blacklist nouveau
    options nouveau modeset=0
    

    保存时命令窗口可能会出现以下提示:

    ** (gedit:4243): WARNING **: Set document metadata failed: 不支持设置属性 metadata::gedit-position
    

    无视此提示,保存后关闭文件,注意此时还需执行以下命令使禁用 nouveau 真正生效:

    sudo update-initramfs -u
    

    然后测试一下,假如输入以下命令什么都没显示那么说明禁用nouveau生效了:

    lsmod | grep nouveau
    

    直接在线安装(此方法需要联网),适用于CUDA 9.0:

    sudo add-apt-repository ppa:graphics-drivers/ppa
    sudo apt-get update
    sudo apt-get install nvidia-384 nvidia-settings nvidia-prime
    

    查看显卡信息:

    nvidia-settings
    

    安装CUDA9.0

    下载CUDA Toolkit

    直接在终端运行安装:

    sudo sh cuda_9.0.176_384.81_linux.run
    

    执行此命令约1分钟后会出现安装协议要你看,刚开始是0%,此时长按回车键让此百分比增长,直到100%,然后按照提示操作即可,先输入 accept ,是否安装显卡驱动选择no:

    Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 387.26?
    (y)es/(n)o/(q)uit: n
    

    其他的都选择yes或者默认,等待安装完成:

    Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 387.26?
    (y)es/(n)o/(q)uit: n
    
    Install the CUDA 9.1 Toolkit?
    (y)es/(n)o/(q)uit: y
    
    Enter Toolkit Location
     [ default is /usr/local/cuda-9.0 ]: 
    
    Do you want to install a symbolic link at /usr/local/cuda?
    (y)es/(n)o/(q)uit: y
    
    Install the CUDA 9.1 Samples?
    (y)es/(n)o/(q)uit: y
    
    Enter CUDA Samples Location
     [ default is /home/ccem ]: 
    
    Installing the CUDA Toolkit in /usr/local/cuda-9.0 ...
    Installing the CUDA Samples in /home/ccem ...
    Copying samples to /home/ccem/NVIDIA_CUDA-9.1_Samples now...
    Finished copying samples.
    
    ===========
    = Summary =
    ===========
    
    Driver:   Not Selected
    Toolkit:  Installed in /usr/local/cuda-9.0
    Samples:  Installed in /home/ccem
    
    Please make sure that
     -   PATH includes /usr/local/cuda-9.0/bin
     -   LD_LIBRARY_PATH includes /usr/local/cuda-9.0/lib64, or, add /usr/local/cuda-9.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-9.0/bin
    
    Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-9.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 384.00 is required for CUDA 9.1 functionality to work.
    To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
     sudo <CudaInstaller>.run -silent -driver
    
    Logfile is /tmp/cuda_install_36731.log
    

    假如出现:

    Installing the CUDA Toolkit in /usr/local/cuda-9.1 … 
    Missing recommended library: libGLU.so 
    Missing recommended library: libX11.so 
    Missing recommended library: libXi.so 
    Missing recommended library: libXmu.so
    

    原因是缺少相关的依赖库,安装相应库就解决了,安装库完成后重装一下CUDA Toolkit即可:

    sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
    

    安装完成后配置CUDA环境变量,使用 gedit 命令打开配置文件:

    sudo gedit ~/.bashrc
    

    在该文件最后加入以下两行并保存:

    export PATH=/usr/local/cuda/bin:$PATH     #/usr/local/cuda和/usr/local/cuda-9.0是同一个文件夹,前者是后者的镜像 
    export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
    

    使该配置生效:

    source ~/.bashrc
    

    检验CUDA 是否安装成功,输入:

    cd /usr/local/cuda-9.0/samples/1_Utilities/deviceQuery
    sudo make
    ./deviceQuery
    

    卸载CUDA 9.0 的方法:

    cd /usr/local/cuda/bin
    sudo ./uninstall_cuda_9.0.pl
    sudo rm -r /usr/local/cuda-9.0
    

    cudnnV7 安装

    下载cudnn

    下载后将后缀命名为 .tar.gz并解压。然后执行:

    sudo cp include/cudnn.h /usr/local/cuda/include/   
    
    sudo cp lib64/lib* /usr/local/cuda/lib64/
    cd /usr/local/cuda/lib64/
    sudo rm -rf libcudnn.so libcudnn.so.7 
    sudo ln -s libcudnn.so.7.6.5 libcudnn.so.7
    sudo ln -s libcudnn.so.7 libcudnn.so
    

    查看cudnn的版本:

    cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
    

    opencv3.4.1 编译安装

    安装依赖项

    sudo apt-get install build-essential
    sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    sudo apt-get install python-dev python-numpy 
    sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev  
    sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
    sudo apt-get install libxvidcore-dev libx264-dev  
    sudo apt-get install libatlas-base-dev gfortran  
    sudo apt-get install ffmpeg
    sudo apt-get install libgtk2.0-dev
    sudo apt-get install pkg-config
    

    下载源码

    git clone https://github.com/opencv/opencv.git
    git clone https://github.com/opencv/opencv_contrib.git
    

    将源码的分支都切换到 3.4.1。
    为了避免第三方依赖的下载,提前手动下载好:
    ippicv_2017u3_lnx_intel64_general_20180518.tgz

    修改源码和相关配置

    修改:3rdparty/ippicv/ippicv.cmake line:47,

    "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
    

    更换为:

    "file:///home/opencv/"  # 更换为ippicv_2017u3_lnx_intel64_general_20180518.tgz的实际目录
    

    在文件modules/stitching/CMakeLists.txt首行加入:

    INCLUDE_DIRECTORIES("/home/work/opencv_contrib/modules/xfeatures2d/include")  # 修改为自己的实际路径
    

    修改文件modules/core/include/opencv2/core/cuda/vec_math.hpp line:203,line:205,

    CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, char, char)
    
    CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, short, short)
    

    在文件 modules/cudev/include/opencv2/cudev/common.hpp中加入:

    #include <cuda_fp16.h>
    

    修改文件OpenCVDetectCUDA.cmake

     ...
      set(__cuda_arch_ptx "")
      if(CUDA_GENERATION STREQUAL "Fermi")
        set(__cuda_arch_bin "2.0")
      elseif(CUDA_GENERATION STREQUAL "Kepler")
        set(__cuda_arch_bin "3.0 3.5 3.7")
      ...
    

    修改为:

      ...
      set(__cuda_arch_ptx "")
      if(CUDA_GENERATION STREQUAL "Kepler")
        set(__cuda_arch_bin "3.0 3.5 3.7")
      elseif(CUDA_GENERATION STREQUAL "Maxwell")
        set(__cuda_arch_bin "5.0 5.2")
      ...
    

    修改文件FindCUDA.cmake
    找到行:find_cuda_helper_libs(nppi) 改为:

      find_cuda_helper_libs(nppial)
      find_cuda_helper_libs(nppicc)
      find_cuda_helper_libs(nppicom)
      find_cuda_helper_libs(nppidei)
      find_cuda_helper_libs(nppif)
      find_cuda_helper_libs(nppig)
      find_cuda_helper_libs(nppim)
      find_cuda_helper_libs(nppist)
      find_cuda_helper_libs(nppisu)
      find_cuda_helper_libs(nppitc)
    

    找到行:set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}") 改为:

    set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")
    

    找到行:unset(CUDA_nppi_LIBRARY CACHE) 改为:

    unset(CUDA_nppial_LIBRARY CACHE)
    unset(CUDA_nppicc_LIBRARY CACHE)
    unset(CUDA_nppicom_LIBRARY CACHE)
    unset(CUDA_nppidei_LIBRARY CACHE)
    unset(CUDA_nppif_LIBRARY CACHE)
    unset(CUDA_nppig_LIBRARY CACHE)
    unset(CUDA_nppim_LIBRARY CACHE)
    unset(CUDA_nppist_LIBRARY CACHE)
    unset(CUDA_nppisu_LIBRARY CACHE)
    unset(CUDA_nppitc_LIBRARY CACHE)
    

    编译安装

    在 opencv 路径下创建 build 文件目录并进入执行:

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/ -D OPENCV_EXTRA_MODULES_PATH=/home/work/opencv_contrib/modules  ..
    

    注意 opencv_contrib 的路径应该为自己的实际路径。
    执行:

    make -j8
    make install
    

    查看opencv信息

    opencv安装版本:

    pkg-config opencv --modversion
    

    opencv安装库:

    pkg-config opencv --libs
    

    安装caffe

    下载源码:

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

    进入caffe目录:

    sudo cp Makefile.config.example Makefile.config
    

    修改 Makefile.config 文件内容:
    应用 cudnn:

    将:
    #USE_CUDNN := 1
    修改为: 
    USE_CUDNN := 1
    

    应用 opencv 3 版本:

    将:
    #OPENCV_VERSION := 3 
    修改为: 
    OPENCV_VERSION := 3
    

    使用 python 接口:

    将:
    #WITH_PYTHON_LAYER := 1 
    修改为 
    WITH_PYTHON_LAYER := 1
    

    修改 python 路径:

    将:
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 
    修改为: 
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial  
    

    去掉compute_20:

    找到
    # CUDA architecture setting: going with all of them.
    # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
    # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
    # For CUDA >= 9.0, comment the *_20 and *_21 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_52,code=sm_52 
                -gencode arch=compute_60,code=sm_60 
                -gencode arch=compute_61,code=sm_61 
                -gencode arch=compute_61,code=compute_61
    
    改为:
    # CUDA architecture setting: going with all of them.
    # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
    # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
    # For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
    CUDA_ARCH := -gencode arch=compute_30,code=sm_30 
                -gencode arch=compute_35,code=sm_35 
                -gencode arch=compute_50,code=sm_50 
                -gencode arch=compute_52,code=sm_52 
                -gencode arch=compute_60,code=sm_60 
                -gencode arch=compute_61,code=sm_61 
                -gencode arch=compute_61,code=compute_61
    

    然后修改 caffe 目录下的 Makefile 文件:

    将:
    NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
    替换为:
    NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
    
    将:
    LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
    改为:
    LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
    

    执行python文件夹下的指令:

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

    编译:

    make all -j8
    

    编译成功后可运行测试:

    make test -j12
    sudo make runtest -j8
    make pycaffe #如果以后用python来开发的话必须执行这一句,一般不管你是否用python,都会执行这一句
    

    Mnist 测试

    下載MNIST数据集:

    sudo sh ./data/mnist/get_mnist.sh
    

    打开下载目录 caffe/data/mnist 查看如下图:

    转换格式:

    ./examples/mnist/create_mnist.sh
    

    完成后在examples/mnist生成了两个目录:mnist_test_lmdbmnist_train_lmdb,每个目录下有两个文件:data.mdblock.mdb
    网络结构定义在./examples/mnist/lenet_train_test.prototxt中。
    训练参数配置在./examples/mnist/lenet_solver.prototxt中。

    训练:

    ./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
    

    最终训练的模型保存在caffe/examples/mnist/lenet_iter_10000.caffemodel文件中,训练状态保存在caffe/examples/mnist/lenet_iter_10000.solverstate文件中

    用训练好的模型对数据进行预测:

    ./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel -iterations=100  -gpu=0
    
  • 相关阅读:
    Delphi代码获取网卡物理地址三种方法 本文来自Delphi之窗,原文地址:http://www.52delphi.com...
    SQL SERVER 中实现公历到农历的转换
    cxgrid相同列合并显示
    rzCheckList.ItemChecked[Index]就可以判断指定节点地状态.
    为什么PING域名得到IP与实际公网IP不符
    如何让sql server2005和sql server2000共存安装在同一机器上
    如何编译通过 Raize 5.3 中的 RzBorder.pas
    u6升级到u890的过程
    技术部门到底该如何管理才能“和谐”
    在CXGRID中如何让字段能以0.00的格式显示
  • 原文地址:https://www.cnblogs.com/xiaojianliu/p/13380517.html
Copyright © 2020-2023  润新知