caffe的依赖库简介
ProtoBuffer
Google Protocol Buffer (简称ProtoBuf) 是一种轻便高效的结构化数据存储格式,可以用于结构化数据的串行化,或者说序列化。它很合适做数据存储或RPC数据的交换格式。可以用于通信协议、数据存储等领域的语言无关、平台无关、可扩展的序列结构数据格式。
要使用Protobuf库,首先需要自己编写一个.proto文件,定义程序中需要处理的结构化数据,在Protobuf中,结构化数据被称为Message。在一个.proto文件中可以定义多喝消息类型。用Protobuf编译器(protoc.exe)将.proto文件编译成目标语言,会生成对应的.h文件和.cc文件,.proto文件中的每一个消息有一个对应的类。
Caffe使用起来很简洁,很大程度上是由于Caffe采用了.proto文件作为用户输入的接口。用户通过编写.proto文件定义网路模型和Solver。
简单来说,ProtoBuffer就是caffe保存模型和解析模型用的数据格式。
Boost
Boost是为C++语言标准库提供扩展的一些C++程序库的总称。Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一,是为C++语言标准库提供扩展的一些C++程序库的总称。
按照实现的功能,Boost可为大致归入下20个分类,在分类中,有些库同时归入几种类别:
boost字符串和文本处理库、boost容器库、boost迭代器库、boost算法库、boost函数对象和高阶编程库、boost泛型编程库、boost模板元编程、boost预处理元编程库、boost并发编程库、boost数学和数字库、boost排错和测试库、boost数据结构库、boost图像处理库、boost输入输出库、boost跨语言混合编程库、boost内存管理库、boost解析库、boost编程接口库、boost综合类库、boost编译器问题的变通方案库等。
Caffe采用C++作为主开发语言,其中大量的代码依赖于Boost库。
高级的C++库,之后需要学习一下。
GFlags
GFlags是Google的一个开源的处理命令行参数的库,使用C++开发,可以替代getopt函数。GFlags与getopt函数不同,在GFlags中,标记的定义分散在源码中,不需要列举在一个地方。Caffe库采用GFlags库开发Caffe的命令行。
GFlags在Caffe中主要起到命令行参数解析的作用,这与ProtoBuffer功能类似,只是参数输入源不同。
GLog
Glog是一个应用程序的日志库,提供基于C++风格的流日志API,以及各种辅助的宏。它的使用方式与C++的stream操作类似。Caffe运行时的日志输出依赖于GLog库,便于开发者根据这些信息决定如何调整参数来控制收敛。
HDF5
HDF(Hierarchical Data File)是NCSA为了满足各种领域研究需要而研制的一种高效存储和分发科学数据的新型数据格式。它还可以存储不同类型的图像和数码数据的文件,并且可以在不同类型的机器上传输,同时还有统一处理这种文件根式的函数库。
Caffe训练模型可以选择保存为HDF5格式或(默认的)ProtoBuffer格式
LevelDB LMDB
LMDB(Lighting Memory-Mapped Database Manager),闪电般的内存映射型数据库管理器,在Caffe中的作用主要是提供数据管理,可以将形形色色的原始数据(JPEG图片、二进制数据)转换为统一的Key-Value存储,便于Caffe的DataLayer获取这些数据。LEVELDB库是Caffe早期的版本使用的数据存储方式,由Google开发。它是一种持续的键值对存储方式,键和值可以为任意字节数组。键的存储顺序可由用户定义的比较函数决定。
LMDB已经替代了LEVELDB,但是为了与以前的版本兼容,仍然将这个依赖库编译到Caffe中。
BLAS
卷积神经网络中用到的数学计算主要是矩阵、向量的计算,Caffe中调用了BLAS(Basic Linear Algebra Subprograms,基本线性代数子程序)中的相应方法。最常见的BLAS实现有IntelMKL、ATLAS、OpenBLAS等,Caffe可以选择其中任一种。
在Makefile.config可以配置选择。
安装实战(ubuntu)
依赖库安装
可以直接用apt-get直接安装的依赖库
sudo apt-get install git
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev python-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler openblas-dev
可能编译安装其他依赖库时会用到的依赖库
sudo apt-get install -y protobuf-c-compiler
sudo apt-get install libffi-dev python-dev build-essential
sudo apt-get install autoconf automake libtool
配置gpu
安装Nvidia显卡驱动
安装前准备
- 查看显卡连接,确认显卡硬件正确连接
lspci | grep -I nvidia
- Nvidia官网下载与系统和显卡版本对应的显卡驱动进行安装,不要使用其他辅助安装工具容易出错
删除以前的驱动,防止相互干扰
sudo apt-get remove --purge nvidia*
安装一些必要的库
sudo apt-get update
sudo apt-get install dkms build-essential linux-headers-generic
禁用nouveau
- 打开blacklist.conf, 在最后加入禁用nouveau的设置, 这是一个开源驱动, 如图所示:
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off
- 禁用nouveau内核模块
echo options nouveau modeset=0
sudo update-initramfs -u
- 检查是否禁用成功,重启后执行以下命令,若无任何打印内容,则禁用成功
lsmod | grep nouveau
安装驱动
- 首先关闭图形界面
sudo systemctl set-default multi-user.target sudo reboot
执行从官网下载的.run脚本
sudo chmod u+x NVIDIA-Linux-x86_64-390.87.run sudo ./NVIDIA-Linux-x86_64-390.87.run
或是视情况而定
sudo ./NVIDIA-Linux-x86_64-390.87.run –no-opengl-files
若安装失败 则应先卸载已安装部分
sudo ./NVIDIA-Linux-x86_64-390.87.run –uninstall
- 中间可能会有报错,缺乏各种依赖库
若弹出Unable to find a suitable destination to install 32-bit compatibility libraries on Ubuntu 18.04 Bionic Beaver Linux的bugsudo dpkg --add-architecture i386 sudo apt update sudo apt install libc6:i386
- 安装完后,执行指令可以 大专栏 caffe(初识2)查看gpu的状态,则证明
nvidia-smi
安装cuda
- 先把前置需要的库安装上
sudo apt-get install freeglut3-dev libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
- 然后从官网上下载,并用md5检查下下载的安装包是否受损
md5sum cuda_10.0.130_410.48_linux.run
- 开始安装cuda,安装时可以暂时不安装OpenGL
sudo sh cuda_10.0.130_410.48_linux.run –no-opengl-files
- 添加环境变量
在其中写入vim ~/.bashrc
保存后,并执行export CUDA_HOME=/usr/local/cuda export PATH=$PATH:$CUDA_HOME/bin export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
source ~/.bashrc
- 查看cuda的版本,判断是否安装成功
nvcc -V
编译安装caffe
- 先下载源码
git clone https//github.com/bvlc/caffe.git
cd caffe/
mv Makefile.config.example Makefile.confile
- 看懂并修改配置文件
# 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 #使用cudnn,将注释去掉
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1 #使用GPU,所保留注释
# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 1 # 使用opencv
# USE_LEVELDB := 0
USE_LMDB := 1 # 使用LMDB
# This code is taken from https://github.com/sh1r0/caffe-android-lib
USE_HDF5 := 1 # 使用HDF5
# 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 # 使用opencv3 以上的版本
# 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 #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 # 如果是使用 apt-get 安装的,用此路径
# CUDA architecture setting: going with all of them.
#在不同的cuda版本和gpu上需要使用不同的匹配选项
#具体可以查看官网信息 https://developer.nvidia.com/cuda-gpus
# https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#compute-capabilities
# 将不兼容的选项注释掉
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 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 := atlas #默认为atlas,也可以选用MKL、ATLAS、OpenBLAS
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)! #此处需要手动配置,blas的库目录和引用目录
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
# 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
# 如果需要安装matlab的接口则需要在此处配置,Matlab相关目录
# 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
# python的相关接口,若不使用python2可以将其全部注释掉
# 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)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include
# $(ANACONDA_HOME)/include/python2.7
# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# 使用python3,在下面几行配置
# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# 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
# 此处配置建议选上,可以通过python接口来生产caffe模型配置
# Uncomment to support layers written in Python (will link against Python libs)
# WITH_PYTHON_LAYER := 1
# Whatever else you find you need goes here.
# 这里需要添加HDF5的相关路径
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/serial
# INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
# LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
# 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 ?= @
CXXFLAGS+=-std=c++11
CUSTOM_CXX := g++ -std=c++11
- 修改Makefile中的链接库
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
将:
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
开始编译
makedir build cd build cmake .. make -j8 make runtest -j8 make pycaffe -j8
安装一下python的依赖
cd python
pip install --yes --file requirements.txt