首頁 > 軟體

Ubuntu 18.04原始碼安裝caffe步驟

2020-06-16 16:36:53

雖然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

IT145.com E-mail:sddin#qq.com