How to install caffe on OS X with macports



CaffeをAirMacにmacportsでインストール．
GPUはないのでCPUモードのみ．


ちなみに，anacondaとmacportsを同時に使うとライブラリ競合でpycaffeがつかえない．．． [CaffeをOSX10.11にmacports+anacondaでインストール](http://qiita.com/tttamaki/items/f61759e7b2c7ea8bafac)を参照．
ここではmacports単体でやる．


# macports

```sh:パッケージのインストール
sudo port install gcc5
sudo port install atlas +gcc5
sudo port install OpenBLAS +gcc5 +lapack
sudo port install py27-altgraph py27-appnope py27-backports py27-backports-ssl_match_hostname py27-bottleneck py27-cairo py27-certifi py27-config py27-cython py27-dateutil py27-decorator py27-functools32 py27-gflags py27-gnureadline py27-h5py py27-ipykernel py27-ipyparallel py27-ipython py27-ipython_genutils py27-ipywidgets py27-jinja2 py27-jsonschema py27-jupyter_client py27-jupyter_core py27-leveldb py27-macholib py27-markupsafe py27-matplotlib py27-mistune py27-modulegraph py27-nbconvert py27-nbformat py27-networkx py27-nose py27-notebook py27-numexpr py27-numpy py27-pandas py27-pandas py27-parsing py27-path py27-pexpect py27-pickleshare py27-Pillow py27-pkgconfig py27-powerline py27-protobuf py27-ptyprocess py27-py2app py27-pygments py27-pyobjc py27-pyobjc-cocoa py27-scientific py27-scikit-image py27-scikit-learn py27-scipy py27-setuptools py27-setuptools_scm py27-simplegeneric py27-six py27-tables py27-terminado py27-tkinter py27-tornado py27-traitlets py27-tz py27-vcversioner py27-yaml py27-zmq
sudo port install boost +python27
sudo port install opencv +python27 +eigen +tbb +vtk +contrib
sudo port install protobuf-c protobuf-cpp gflags google-glog hdf5 leveldb snappy lmdb 
```

caffe自体のコンパイルはclang++でやる（g++でやると、protbufやopencvなどのライブラリが見つからないと言われる。たぶんclangでコンパイルしているから？）

## port select

```bash:portのselectを使おう
sudo port select --set python python27
sudo port select --set python2 python27
sudo port select --set ipython2 py27-ipython
sudo port select --set nosetests nosetests27
```

以下、caffeをpythonで使うなら2.7で。（最近のバージョンなら3.3以降でもOKらしい。公式webに記載あり。試していない）


# caffe

```sh:caffeのダウンロード
git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
```

ここでMakefile.configを書き換え．

```diff:Makefile.configの変更点
--- Makefile.config.example	2016-07-07 15:42:18.000000000 +0900
+++ Makefile.config	2016-07-07 15:46:08.000000000 +0900
@@ -5,7 +5,7 @@
 # USE_CUDNN := 1
 
 # CPU-only switch (uncomment to build without GPU support).
-# CPU_ONLY := 1
+CPU_ONLY := 1
 
 # uncomment to disable IO dependencies and corresponding data layers
 # USE_OPENCV := 0
@@ -43,7 +43,7 @@
 # atlas for ATLAS (default)
 # mkl for MKL
 # open for OpenBlas
-BLAS := atlas
+BLAS := open
 # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
 # Leave commented to accept the defaults for your choice of BLAS
 # (which should work)!
@@ -61,14 +61,15 @@
 
 # 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 := /usr/include/python2.7 \
-		/usr/lib/python2.7/dist-packages/numpy/core/include
+#PYTHON_INCLUDE := /usr/include/python2.7 \
+#		/usr/lib/python2.7/dist-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 \
+ANACONDA_HOME := /opt/local/Library/Frameworks/Python.framework/Versions/2.7/
+PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
+		  $(ANACONDA_HOME)/include/python2.7 \
+		  $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
 
 # Uncomment to use Python 3 (default is Python 2)
 # PYTHON_LIBRARIES := boost_python3 python3.5m
@@ -76,19 +77,19 @@
 #                 /usr/lib/python3.5/dist-packages/numpy/core/include
 
 # We need to be able to find libpythonX.X.so or .dylib.
-PYTHON_LIB := /usr/lib
-# PYTHON_LIB := $(ANACONDA_HOME)/lib
+# PYTHON_LIB := /usr/lib
+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
+WITH_PYTHON_LAYER := 1
 
 # Whatever else you find you need goes here.
-INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
-LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
+INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /opt/local/include
+LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /opt/local/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
@@ -96,7 +97,7 @@
 
 # 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
+USE_PKG_CONFIG := 1
 
 # N.B. both build and distribute dirs are cleared on `make clean`
 BUILD_DIR := build
@@ -109,4 +110,4 @@
 TEST_GPUID := 0
 
 # enable pretty build (comment to see full commands)
-Q ?= @
+#Q ?= @
```

Mekfileも書きかえ．
- boostライブラリに-mtをつける．

```diff:Makefileの変更点
--- Makefile.org	2016-07-07 15:42:18.000000000 +0900
+++ Makefile	2016-07-07 15:58:24.000000000 +0900
@@ -178,7 +178,7 @@
 	LIBRARIES := cudart cublas curand
 endif
 
-LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
+LIBRARIES += glog gflags protobuf boost_system-mt boost_filesystem-mt m hdf5_hl hdf5
 
 # handle IO dependencies
 USE_LEVELDB ?= 1
@@ -199,7 +199,7 @@
 	endif
 		
 endif
-PYTHON_LIBRARIES ?= boost_python python2.7
+PYTHON_LIBRARIES ?= boost_python-mt python2.7
 WARNINGS := -Wall -Wno-sign-compare
 
 ##############################
```


それからmake．

```sh:ビルド
make all pycaffe
```
```sh:テスト
make all test runtest pytest
```

# インストール

ローカルにインストールする方法．

```bash:
make distribute

mkdir -p $HOME/local/usr/local/lib/python2.7/site-packages/

cp -rv distribute/include $HOME/local/usr/local/
cp -rv distribute/lib $HOME/local/usr/local/
cp -rv distribute/python/caffe $HOME/local/usr/local/lib/python2.7/site-packages/
```




# pycaffeを使うなら

```sh:.bash_profileなどに書く設定
# caffe
export LD_LIBRARY_PATH=$HOME/local/usr/local/lib/:$LD_LIBRARY_PATH
export PYTHONPATH=$HOME/local/usr/local/lib/python2.7/site-packages/:$PYTHONPATH
```

これで
`python2.7`
や
`jupyter-notebook-2.7`
でpycaffeが使える．

```python:インポート
import caffe
```
でエラーが出なければOK。