[PYTHON] Steps to get Caffe into Mac OS X 10.10 in CPU Mode

Introduction

Basically follow the original tutorial. However, there is a part that is a little thoughtful, so I will write it without omitting that part in this procedure manual.

** Note that it is assumed that anaconda-2.1.0 is included in pyenv. ** **

I strongly recommend that you can use anaconda even at the head family, so I will obediently follow it. If you haven't done so already, please refer to here and install anaconda-2.1.0 first.

Execution date

2015-08-17。

environment

The procedure for inserting brew and pyenv is here.

Rough flow

  1. Install the required packages for Caffe with brew
  2. Clone caffe repo and rewrite the configuration file for building
  3. Build
  4. Operation test

1. Install the required packages for Caffe with brew

What to put is snappy leveldb gflags glog szip lmdb protobuf boost boost-python openblas opencv hdf5 Except for the last two, it's easy to enter. The last two can only be done after editing the settings a little.

You don't have to add OpenBLAS, but it seems to be a little faster in CPU mode, so I'll put it in quickly. Note that you will have to change one line of caffe's build settings later.

1-1. snappy leveldb gflags glog szip lmdb protobuf boost boost-python openblas

First of all, the simple one.

$ brew install -vd snappy leveldb gflags glog szip lmdb
$ brew install --build-from-source --with-python --fresh -vd protobuf
$ brew install --build-from-source --fresh -vd boost boost-python
$ brew tap homebrew/science # need the homebrew science source for OpenCV and hdf5
$ brew install openblas

1-2. opencv

$ brew edit opencv

So, edit -DPYTHON_LIBRARY and -DPYTHON_INCLUDE_DIR as follows.

/usr/local/Library/Taps/homebrew/homebrew-science/opencv.rb#L93-About 94


...

  args << "-DPYTHON_LIBRARY=#{py_lib}/libpython2.7.#{dylib}"
  args << "-DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7"

...

Fully

$ brew install opencv

1-3. hdf5 It seems that brew install was fine as it was until a while ago, but the latest version hdf5-1.8.14 is not the version used by caffe, and an error occurs when trying to use caffe later. So edit it to install hdf5-1.8.13.

$ brew edit hdf5

So, edit the part in the first few lines as follows.

/usr/local/Library/Taps/homebrew/homebrew-science/opencv.rb


  url "http://www.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8.13/src/hdf5-1.8.13.tar.bz2"  
  sha1 "712955025f03db808f000d8f4976b8df0c0d37b5"

Fully

$ brew install hdf5

This completes the package installation. It's easy up to this point.

2. Clone caffe repo and rewrite the configuration file for building

2-1. Clone caffe

$ git clone https://github.com/BVLC/caffe.git

So, because it is convenient and will be used later, define the following environment variables.

.zshrc


...
export CAFFE_ROOT=/path/to/caffe
...

2-2. Copy the build configuration file

$ cd $CAFFE_ROOT
$ cp Makefile.config.example Makefile.config

2-3. Rewrite the build configuration file according to your environment

Rewrite as follows. (The environment variable PYENV_ROOT should have been written when setting pyenv.)

## 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++
CUSTOM_CXX := /usr/bin/clang++

# 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 := 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)!
# 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 := /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 := $(PYENV_ROOT)/versions/anaconda-2.1.0
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 := /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

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/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 ?= @

3. Build Caffe

3-1. Test & Build

Finally build. -j is the number of parallel processes, so set it to your liking. You don't have to. It's late.

$ cd $CAFFE_ROOT
$ make clean
$ make all -j4
$ make test -j4
$ make runtest

As a result of execution, if there is no particular error, k

3-2. Insert PyCaffe.

Also build caffe wrapped in python so that caffe can be called in python.

$ cd $CAFFE_ROOT/python
$ make pycaffe
$ make distribute

As a result of executing this, if there is no particular error, k

3-3. Setting environment variables for Caffe

Set the following. If DYLD_FALLBACK_LIBRARY_PATH is not set, import caffe will fail.

# Caffe
export ANACONDA_HOME=$PYENV_ROOT/versions/anaconda-2.1.0
export PYTHONPATH=$CAFFE_ROOT/python:$PYTHONPATH
export DYLD_FALLBACK_LIBRARY_PATH=$ANACONDA_HOME/lib:/usr/local/lib:/usr/lib

4. Operation test

Launch the python interpreter and test the operation.

$ python 
>>> import caffe
>>>

If you don't get angry, you're successful.

If you get an error saying that google.protobuf does not exist, try inserting protobuf from python with the following command.

$ pip install protobuf

Recommended Posts

Steps to get Caffe into Mac OS X 10.10 in CPU Mode
Notes on installing Caffe in CPU mode on Mac OSX 10.11 (El Captain)
Steps to get KeePassX key on OS X with one command line
Introduction to Graph Database Neo4j in Python for Beginners (for Mac OS X)
Preparing to use aws cli on Mac OS X
Very easy to install SciPy on Mac OS X
How to install caffe on OS X with macports
Steps to use the AWS command line interface (Python / awscli) on Mac OS X
I learned MNIST with Caffe and tried to draw it (MAC OS X El Capitan)
How to install Theano on Mac OS X with homebrew
Memo on Mac OS X
Get started with the Python framework Django on Mac OS X
How to get the printer driver for Oki Mac into Linux
The easiest way to get started in Slack socket mode (Go)
How to install Theano on Mac OS X 10.10 (using pyenv, anaconda)
Install Caffe on Ubuntu 14.04 (CPU mode)
Steps to install matplotlib on Mac
Steps to install Ubuntu in VirtualBox
Install Sphinx on Mac OS X
Installation of scikit-learn (Mac OS X)
Install mitmproxy on Mac OS X
Steps to install python3 on mac
Install pgmagick on Mac OS X 10.9