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的默認依賴.
- Ubuntu: sudo apt-get install libatlas-base-dev
- CentOS/RHEL/Fedora: sudo yum install atlas-devel
- OS X: 已經安裝為 Accelerate / vecLib Framework.
- Intel MKL: 商業版并且針對Intel的CPU做了優化, 可以免費試用,并且有學生版本.
- 安裝 MKL.
- 在Makefile.config文件中設置 BLAS := mkl
- OpenBLAS: 免費的并且開源; 優化的可并行的 BLAS, 雖然安裝需要花點時間,但是確實可以提速 .
- 安裝 OpenBLAS
- 在Makefile.config文件中設置 BLAS := open
Python 以及/或者 MATLAB 包 (可選)
Python
主要的依賴為 numpy 和boost.python (通過 boost提供). pandas 也是有用的并且是一些例子的依賴。
可以通過以下命令安裝依賴:
for req in $(cat requirements.txt); do sudo pip install $req; done但是我們強烈推薦首先安裝提供了大部分需要的的依賴包和hdf5依賴庫的 Anaconda Python distribution。
Ubuntu, 如果你的系統中使用的是默認的 Python,你需要通過 sudo apt-get installpython-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系統, 大部分的依賴可以通過下面的命令安裝
對于 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-develGoogle 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權限)
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; doneBuilding 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-pythonNote 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-pythonNote 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/masterOne 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 runtestUbuntu 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.
沒有完整翻譯,翻譯的水平也有限,希望大家多提寶貴意見
總結
以上是生活随笔為你收集整理的Caffe 编译安装的全部內容,希望文章能夠幫你解決所遇到的問題。

- 上一篇: 港中大教授研发DeepID人脸识别技术
- 下一篇: ubuntu pdf转jpg或txt