1. 程式人生 > >【caffe原始碼研究】第一章:安裝篇(上): jumbo版

【caffe原始碼研究】第一章:安裝篇(上): jumbo版

下載caffe

下載caffe的原始碼

[fangjin@caffe ~]$ git clone https://github.com/BVLC/caffe.git 

安裝依賴庫

caffe涉及到一些依賴庫,先使用jumbo安裝。

[fangjin@caffe ~]$ jumbo  install pkg-config snappy  opencv hdf5  google-gflags google-glog  leveldb protobuf

這裡為了方便統一將其他依賴庫安裝到/home/users/fangjin/opt目錄下。更合理的做法是為不同的依賴庫建立不同的子目錄。

安裝 openblas

修改環境變數,升級gcc為4.6或以上版本

[fangjin@caffe ~]$ jumbo install gcc46
[fangjin@caffe ~]$ export PATH=${JUMBO_ROOT}/bin:${JUMBO_ROOT}/opt/gcc46/bin:$PATH
[fangjin@caffe ~]$ source ~/.bashrc

下載安裝openblas

[fangjin@caffe ~]$ git clone https://github.com/xianyi/OpenBLAS.git
[fangjin@caffe ~]$ cd OpenBLAS
[fangjin@caffe
~]$ make CC=gcc FC=gfortran [fangjin@caffe ~]$ make PREFIX=/home/users/fangjin/opt install

其中/home/users/fangjin/opt使用完整路徑, 不要使用~/opt這種。

安裝protobuf

jumbo 有現成的protobuf,可不必原始碼安裝。此小節可略過。

[fangjin@caffe ~]$ git clone https://github.com/google/protobuf.git
[fangjin@caffe ~]$ cd protobuf-2.5.0
[fangjin@caffe
~]$ ./configure --prefix=/home/users/fangjin/opt [fangjin@caffe ~]$ make [fangjin@caffe ~]$ make install

安裝boost

在網站下載最新原始碼包,例如boost_1_56_0.tar.gz

[fangjin@caffe ~]$ tar zxvf boost_1_56_0.tar.gz
[fangjin@caffe ~]$ cd boost_1_56_0
[fangjin@caffe ~]$ ./bootstrap.sh  --prefix=/home/users/fangjin/opt #通過執行 ./bootstrap.sh  --help來檢視
[fangjin@caffe ~]$ ./b2 install #檢視help也可在此步驟來指定--prefix

安裝lmdb

[fangjin@caffe ~]$ git clone https://github.com/LMDB/lmdb.git
[fangjin@caffe ~]$ cd lmdb/libraries/liblmdb/
[fangjin@caffe ~]$ make
[fangjin@caffe ~]$ make prefix=/home/users/fangjin/opt install  #從src中Makefile中可以看到有一個prefix變數

安裝python依賴庫

如果需要編譯python介面,需要安裝相關依賴庫。
內網環境無法連線http://pypi.douban.com/,可以參考pip.baidu.com.

#先安裝pip
[fangjin@caffe ~]$ jumbo install python python-pip

pypi.douban源改成baidu的源。

mkdir -p $HOME/.pip/
echo -e "[global]\nindex-url = http://pip.baidu.com/pypi/simple" >> $HOME/.pip/pip.conf
echo -e "[install]\ntrusted-host = pip.baidu.com" >> $HOME/.pip/pip.conf
echo -e "[easy_install]\nindex-url = http://pip.baidu.com/pypi/simple" >> $HOME/.pydistutils.cfg

注意,這種方法pip search無法使用。
安裝相關依賴。

[fangjin@caffe ~]$ pip install --upgrade pip
[fangjin@caffe ~]$ cd caffe/python
[fangjin@caffe ~]$ for i in $(cat requirements.txt); do pip install "$i" ;done

配置安裝項

Makefile.config.example複製一份為Makefile.config

[[email protected] ~]$ cp Makefile.config.example Makefile.config

修改Makefile.config中的配置
根據自己的需求來修改,注意的地方有兩個
一個是PYTHON_INCLUDE 的路徑,一個是PYTHON_LIB 的路徑
然後自己新增LOCAL_PATH的路徑。
INCLUDE_DIRSLIBRARY_DIRS定義用到的庫和標頭檔案。

一個Makefile.config的示例如下

# 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
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
# 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
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 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_50,code=compute_50
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# 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
# 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
# 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 := /home/users/fangjin/.jumbo/include/python2.7 \
                /home/users/fangjin/.jumbo/lib/python2.7/site-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 \
# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /home/users/zouxidong/.jumbo/lib/python2.7
# 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
# Uncomment to support layers written in Python (will link against Python libs)
# WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include \
        ${JUMBO_ROOT}/include \
         ${JUMBO_ROOT}/opt/gcc46/include \
        /home/users/fangjin/opt/include 
LIBRARY_DIRS := $(PYTHON_LIB) \
        ${JUMBO_ROOT}/lib \
        ${JUMBO_ROOT}/opt/gcc46/lib64 \
       /home/users/fangjin/opt/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
# 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
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 ?= @

同時,在.bashrc中需要定義之前安裝的各種依賴庫的bin和lib。

# export PATH=/home/users/fangjin/opt/bin:${JUMBO_ROOT}/bin:${JUMBO_ROOT}/opt/gcc46/bin:$PATH
# export LD_LIBRARY_PATH=/home/users/fangjin/opt/lib:${JUMBO_ROOT}/lib:${JUMBO_ROOT}/opt/gcc46/lib64:${LD_LIBRARY_PATH}

然後

[fangjin@caffe ~]$ source ~/.bashrc

安裝

執行make -j8 編譯caffe,執行成功時會生成libcaffe.so
執行make pycaffe輸出如下內容PROTOC (python) src/caffe/proto/caffe.proto表示安裝成功

測試

如何知道是否正確安裝呢?
編譯後的檔案在caffe/build中。
可以執行build/tools/caffe看是否可以執行。
來利用提供的example做測試。

# 下載資料集
[fangjin@caffe ~]$ sh data/mnist/get_mnist.sh
# 轉換資料集
[fangjin@caffe ~]$ sh examples/mnist/create_mnist.sh
# 開始訓練
[fangjin@caffe ~]$ sh examples/mnist/train_lenet.sh

注意,如果不是使用GPU,可以看看train_lenet.sh中的solver檔案,修改成cpu。

下面測試python的caffe介面是否安裝成功。
可以將caffe介面直接寫入PATH變數。
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
也可以直接將caffe/python/caffe複製到/home/users/fangjin/.jumbo/lib/python2.7/site-packages/
然後

[fangjin@caffe ~]$ source ~/.bashrc
  1. 特別注意,路徑是caffe/python,而不是caffe/python/caffe,否則numpy會報錯。
  2. export新增環境變數之後,即使修改了再次source ~/.bashrc,因為是使用:,前一次的變數值依舊儲存,最簡單的方法是退出終端,重新連線。

測試python介面

[fangjin@caffe ~]$ python
Python 2.7.3 (default, Aug  4 2016, 12:06:54) 
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>

不報錯,則成功。

但是開發機上安裝基本都會報錯

>>> import caffe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/users/fangjin/my_workplace/c_workplace/010_learn_caffe/source_code/caffe/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
  File "/home/users/fangjin/my_workplace/c_workplace/010_learn_caffe/source_code/caffe/python/caffe/pycaffe.py", line 15, in <module>
    import caffe.io
  File "/home/users/fangjin/my_workplace/c_workplace/010_learn_caffe/source_code/caffe/python/caffe/io.py", line 2, in <module>
    import skimage.io
  File "/home/users/fangjin/.jumbo/lib/python2.7/site-packages/skimage/io/__init__.py", line 7, in <module>
    from .manage_plugins import *
  File "/home/users/fangjin/.jumbo/lib/python2.7/site-packages/skimage/io/manage_plugins.py", line 28, in <module>
    from .collection import imread_collection_wrapper
  File "/home/users/fangjin/.jumbo/lib/python2.7/site-packages/skimage/io/collection.py", line 12, in <module>
    from PIL import Image
  File "/home/users/fangjin/.jumbo/lib/python2.7/site-packages/PIL/Image.py", line 67, in <module>
    from PIL import _imaging as core
ImportError: /home/users/fangjin/.jumbo/lib/python2.7/site-packages/PIL/_imaging.so: undefined symbol: TIFFSetWarningHandlerExt

單獨from PIL import Image 也會出錯,所以問題與caffe無關,具體原因經過分析,應該是libtiff庫引起的。舊版本的libtiff庫沒有TIFFSetWarningHandlerExt,所以需要重新安裝一個libtiff庫。

下載地址http://dl.maptools.org/dl/libtiff/,下載tiff-3.8.2.tar.gz這個版本基本夠用。原先的libtiff庫檔案安裝在/usr/include/usr/lib下,沒有許可權進行刪除,這裡安裝到新路徑下/home/fangjin/opt

安裝

[fangjin@com download]$ tar zxvf tiff-3.8.2.tar.gz
[fangjin@com download]$ cd tiff-3.8.2
[fangjin@com tiff-3.8.2]$ ./configure --prefix=/home/fangjin/opt/
[fangjin@com tiff-3.8.2]$ make

會報錯

../libtool: eval: line 6176: unexpected EOF while looking for matching `''
../libtool: eval: line 6177: syntax error: unexpected end of file
make[2]: *** [libtiffxx.la] Error 1
make[2]: Leaving directory `/home/data/fangjin/download/tiff-3.8.2/libtiff'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/data/fangjin/download/tiff-3.8.2/libtiff'
make: *** [all-recursive] Error 1

按照提示修改libtool檔案,將6176行中的的”$cmd”變數上雙引號,換成單引號即可。
重新

[fangjin@com tiff-3.8.2]$ make; make install

把tiff庫的路徑加入到Makefile.config和PATH路徑中(已經有則不需要重複新增),重新生成caffe和pycaffe。
再嘗試python介面,應該可以提示執行成功了。

相關推薦

caffe原始碼研究第一安裝(): jumbo

下載caffe 下載caffe的原始碼 [fangjin@caffe ~]$ git clone https://github.com/BVLC/caffe.git 安裝依賴庫 caffe涉及到一些依賴庫,先使用jumbo安裝。 [fan

無線網絡第一簡介

架構 comm 負責 能量 參與 AC cal 傳感 research 課時1 :課程簡介及計算機網絡發展歷史   電話網絡   分時系統—無處理能力的終端連接中心計算機,無專用的網絡設備   ARPANET—   Internet—   高速網絡—光纖技術   泛在網絡

雅思寫作第一寫作基礎

【雅思寫作】第一章:寫作基礎 標籤(空格分隔):【雅思寫作】 第一章:寫作基礎 文章目錄 第一章:寫作基礎 1.1句子翻譯練習 1.1.1 簡單的主謂賓結構 1.1.2 主語 + 及物動詞 + 賓語 +

SpringCloud Greenwich版本第一服務註冊中心(eureka)

一、SpringCloud版本 本文介紹的Springboot版本為2.1.1.RELEASE,SpringCloud版本為Greenwich.RC1,JDK版本為1.8,整合環境為IntelliJ IDEA 二、Eureka服務端介紹 要在專案中包含Eureka伺服器,請使用組

caffe原始碼研究caffe筆記

結構分析 caffe分為 1. Solver 2. Net 3. Layer 4. Blob 四層結構. Solver : 負責模型的求解 Net : 負責定義網路的結構 Layer: 負責每一層網路功能的具體實現 Blob : 則定義資料,

Weex學習第一環境搭建

下載鏈接 spa 鏈接 java androi tps http nload 第一章 1、安裝Node.js //查看版本node -vnpm -v 2、安裝Java 下載鏈接:https://www.java.com/zh_CN/ 3、安裝Git 下載鏈接:https

大學物理第一質點運動學

【大學物理】第一章:質點運動學 標籤(空格分隔):【大學物理】 第一章:質點運動學 第一章:質點運動學 1.1 基本內容 1.1 基本內容 質點和參考系 質

考研寫作第一小作文

【考研寫作】 標籤(空格分隔):【考研寫作】 第一章:小作文 文章目錄 第一章:小作文 2007年 建議信 2009年 建議信 2012年 建議信 2014年 建議信 2011年

機器學習筆記第二模型評估與選擇

機器學習 ini ppi 第二章 err cap ner rate rac 2.1 經驗誤差與過擬合 1. error rate/accuracy 2. error: training error/empirical error, generalization error

Python自學路第一python應用

flask ipython ack ott tac font 應用 網絡 gui python應用:   web開發:Django、Pyramid、Tornado、Bottle、Flask、webPy   網絡編程:Twisted、Requests、Scrapy、Param

計算機組成原理第一 計算機系統概論

1.1 計算機系統簡介 1.1.1 計算機的軟硬體概念 計算機組成概覽: 1.1.2 計算機系統的層次結構 物理角度: 程式設計師角度: 系統複雜性管理的方法之一:抽象 區別兩點: 計算

演算法視覺化第一 Java GUI程式設計基礎

1.1 使用JFrame import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { EventQu

資料庫系統概念第二關係模型介紹

關係資料庫的結構 關係(relation) 關係資料庫由表(table)的集合構成。 在關係模型的術語中,關係就用來指代表。 元組(tuple) 指代表中的一行,表示一組值的序列(或列

計算機組成原理第一 馮諾依曼計算機特點

一、主要內容: 組成原理知識點彙總與複習 授課:sunnyACT張思鵬(中城投絲路@180科技)   二、學習參考: sunnyACT張思鵬(中城投絲路@180科技)xmind使用參考: 必備工具|三分鐘帶你認識神祕的思維導圖【10分鐘掌

SpringCloud Greenwich版本第二服務提供者(eureka)

一、SpringCloud版本 本文介紹的Springboot版本為2.1.1.RELEASE,SpringCloud版本為Greenwich.RC1,JDK版本為1.8,整合環境為IntelliJ IDEA 二、Eureka客戶端介紹 服務發現是基於微服務架構的關鍵原則之一。嘗

大話資料結構第一總結——資料結構緒論

程式設計 = 資料結構 + 演算法   前言 邊讀書邊做筆記,才能更好的學習。 以下是我根據《大話資料結構》做的總結,在加深自己印象的同時,希望可以幫助各位更好地瞭解資料結構。   1、基本概念和術語 資料: 是描述客觀事物的符號,是計算機

軟體工程導論 第一 軟體工程學概述

引言:文章是按照markdown格式寫的,可直接複製到markdown上進行檢視 #第一章 軟體工程學概述# ------------------ ###1.1 軟體危機###     軟體危機的介紹:軟體危機是指在計算機開發和維護過程中所遇到的一系列嚴重

“全棧2019”Java第一安裝JDK11(Mac)

安裝 開發環境 ref 進步 技術分享 mac com 第一章 計劃 難度 初級 學習時間 10分鐘 適合人群 零基礎 開發語言 Java 開發環境 JDK v11 文章原文鏈接 “全棧2019”Java第一章:安裝JDK11(Mac) 下一章 “全棧2019”Java

第一安裝部署伺服器nginx

vue的專案是部署到ngnix伺服器上的 ngnix安裝時不能有中文路徑 首先要去nginx官網下下載nginx: 下載地址:https://nginx.org/en/download.html 下載下來會是一個解壓包,解壓到你想放的資料夾下 執

讀書筆記《csapp》第一計算機系統漫遊

第一章 計算機系統漫遊 這是跟著劉欣大佬讀csapp的作業。 這裡不是純粹一本書的讀書筆記,只是摘錄了我感興趣的部分,結合之前的書和感想。儘量讓這個作業有那麼點意義。 幾個重要概念 1.抽象 還記得大