在Windows下編譯CAFFE並使用其matlab和python接口

轉自:http://blog.csdn.net/happynear/article/details/45372231

零、最近更新

2015/05/29 發現上個版本的lmdb.lib使用了別人在vs2013下編譯的版本,現改爲vs2012版; 
2015/05/29 添加了提取任意層特徵的matlab接口,使用方法:

<code class="language-matlab hljs  has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">OUTPUT = caffe(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'get_features'</span>, INPUT, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'layername1,layername2...'</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

例如:f = caffe('get_features', H, 'conv51,pool5'); 返回的f爲2*1的cell類型,裏面記錄了層的名稱和該層的特徵。

一、準備

  需要用到的東西我已經幫大家全部準備好了,有3月31日剛剛從caffe官方dev分支fork過來的源代碼:https://www.github.com/happynear/caffe-windows。有我自己親手製作的第三方庫http://pan.baidu.com/s/1gdAfUPt ,在源碼的根目錄下建立個3rdparty文件夾,把文件解壓進去就可以了。 
  解壓好之後,要將3rdparty/bin文件夾添加到系統目錄,這樣才能讓程序找到這些第三方庫的dll。 
  然後是Visual Studio 2012,因爲我的第三方庫都是VS2012環境下編譯的,所以用其他版本VS的請自行編譯。
  最後是CUDA和MKL了,MKL是可選的,大家可以去Intel官方申請,如果不用cpu模式的話其實也無所謂,在第三方庫包中我還提供了openblas的庫文件。

二、編譯

  編譯非常簡單,分爲以下幾步: 
  1、雙擊./src/caffe/proto/extract_proto.bat批處理文件來生成caffe.pb.h 和caffe.pb.cc兩個c++文件,和caffe_pb2.py這個python使用的文件。 
  2、打開./build/MSVC/MainBuilder.sln,打開之後切換編譯模式至Release X64模式。如果打開之後顯示加載失敗,可能你的CUDA版本和我的不一致,我的是CUDA 6.5版,這時就要用記事本打開./build/MSVC/MainBuilder.vcxproj,搜索CUDA 6.5,把這個6.5換成你自己的CUDA版本,就可以正常打開了。 
  3、右鍵點擊左邊的MainCaller項目,選擇屬性,在C/C++選項卡中,把附加包含目錄中的cuda include目錄改成你自己的;在鏈接器選項卡中,把附加庫目錄中的cuda library目錄也改成你自己的。 
  4、點上邊工具欄中的綠色三角編譯吧。 
   
  如果要用matlab wrapper來提取特徵、觀察訓練好的權重的話呢,只需要把matcaffe項目裏面的cuda、matlab目錄修改成你自己的,然後編譯,你就能從matlab/caffe文件夾裏面找到一個叫caffe.mexw64的文件啦。 
  python的wrapper類似,把pycaffe項目裏的目錄改成你自己的,就能在python/caffe文件夾中生成_caffe.pyd的python dll文件。

三、測試

  到 http://pan.baidu.com/s/1mgl9ndu 下載已經轉換好的MNIST的leveldb數據文件,解壓至./examples/mnist文件夾中,然後運行根目錄下的run_mnist.bat即可開始訓練,訓練日誌會保存在./log文件夾中,以INFO開頭,txt格式的日誌文件中。

ps:如果你編譯成功的話,不要忘了給我的github工程點個star! 
star

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章