Caffe安装过程记录

Caffe安装过程记录


总体配置

  • Ubuntu16.04
  • GCC 5.4.0
  • Caffe + MKL + Anaconda3

参考文档

Caffe官网
Caffe安装官方文档
Python3.5 Anaconda3 Caffe深度学习框架搭建
本文未提及的问题可能会在其中出现,请经常翻阅并仔细阅读;出现问题还是Google比较靠谱

安装过程

依赖项目

安装过程中为了防止被墙,并且更好地看见每一项是否安装成功,我选择了一个一个地装

sudo apt-get install libprotobuf-dev
sudo apt-get install libleveldb-dev 
sudo apt-get install libsnappy-dev
sudo apt-get install libopencv-dev
sudo apt-get install libhdf5-serial-dev 
sudo apt-get install protobuf-compiler

sudo apt-get install --no-install-recommends libboost-all-dev

sudo apt-get install libgflags-dev 
sudo apt-get install libgoogle-glog-dev
sudo apt-get install liblmdb-dev
  • CUDA(GPU模式依赖)因为我只装CPU-ONLY,故此步省略
  • BLAS 分为ATLAS,OPENBLAS, MKL,需要选择合适自己的一项,因为我的是Intel的CPU,所以选择MKL:
    • Intel MKL,先到此处下载MKL(691MB),然后解压,终端输入./install_GUI.sh进入GUI安装界面,安装(默认路径安装即可)
    • 如果是默认路径安装,安装完成后输入以设置环境变量source /opt/intel/mkl/bin/mklvars.sh intel64
    • (编译时)在Makefile.config中设置BLAS := mkl
  • Python/MATLAB 我选择的是Python,并且使用的是Anaconda,所以不需要怎么配置

编译

  • 首先获取源码: git clone https://github.com/BVLC/caffe.git,推荐放在目录下
  • 接着进入python文件夹,输入for req in $(cat requirements.txt); do conda install $req; done将需要的包装上(Anaconda使用conda安装)
  • 然后输入cp Makefile.config.example Makefile.config,因为我使用了Anaconda,而且安装的是CPU模式,所以需要修改Makefile.config
    • 将第8行# CPU_ONLY := 1反注释,用以设置CPU模式
    • 50-55行设置为BLAS := mkl,设置BLAS为MKL,然后设置BLAS_INCLUDEBLAS_LIB
    • 设置Anaconda:68、69行注释掉;Anaconda相关的72-75行反注释,并且设置合适的路径(要进入所设置的路径,看看是否合法);78行反注释并修改Python版本;83行注释,84行反注释;91行反注释
  • 添加Caffe/Python至环境变量echo export PYTHONPATH=$CAFE_ROOT/python:$PYTHONPATH,$CAFE_ROOT为自己的caffe所在路径,添加后需要运行source ~./bashrc变量才能生效
  • 尝试make all

出现的错误

  • -lboost_python3 未找到 – 设置链接sudo ln -s /usr/lib/x86_64-linux-gnu/libbost_python-py35.so.1.58.0 /usr/local/lib/libboost_python3.so,其中/usr/lib/x86_64-linux-gnu/libbost_python-py35.so.1.58.0要视具体情况修改
  • GLIBCXX_3.4.21 – 输入conda install libgcc
  • google protobuf – 输入conda list | grep protobuf,将所有包用conda uninstall libprotobuf protobuf移除。重装protobuf,先到Github - Google protobuf下载C++Python两个版本的protobuf,然后依照README安装
    • undefined symbol: PC – 输入locate libreadline.so.6,找到后复制到anaconda/lib
    • TIFFconda remove libtiff(不知是否可行,忘了记录了)
  • leveldbconda uninstall leveldb

使用MNIST测试

数据集准备

caffe自带了获取数据集的script,进入caffe主目录,输入./data/mnist/get_mnist.sh,./examples/mnist/create_mnist.sh获取

运行

  • 需要配置examples/mnist/lenet_train_test.protxt, lenet_solver.prototxt, train_lenet.sh三个文件,简单的把路径设置好就可以了,需要注意这里不能用~作为/home/user的路径,必须把绝对路径完完整整地写上去
  • 运行./train_lenet.sh
  • 运行结果
...
I0922 15:04:22.516085  2727 solver.cpp:310] Iteration 10000, loss = 0.00184795
I0922 15:04:22.516126  2727 solver.cpp:330] Iteration 10000, Testing net (#0)
I0922 15:04:24.965847  2730 data_layer.cpp:73] Restarting data prefetching from start.
I0922 15:04:25.071223  2727 solver.cpp:397]     Test net output #0: accuracy = 0.9919
I0922 15:04:25.071271  2727 solver.cpp:397]     Test net output #1: loss = 0.0264297 (* 1 = 0.0264297 loss)
I0922 15:04:25.071283  2727 solver.cpp:315] Optimization Done.
I0922 15:04:25.071291  2727 caffe.cpp:259] Optimization Done.

总结

  • 不妨使用make -j n加快速度,n替换为处理器个数
  • make时候会出现非常多奇奇怪怪的错误,细想一下还是跟Anaconda3有很大关系,所以如果有怎么也无法解决的问题的时候,可以尝试使用conda uninstall去把Anaconda里面的包卸载了。
  • 可以尝试将出错的包手动编译。不过整个过程中我是用的是GCC5,至少是能顺利通过make,make test,make runtest的;整个过程下来也只要手动编译protobuf而已。
  • 这里我没有记录安装过程中LD_LIBRARY_PATH的变化,但是整个安装结束后是这样的
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=~/caffe/python/caffe:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
  • 下面是Makefile.config
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1

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

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#   You should not set this flag if you will be reading LMDBs with any
#   possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
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_60,code=sm_60 \
        -gencode arch=compute_61,code=sm_61 \
        -gencode arch=compute_61,code=compute_61

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := mkl
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /opt/intel/mkl/include
BLAS_LIB := /opt/intel/mkl/lib

# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# NOTE: this is required only if you will compile the python interface.
# 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
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python3.6m \
        $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib

# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1

# 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 /usr/lib/x86_64-linux-gnu/hdf5/serial

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1

# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

# enable pretty build (comment to see full commands)
Q ?= @

你可能感兴趣的:(ubuntu,机器学习,caffe)