LoginSignup
70
68

More than 5 years have passed since last update.

deepdream を動かしてみる

Last updated at Posted at 2015-07-03

Google の deep learning のきもいやつdeepdream を動かしてみる。
環境は OS X 10.10.4, iMac (27-inch, Late 2012), NVIDIA GeForce GTX 675MX 1024 MB。

まず Caffe インストール

ここ参考に。
http://caffe.berkeleyvision.org/installation.html
http://caffe.berkeleyvision.org/install_osx.html

CUDA
最新版の 7 が strongly suggested
https://developer.nvidia.com/cuda-downloads

Caffe は Anaconda の Python がいいつってるので、そっちいれる。
http://continuum.io/downloads
(brew のは uninstall か unlink する、はいってるなら)

ほか、必要なパッケージを brew でいれる。

brew install --fresh -vd snappy leveldb gflags glog szip lmdb
brew tap homebrew/science
# Anaconda の Python なら hdf5 はインストール済みっぽい
brew install hdf5 opencv
brew install --build-from-source --with-python --fresh -vd protobuf

boost と boost-python は最新版の 1.58.0 では、caffe コンパイルエラーになっちゃうのでいっこまえの 1.57.0 を入れる。↓いれかた
Itinerant Bioinformaticist: Caffe incompatible with Boost 1.58.0

brew uninstall boost boost-python
cd $(brew --prefix)
cd Library/Formula
curl -O https://raw.githubusercontent.com/Homebrew/homebrew/6fd6a9b6b2f56139a44dd689d30b7168ac13effb/Library/Formula/boost.rb
curl -O https://raw.githubusercontent.com/Homebrew/homebrew/3141234b3473717e87f3958d4916fe0ada0baba9/Library/Formula/boost-python.rb
brew install boost boost-python

ようやく caffe 本体

git clone --depth 1 git@github.com:BVLC/caffe.git
cd caffe
pip install -r python/requirements.txt
cp Makefile.config.example Makefile.config

Makefile.config は Anaconda 使うようのに書き換える。(Anaconda つかうなら)

Makefile.config
# 一部抜粋
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
#       /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/anaconda
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python2.7 \
        $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib

コンパイル。

make all -j4
make test
# CUDA ライブラリあるとこを適当に指定
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:~/anaconda/lib
make runtest
# ↓ ドキュメントにないけど Python から caffe つかうには必要、たぶん
make pycaffe

caffemodel ファイルだけ別途配布なので↓から。
https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet
caffemodel_url ってかいてあるやつ。おとして caffe/models/bvlc_googlenet に入れる。

iPython Notebook

deepdream のサンプルコードは iPython Notebook 形式で配布されてるので、iPython Notebook をいれる。(Anaconda にはもとからはいってるっぽい)

pip install "ipython[notebook]"

deepdream 動かしてみる

入ってないならいれる。(Anaconda にはもとからはいってるっぽい)

pip install numpy scipy pil

deepdream おとす。

git clone --depth 1 git@github.com:google/deepdream.git

iPython Notebook 立ち上げる

cd deepdream-master
# CUDA まわりのライブラリがみつからない場合↓設定
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:~/anaconda/lib
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
ipython notebook

ブラウザ勝手にひらくので dream.ipynb 選ぶ。あとはツールバーの再生ボタンぽちぽちやれば上から python スクリプト実行されてく。In[3] のとこの 1 行目、caffemodel ファイルがあるパスに書き換えること。

model_path = '/some/path/caffe/models/bvlc_googlenet/' # substitute your path here

エラーでなければ In[7] のスクリプト実行すれば例のキモいやつできあがる。
inception.jpeg

iPython Notebook つかわずに

ふつうの Python からやるには、dream.ipynb にはいってたスクリプトをコピペでいっこのファイルにまとめて、あとはてきとうに。こんなかんじ。
https://gist.github.com/Saqoosha/50b83b10f8dfc1cc0a3b
↑ので↓これできる。
https://vimeo.com/132849531 (連番出力したあと After Effects でタイムリマップかけてる)
これはデフォルトの bvlc_googlenet じゃなくって finetune_flickr_style をつかってる。end パラメタも変更必要。pool5 つかってる。これかえるといろいろ絵もかわる。end に何が指定できるかは net.blobs.keys() を呼べばリストがでてくるのでその中から。

70
68
2

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
70
68