LoginSignup
7
7

More than 5 years have passed since last update.

macに機械学習環境を構築する(pyenv, deeplearning, opencv)

Last updated at Posted at 2017-01-07

macへのpython環境の構築手順メモ

1. X-codeをインストール

app store から。

2. homebrewをインストール

公式サイト: http://brew.sh/index_ja.html

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

おまけ wget

$ brew install wget

3. Gitをインストール

$ brew install git
$ git config --global user.name msrks
$ git config --global user.email <email>
# for check
$ git config —-global —-list

4. terminalにSolarizedのテーマを設定

$ mkdir config
$ cd config
$ git clone https://github.com/tomislav/osx-terminal.app-colors-solarized

terminal の環境設定メニューから、solarized-darkをデフォルトに設定

5. atom をインストール

https://atom.io から。

editer設定

- ペースト時にインデントしない
- font-size: 16
- ソフトラップ
- タブ幅: 4

package導入

- atom-runner : Ctrl+R でコード実行可能
- japanese-menu
- autocomplete-python
- python-indent
- file-icons
- minimap
- language-restructuredtext
- atom-beautify : pipでautopep8を入れること。

6. Pythonをインストール

systemのpython2をアップデート(2.7.13)。同時にpipも入る。

$ brew install python
$ pip install numpy scipy matplotlib seaborn pandas

pyenvをいれる

$ brew install pyenv-virtualenv
$ cd
$ touch .bash_profile
$ open .bash_profile

export PYENV_ROOT=${HOME}/.pyenv
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
fi

python 3.6.0 も入れておく

$ pyenv install -l
$ pyenv install 3.6.0

7. 研究用のpython2環境を構築

$ pyenv virtualenv system ML-2.7.13
$ pyenv global ML-2.7.13

data analysis

$ pip install jupyter
$ jupyter nbextension enable --py widgetsnbextension
$ pip install scikit-learn
$ pip install networkx
$ pip install statsmodels

web-development

$ pip install flask
$ pip install sphinx_rtd_theme mkdocs sphinx_rtd_theme

documentation

$ pip install sphinx mkdocs
$ pip install sphinx_rtd_theme sphinx_bootstrap_theme numpydoc

web-scraping

$ pip install beautifulsoup4 lxml

deeplearning (tensorflow, theano, chainer, keras, tflearn)

$ pip install tensorflow
$ pip install tflearn
$ pip install keras h5py
$ brew install graphviz
$ pip install pydot pydot-ng
$ pip install chainer

画像処理

$ pip install scikit-image Pillow

# opencv
$ brew install gcc
$ brew install cmake
$ brew install tesseract
$ brew tap homebrew/science
# opencv2
$ brew install opencv
$ ln -s /usr/local/Cellar/opencv/2.4.13.2/lib/python2.7/site-packages/cv2.so /Users/riki/.pyenv/versions/ML-2.7.13/lib/python2.7/site-packages/

Docker for mac

https://docs.docker.com/docker-for-mac/
ステータスバーのクジラアイコン > Preference... > Advancedタブにある「HTTP proxy settings:」を設定

OpenAI-gym

$ pip install gym
$ brew install swig
$ pip install gym[all]

OpenAI-Universe

$ brew install golang libjpeg-turbo
$ pip install numpy incremental
$ git clone https://github.com/openai/universe.git
$ cd universe
$ pip install -e .

Universe-Starter-Agent

$ brew install tmux htop
$ cd <workspace>
$ git clone https://github.com/openai/universe-starter-agent

wxpython [option]

$ brew install wxpython wxmac
#$ ln -s #/usr/local/Cellar/wxpython/3.0.2.0/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx #/Users/riki/.pyenv/versions/ML-2.7.13/lib/python2.7/site-packages/

opencv3 [option]

  • anaconda経由で入れる。
$ pyenv install anaconda3-4.1.1
$ cd <workspace with opencv3>
$ pyenv local anaconda3-4.1.1
$ conda install -c https://conda.anaconda.org/menpo opencv3
$ conda update hdf5

8. 動作確認

opencv

$ mkdir workspace
$ cd workspace
$ wget --no-check-certificate -O gorilla.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Male_silverback_Gorilla.JPG/236px-Male_silverback_Gorilla.JPG
$ touch test-cv2.py
$ atom test-cv2.py
test-cv2.py
import cv2
img = cv2.imread("gorilla.jpg")
img
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
$ python test-cv2.py

keras: training

最初うまくいかなかった。(下記リンクのエラー)
http://qiita.com/Kodaira_/items/1a3b801c7a5a41c9ce49
リンクを参考にmatplotlibrc修正

$ python -c "import matplotlib;print(matplotlib.matplotlib_fname())"
/Users/riki/.pyenv/versions/ML-2.7.13/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc
$ atom /Users/riki/.pyenv/versions/ML-2.7.13/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc

# line 38
- backend : macosx
+ backend : Tkagg
$ wget --no-check-certificate https://gist.githubusercontent.com/msrks/ea9ce906fe0c6a2947e7dec44291ec91/raw/ac8e24077e4127ae3dc392c3a4ca2235f3861aba/mnist_mlp.py
$ python mnist_mlp.py
$ open model_mlp.png
$ open log_acc.png
$ open log_loss.png

keras: prediction

$ wget --no-check-certificate -O gorilla.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Male_silverback_Gorilla.JPG/236px-Male_silverback_Gorilla.JPG
$ wget --no-check-certificate https://gist.githubusercontent.com/msrks/6422288f60f4f56a5d8fcbc95acf02dc/raw/e32966364ab08389490b5cadc260ba7220e42550/test_vgg16.py
$ python test_vgg16.py
$ open model_vgg16.png

9. 追記

メニューバーに IPアドレスを表示させる

$ brew cask install ip-in-menu-bar

treeコマンドを使えるようにする

$ brew install tree
$ tree -L <depth>

英数入力時の入力補完・スペル自動変換をOFFにしておくと便利

mongoDBのインストール

$ brew update
$ brew install mongodb

$ sudo mkdir /var/lib/mongodb
$ sudo touch /var/log/mongodb.log
$ sudo mongod --fork --dbpath /var/lib/mongodb --logpath /var/log/mongodb.log
$ brew services start mongodb
$ brew services list
7
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7