Ubuntu环境准备

GPU环境CUDA和CuDNN安装 (CPU版本忽略)

参考安装教程

依赖库安装:

  1. sudo apt-get install build-essential
  2. sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  3. sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Python2.7 or python3.x:

  1. sudo apt-get install python3

numpy:

  1. sudo apt-get install python-numpy

protobuf:

  1. sudo apt-get install python-protobuf

gflags:

  1. sudo apt-get install libgflags-dev

也可以自己编译https://github.com/gflags/gflags/releases:

  1. cd gflags-2.2.2
  2. mkdir build
  3. cd build
  4. cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_SHARED_LIBS=ON -DGFLAGS_NAMESPACE=google -G"Unix Makefiles" ../
  5. make
  6. sudo make install
  7. sudo ldconfig

安装opencv:

默认版本安装:

  1. sudo apt-get install python-opencv

需要安装制定版本需要自己编译:https://opencv.org/releases.html

  1. cd opencv-3.4.3
  2. mkdir release
  3. cd release
  4. cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
  5. make
  6. sudo make install

python中测试是否安装成功:

  1. import cv2
  2. import numpy

开始安装

下载caffe与配置

先clone

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

进入clone的caffe文件夹,执行以下命令,把Makefile.config.example复制更名为Makefile.config

  1. sudo cp Makefile.config.example Makefile.config

修改此文件(很关键)相当于caffe编译配置文件:

  1. sudo vim Makefile.config

有GPU支持则使用cudnn:

  1. #USE_CUDNN := 1
  2. 修改成:
  3. USE_CUDNN := 1
  4. 只有CPU可以只开
  5. CPU_ONLY := 1

修改python路径:

  1. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
  2. LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
  3. 修改为:
  4. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
  5. LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

修改opencv版本(如果使用opencv3的话):

  1. #OPENCV_VERSION := 3
  2. 修改为:
  3. OPENCV_VERSION := 3

编译

  1. make all

一些其他库可能需要proto协议:

  1. protoc src/caffe/proto/caffe.proto --cpp_out=.
  2. mkdir include/caffe/proto
  3. mv src/caffe/proto/caffe.pb.h include/caffe/proto

相关错误

nvcc fatal : Unsupported gpu architecture ‘compute_20’

问题在于CUDA在CUDA architecture setting有版本兼容问题,修改Makefile.config:

  1. CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
  2. -gencode arch=compute_20,code=sm_21 \
  3. -gencode arch=compute_30,code=sm_30 \
  4. -gencode arch=compute_35,code=sm_35 \
  5. -gencode arch=compute_50,code=sm_50 \
  6. -gencode arch=compute_52,code=sm_52 \
  7. -gencode arch=compute_60,code=sm_60 \
  8. -gencode arch=compute_61,code=sm_61 \
  9. -gencode arch=compute_61,code=compute_61

改为:

  1. CUDA_ARCH := #-gencode arch=compute_20,code=sm_20 \
  2. #-gencode arch=compute_20,code=sm_21 \
  3. -gencode arch=compute_30,code=sm_30 \
  4. -gencode arch=compute_35,code=sm_35 \
  5. -gencode arch=compute_50,code=sm_50 \
  6. -gencode arch=compute_52,code=sm_52 \
  7. -gencode arch=compute_60,code=sm_60 \
  8. -gencode arch=compute_61,code=sm_61 \
  9. -gencode arch=compute_61,code=compute_61

/usr/bin/ld: 找不到 -lcudnn

进入cudnn解压得到的cudn文件夹lib64文件夹内:

  1. sudo cp libcudnn.so /usr/local/cuda/lib64/
  2. sudo cp libcudnn.so.7 /usr/local/cuda/lib64/
  3. sudo cp libcudnn.so.7.4.1 /usr/local/cuda/lib64/
  4. sudo cp libcudnn_static.a /usr/local/cuda/lib64/

tools/convert_imageset.cpp:56:3: error: ‘gflags’ has not been declared

由于gflags2.1之后将命名空间由google改为了gflags,所以这里暂时可以这样解决

  • include/caffe/common.hpp
  • examples/mnist/convert_mnist_data.cpp

在以上文件中注释掉#ifndef、#endif

  1. // #ifndef GFLAGS_GFLAGS_H_
  2. namespace gflags = google;
  3. // #endif // GFLAGS_GFLAGS_H_