Caffe 编译安装

From: http://caffe.berkeleyvision.org/installation.html

安装

安装前,最好阅读下面的安装指导并注意和你的系统平台相关的细节问题。我们已经在 Ubuntu 14.04, Ubuntu 12.04, OS X 10.9,和OS X 10.8系统上成功的编译安装了Caffe。

Prerequisites

Caffe 依赖于很多的软件包。

  • CUDA 依赖库,版本 6.5 (推荐), 6.0, 5.5, 或 5.0,以及他们最新版本的驱动,  CUDA 5使用319.*(不要使用 331.*)。
  • BLAS (通过 ATLAS, MKL,或 OpenBLAS提供).
  • OpenCV.
  • Boost (>= 1.55,  只有1.55和1.56测试过)
  • glog, gflags, protobuf, leveldb,snappy,hdf5,lmdb
  • Python
    • Python 2.7, numpy (>= 1.7), boost-provided boost.python
  • MATLAB
    • 有mex编译器的MATLAB .

cuDNN Caffe: Caffe通过drop-in integration of NVIDIA cuDNN的加速方式来达到最快的执行速度。要加速你的Caffe模型, 请安装 cuDNN 然后在Makefile.config 文件中解注释USE_CUDNN := 1,这样安装Caffe后,就会自动加速.

CPU-only Caffe:  对于cold-brewed 只有CPU模式的Caffe,请在文件Makefile.config中,解注释CPU_ONLY := 1,然后以无CUDA的方式编译Caffe.这有助于云和集群方式的部署。

CUDA以及BLAS

Caffe 需要 CUDA nvcc 编译器来编译GPU 代码 ,需要CUDA驱动来操作GPU. 要安装 CUDA, 请参照NVIDIA CUDA 网站和网站上的安装说明.分别安装依赖库和最新的驱动; 和依赖库绑定的驱动通常版本比较老.警告 331.* CUDA 系列驱动有严重的性能问题: 不要使用.

为了达到最好的性能, Caffe 可以通过NVIDIA cuDNN加速.  cuDNN 网站注册是免费的, 安装完后继续下面的安装说明. 要使用cuDNN编译请在你的Makefile.config种设置USE_CUDNN := 1 .

Caffe 需要 BLAS作为矩阵和向量计算的后端( backend).有很多这个库的移植版本.可以自己根据情况选择:

  • ATLAS: 免费,开源代码,Caffe的默认依赖.
  • Intel MKL: 商业版并且针对Intel的CPU做了优化, 可以免费试用,并且有学生版本.
    1. 安装 MKL.
    2. Makefile.config文件中设置 BLAS := mkl
  • OpenBLAS: 免费的并且开源; 优化的可并行的 BLAS, 虽然安装需要花点时间,但是确实可以提速 .
    1. 安装 OpenBLAS
    2. Makefile.config文件中设置 BLAS := open

Python 以及/或者 MATLAB 包 (可选)

Python

主要的依赖为 numpyboost.python (通过 boost提供). pandas 也是有用的并且是一些例子的依赖。

可以通过以下命令安装依赖:

for req in $(cat requirements.txt); do sudo pip install $req; done

但是我们强烈推荐首先安装提供了大部分需要的的依赖包和hdf5依赖库的 Anaconda Python distribution。

Ubuntu, 如果你的系统中使用的是默认的 Python,你需要通过 sudo apt-get 安装python-dev 包来安装Python 头文件。

Fedora,如果你的系统中使用的是默认的 Python,你需要通过 sudo yum 安装 python-devel包来安装Python 头文件。

OS X, Anaconda is the preferred Python. If you decide against it, please use Homebrew – but beware of potential linking errors!

安装完成后,如果要导入 caffe的Python模块,需要通过 export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH或者类似的方法添加模块路径到你的环境变量$PYTHONPATH .不可以在caffe/python/caffe路径中导入!

Caffe’的 Python 接口支持 Python 2.7. Python 3 或者更早的 Pythons.

MATLAB

安装MATLAB, 确保Matalab的 mex在你的环境变量 $PATH中.

Caffe的 MATLAB接口兼容于版本 2012b, 2013a/b, 和 2014a.

剩余的依赖

Linux

Ubuntu系统, 大部分的依赖可以通过下面的命令安装

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev

对于 Ubuntu 14.04剩余的依赖可以通过下面的命令安装

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

继续往下读,可以找到在Ubuntu 12.04如何手工编译和安装Google flags library, Google logging library 和 LMDB 的说明.

 CentOS / RHEL / Fedora, 大部分的依赖可以通过下面的命令安装

sudo yum install protobuf-devel leveldb-devel snappy-devel opencv-devel boost-devel hdf5-devel

Google flags library, Google logging library 和 LMDB  已经有了CentOS / RHEL / Fedora上的新版本 所以最好先使用yum尝试安装这些库

sudo yum install gflags-devel glog-devel lmdb-devel

最后, 万一在你的系统源里找不到上面说的这些扩展库, 下面的命令教你如何在Ubuntu 12.04 / CentOS / RHEL / Fedora手工编译安装 (安装的时候可能需要root权限)

# glog
wget https://google-glog.googlecode.com/files/glog-0.3.3.tar.gz
tar zxvf glog-0.3.3.tar.gz
cd glog-0.3.3
./configure
make && make install

 # gflags
wget https://github.com/schuhschuh/gflags/archive/master.zip
unzip master.zip
cd gflags-master
mkdir build && cd build
export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1
make && make install

 # lmdb
git clone git://gitorious.org/mdb/mdb.git
cd mdb/libraries/liblmdb
make && make install

Note that glog does not compile with the most recent gflags version (2.1), so before that is resolved you will need to build with glog first.

OS X

OS X系统中, 极力推荐使用 Homebrew 软件管理器, 并且最好从一个干净的系统开始安装(或者  wiped/usr/local) 防止软件冲突.接下来,假设你使用的是Anaconda Python 和 Homebrew.

要安装 OpenCV,我们需要提供一个附加源给 Homebrew:

brew tap homebrew/science

如果使用的是Anaconda Python, 需要修改下 OpenCV的 配置. Do brew edit opencv and 修改和下面两行相似的两行位下面的两行.

  -DPYTHON_LIBRARY=#{py_prefix}/lib/libpython2.7.dylib
  -DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7

备注: We find that everything compiles successfully if $LD_LIBRARY_PATH is not set at all, and$DYLD_FALLBACK_LIBRARY_PATH is set to to provide CUDA, Python, and other relevant libraries (e.g./usr/local/cuda/lib:$HOME/anaconda/lib:/usr/local/lib:/usr/lib).In otherENV settings, things may not work as expected.

NOTE: There is currently a conflict between boost 1.56 and CUDA in some configurations. Check theconflict description and try downgrading to 1.55.

10.8-specific Instructions

运行下面的指令:

brew install --build-from-source boost boost-python
brew install --with-python protobuf
for x in snappy leveldb gflags glog szip lmdb homebrew/science/opencv; do brew install $x; done

Building boost from source is needed to link against your local Python (exceptions might be raised during some OS X installs, butignore these and continue). If you do not need the Python wrapper, simply doingbrew install boost is fine.

Note that the HDF5 dependency is provided by Anaconda Python in this case.If you’re not using Anaconda, includehdf5 in the list above.

10.9-specific Instructions

在 OS X 10.9中, clang++ 是默认的 C++编译器,使用 libc++ 作为标准库. 但是, NVIDIA CUDA (甚至 6.0版) 但前只链接libstdc++. 这样就需要对每一个依赖改变编译设置.

在安装任何软件包前通过修改 Homebrew 的配置(formulae)实现编译设置 .确保Homebrew之前不安装任何软件依赖;所有的软件包都必须链接到libstdc++.

先前 Homebrew的配置为

boost snappy leveldb protobuf gflags glog szip lmdb homebrew/science/opencv

对每一个配置 brew edit FORMULA, 并且添加ENV 定义按照下面所示:

  def install
      # ADD THE FOLLOWING:
      ENV.append "CXXFLAGS", "-stdlib=libstdc++"
      ENV.append "CFLAGS", "-stdlib=libstdc++"
      ENV.append "LDFLAGS", "-stdlib=libstdc++ -lstdc++"
      # The following is necessary because libtool likes to strip LDFLAGS:
      ENV["CXX"] = "/usr/bin/clang++ -stdlib=libstdc++"
      ...

按照顺序修改配置, 运行

for x in snappy leveldb protobuf gflags glog szip boost boost-python lmdb homebrew/science/opencv; do brew edit $x; done

然后,运行

for x in snappy leveldb gflags glog szip lmdb homebrew/science/opencv; do brew uninstall $x; brew install --build-from-source --fresh -vd $x; done
brew uninstall protobuf; brew install --build-from-source --with-python --fresh -vd protobuf
brew install --build-from-source --fresh -vd boost boost-python

Note that brew install --build-from-source --fresh -vd boost is fine if you do not need the Caffe Python wrapper.

Note that the HDF5 dependency is provided by Anaconda Python in this case.If you’re not using Anaconda, includehdf5 in the list above.

Note that in order to build the Caffe Python wrappers you must installboost andboost-python:

brew install --build-from-source --fresh -vd boost boost-python

Note that Homebrew maintains itself as a separate git repository and making the abovebrew edit FORMULA changes will change files in your local copy of homebrew’s master branch. By default, this will prevent you from updating Homebrew usingbrew update, as you will get an error message like the following:

$ brew update
error: Your local changes to the following files would be overwritten by merge:
  Library/Formula/lmdb.rb
Please, commit your changes or stash them before you can merge.
Aborting
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

One solution is to commit your changes to a separate Homebrew branch, run brew update, and rebase your changes onto the updated master. You’ll have to do this both for the main Homebrew repository in/usr/local/ and the Homebrew science repository that contains OpenCV in/usr/local/Library/Taps/homebrew/homebrew-science, as follows:

cd /usr/local
git checkout -b caffe
git add .
git commit -m "Update Caffe dependencies to use libstdc++"
cd /usr/local/Library/Taps/homebrew/homebrew-science
git checkout -b caffe
git add .
git commit -m "Update Caffe dependencies"

然后,任何时候想要更新你自己的代码, 转回master分支, 进行update, rebase caffe 分支到master, 修正所有的conflicts:

# Switch batch to homebrew master branches
cd /usr/local
git checkout master
cd /usr/local/Library/Taps/homebrew/homebrew-science
git checkout master

# Update homebrew; hopefully this works without errors!
brew update

# Switch back to the caffe branches with the forumlae that you modified earlier
cd /usr/local
git rebase master caffe
# Fix any merge conflicts and commit to caffe branch
cd /usr/local/Library/Taps/homebrew/homebrew-science
git rebase master caffe
# Fix any merge conflicts and commit to caffe branch

# Done!            

At this point, you should be running the latest Homebrew packages and your Caffe-related modifications will remain in place.

Windows

There is an unofficial Windows port of Caffe at niuzhiheng/caffe:windows. Thanks @niuzhiheng!

编译

现在已经具备了所有的依赖条件, 编辑你的配置文件 Makefile.config,改变你的安装路径 (you should especially uncomment and setBLAS_LIB accordingly on distributions likeCentOS / RHEL / Fedora where ATLAS is installed under/usr/lib[64]/atlas),默认的配置是可以的,但是如果你使用的是Anaconda Python,请解注释相关的行。

cp Makefile.config.example Makefile.config
# Adjust Makefile.config (for example, if using Anaconda Python)
make all
make test
make runtest

编译的时候如果要引入cuDNN加速,你需要在文件Makefile.config中解注释USE_CUDNN := 1。

如果你的电脑上没有 GPU,你应该使用CPU-only模式的Caffe,请在文件Makefile.config中解注释CPU_ONLY := 1 。

要编译 Python 和 MATLAB 包,请独立的执行make pycaffe和make matcaffe . 请务必首先在Makefile.config中配置你的MATLAB 和 Python路径!

Distribution: 运行 make distribute来创建一个包含所有的Caffe 头文件, 编译好的依赖库,二进制文件等的分布式路径,用于其他机器的配置.

Speed: 如果要快速的编译,请通过make all -j8的方式并行编译,其中8是并行编译的线程的数量(并行线程的数量最好等于你机器CPU核的数量).

到目前为止,你已经安装了Caffe, 请参照 MNIST tutorial reference ImageNet model tutorial进行进一步的操作.

使用CMake (beta)编译

通过手工编辑Makefile.config的方式告诉 Caffe 寻找依赖的的位置, Caffe 也提供了一个基于 CMake的编译系统 (currently in “beta”).要求CMake 版本 >= 2.8.8. 基本的安装步骤如下:

mkdir build
cd build
cmake ..
make all
make runtest
Ubuntu 12.04

注意,在  Ubuntu 12.04中, Aptitude默认安装的CMake版本为2.8.7, 不能满足编译Caffe的CMake 版本需求 (版本至少为 2.8.8) ,如果你使用的系统是Ubuntu 12.04,可以尝试下面的安装步骤安装CMake 2.8.9或者升级到这个版本 :

sudo add-apt-repository ppa:ubuntu-sdk-team/ppa -y
sudo apt-get -y update
sudo apt-get install cmake

硬件问题

Laboratory Tested Hardware: Berkeley Vision runs Caffe with K40s, K20s, and Titans including models at ImageNet/ILSVRC scale. We also run on GTX series cards and GPU-equipped MacBook Pros. We have not encountered any trouble in-house with devices with CUDA capability >= 3.0. All reported hardware issues thus-far have been due to GPU configuration, overheating, and the like.

CUDA compute capability: 如果设备的运算能力 <= 2.0, 受限于设备的性能, 可能需要减少 CUDA的线程数量 和 批处理的 大小(sizes). 你的吞吐量可能会发生变化.

安装后, 参照我们的 reference performance numbers 确保所有的配置是正确的.

工程问题请参照  hardware/compatibility.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值