准备Caffe环境


【如果是其他环境下的配置就请绕道喽,我也没有专门去试一试各个环境下的配置,请谅解~】




官网 http://caffe.berkeleyvision.org/installation.html;



首先在这里介绍一下我的硬件环境:


Ubuntu 14.04 ---------[ win10远程连接ssh(putty)+VNC ]:


gcc 4.8.4   || cuda 7.5.18  ||  




1、安装ssh、vnc;


win10 :  putty   、  VNC


Unbuntu : sudo apt-get install ssh


 


cuda7.5安装


  1. cuda7.5下载:地址 https://developer.nvidia.com/cuda-downloads 
    文件: cuda_7.5.18_linux.run
  2. 登录界面前按Ctrl+Alt+F1进入命令提示符 【禁用nouveau驱动】
  3. 执行命令: sudo vi /etc/modprobe.d/blacklist-nouveau.conf
  4. 输入以下内容
blacklist nouveau
options nouveau modset=0

最后保存退出(:wq)

  1. 执行命令: sudo update-initramfs -u 
    再执行命令: lspci | grep nouveau 查看是否有内容 
    如果没有内容 ,说明禁用成功,如果有内容,就重启一下再查看 
    sudo reboot 
    重启后,进入登录界面的时候,不要登录进入桌面,直接按Ctrl+Alt+F1进入命令提示符。
  2. 重启后,登录界面时直接按Ctrl+Alt+F1进入命令提示符
  3. 安装依赖项: 
    sudo service lightdm stop 
    sudo apt-get install g++ 
    sudo apt-get install Git 
    sudo apt-get install freeglut3-dev
  4. 假设cuda_7.5.18_linux.run位于 ~ 目录,切换到~目录: cd ~
  5. 执行命令: sudo sh cuda_7.5.18_linux.run
  6. 安装的时候,要让你先看一堆文字(EULA),我们直接不停的按空格键到100%,然后输入一堆accept,yes,yes或回车进行安装。安装完成后,重启,然后用ls查看一下: 
    ls /dev/nvidia* 
    会看到/dev目录下生成多个nvidia开头文件(夹) 
    或者输入命令: sudo nvcc –version 会显示类似以下信息
dl@dl-Z170X-Gaming-3:~$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2015 NVIDIA Corporation
Built on Tue_Aug_11_14:27:32_CDT_2015
Cuda compilation tools, release 7.5, V7.5.17

    1. 配置环境变量 
      执行命令: sudo vi /etc/profile 
      文件底部添加以下内容:
    export PATH=/usr/local/cuda-7.5/bin:$PATH
    export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH

      1. 编译samples 
        安装成功后在~目录下可以看到一个NVIDIA_CUDA-7.5_Samples文件夹,切换到目录 
        输入sudo make, 大概等个十多分钟后就会把全部的samples编译完毕。生成的可执行文件位于 
        NVIDIA_CUDA-7.5_Samples/bin/x86_64/Linux/release 目录下 
        比如运行 ./nbody可以看到以下demo 

      cuda安装过程中遇到的问题

      Q1

      1. 在执行命令: sudo apt-get install g++ 时出现以下错误 
        g++ : Depends: g++-4.8 (>= 4.8.2-5~) but it is not going to be installed
      2. 是因为ubuntu 14.04的源过旧或不可访问导致,可以通过更新源解决。
      3. 首先,备份原始源文件source.list 
        sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup
      4. 然后 
        sudo gedit /etc/apt/source.list 
        在文件尾部添加以下内容
      deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
      deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
      deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
      deb http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
      deb http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
      deb-src http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
      deb-src http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
      deb-src http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
      deb-src http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
      deb-src http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multivers
      1. 最后 sudo apt-get update

      Q2

      W: GPG 错误:http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY 8D5A09DC9B929006
      W: 仓库 “http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease” 没有数字签名。
      N: 无法认证来自该源的数据,所以使用它会带来潜在风险。
      N: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。
      W: 以下 ID 的密钥没有可用的公钥:
      8D5A09DC9B929006

      solution:

      sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8D5A09DC9B929006

      注意最后的一串密钥就是报错信息里的, 每个人的不一样

      Q3

      1080Ti显卡安装完CUDA之后要安装显卡驱动,否则提示找不到显卡设备

      sudo service lightdm stop  
      sudo chmod 777 NVIDIA-Linux-x86_64-378.13.run   
      sudo ./NVIDIA-Linux-x86_64-367.27.run  
      sudo service lightdm start

      安装caffe

      1. 下载caffe:执行命令: sudo git clone https://github.com/BVLC/caffe.git
      2. 安装依赖项:
        sudo apt-get install libatlas-base-dev 
        sudo apt-get install libprotobuf-dev 
        sudo apt-get install libleveldb-dev 
        sudo apt-get install libsnappy-dev 
        sudo apt-get install libopencv-dev 
        sudo apt-get install libboost-all-dev 
        sudo apt-get install libhdf5-serial-dev 
        sudo apt-get install libgflags-dev 
        sudo apt-get install libgoogle-glog-dev 
        sudo apt-get install liblmdb-dev 
        sudo apt-get install protobuf-compiler
      3. 编译caffe 
        cd ~/caffe 
        sudo cp Makefile.config.example Makefile.config 
        make all
      4. 配置运行环境 
        sudo vi /etc/ld.so.conf.d/caffe.conf 
        添加内容: 
        /usr/local/cuda/lib64
      5. 更新配置 
        sudo ldconfig
      6. caffe测试,执行以下命令: 
        cd ~/caffe 
        sudo sh data/mnist/get_mnist.sh 
        sudo sh examples/mnist/create_mnist.sh 
        最后测试: 
        sudo sh examples/mnist/train_lenet.sh

      运行结果如下: 

      深度学习21天实战caffe学习笔记《3 :准备Caffe环境》_hive

      其他依赖项

      我们查看caffe目录下 Makefile.config 内容如下:

      ## 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
      
      # uncomment to disable IO dependencies and corresponding data layers
      # USE_OPENCV := 0
      # USE_LEVELDB := 0
      # USE_LMDB := 0
      
      # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
      #   You should not set this flag if you will be reading LMDBs with any
      #   possibility of simultaneous read and write
      # ALLOW_LMDB_NOLOCK := 1
      
      # Uncomment if you're using OpenCV 3
       OPENCV_VERSION := 3
      
      # 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 := mkl
      # 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 \
      
      # Uncomment to use Python 3 (default is Python 2)
      # PYTHON_LIBRARIES := boost_python3 python3.5m
      # PYTHON_INCLUDE := /usr/include/python3.5m \
      #                 /usr/lib/python3.5/dist-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 ?= @

      可以看到诸如

      # cuDNN acceleration switch (uncomment to build with cuDNN).
      # USE_CUDNN := 1
      
      # Uncomment if you're using OpenCV 3
      # OPENCV_VERSION := 3
      
      # BLAS choice:
      # atlas for ATLAS (default)
      # mkl for MKL
      # open for OpenBlas
      BLAS := atlas

      都是使用默认的设置,我们可以安装其他依赖项提高caffe运行效率

      opencv3.0安装

      1. github上有人写好完整的运行脚本自动下载OpenCV,编译,安装,配置等
      2. Caffe + Ubuntu 15.04 + CUDA 7.5 新手安装配置指南 作者 Xin-Yu Ou(欧新宇) 可以到他的网盘中下载 
        PS:为了方便大家使用,我提供一个百度云盘,用于分享部分安装过程中需要用到的软件包和链接地址(所有软件包仅供学术交流使用,请大家尽量去官网下载。)。百度云盘链接:http://pan.baidu.com/s/1qX1uFHa 密码:wysa
      3. 在Install-opencv-master文件夹中包含安装各个版本opencv脚本
      4. 切换到目录执行: 
        sudo sh Ubuntu/dependencies.sh 
        安装依赖项
      5. 执行opencv3.0安装脚本 
        sudo sh Ubuntu/3.0/opencv3_0_0.sh 
        等待安装完成即可
      6. 修改Makefile.config
      # Uncomment if you're using OpenCV 3
      
      OPENCV_VERSION := 3
      1. (可选)opencv3.1已经发布,如果要安装最新的opencv3.1,我们可以先执行 
        sudo sh get_latest_version_download_file.sh 
        获取最新的地址,然后更新opencv3_0_0.sh中的下载地址,同时需要修正文件名等
      arch=$(uname -m)
      if [ "$arch" == "i686" -o "$arch" == "i386" -o "$arch" == "i486" -o "$arch" == "i586" ]; then
      flag=1
      else
      flag=0
      fi
      echo "Installing OpenCV 3.0.0"
      mkdir OpenCV
      cd OpenCV
      echo "Removing any pre-installed ffmpeg and x264"
      sudo apt-get -y remove ffmpeg x264 libx264-dev
      echo "Installing Dependenices"
      sudo apt-get -y install libopencv-dev
      sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
      sudo apt-get -y install libtiff4-dev libjpeg-dev libjasper-dev
      sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
      sudo apt-get -y install python-dev python-numpy
      sudo apt-get -y install libtbb-dev
      sudo apt-get -y install libqt4-dev libgtk2.0-dev
      sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev
      sudo apt-get -y install x264 v4l-utils ffmpeg
      sudo apt-get -y install libgtk2.0-dev
      echo "Downloading OpenCV 3.0.0"
      wget -O opencv-3.0.0.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.0.0/opencv-3.0.0.zip/download
      echo "Installing OpenCV 3.0.0"
      unzip opencv-3.0.0.zip
      cd opencv-3.0.0
      mkdir build
      cd build
      cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
      make -j8
      sudo make install
      sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
      sudo ldconfig
      echo "OpenCV 3.0.0 ready to be used"

      安装opencv3遇到的问题

      1. 在执行 
        sudo sh Ubuntu/3.0/opencv3_0_0.sh 
        出现有个地方一直卡住了,显示在下载一个文件: ippicv_linux_20141027.tgz 
        因为墙的原因,这个文件无法下载下来
      2. [其他文档] ippicv_linux_20141027.tgz 处下载文件 ippicv_linux_20141027.tgz
      3. 下载后拷贝到opencv/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b/ 目录下即
      4. http://stackoverflow.com/questions/25726768/opencv-3-0-trouble-with-installation

      安装BLAS——选择MKL

      1. 首先下载 MKL(Intel(R) Parallel Studio XE Cluster Edition for Linux 2016) 
        网址: https://software.intel.com/en-us/intel-education-offerings 
        Caffe + Ubuntu 15.04 + CUDA 7.5 新手安装配置指南 作者 Xin-Yu Ou(欧新宇) 可以到他的网盘中下载, 需要自己申请序列号
      2. 下载完成后: parallel_studio_xe_2016.tgz
      3. 执行以下命令: 
        $ tar zxvf parallel_studio_xe_2016.tar.gz
        $ chmod a+x parallel_studio_xe_2016 -R
        $ sh install_GUI.sh
      4. 环境配置: 
        $ sudo gedit /etc/ld.so.conf.d/intel_mkl.conf 
        然后添加以下内容
      /opt/intel/lib/intel64
      /opt/intel/mkl/lib/intel64

      配置生效: sudo ldconfig -v 
      安装MKL完成

      1. 修改Makefile.config
      # BLAS choice:
          # atlas for ATLAS (default)
          # mkl for MKL
          # open for OpenBlas
          BLAS := mkl

      cuDNN安装

      1. cudnn下载 
        下载地址:https://developer.nvidia.com/cudnn 
        或者到网盘: http://pan.baidu.com/s/1bnOKBO 下载 
        下载相应文件cudnn-7.0-linux-x64-v4.0-rc.tgz, 放到~根目录下
      2. 切换到~目录,执行命令
      sudo tar xvf cudnn-7.0-linux-x64-v4.0-rc.tgz
      cd cuda/include
      sudo cp *.h /usr/local/include/
      cd ../lib64
      sudo cp lib* /usr/local/lib/
      cd /usr/local/lib
      sudo chmod +r libcudnn.so.4.0.4
      sudo ln -sf libcudnn.so.4.0.4 libcudnn.so.4
      sudo ln -sf libcudnn.so.4 libcudnn.so
      sudo ldconfig

        1. 修改Makefile.config
        # cuDNN acceleration switch (uncomment to build with cuDNN).
        USE_CUDNN := 1

        cudnn版本问题

        在make工程的时候出现以下错误:

        ...
        
        NVCC src/caffe/layers/deconv_layer.cu
        NVCC src/caffe/layers/cudnn_conv_layer.cu
        src/caffe/layers/cudnn_conv_layer.cu(81): error: argument of type "cudnnAddMode_t" is incompatible with parameter of type "const void *"
                  detected during instantiation of "void caffe::CuDNNConvolutionLayer<Dtype>::Forward_gpu(const std::vector<caffe::Blob<Dtype> *, std::allocator<caffe::Blob<Dtype> *>> &, const std::vector<caffe::Blob<Dtype> *, std::allocator<caffe::Blob<Dtype> *>> &) [with Dtype=float]" 
        (157): here
        
        ...
        
        20 errors detected in the compilation of "/tmp/tmpxft_00002703_00000000-16_cudnn_conv_layer.compute_50.cpp1.ii".
        make: *** [.build_release/cuda/src/caffe/layers/cudnn_conv_layer.o] Error 1
        make: *** Waiting for unfinished jobs....
        解决方案:

        更换V3版本cudnn Caffe 工程的一些编译错误以及解决方案

        $ cd lib64/
        $ sudo cp lib* /usr/local/cuda/lib64/
        $ cd ../include/
        $ sudo cp cudnn.h /usr/local/cuda/include/
        $ cd /usr/local/cuda/lib64/
        $ sudo rm -r libcudnn.so libcudnn.so.7.0
        $ sudo ln -sf libcudnn.so.7.0.64 libcudnn.so.7.0
        $ sudo ln -sf libcudnn.so.7.0 libcudnn.so
        $ sudo ldconfig

        重新编译测试caffe

        1. 编译
          sudo make clean 
          sudo make all
        2. sample测试: ( 比不使用cudnn快很多) 
          sh data/mnist/get_mnist.sh 
          sh examples/mnist/create_mnist.sh
        3. 我们可以将迭代次数增加到50000次 
          sudo gedit examples/mnist/lenet_solver.prototxt 
          修改max_iter: 50000 
          最后: 
          sh examples/mnist/train_lenet.sh 

        编译Python接口

        依赖项
        sudo apt-get install -y python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags cython ipython

        sudo vi ~/.bashrc
        添加: export PYTHONPATH=/home/dl/caffe/python:$PYTHONPATH
        sudo ldconfig
        sudo make pycaffe -j8


        编译matlab接口

        1. 安装matlab2014 
          sh /usr/local/MATLAB/R2014a/bin/matlab
        2. Makefile.config 中修改 : MATLAB_DIR := /usr/local/MATLAB/R2014a
        3. sudo make matcaffe -j8

        其他

        1. Vi编辑命令 常用vi编辑器命令行
        A:当前行的尾部追加内容
        
        i:游标前插入内容
        
        I:游标后插入内容
        
        o:在鼠标所在行的下面添加内容
        
        O:在鼠标所在行的上面添加内容
        
        ESC:退出编辑模式
        
        Ctrl-T:移动到下一个tab
        
        Backspace:向后移动一个字符
        
        Ctrl-U:删除当前
        
        cw:删除游标所在的字符,然后进入编辑模式
        
        cc:删除游标所在的行,然后进入编辑模式
        
        C:删除从游标所在的位置到行尾的字符,然后进入编辑模式
        
        dd:删除当前行
        
        ndd:删除第n行
        
        D:删除当前行游标所在的位置后面的字符
        
        dw:删除邮编所在的字符
        
        d}:删除当前段剩余的字符
        
        d^:删除游标前到行首的字符
        
        c/pat:删除游标后面到第一次匹配字符间的内容
        
        dn:删除游标后面到下一个匹配字符间的内容
        
        dfa:删除当前行游标到匹配字符间的内容(匹配的字符也将被删)
        
        dta:删除当前行游标到匹配字符间的内容(匹配的字符不被删)
        
        dL:删除从游标到屏幕的最后一行之间的内容
        
        dG:删除从游标到文件末尾之间的内容
        J:连结上下两行的内容
        
        p:在游标后面插入buffer中的内容
        
        P:在游标前面插入buffer中的内容
        
        rx:用x替换字符
        
        Rtext:用text从游标开始处进行替换
        
        u:撤销最后的改变
        
        U:还原当前行的内容
        
        x:向后删除游标所在位置的字符
        
        X:向前删除游标前面的字符
        
        nX:删除前面的n个字符,游标所在的字符将不会被删
        
        .:还原最后的改变
        
        ~:反转字母的大小写
        
        y:拷贝当前行到新的buffer
        
        yy:拷贝当前行
        
        "xyy:拷贝当前行的buffer名为x的buffer
        
        ye:拷贝当单词的末尾
        1. 搜狗输入法安装 
          Ubuntu14.04安装搜狗输入法
        2. im-config 然后 ibus选取fcitx
        3. fcitx-config-gtk3

        参考资料

        1. Caffe学习系列(1):安装配置ubuntu14.04+cuda7.5+caffe+cudnn
        2. Caffe + Ubuntu 15.04 + CUDA 7.5 新手安装配置指南
        3. ubuntu 14.04 install g++问题"g++:Depends:g++-4.8(>= 4.8.2-5
        4. ippicv_linux_20141027.tgz
        5. http://stackoverflow.com/questions/25726768/opencv-3-0-trouble-with-installation