Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV

Introduction

Now that you've purchased a new MacBook Air, here's what you need to do to build the same environment as your old MacBook Air. It feels like I'm writing it down for myself so I don't have to worry about re-setup, but I'm hoping that sharing it might be good for someone else.

Main steps

Install in the following order.

  1. Xcode
  2. pyenv
  3. anaconda
  4. autoconf、automake、libtool、CMake
  5. OpenCV
  6. TensorFlow

Xcode

Launch the App Store and install Xcode.

スクリーンショット 2017-05-06 8.07.17.png

pyenv and anaconda

Create and execute the following script.

pyenv_installer.sh


git clone https://github.com/yyuu/pyenv.git ~/.pyenv

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

echo 'if [ -f ~/.bashrc ] ; then' >> ~/.bash_profile
echo '. ~/.bashrc' >> ~/.bash_profile
echo 'fi' >> ~/.bash_profile

source ~/.bashrc

pyenv install anaconda3-4.3.1
pyenv rehash
pyenv global anaconda3-4.3.1

echo 'export PATH="$PYENV_ROOT/versions/anaconda3-4.3.1/bin/:$PATH"' >> ~/.bashrc
source ~/.bashrc

XGBoost

Execute the following command.

python


conda install py-xgboost

Jupyter Notebook Settings

Download the IPA font.

Download IPA font

After downloading, create and execute the following script

jupyter_setup.sh


cd ~/Downloads
unzip IPAfont00303.zip
mv IPAfont00303/*.ttf ~/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/

jupyter notebook --generate-config
echo "c.NotebookApp.notebook_dir = u'notebook'" >> ~/.jupyter/jupyter_notebook_config.py

autoconf、automake、libtool、CMake

Create the following script.

tools_installer.sh


cd ~/Downloads
curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-latest.tar.gz
tar xzf autoconf-latest.tar.gz
cd autoconf-*
./configure --prefix=/usr/local
make && sudo make install

cd ~/Downloads
curl -fL http://ftpmirror.gnu.org/automake/automake-1.15.tar.gz | tar xzf -
cd automake-*
./configure --prefix=/usr/local
make && sudo make install

cd ~/Downloads
curl -fL http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz | tar xzf -
cd libtool-*
./configure --prefix=/usr/local
make && sudo make install

cd ~/Downloads
curl -OL https://cmake.org/files/v3.8/cmake-3.8.1-Darwin-x86_64.tar.gz
tar xzf cmake-3.8.1-Darwin-x86_64.tar.gz
cd cmame-*
cp CMake.app /Applications/

ln -s "/Applications/CMake.app/Contents/bin/ccmake" /usr/local/bin/ccmake
ln -s "/Applications/CMake.app/Contents/bin/cmake" /usr/local/bin/cmake
ln -s "/Applications/CMake.app/Contents/bin/cmake-gui" /usr/local/bin/cmake-gui
ln -s "/Applications/CMake.app/Contents/bin/cmakexbuild" /usr/local/bin/cmakexbuild
ln -s "/Applications/CMake.app/Contents/bin/cpack" /usr/local/bin/cpack
ln -s "/Applications/CMake.app/Contents/bin/ctest" /usr/local/bin/ctest

Execute with administrator privileges.

python


$ sudo sh tools_installer.sh

FFmpeg

Install FFmpeg to handle videos with OpenCV. It also installs pkg-config and yasm, which are required to install FFmpeg.

ffmpeg_installer.sh


cd ~/Downloads
curl -LO https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
tar xf pkg-config-0.29.2.tar.gz
cd pkg-config-0.29.2
./configure --with-internal-glib
make && make install

cd ~/Downloads
curl -LO http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure
make && make install

cd ~/Downloads
curl -LO http://www.ffmpegmac.net/resources/ffmpeg-3.3.1.tar.bz2
tar xf ffmpeg-3.3.1.tar.bz2
cd ffmpeg-3.3.1
./configure
make && make install

Execute with administrator privileges.

python


$ sudo sh ffmpeg_installer.sh

OpenCV

Create the following script.

opencv_installer.sh


cd ~/Downloads
git clone https://github.com/Itseez/opencv.git opencv
git clone https://github.com/Itseez/opencv_contrib.git opencv_contrib

cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_SHARED_LIBS=OFF \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D FFMPEG=ON \
-D PYTHON3_EXECUTABLE=/Users/[username]/.pyenv/versions/anaconda3-4.3.1/bin/python \
-D PYTHON3_INCLUDE_DIR=/Users/[username]/.pyenv/versions/anaconda3-4.3.1/include/python3.6m \
-D PYTHON3_LIBRARY=/Users/[username]/.pyenv/versions/anaconda3-4.3.1/lib/libpython3.6m.dylib \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/Users/[username]/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/Users/[username]/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib/modules \
..
make -j4
make install

Execute with administrator privileges.

python


$ sudo sh opencv_installer.sh

TensorFlow

Execute the following command.

python


$ pip install tensorflow

Operation check

Check if OpenCV and TensorFlow can be read. If there is no error, OK.

python


$ python
Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import tensorflow as tf
>>> exit()
$ 

It was done (^-^)

This will set up the operating environment for the code in the following article.

Try porting the code of the article "Determining which university a beautiful woman is likely to be in by CNN" to Python3 series

bonus

I also installed iRuby, which enables you to use Ruby with Jupyter Notebook. Create and execute the following script.

iruby_installer.sh


git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

echo 'export RBENV_ROOT="$HOME/.rbenv"' >> ~/.bashrc
echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

source ~/.bashrc

rbenv install 2.4.1
rbenv rehash
rbenv global 2.4.1

gem install rbczmq
gem install iruby
gem install erector
iruby notebook

スクリーンショット 2017-05-06 8.36.49.png

It was done (^-^)

Another bonus

In order for Safari to start automatically when jupyter notebook is started, add the following line to the configuration file.

python


c.NotebookApp.browser = u'/usr/bin/open -a Safari %s'

Recommended Posts

Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV
Steps to create a Python virtual environment with VS Code on Windows
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
How to quickly create a machine learning environment using Jupyter Notebook on macOS Sierra with anaconda
Build a machine learning environment on mac (pyenv, deeplearning, opencv)
Create a Python environment on Mac (2017/4)
How to quickly create a machine learning environment using Jupyter Notebook with UbuntuServer 16.04 LTS
Until you create a machine learning environment with Python on Windows 7 and run it
Deep Learning with Shogi AI on Mac and Google Colab
How to quickly create a machine learning environment using Jupyter Notebook with UbuntuServer 16.04 LTS with anaconda
Deep Learning with Shogi AI on Mac and Google Colab Chapter 11
Deep Learning with Shogi AI on Mac and Google Colab Chapters 1-6
Deep Learning with Shogi AI on Mac and Google Colab Chapter 8
Build a Python environment on your Mac with Anaconda and PyCharm
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7
Deep Learning with Shogi AI on Mac and Google Colab Chapter 10
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 5-7
Deep Learning with Shogi AI on Mac and Google Colab Chapter 9
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 1-2
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3 ~ 5
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 9
Deep Learning with Shogi AI on Mac and Google Colab Chapter 8 5-9
Deep Learning with Shogi AI on Mac and Google Colab Chapter 8 1-4
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 8
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 1-4
Create a python environment on your Mac
Quickly build a python environment for deep learning and data science (Windows)
Build Linux on a Windows environment. Steps to install Laradock and migrate
Learn with Shogi AI Deep Learning on Mac and Google Colab Use Google Colab
Deep Learning on Mac and Google Colab Words Learned with Shogi AI
[Note] How to create a Mac development environment
From nothing on Ubuntu 18.04 to setting up a Deep Learning environment in Tensor
How to quickly create a morphological analysis environment using Elasticsearch on macOS Sierra
Build a python environment to learn the theory and implementation of deep learning
Install and set Jupyter Notebook to create a study note creation environment [Mac]
Build a TensorFlow development environment on Amazon EC2 with command copy and paste
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Build a machine learning Python environment on Mac OS
How to build a development environment for TensorFlow (1.0.0) (Mac)
Create a decent shell and python environment on Windows
Create a machine learning environment from scratch with Winsows 10
How to build a beautiful Python environment on a new Mac and install Jupter Notebook
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
I tried to create a reinforcement learning environment for Othello with Open AI gym
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
Try deep learning with TensorFlow
Try to build a deep learning / neural network with scratch
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Build a machine learning scikit-learn environment with VirtualBox and Ubuntu
Create a web surveillance camera with Raspberry Pi and OpenCV
Building a Python environment on a Mac and using Jupyter lab
Create an environment for "Deep Learning from scratch" with Docker
Minimum Makefile and buildout.cfg to create an environment with buildout
(Now) Build a GPU Deep Learning environment with GeForce GTX 960
Create a GO development environment with [Mac OS Big Sur]
Machine learning environment settings based on Python 3 on Mac (coexistence with Python 2)
Create a virtual environment for python on mac [Very easy]