安裝caffe(CPU版本)的一些參考和問題的解決


1.安裝前的預備工作

  依次按照一下命令安裝:

    

 1 sudo apt-get install libprotobuf-dev 
 2 sudo apt-get install libleveldb-dev 
 3 sudo apt-get install libsnappy-dev 
 4 sudo apt-get install libopencv-dev 
 5 sudo apt-get install libhdf5-serial-dev 
 6 sudo apt-get install protobuf-compiler
 7 sudo apt-get install --no-install-recommends libboost-all-dev
 8 sudo apt-get install libatlas-base-dev
 9 sudo apt-get install python-dev
10 sudo apt-get install libgflags-dev
11 sudo apt-get install libgoogle-glog-dev 
12 sudo apt-get install liblmdb-dev

2.下載caffe源碼

  在github上下載

  

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

 

  接着裝caffe要求裝的一些依賴庫(如requirements.txt【$caffe_root/python】中的內容所示):

Cython>=0.19.2
numpy>=1.7.1
scipy>=0.13.2
scikit-image>=0.9.3
matplotlib>=1.3.1
ipython>=3.0.0
h5py>=2.2.0
leveldb>=0.191
networkx>=1.8.1
nose>=1.3.0
pandas>=0.12.0
python-dateutil>=1.4,<2
protobuf>=2.5.0
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.0
six>=1.1.0

  可以自己一個一個裝,也可以通過以下命令:

1 pip install -r requirements.txt

3.編譯caffe

  3.1 使用Cmake編譯

    進入caffe根目錄,創建一個build文件夾並進入

 

1 mkdir build && cd build

 

      進行cmake

cmake -DCPU_ONLY=1 .. 

      接着make

make -j"$(nproc)"

   3.2 使用make編譯(我使用的這種)

    首先因為我們需要的是CPU版本的caffe,因此需要對Makefile進行修改,我們先創建一個副本

 

1 cp Makefile.config.example Makefile.config

 

      接着對內容進行修改

     我們去掉CPU_ONLY前面的注釋

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

修改為

# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1

 

 

 

 

 

    接着python的文件路徑需要我們修改

 

# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/lib/python2.7/dist-packages/numpy/core/include

修改為

PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/local/lib/python2.7/dist-packages/numpy/core/include

 

      hdf5庫的路徑也需要添加

 

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

修改為

# Whatever else you find you need goes here.
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/hdf5

 

      同時Makefile中的關於hdf5的內容也需要修改

 

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

 

      不然可能會出現以下錯誤

 

/usr/bin/ld: cannot find -lhdf5_hl
/usr/bin/ld: cannot find -lhdf5
collect2: error: ld returned 1 exit status

 

      接着開始編譯過程

 

make pycaffe
make all
make test
make runtest

 

      可在其后加上-j"$(nproc)"以加快速度

     可能在進行make runtest時出現一些錯誤,但若前三個命令沒有錯誤出現,就不妨礙使用(暫時沒發現什么妨礙的地方)

 

4.配置環境變量

  在.bashrc中配置環境變量

  

sudo gedit ~/.bashrc

 

  在后面加上

 

export PYTHONPATH=/your path/caffe/python:$PYTHONPATH

 

  接着

 

source ~/.bashrc

 

5.測試

 

Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>> dir(caffe)
['AdaDeltaSolver', 'AdaGradSolver', 'AdamSolver', 'Classifier', 'Detector', 'Layer', 'NCCL', 'NesterovSolver', 'Net', 'NetSpec', 'RMSPropSolver', 'SGDSolver', 'TEST', 'TRAIN', 'Timer', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_caffe', 'classifier', 'detector', 'get_solver', 'has_nccl', 'init_log', 'io', 'layer_type_list', 'layers', 'log', 'net_spec', 'params', 'proto', 'pycaffe', 'set_device', 'set_mode_cpu', 'set_mode_gpu', 'set_multiprocess', 'set_random_seed', 'set_solver_count', 'set_solver_rank', 'solver_count', 'solver_rank', 'to_proto']
>>> 

 

  沒問題,OK~

 

官方github:https://github.com/BVLC/caffe

官方安裝文檔:http://caffe.berkeleyvision.org/installation.html

參考:https://blog.csdn.net/muzilinxi90/article/details/53673184

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM