Ubuntu 18.04源码安装caffe步骤

虽然Ubuntu 18.04可以通过apt安装caffe,但是为了使用最新的代码,还是值得从源码安装一遍的。

安装环境

OS: Ubuntu 18.04 64 bit显卡: NVidia GTX 1080CUDA: 10.0cuDNN: 7.4Python: 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_GRAYSCALECV_LOAD_IMAGE_COLOR     -> cv::IMREAD_COLOR

替换命令如下:

sed -i 's/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g' src/caffe/layers/window_data_layer.cppsed -i 's/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g' src/caffe/util/io.cppsed -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.6mPYTHON_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/opencv4LIBRARY_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




    延伸阅读