日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Caffe 编译安装

發(fā)布時(shí)間:2025/7/25 122 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Caffe 编译安装 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

安裝

安裝前,最好閱讀下面的安裝指導(dǎo)并注意和你的系統(tǒng)平臺(tái)相關(guān)的細(xì)節(jié)問(wèn)題。我們已經(jīng)在 Ubuntu 14.04, Ubuntu 12.04, OS X 10.9,和OS X 10.8系統(tǒng)上成功的編譯安裝了Caffe。

Prerequisites

Caffe 依賴(lài)于很多的軟件包。

  • CUDA 依賴(lài)庫(kù),版本 6.5 (推薦), 6.0, 5.5, 或 5.0,以及他們最新版本的驅(qū)動(dòng),? CUDA 5使用319.*(不要使用 331.*)。
  • BLAS (通過(guò) ATLAS, MKL,或 OpenBLAS提供).
  • OpenCV.
  • Boost (>= 1.55,? 只有1.55和1.56測(cè)試過(guò))
  • glog, gflags, protobuf, leveldb,snappy,hdf5,lmdb
  • Python
    • Python 2.7, numpy (>= 1.7), boost-provided boost.python
  • MATLAB
    • 有mex編譯器的MATLAB .

cuDNN Caffe: Caffe通過(guò)drop-in integration of?NVIDIA cuDNN的加速方式來(lái)達(dá)到最快的執(zhí)行速度。要加速你的Caffe模型, 請(qǐng)安裝 cuDNN 然后在Makefile.config?文件中解注釋USE_CUDNN := 1,這樣安裝Caffe后,就會(huì)自動(dòng)加速.

CPU-only Caffe: ?對(duì)于cold-brewed 只有CPU模式的Caffe,請(qǐng)?jiān)谖募?span style="font-family:monospace">Makefile.config中,解注釋CPU_ONLY := 1,然后以無(wú)CUDA的方式編譯Caffe.這有助于云和集群方式的部署。

CUDA以及BLAS

Caffe 需要 CUDA nvcc 編譯器來(lái)編譯GPU 代碼 ,需要CUDA驅(qū)動(dòng)來(lái)操作GPU. 要安裝 CUDA, 請(qǐng)參照NVIDIA CUDA 網(wǎng)站和網(wǎng)站上的安裝說(shuō)明.分別安裝依賴(lài)庫(kù)和最新的驅(qū)動(dòng); 和依賴(lài)庫(kù)綁定的驅(qū)動(dòng)通常版本比較老.警告!? 331.* CUDA 系列驅(qū)動(dòng)有嚴(yán)重的性能問(wèn)題: 不要使用.

為了達(dá)到最好的性能, Caffe 可以通過(guò)NVIDIA cuDNN加速.? cuDNN 網(wǎng)站注冊(cè)是免費(fèi)的, 安裝完后繼續(xù)下面的安裝說(shuō)明. 要使用cuDNN編譯請(qǐng)?jiān)谀愕腗akefile.config種設(shè)置USE_CUDNN := 1?.

Caffe 需要 BLAS作為矩陣和向量計(jì)算的后端( backend).有很多這個(gè)庫(kù)的移植版本.可以自己根據(jù)情況選擇:

  • ATLAS: 免費(fèi),開(kāi)源代碼,Caffe的默認(rèn)依賴(lài).
    • Ubuntu: sudo apt-get install libatlas-base-dev
    • CentOS/RHEL/Fedora: sudo yum install atlas-devel
    • OS X: 已經(jīng)安裝為 Accelerate / vecLib Framework.
  • Intel MKL: 商業(yè)版并且針對(duì)Intel的CPU做了優(yōu)化, 可以免費(fèi)試用,并且有學(xué)生版本.
  • 安裝 MKL.
  • 在Makefile.config文件中設(shè)置 BLAS := mkl
  • OpenBLAS: 免費(fèi)的并且開(kāi)源; 優(yōu)化的可并行的 BLAS, 雖然安裝需要花點(diǎn)時(shí)間,但是確實(shí)可以提速 .
  • 安裝 OpenBLAS
  • 在Makefile.config文件中設(shè)置 BLAS := open

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

Python

主要的依賴(lài)為 numpy 和boost.python (通過(guò) boost提供). pandas 也是有用的并且是一些例子的依賴(lài)。

可以通過(guò)以下命令安裝依賴(lài):

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

但是我們強(qiáng)烈推薦首先安裝提供了大部分需要的的依賴(lài)包和hdf5依賴(lài)庫(kù)的 Anaconda Python distribution。

Ubuntu, 如果你的系統(tǒng)中使用的是默認(rèn)的 Python,你需要通過(guò) sudo apt-get installpython-dev 包來(lái)安裝Python 頭文件。

Fedora,如果你的系統(tǒng)中使用的是默認(rèn)的 Python,你需要通過(guò) sudo yum 安裝 python-devel包來(lái)安裝Python 頭文件。

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

安裝完成后,如果要導(dǎo)入 caffe的Python模塊,需要通過(guò) export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH或者類(lèi)似的方法添加模塊路徑到你的環(huán)境變量$PYTHONPATH .不可以在caffe/python/caffe路徑中導(dǎo)入!

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

MATLAB

安裝MATLAB, 確保Matalab的 mex在你的環(huán)境變量 $PATH中.

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

剩余的依賴(lài)

Linux

Ubuntu系統(tǒng), 大部分的依賴(lài)可以通過(guò)下面的命令安裝

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

對(duì)于 Ubuntu 14.04剩余的依賴(lài)可以通過(guò)下面的命令安裝

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

繼續(xù)往下讀,可以找到在Ubuntu 12.04如何手工編譯和安裝Google flags library, Google logging library 和 LMDB?的說(shuō)明.

?CentOS / RHEL / Fedora, 大部分的依賴(lài)可以通過(guò)下面的命令安裝

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

Google flags library, Google logging library 和 LMDB? 已經(jīng)有了CentOS / RHEL / Fedora上的新版本 所以最好先使用yum嘗試安裝這些庫(kù)

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

最后, 萬(wàn)一在你的系統(tǒng)源里找不到上面說(shuō)的這些擴(kuò)展庫(kù), 下面的命令教你如何在Ubuntu 12.04 / CentOS / RHEL / Fedora手工編譯安裝 (安裝的時(shí)候可能需要root權(quán)限)

# 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系統(tǒng)中, 極力推薦使用 Homebrew 軟件管理器, 并且最好從一個(gè)干凈的系統(tǒng)開(kāi)始安裝(或者? wiped/usr/local) 防止軟件沖突.接下來(lái),假設(shè)你使用的是Anaconda Python 和 Homebrew.

要安裝 OpenCV,我們需要提供一個(gè)附加源給 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

運(yùn)行下面的指令:

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++ 是默認(rèn)的 C++編譯器,使用 libc++ 作為標(biāo)準(zhǔn)庫(kù). 但是, NVIDIA CUDA (甚至 6.0版) 但前只鏈接libstdc++. 這樣就需要對(duì)每一個(gè)依賴(lài)改變編譯設(shè)置.

在安裝任何軟件包前通過(guò)修改 Homebrew 的配置(formulae)實(shí)現(xiàn)編譯設(shè)置 .確保Homebrew之前不安裝任何軟件依賴(lài);所有的軟件包都必須鏈接到libstdc++.

先前 Homebrew的配置為

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

對(duì)每一個(gè)配置 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++"...

按照順序修改配置, 運(yùn)行

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

然后,運(yùn)行

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"

然后,任何時(shí)候想要更新你自己的代碼, 轉(zhuǎn)回master分支, 進(jìn)行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!

編譯

現(xiàn)在已經(jīng)具備了所有的依賴(lài)條件, 編輯你的配置文件?Makefile.config,改變你的安裝路徑?(you should especially uncomment and setBLAS_LIB accordingly on distributions likeCentOS / RHEL / Fedora where ATLAS is installed under/usr/lib[64]/atlas),默認(rèn)的配置是可以的,但是如果你使用的是Anaconda Python,請(qǐng)解注釋相關(guān)的行。

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

編譯的時(shí)候如果要引入cuDNN加速,你需要在文件Makefile.config中解注釋USE_CUDNN := 1。

如果你的電腦上沒(méi)有 GPU,你應(yīng)該使用CPU-only模式的Caffe,請(qǐng)?jiān)谖募?span style="font-family:monospace">Makefile.config中解注釋CPU_ONLY := 1?。

要編譯 Python 和 MATLAB 包,請(qǐng)獨(dú)立的執(zhí)行make pycaffe和make matcaffe?. 請(qǐng)務(wù)必首先在Makefile.config中配置你的MATLAB 和 Python路徑!

Distribution: 運(yùn)行 make distribute來(lái)創(chuàng)建一個(gè)包含所有的Caffe 頭文件, 編譯好的依賴(lài)庫(kù),二進(jìn)制文件等的分布式路徑,用于其他機(jī)器的配置.

Speed: 如果要快速的編譯,請(qǐng)通過(guò)make all -j8的方式并行編譯,其中8是并行編譯的線(xiàn)程的數(shù)量(并行線(xiàn)程的數(shù)量最好等于你機(jī)器CPU核的數(shù)量).

到目前為止,你已經(jīng)安裝了Caffe, 請(qǐng)參照 MNIST tutorial 和 reference ImageNet model tutorial進(jìn)行進(jìn)一步的操作.

使用CMake (beta)編譯

通過(guò)手工編輯Makefile.config的方式告訴 Caffe 尋找依賴(lài)的的位置, Caffe 也提供了一個(gè)基于 CMake的編譯系統(tǒng) (currently in “beta”).要求CMake 版本 >= 2.8.8. 基本的安裝步驟如下:

mkdir build cd build cmake .. make all make runtest

Ubuntu 12.04

注意,在? Ubuntu 12.04中, Aptitude默認(rèn)安裝的CMake版本為2.8.7, 不能滿(mǎn)足編譯Caffe的CMake 版本需求 (版本至少為 2.8.8) ,如果你使用的系統(tǒng)是Ubuntu 12.04,可以嘗試下面的安裝步驟安裝CMake 2.8.9或者升級(jí)到這個(gè)版本 :

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

硬件問(wèn)題

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: 如果設(shè)備的運(yùn)算能力 <= 2.0, 受限于設(shè)備的性能, 可能需要減少 CUDA的線(xiàn)程數(shù)量 和 批處理的 大小(sizes). 你的吞吐量可能會(huì)發(fā)生變化.

安裝后, 參照我們的 reference performance numbers 確保所有的配置是正確的.

工程問(wèn)題請(qǐng)參照? hardware/compatibility.



沒(méi)有完整翻譯,翻譯的水平也有限,希望大家多提寶貴意見(jiàn)


總結(jié)

以上是生活随笔為你收集整理的Caffe 编译安装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。