红联Linux门户
Linux协助

Ubuntu 18.04源码装置caffe过程

发布时刻:2019-05-23 00:00:00来历:不知道作者:admin

尽管Ubuntu 18.04能够经过apt装置caffe,可是为了运用最新的代码,仍是值得从源码装置一遍的。

装置环境

  • OS: Ubuntu 18.04 64 bit
  • 显卡: NVidia GTX 1080
  • CUDA: 10.0
  • cuDNN: 7.4
  • Python: 3.6

使命:从源码编译装置caffe

装置OpenCV

参阅Ubuntu 18.04源码编译装置OpenCV 4.0过程,装置OpenCV。

装置依靠

$ apt install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
$ apt install -y --no-install-recommends libboost-all-dev
$ apt install -y libatlas-base-dev
$ apt install -y libgflags-dev libgoogle-glog-dev liblmdb-dev

下载源码

$ git clone https://github.com/BVLC/caffe.git
$ cd caffe
$ cp Makefile.config.example Makefile.config

更新代码

由于运用OpenCV 4.0的版别,所以有些常量需求更新:

CV_LOAD_IMAGE_GRAYSCALE -> cv::ImreadModes::IMREAD_GRAYSCALE
CV_LOAD_IMAGE_COLOR     -> cv::IMREAD_COLOR

替换指令如下:

sed -i 's/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g' src/caffe/layers/window_data_layer.cpp
sed -i 's/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g' src/caffe/util/io.cpp
sed -i 's/CV_LOAD_IMAGE_GRAYSCALE/cv::ImreadModes::IMREAD_GRAYSCALE/g' src/caffe/util/io.cpp

修正装备

为了运用OpenCV 4.0 + CUDA 10 + cuDNN + python 3.6,修正Makefile.config如下:

...
USE_CUDNN := 1
...
OPENCV_VERSION := 3
...
CUDA_DIR := /usr/local/cuda-10.0
...
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
...
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := /usr/include/python3.6m \
                /usr/local/lib/python3.6/dist-packages/numpy/core/include
...
WITH_PYTHON_LAYER := 1
...
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /usr/local/cuda/include /usr/local/include/opencv4
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

编译caffe

$ make -j8
$ make pycaffe

一切顺利的话,生成的东西在build/tools目录下。

错误处理

假如报错如下:

/usr/local/lib/libopencv_imgcodecs.so: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0'

处理方案:

$ conda uninstall libtiff