[PYTHON] How to install Deep Learning framework Caffe on Mac in CPU mode

What is Caffe

It is an open source framework of Deep Learning, which has been a hot topic recently. It seems that a graduate student of the University of California, Berkeley started making it, it's amazing! The official homepage can be used with here, C ++, Python, and MATLAB, so you can choose the one you are good at. The update is very quick, so it seems interesting to follow the latest information. You can see a demo of image classification from here, so if you are interested, please try it.

Background that I decided to use Caffe

When I first started studying Deep Learning, it was a functional language and I usually use it, so I will do Deep Learning with Scala! I tried various things with a sweet idea, but at one point I realized that the number of image processing and machine learning libraries and frameworks was overwhelmingly small compared to languages such as Python. Rather than designing and building a Neural Network from scratch on my own, I wanted to move it anyway, so for the time being I decided to use Caffe, which has a relatively large amount of official documents and information in Japanese (a lot of sample code if you google). Come out). In addition, Caffe basically uses a GPU, but the CPU_ONLY option is also properly prepared, so I'm happy to handle it even for myself who does not have a powerful GPU with MacBook Pro 13inch!

However, there are rumors that it is difficult to build an environment for Caffe, and when I was looking for an easy way, I was grateful that Caffe was already built on Docker Hub containers I found? q = caffe & s = stars) and thought I did it, but most of the CPU mode containers were last updated months ago! I want to use the latest version anyway! So I decided to build it in my local environment. I want to use it in Python, so I'll do my best until I can do PyCaffe (ʻimport caffe` in Python).

Local environment

The road to building Caffe and importing it in Python

What you need to use Caffe

There are quite a few, but most of them can be installed at once if homebrew is included.

procedure

Python is already included in the Mac, but it seems that the version is old (it often goes wrong with build?), So I put it in a new one with homebrew, this time it is troublesome so I did not use pyenv (actually it is definitely better to use it) ).

brew install python

Next, put them all together with brew!

brew install --fresh -vd snappy leveldb gflags glog szip lmdb
brew tap homebrew/science
brew install hdf5 opencv

I want to do PyCaffe this time, so I'll put this in at once.

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

Next, clone the Caffe body from the repository and create Makefile.config from the prepared template.

git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config

Rewrite a part of Makefile. Since the BLAS path changes depending on the Mac version, change the path of the BLAS_INCLUDE part as follows. Since there are two places, rewrite both for the time being.

BLAS_INCLUDE ?= /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers/

Rewrite Makefile.config in several places. First uncomment the CPU_ONLY at the top, which will allow the CPU mode Caffe to be built. Next, change the path of PYTHON_INCLUDE and PYTHON_LIB to python as follows.

Makefile.config


CPU_ONLY := 1

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

PYTHON_LIB := /usr/local/Cellar/python/{python version}/Frameworks/Python.framework/Versions/2.7/lib/

Now that you're ready to build Caffe, let's build Caffe and run test. -j is the number of parallels when building, the more it is, the faster it will finish.

make clean
make all -j4
make test -j4
make runtest

If the runtest goes well, Caffe build is successful for the time being, then let's do this with PyCaffe. (It seems that make test fails depending on the version of Xcode, but if make all passes, you can proceed to the next step by not performing the test.)

Go to the python (caffe / python) folder in Caffe and pip the libraries you need for PyCaffe at once.

cd python
for li in $(cat requirements.txt); do sudo pip install $li; done

Next, build PyCaffe.

cd ../
make pycaffe
make distribute

Write the following PYTHONPATH in .bashrc or .zshrc.

export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH

This is the end of the installation procedure. After reading with source ~ / .bashrc etc., try ʻimport caffe` with the Python interpreter, if there is no error, it is successful! Thanks to homebrew, Caffe was easier to install than I expected, thank you very much. I'm just starting to play with Caffe, but once I understand how to use it, I'd like to write an article about it as well. Thank you.

Please let me know if you have any problems or improvements.

Recommended Posts

How to install Deep Learning framework Caffe on Mac in CPU mode
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
Install Caffe on Ubuntu 14.04 (CPU mode)
How to install OpenCV on Mac
How to install drobertadams / toggl-cli on Mac
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)
Install Caffe on Mac
How to install caffe on OS X with macports
How to install Theano on Mac OS X with homebrew
Steps to install matplotlib on Mac
How to install graph-tool on macOS
How to install VMware-Tools on Linux
How to install pycrypto on Windows
How to install PyPy on CentOS
How to install TensorFlow on CentOS 7
How to install Maven on CentOS
Steps to install python3 on mac
How to install Go on Ubuntu
How to install music 21 on windows
How to install OpenCV on Cloud9 and run it in Python
How to install Theano on Mac OS X 10.10 (using pyenv, anaconda)
How to install aws-session-manager-plugin on Manajro Linux
[Kivy] How to install Kivy on Windows [Python]
How to study deep learning G test
Accelerate Deep Learning on Raspberry Pi 4 CPU
How to erase Python 2.x on Mac.
How to install packages on Alpine Linux
How to install Anisble on Amazon Linux 2
How to install richzhang / colorization on Windows 10
How to install Apache (httpd) on CentOS7
How to install php7.4 on Linux (Ubuntu)
How to install Eclipse GlassFish 5.1.0 on CentOS 7
How to install Apache (httpd) on CentOS8
How to install NumPy on Raspberry Pi
How to install cx_Oracle on macOS Sierra
How to read environment variables from .env file in PyCharm (on Mac)
From nothing on Ubuntu 18.04 to setting up a Deep Learning environment in Tensor
[2020 version] How to install Python3 on AWS EC2
How to install OpenCV on Jetson Nano Python
How to install pip, numpy in Autodesk MAYA
How to quickly install h5py on Windows 10 [Unofficial]
How to install OpenGM on OSX with macports
How to install Camunda Modeler on Manjaro Linux
[Python] How to install OpenCV on Anaconda [Windows]
How to install / verify graphviz on anaconda / windows10
How to build a beautiful Python environment on a new Mac and install Jupter Notebook
Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV
Steps to install the latest Python on your Mac
Very easy to install SciPy on Mac OS X
How to install Linux on a 32bit UEFI PC
How to install Git GUI and Gitk on CentOS
A memorandum on how to use keras.preprocessing.image in Keras
How to install Google Test / Google Mock in Visual Studio 2019
How to allow nologin users to log in on Linux
How to perform learning in SageMaker without session timeout
How to exit when using Python in Terminal (Mac)
[AWS EC2] How to install Maven on Amazon Linux 2
How to install git on Linux such as EC2
Let's Deep Learning on Windows! (VS2013 + caffe + CUDA7.5 + cudnn5.1)
How to install poetry (error handling) in zsh environment