1. 程式人生 > >Ubuntu18.04下安裝配置Caffe-SSD-GPU版本

Ubuntu18.04下安裝配置Caffe-SSD-GPU版本

  • 系統版本:Ubuntu18.04
  • 系統環境:OpenCV3.4.1,Python2

1. 安裝依賴項

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

2. Clone Caffe-SSD原始碼

git clone https://github.com/weiliu89/caffe.git caffe-ssd
cd caffe
git checkout ssd

這裡需出現分支成功的提示方可繼續:

Branch ssd set up to track remote branch ssd from origin.
Switched to a new branch ‘ssd’

3. 檔案配置

cd caffe-ssd
cp Makefile.config.example Makefile.config
  • 修改Makefile.config檔案
1.若使用Cudnn
  將# USE_CUDNN := 1 前的註釋取消
  即:USE_CUDNN := 1

2.若使用opencv3.x
  取消 # OPENCV_VERSION := 3 前的註釋
  即:OPENCV_VERSION := 3
 
3.若是用Python程式設計
  取消 # WITH_PYTHON_LAYER := 1 前的註釋
  即:WITH_PYTHON_LAYER := 1 
 
4
.加入hdf5的目錄: 將 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 修改為: INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial 5. 如果使用的是Cuda>8.0,則將 CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ -gencode arch=compute_20,code=sm_21 \ -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_61,code=sm_61 修改為: 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_61,code=sm_61
  • 修改Makefile檔案
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
 
修改為:
 
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
 
修改為:
 
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += boost_thread stdc++後加boost_regex
 
修改為:
 
LIBRARIES += boost_thread stdc++ boost_regex

4. 編譯Caffe-SSD

make -j16
make py
make runtest -j16
make test -j16

編譯過程可能會出現如下錯誤:

  • 錯誤1:Caffe-ssd編譯錯誤.
Makefile:570: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc3' failed
  • 錯誤2:
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail_106501::cpp_regex_traits_implementation<char>::transform(char const*, char const*) const'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail_106501::cpp_regex_traits_implementation<char>::transform_primary(char const*, char const*) const'
collect2: error: ld returned 1 exit status
······

5. 配置環境變數

首先,開啟配置檔案

vim ~/.bashrc

在檔案的最後一行新增以下路徑

export PYTHONPATH=~/caffe-ssd/python:$PYTHONPATH

讓配置檔案生效

source ~/.bashrc

6. 測試

[email protected]:~$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>

如果不報錯,則配置成功!

在import caffe的時候可以會遇到以下問題:

  • 錯誤1:ImportError: No module named skimage.io. 詳細如下:
>>> import caffe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/liguangyao/Programming/caffe-ssd/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
  File "/home/liguangyao/Programming/caffe-ssd/python/caffe/pycaffe.py", line 15, in <module>
    import caffe.io
  File "/home/liguangyao/Programming/caffe-ssd/python/caffe/io.py", line 2, in <module>
    import skimage.io
ImportError: No module named skimage.io
  • 錯誤2: ImportError: No module named google.protobuf.internal. 詳細如下:
>>> import caffe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/liguangyao/Programming/caffe-ssd/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
  File "/home/liguangyao/Programming/caffe-ssd/python/caffe/pycaffe.py", line 15, in <module>
    import caffe.io
  File "/home/liguangyao/Programming/caffe-ssd/python/caffe/io.py", line 8, in <module>
    from caffe.proto import caffe_pb2
  File "/home/liguangyao/Programming/caffe-ssd/python/caffe/proto/caffe_pb2.py", line 6, in <module>
    from google.protobuf.internal import enum_type_wrapper
ImportError: No module named google.protobuf.internal