docker--cuda10.1-ubuntu18.04-opencv4.0-caffe

文章目录

        • 1 cuda镜像
        • 2 ubuntu18.04基础环境
        • 3 安装python3.6
        • 4 opencv4.0
        • 5 caffe

1 cuda镜像

# 查找docker源中的cuda镜像
docker search nvida/cuda # 此镜像默认是cuda10.0,os是ubuntu16.04
# 查询docker hub网站链接到https://gitlab.com/nvidia/cuda/blob/ubuntu18.04/10.1/devel/cudnn7/Dockerfile到dockerfile内容,发现可以定制docker进行内容,因此下载如下docker镜像
docker pull nvidia/cuda:10.1-cudnn7-devel-ubuntu18.04

2 ubuntu18.04基础环境

apt update
apt install vim -y

# ubuntu换国内源
vim /etc/apt/sources.list
# 文件头新增清华源:
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

apt update
apt upgrade
apt install wget git cmake -y

3 安装python3.6

apt install python3 -y
apt install python3-pip -y
pip3 install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

4 opencv4.0

# 安装依赖包
apt install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev -y

pip install numpy

# 下载opencv 4.0
https://opencv.org/releases.html

# 源码编译安装
cd opencv-4.0.1
mkdir build
cd build
cmake ..
make all -j8
make install

问题:

  • – Could not find OpenBLAS lib. Turning OpenBLAS_FOUND

    apt install libopenblas-dev libopenblas-base liblapacke-dev -y
    
    # 软连接正确的库路径
    ln -s /usr/include/lapacke.h /usr/include/x86_64-linux-gnu 
    
  • 无法编译python3

    解决方式如下:

    cmake -DBUILD_LIBPROTOBUF_FROM_SOURCES=ON\
    	-D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=OFF \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D BUILD_EXAMPLES=ON \
      -D PYTHON3_EXECUTABLE=$(which python3) \
      -D PYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
      -D PYTHON_INCLUDE_DIR2=$(python3 -c "from os.path import dirname; from distutils.sysconfig import get_config_h_filename; print(dirname(get_config_h_filename()))") \
      -D PYTHON_LIBRARY=$(python3 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))") \
      -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print(numpy.get_include())") \
      -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
      -D WITH_QT=OFF \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_EIGEN=ON \
      -D CUDA_GENERATION=Kepler ..
    
  • pyopencv_generated_include.h’ failed

    ./opencv 目录下运行

    python3 ./modules/python/src2/gen2.py ./build/modules/python_bindings_generator ./build/modules/python_bindings_generator/headers.txt
    

5 caffe

  1. 源码安装

    # 安装支持库
    apt install doxygen libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev -y
    apt install --no-install-recommends libboost-all-dev -y
    
    # 下载编译nccl
    git clone https://github.com/NVIDIA/nccl.git
    cd nccl
    make install -j8
    
    # 升级默认的cmake 3.10.2到最新版3.14
    wget https://cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.tar.gz
    mv cmake-3.14.0-Linux-x86_64 /opt/cmake
    ln -sf /opt/cmake/bin/*  /usr/bin/ 
    
    # 下载caffe
    git clone https://github.com/BVLC/caffe.git
    cd caffe/
    cp Makefile.config.example Makefile.config
    
    # 预编译
    mkdir build
    cd build
    camke ..
    
    # 安装测试
    make all -j8
    make runtest -j8
    
    # 安装pycaffe
    make pycaffe -j8
    
  2. 配置

    • 使用opencv4

      ./include/caffe/common.hpp第70行后新增如下内容:

      // Supporting OpenCV4
      #if (CV_MAJOR_VERSION == 4)
      #define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR
      #define CV_LOAD_IMAGE_GRAYSCALE cv::IMREAD_GRAYSCALE
      #endif
      
    • 配置./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 := 1
      # USE_LEVELDB := 0
      # USE_LMDB := 0
      # This code is taken from https://github.com/sh1r0/caffe-android-lib
      # USE_HDF5 := 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 := 4
      
      # 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-10.1
      # 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 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_75,code=sm_75 \
                      -gencode arch=compute_75,code=compute_75
      # BLAS choice:
      # atlas for ATLAS (default)
      # mkl for MKL
      # open for OpenBlas
      BLAS := open
      # 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.6m
      PYTHON_INCLUDE := /usr/include/python3.6m \
                       /usr/lib/python3.6/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
      
      # NCCL acceleration switch (uncomment to build with NCCL)
      # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
      USE_NCCL := 1
      
      # 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
      
      # N.B. both build and distribute dirs are cleared on `make clean`
      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 ?= @
      
    • 配置./caffe/Makefile

      # 1 将
      LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
      # 修改为:
      LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial  
      
      # 2 在第331行添加
      # NCCL acceleration configuration
      ifeq ($(USE_NCCL), 1)
          LIBRARIES += nccl
          COMMON_FLAGS += -DUSE_NCCL
      endif
      
    • 配置./caffe/build/CMakeCache.txt

      //Path to a program.
      PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3.6
      
      //Path to a file.
      PYTHON_INCLUDE_DIR:PATH=/usr/include/python3.6
      
      //Path to a library.
      PYTHON_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
      
      
      //Boost python library (debug)
      Boost_PYTHON_LIBRARY_DEBUG:FILEPATH=/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so
      
      //Boost python library (release)
      Boost_PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so
      
      
      //Build Caffe with NCCL library support
      USE_NCCL:BOOL=ON
      
  3. 安装python依赖

    pip install scipy scikit-image pandas matplotlib protobuf
    
  4. 问题

  • CUDA_cublas_device_LIBRARY (ADVANCED)

    升级默认的cmake 3.10.2到最新版3.14解决CUDA_cublas_device_LIBRARY问题

    wget https://cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.tar.gz

    mv cmake-3.14.0-Linux-x86_64 /opt/cmake
    ln -sf /opt/cmake/bin/*  /usr/bin/ 
    

    然后执行命令检查一下:

    cmake --version
    
  • 出现nvcc fatal : Unsupported gpu architecture ‘compute_20

    cd ./caffe/cmake
    vim Cuda.cmake 
    
    # 移除第7行的20和21
    set(Caffe_known_gpu_archs "30 35 50 52 60 61 75")
    
  • /usr/lib/gcc/x86_64-linux-gnu/7/…/…/…/x86_64-linux-gnu/libboost_python.so: undefined reference to `PyClass_Type’

    …….

    tools/CMakeFiles/caffe.bin.dir/build.make:127: recipe for target ‘tools/caffe’ failed

    # 修改CMakeCache.txt中boost库
    
    //Boost python library (debug)
    Boost_PYTHON_LIBRARY_DEBUG:FILEPATH=/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so
    
    //Boost python library (release)
    Boost_PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so
    
  • – Could NOT find GFlags (missing: GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)

    – Could NOT find Glog (missing: GLOG_INCLUDE_DIR GLOG_LIBRARY)

    –Could NOT find LMDB (missing: LMDB_INCLUDE_DIR LMDB_LIBRARIES)

    apt install libgflags-dev libgoogle-glog-dev liblmdb-dev -y
    
  • src/caffe/layers/hdf5_output_layer.cu:4:10: fatal error: hdf5.h: No such file or directory

    # ./caffe/Makefile中
    LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
    # 修改为:
    LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial  
    
  • F0325 11:10:19.416651 9160 caffe.cpp:245] Multi-GPU execution not available - rebuild with USE_NCCL

    NCC is used for multi gpu communication. You have to enable USE_NCCL := 1 in makefile.config. Then rebuild. – lnman Apr 9 '17 at 13:05

    # 1 下载编译nccl
    git clone https://github.com/NVIDIA/nccl.git
    cd nccl
    make CUDA_HOME=/usr/local/cuda-10.1/
    make install -j8
    ldconfig
    
    # 2 在Makefile.config文件中
    USE_NCCL := 1
    
    # 3 在Makefile中第331行添加
    # NCCL acceleration configuration
    ifeq ($(USE_NCCL), 1)
        LIBRARIES += nccl
        COMMON_FLAGS += -DUSE_NCCL
    endif
    
    # 4 修改CMakeCache.txt
    //Build Caffe with NCCL library support
    USE_NCCL:BOOL=ON
    
    
    # 5 重新编译caffe
    sudo make clean
    sudo make all -j
    

你可能感兴趣的:(AI,机器学习)