[PYTHON] I installed Caffe so that I can do deep learning on MAC OS X El Capitan

Introduction

It is a story that an amateur wants to use Deep Learning and installed Caffe (CPU_only mode). I couldn't find any information about Mac OS 10.11, so I'll make a note of it as a memorandum.

(1) Installation environment (2) Installation preparation (3) Installation procedure (4) Errors actually encountered and how to deal with them (5) Referenced site

They are listed in the above order. Basically, I refer to OS X Installation on Caffe's official website.

(1) Installation environment

OS:Mac OSX El Capitan (10.11.6) CPU: Core i5 1.6GHz GPU: Intel HD Graphics 6000 1536 MB Memory: 8GB

(2) Installation preparation

I have installed Home-brew, Pyenv and Anaconda.

■ Home-brew Install according to home-brew official website.

■ Pyenv and Anaconda

I referred to the following article. Note from installing Homebrew to building an Anaconda environment for Python with pyenv

(3) Installation procedure

Enter the following command in the terminal according to the official Mac installation method of Caffe.

3-1: Dependency installation

■ General dependencies The homebrew repository (homebrew / science) is added and each formula (leveldb, gflags, glog, ship, lmdv, hdf5, opencv, openblas) is installed.

brew install -vd snappy leveldb gflags glog ship lmdv
# need the homebrew science source for OpenCV and hdf5
brew tap homebrew/science
brew install hdf5 opencv

If I didn't put openblas, I got an error at the end, so I think it's a good idea to put it here.

brew install openblas

■ Remaining dependencies with python

Install each formula (protobuf, boost, boost-python).

# with Python pycaffe needs dependencies built from source
brew install --build-from-source --with-python -vd protobuf
brew install --build-from-source -vd boost boost-python

3-2: Addition of definition in formula

Launch an editor in the terminal to edit each formula (snappy leveldb protobuf gflags glog szip boost boost-python lmdb opencv).

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

Find the def install section and add the following items:

      # 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++"

3-3: Download caffe

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

3-4: Creating and editing Makefile.config file

■ Creating Makefile.config

Make Makefile.config by copying Makefile.config.sample.

cp Makefile.config.example Makefile.config

■ Change to CPU_only mode

To put it in CPU_only mode, on line 8 of the Makefile.config file,

  CPU_ONLY := 1  #Uncommented to enter CPU mode

■ Change openblas path

Find the location of your openblas path and fill it out. The following is an example.

  BLAS_INCLUDE := /usr/local/cellar/openblas/0.2.18_2/include
  BLAS_LIB := /usr/local/cellar/openblas/0.2.18_2/lib

■ Change Python path

Open the Makefile.config file and pass it through the Python path. Change usr / lib / in PYTHON_INCLUDE to usr / local / lib. (Confirmation: I have Anaconda installed above, but I have a path to Python, is that okay?)

PYTHON_INCLUDE := /usr/include/python2.7 \
		/usr/local/lib/python2.7/dist-packages/numpy/core/include

3-5: Build and test

Go back to the Caffe hierarchy and build with make all. If you get an error, fix that part. If there are no errors, run up to make run test.

cd caffe
make all
make test
make runtest

If there is no problem, it will be "PASSED" as shown below.

[ RUN      ] MVNLayerTest/1.TestForwardMeanOnly
[       OK ] MVNLayerTest/1.TestForwardMeanOnly (0 ms)
[----------] 6 tests from MVNLayerTest/1 (714 ms total)

[----------] Global test environment tear-down
[==========] 1096 tests from 150 test cases ran. (62049 ms total)
[  PASSED  ] 1096 tests.

(4) Errors actually encountered and how to deal with them

■ Error due to incorrect Python path

·Error message

/bin/sh: /usr/local/cuda/bin/nvcc: No such file or directory
awk: syntax error at source line 1
 context is
	{exit  >>>  < <<<  7.0;}
awk: illegal statement at source line 1

·Workaround Open the Makefile.config file and pass it through the Python path. Change usr / lib / in PYTHON_INCLUDE to usr / local / lib. (Confirmation: I have Anaconda installed above, but I have a path to Python, is that okay?)

PYTHON_INCLUDE := /usr/include/python2.7 \
		/usr/local/lib/python2.7/dist-packages/numpy/core/include

■ Error due to lack of protobuf

·Error message

PROTOC src/caffe/proto/caffe.proto
make: protoc: No such file or directory
make: *** [.build_release/src/caffe/proto/caffe.pb.cc] Error 1

·Workaround

brew install --build-from-source --with-python -vd protobuf

■ Error due to lack of openblas

·Error message

XX src/caffe/blob.cpp
In file included from src/caffe/blob.cpp:7:
In file included from ./include/caffe/util/math_functions.hpp:11:
./include/caffe/util/mkl_alternate.hpp:14:10: fatal error: 'cblas.h' file not found
#include <cblas.h>
         ^
1 error generated.
make: *** [.build_release/src/caffe/blob.o] Error 1

·Workaround

brew install openblas

■ Error due to incorrect openblas path

·Error message

CXX src/caffe/blob.cpp
In file included from src/caffe/blob.cpp:7:
In file included from ./include/caffe/util/math_functions.hpp:11:
./include/caffe/util/mkl_alternate.hpp:14:10: fatal error: 'cblas.h' file not found
#include <cblas.h>
         ^
1 error generated.
make: *** [.build_release/src/caffe/blob.o] Error 1

·Workaround Check the location of your openblas path and fill it in (example below)

  BLAS_INCLUDE := /usr/local/cellar/openblas/0.2.18_2/include
  BLAS_LIB := /usr/local/cellar/openblas/0.2.18_2/lib

(5) Referenced site

home-brew official website Note from installing Homebrew to building an Anaconda environment for Python with pyenv Caffe Official Website | OS X Installation Amateur tried Deep Learning using Caffe (Introduction) Installed Caffe on OS X 10.10 protoc: No such file or directory when running make all on MAC


Recommended Posts

I installed Caffe so that I can do deep learning on MAC OS X El Capitan
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
I learned MNIST with Caffe and tried to draw it (MAC OS X El Capitan)
Install matplotlib on OS X El Capitan
Installed aws-cli On Mac OS X Lion
Installing TensorFlow 0.11.0rc2 on OS X El Capitan (10.11.6)
Deep learning course that can be crushed on site
When import tkinter is not possible on Mac OS X 10.11.3 (El Capitan) + pyenv + Python 3.5.1.
Memo on Mac OS X
Install Sphinx on Mac OS X
Install mitmproxy on Mac OS X
I installed Linux on my Mac
Install pgmagick on Mac OS X 10.9
I installed Kivy on a Mac environment
Run NASA CEA on Mac OS X
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
R environment construction with Jupyter (formerly IPython notebook) (on OS X El Capitan 10.11.3)
Run Qiita API v2 Python wrapper in Python3 environment (Mac OS X 10.11 (El Capitan))
Run Zookeeper x python (kazoo) on Mac OS X
Shpinx (Python documentation builder) on Mac OS X
I installed Chainer, a framework for deep learning
I tried to build an environment for machine learning with Python (Mac OS X)