Kerasインストール

# 概要

- Macに以下をインストールする
    - TensorFlow 1.8.0
    - Keras 2.2.0

# 準備

- Mac OS X 10.11 El Capitan
    - [TensorFlow公式](https://www.tensorflow.org/install/install_mac)では10.12.6 Sierra以降サポートとなっているが、筆者都合でMacOSをupgradeしたくないので10.11のまま使用してみた。(→なぜかできてしまった。)読者の方には10.12.6以降を推奨する。
- python3.5.2 anaconda3-4.2.0 (anacondaのインストールは[こちら](https://qiita.com/berry-clione/items/24fb5d97e4c458c0fc28))
- pip 10.0.1
    - pip 9.0.1ではインストール時にエラーが出た
- oh-my-zsh

---

# インストール
### TensorFlowインストール

> KerasはTensorFlowなどのdeep learning系ライブラリをuser-friendlyにしたものらしい。そのため、TensorFlowなどのライブラリに依存する。[Keras公式](https://keras.io/ja/)ではTensorFlowのインストールを推奨している。

> deep learning系ライブラリを__anaconda__でインストールすると[カクカクシカジカ](https://qiita.com/berry-clione/items/619d64d646d0385a7e01)により処理が遅くなる気がするので、__pip__でインストールすることにする。

> [TensorFlow公式](https://www.tensorflow.org/install/install_mac)ではMac OS 10.12.6以降サポートとあるが、筆者はMac製品のOSをupgradeしたくないので無視。(なぜかinstallできてしまう。)

- pip 9.xだとtensorflowのインストールでエラー吐く

```zsh
$ pip3 install --upgrade tensorflow

Collecting tensorflow
Downloading https://files.pythonhosted.org/packages/82/89/a7934203bccbcab5d0298b4b20fc7ac8ea5fe72cfe5ed2dc7e9a14559c7c/tensorflow-1.8.0
-cp35-cp35m-macosx_10_11_x86_64.whl (47.1MB)
    100% |████████████████████████████████| 47.1MB 11kB/s
Collecting grpcio>=1.8.6 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/76/27/b03ec8fc96745cde68d6ec29115f9a444945a6acc45209c5772378cc4d66/grpcio-1.12.1-cp35-cp35m-macosx_10_7_intel.whl (1.9MB)
    100% |████████████████████████████████| 1.9MB 136kB/s
Collecting protobuf>=3.4.0 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/19/ff/6875f83b71710e6f1a2fe744676d147b1f1d500d0b65b46f683f0e7fe5b4/protobuf-3.6.0-cp35-cp35m-
macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (1.2MB)
    100% |████████████████████████████████| 1.2MB 270kB/s
Collecting absl-py>=0.1.6 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/57/8d/6664518f9b6ced0aa41cf50b989740909261d4c212557400c48e5cda0804/absl-py-0.2.2.tar.gz (82kB)
    100% |████████████████████████████████| 92kB 455kB/s
Collecting tensorboard<1.9.0,>=1.8.0 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/59/a6/0ae6092b7542cfedba6b2a1c9b8dceaf278238c39484f3ba03b03f07803c/tensorboard-1.8.0-py3-none-any.whl (3.1MB)
    100% |████████████████████████████████| 3.1MB 134kB/s
Collecting astor>=0.6.0 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/b2/91/cc9805f1ff7b49f620136b3a7ca26f6a1be2ed424606804b0fbcf499f712/astor-0.6.2-py2.
py3-none-any.whl
Collecting gast>=0.2.0 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/5c/78/ff794fcae2ce8aa6323e789d1f8b3b7765f601e7702726f430e814822b96/gast-0.2.0.tar.g
z
Collecting wheel>=0.26 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/81/30/e935244ca6165187ae8be876b6316ae201b71485538ffac1d718843025a9/wheel-0.31.1-py2
.py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 783kB/s
Collecting numpy>=1.13.3 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/50/9d/769515cb321d3ab39af31ec14ce26595f929f02078e7d0a54c0410992e5c/numpy-1.14.5-cp3
5-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.7MB)
    100% |████████████████████████████████| 4.7MB 97kB/s
Collecting termcolor>=1.1.0 (from tensorflow)
  Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.
tar.gz
Requirement already up-to-date: six>=1.10.0 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from tensorflow)
Collecting setuptools (from protobuf>=3.4.0->tensorflow)
  Downloading https://files.pythonhosted.org/packages/7f/e1/820d941153923aac1d49d7fc37e17b6e73bfbd2904959fffbad77900cf92/setuptools-39.2.
0-py2.py3-none-any.whl (567kB)
    100% |████████████████████████████████| 573kB 287kB/s
Collecting bleach==1.5.0 (from tensorboard<1.9.0,>=1.8.0->tensorflow)
  Downloading https://files.pythonhosted.org/packages/33/70/86c5fec937ea4964184d4d6c4f0b9551564f821e1c3575907639036d9b90/bleach-1.5.0-py2
.py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorboard<1.9.0,>=1.8.0->tensorflow)
  Downloading https://files.pythonhosted.org/packages/ae/ae/bcb60402c60932b32dfaf19bb53870b29eda2cd17551ba5639219fb5ebf9/html5lib-0.99999
99.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 261kB/s
Collecting markdown>=2.6.8 (from tensorboard<1.9.0,>=1.8.0->tensorflow)
  Downloading https://files.pythonhosted.org/packages/6d/7d/488b90f470b96531a3f5788cf12a93332f543dbab13c423a5e7ce96a0493/Markdown-2.6.11-
py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 284kB/s
Collecting werkzeug>=0.11.10 (from tensorboard<1.9.0,>=1.8.0->tensorflow)
  Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-
py2.py3-none-any.whl (322kB)
    100% |████████████████████████████████| 327kB 261kB/s
Building wheels for collected packages: absl-py, gast, termcolor, html5lib
  Running setup.py bdist_wheel for absl-py ... done
  Stored in directory: /Users/hogehoge/Library/Caches/pip/wheels/a0/f8/e9/1933dbb3447ea6ef57062fd5461cb118deb8c2ed074e8344bf
  Running setup.py bdist_wheel for gast ... done
  Stored in directory: /Users/hogehoge/Library/Caches/pip/wheels/9a/1f/0e/3cde98113222b853e98fc0a8e9924480a3e25f1b4008cedb4f
  Running setup.py bdist_wheel for termcolor ... done
  Stored in directory: /Users/hogehoge/Library/Caches/pip/wheels/7c/06/54/bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6
  Running setup.py bdist_wheel for html5lib ... done
  Stored in directory: /Users/hogehoge/Library/Caches/pip/wheels/50/ae/f9/d2b189788efcf61d1ee0e36045476735c838898eef1cad6e29
Successfully built absl-py gast termcolor html5lib
Installing collected packages: grpcio, setuptools, protobuf, absl-py, html5lib, bleach, wheel, numpy, markdown, werkzeug, tensorboard, as
tor, gast, termcolor, tensorflow
  Found existing installation: setuptools 27.2.0
Cannot remove entries from nonexistent file /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/easy-install.pth
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
```
- pipを9.xから10.xにupgradeする

```zsh
$ pip install --upgrade pip
```

- 改めてtensorflowを__pip__ってみる

```zsh
$ pip3 install --upgrade tensorflow

Collecting tensorflow
  Using cached https://files.pythonhosted.org/packages/82/89/a7934203bccbcab5d0298b4b20fc7ac8ea5fe72cfe5ed2dc7e9a14559c7c/tensorflow-1.8.
0-cp35-cp35m-macosx_10_11_x86_64.whl
Collecting tensorboard<1.9.0,>=1.8.0 (from tensorflow)
  Using cached https://files.pythonhosted.org/packages/59/a6/0ae6092b7542cfedba6b2a1c9b8dceaf278238c39484f3ba03b03f07803c/tensorboard-1.8
.0-py3-none-any.whl
Requirement not upgraded as not directly required: wheel>=0.26 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-package
s (from tensorflow) (0.29.0)
Collecting termcolor>=1.1.0 (from tensorflow)
Collecting gast>=0.2.0 (from tensorflow)
Requirement not upgraded as not directly required: numpy>=1.13.3 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packa
ges (from tensorflow) (1.13.3)
Collecting astor>=0.6.0 (from tensorflow)
  Using cached https://files.pythonhosted.org/packages/b2/91/cc9805f1ff7b49f620136b3a7ca26f6a1be2ed424606804b0fbcf499f712/astor-0.6.2-py2
.py3-none-any.whl
Requirement not upgraded as not directly required: grpcio>=1.8.6 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packa
ges (from tensorflow) (1.12.1)
Requirement not upgraded as not directly required: six>=1.10.0 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-package
s (from tensorflow) (1.11.0)
Collecting protobuf>=3.4.0 (from tensorflow)
  Using cached https://files.pythonhosted.org/packages/19/ff/6875f83b71710e6f1a2fe744676d147b1f1d500d0b65b46f683f0e7fe5b4/protobuf-3.6.0-
cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting absl-py>=0.1.6 (from tensorflow)
Collecting bleach==1.5.0 (from tensorboard<1.9.0,>=1.8.0->tensorflow)
  Using cached https://files.pythonhosted.org/packages/33/70/86c5fec937ea4964184d4d6c4f0b9551564f821e1c3575907639036d9b90/bleach-1.5.0-py
2.py3-none-any.whl
Collecting markdown>=2.6.8 (from tensorboard<1.9.0,>=1.8.0->tensorflow)
  Using cached https://files.pythonhosted.org/packages/6d/7d/488b90f470b96531a3f5788cf12a93332f543dbab13c423a5e7ce96a0493/Markdown-2.6.11
-py2.py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorboard<1.9.0,>=1.8.0->tensorflow)
Requirement not upgraded as not directly required: werkzeug>=0.11.10 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-p
ackages (from tensorboard<1.9.0,>=1.8.0->tensorflow) (0.11.11)
Requirement not upgraded as not directly required: setuptools in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages
/setuptools-27.2.0-py3.5.egg (from protobuf>=3.4.0->tensorflow) (27.2.0)
Installing collected packages: html5lib, bleach, markdown, protobuf, tensorboard, termcolor, gast, astor, absl-py, tensorflow
Successfully installed absl-py-0.2.2 astor-0.6.2 bleach-1.5.0 gast-0.2.0 html5lib-0.9999999 markdown-2.6.11 protobuf-3.6.0 tensorboard-1.
8.0 tensorflow-1.8.0 termcolor-1.1.0
```

- 上記でインストールしたtensorflowをanaconda上でimportできることを確認。
    - python3.5より3.6の方がいいっぽい。

```zsh
$ python
Python 3.5.2 |Anaconda custom (x86_64)| (default, Jul  2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
/Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.6 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
  return f(*args, **kwds)
>>>
```

---

### Kerasインストール

- __pip__でkerasをインストール

```zsh
$ pip3 install keras

Collecting keras
  Downloading https://files.pythonhosted.org/packages/68/12/4cabc5c01451eb3b413d19ea151f36e33026fc0efb932bf51bcaf54acbf5/Keras-2.2.0-py2.py3-none-any.whl (300kB)
    100% |████████████████████████████████| 307kB 1.2MB/s
Requirement already satisfied: numpy>=1.9.1 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from keras) (1.13.3)
Requirement already satisfied: scipy>=0.14 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from keras) (1.0.0)
Collecting keras-preprocessing==1.0.1 (from keras)
  Downloading https://files.pythonhosted.org/packages/f8/33/275506afe1d96b221f66f95adba94d1b73f6b6087cfb6132a5655b6fe338/Keras_Preprocessing-1.0.1-py2.py3-none-any.whl
Requirement already satisfied: six>=1.9.0 in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from keras) (1.11.0)
Requirement already satisfied: pyyaml in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from keras) (3.12)
Collecting keras-applications==1.0.2 (from keras)
  Downloading https://files.pythonhosted.org/packages/e2/60/c557075e586e968d7a9c314aa38c236b37cb3ee6b37e8d57152b1a5e0b47/Keras_Applications-1.0.2-py2.py3-none-any.whl (43kB)
    100% |████████████████████████████████| 51kB 1.7MB/s
Requirement already satisfied: h5py in /Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from keras) (2.7.0)
Installing collected packages: keras-preprocessing, keras-applications, keras
Successfully installed keras-2.2.0 keras-applications-1.0.2 keras-preprocessing-1.0.1
```

- 上記でインストールしたkerasをanaconda上でimportできることを確認。

```zsh
$ python
Python 3.5.2 |Anaconda custom (x86_64)| (default, Jul  2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
/Users/hogehoge/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.6 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
  return f(*args, **kwds)
>>>
```