[PYTHON] Half-day course to try 5 famous deep learning libraries in the shortest distance (TensorFlow, Chainer, Caffe, DeepDream, style conversion)

I found and archived articles on topics such as artificial intelligence / machine learning / deep learning, saying "Let's study someday", but in the end, 2015 ended without doing anything, so just take a step. In order to take a step forward, I tried ** aside from the essential understanding, etc., and try it anyway **.

I tried 3 typical libraries / frameworks for machine learning and deep learning such as TensorFlow, Chainer, and Caffe, and 2 applications using deep learning that became a hot topic in 2015 (DeepDream, chainer-gogh). ..

(Result image of DeepDream test) </ span>

I wrote half a day in the title, but for example, TensorFlow will be finished in 10 minutes </ b> for environment construction only, and Chainer is about 5 seconds because it is a single command </ b>. .. I'm addicted to Caffe, but I've written the shortest steps that worked </ b>, so people in the same environment may feel better.

The recommended course is ** Skip Chaffe & DeepDream (Caffe required) and try TensorFlow, Chainer & style conversion **. I'm not addicted to building an environment, so I think it's easy to finish (although it takes time to execute). If you like me and say "I was curious but I haven't touched it at all", please try it during today's lunch break!

Environment tried:

  • Mac OS X 10.11.1 El Capitan --Xcode 7.2 installed

TensorFlow

A machine learning library made by Google, announced in November 2015. It is said to read "tensor flow". It is actually used in the company's service.

In addition to voice recognition and translation that you often hear, Google Photos subject recognition and face recognition, optimization of web search results, Gmail mail sorting, automatic reply text creation of the new mail software Inbox, and even YouTube and advertising business , It is a new core technology that supports most products.

The features of TensorFlow are that it can process anything if it can be expressed as a data flow graph, and if you feel like it, it is versatile enough to be handwritten by low-level operators, high performance used in actual Google products, running on both CPU and GPU, from a notebook PC. Scalability that allows you to move to a huge data center with the same code and deploy it to mobile terminals, efficiency that can handle everything from computer science research to actual products, ease of use that can be written in Python or C ++ with documentation and samples, etc.

  • http://japanese.engadget.com/2015/11/09/google-tensorflow/

Aim of openness

The license is Apache 2.0, which is also available for commercial use, and why did Google open up such a great thing that is also the core of its product? That is where I am concerned.

Open source TensorFlow will allow the machine learning community of everyone, from academic researchers to engineers to hobbyists, to exchange ideas much faster through working code than research papers. Are expected. This will facilitate research on machine learning and ultimately make the technology work better for all. Moreover, TensorFlow's application fields are not limited to machine learning. It may be useful for researchers in all fields working on the elucidation of highly complex data, from protein folding to the processing of space data.

  • http://japan.cnet.com/news/service/35073215/

According to Google, machine learning will be an indispensable element of future breakthrough products and technologies, and although research is underway around the world, there is no standard tool. Task. Google wants to accelerate research and dissemination of machine learning and machine intelligence itself by providing TensorFlow as a standard tool that can be used by researchers, students, and product developers.

  • http://japanese.engadget.com/2015/11/09/google-tensorflow/

Environment construction procedure (time required: 10 minutes)

This is the installation procedure for OS X El Capitan (10.11.1).

  • https://www.tensorflow.org/versions/master/get_started/os_setup.html

1. Install pip

$ sudo easy_install pip

2. Install Virtualenv

$ sudo pip install --upgrade virtualenv

3. Create a Virtualenv environment

$ virtualenv --system-site-packages ~/tensorflow

(The above command will be created in ~ / tensorflow)

4. Activate the created environment

$ source ~/tensorflow/bin/activate

→ Command prompt changes

5. Install TensorFlow

for_Python2.7


$ pip install --upgrade tensorflow

for_Python3.n


$ pip3 install --upgrade tensorflow

Successfully installed appdirs-1.4.0 numpy-1.12.0 packaging-16.8 protobuf-3.2.0 setuptools-34.2.0 tensorflow-1.0.0 wheel-0.29.0

that's all.

Operation check: Try running Hello World

Create a script and save it with a file name of your choice.

import tensorflow as tf
import multiprocessing as mp
 
core_num = mp.cpu_count()
config = tf.ConfigProto(
    inter_op_parallelism_threads=core_num,
    intra_op_parallelism_threads=core_num )
sess = tf.Session(config=config)
 
hello = tf.constant('hello, tensorflow!')
print sess.run(hello)

Run in a TensorFlow environment.

(tensorflow)$ python {file name}.py

Execution result:

I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
hello, tensorflow!

Reference page:

  • http://dev.classmethod.jp/machine-learning/tensorflow-hello-world/

Try learning handwritten numbers

On the TensorFlow official page, there is a tutorial using a handwritten character dataset called "MNIST For ML Beginners".

(MNIST dataset) </ span>

Here's the quickest way to run the official tutorial code (on GitHub).

Clone the source code

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow

Modify lines 30 and 31 of fully_connected_feed.py as follows: [^ 1]

[^ 1]: If you don't do this, you will get the error "ImportError: No module named examples.tutorials.mnist" at runtime.

  • Before correction
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.examples.tutorials.mnist import mnist
  • Revised
import input_data
import mnist

Run fully_connected_feed.py.

$ cd tensorflow/
$ python tensorflow/examples/tutorials/mnist/fully_connected_feed.py 

You don't have to manually download the MNIST dataset, this script will get the dataset and train the model </ b>.

Execution result:

Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting data/train-images-idx3-ubyte.gz
//Omission
Step 0: loss = 2.32 (0.025 sec)
Step 100: loss = 2.19 (0.003 sec)
Step 200: loss = 1.97 (0.003 sec)
//Omission
Step 1900: loss = 0.46 (0.004 sec)
Training Data Eval:
  Num examples: 55000  Num correct: 49489  Precision @ 1: 0.8998
Validation Data Eval:
  Num examples: 5000  Num correct: 4534  Precision @ 1: 0.9068
Test Data Eval:
  Num examples: 10000  Num correct: 9019  Precision @ 1: 0.9019

Datasets are downloaded, trained, and evaluated with test data. The final accuracy is about 90%.

Reference page:

  • http://nextdeveloper.hatenablog.com/entry/2015/11/10/204609
  • http://qiita.com/sergeant-wizard/items/fdf4d64a0d221a81da34

Try TensorBoard to visualize learning results

It seems that they also have a TensorBoard that graphs the learning results and visualizes and displays the model.

If you specify the directory containing the learning progress log data with absolute path </ b> [^ 2] and execute the tensorboard command,

$ tensorboard --logdir=/Users/xxxx/xxxx/tensorflow/tensorflow/data

[^ 2]: TensorBoard will start even if the path is wrong, but the graph etc. will not be displayed even if it is accessed.

TensorBoard will start.

Starting TensorBoard on port 6006
(You can navigate to http://localhost:6006)

So, when you access this URL from a browser, the GUI is displayed,

A graph showing the relationship between the number of learnings and xentropy_mean (cross entropy),

You can see a visualization of the model (neural network).

More

  • http://qiita.com/shuhei_f/items/9251084b00ed9319033e
  • http://qiita.com/uramonk/items/c207c948ccb6cd0a1346 --The tutorial code is explained in the comments.
  • http://qiita.com/haminiku/items/36982ae65a770565458d --There is also an explanation of advanced tutorials
  • http://d.hatena.ne.jp/sugyan/20151124/1448292129 ―― It has been tried to the point where you can use the learning results to identify the numbers you wrote </ b> --Code is also released

Chainer

A library for implementing neural networks developed by Preferred Networks. Published in June 2015. As a feature,

--Provided as a Python library --Flexible support for all neural network structures --Intuitive code with dynamic calculation graph construction --Supports GPU and can intuitively describe learning using multiple GPUs

Is listed.

  • https://research.preferred.jp/2015/06/deep-learning-chainer/

Comparison with TensorFlow

Looking at the above features, it seemed to be similar in positioning to TensorFlow, so I googled what the difference was.

Probably there is no big difference in what you can do, but in the case of Chainer, you have to manage the GPU yourself when making it a multi-GPU, but I wonder if Tenror Flow is a little easier.

  • http://d.hatena.ne.jp/shi3z/20151122/1448150781

TensorFlow requires rebuilding to use GPU, bazel is required for that, Java8 is required for that, and GPU seems to have restrictions of CC3.5 or higher, so if you can use it, why not use it? The feeling is amazing. With the same Python, Chainer feels lower threshold.

  • https://twitter.com/hrmk/status/663935918768656384

TensorFlow seems to be twice as fast as Dist Belief, but Chainer outperformed it.

The amount of description does not change that much, and I personally think that Chainer is easier to handle (though I think it is a matter of familiarity).

  • http://blog.albert2005.co.jp/2015/11/12/tensorflow%E3%81%A8chainer%E3%81%AE%E6%AF%94%E8%BC%83/

The accuracy was the same for both TensorFlow and Chainer. In the first place, MNIST has a simple problem, so it seems that high accuracy will come out even if it is not deep learning. The execution time was faster for Chainer.

  • http://qiita.com/supersaiakujin/items/bc05b9f329aca48329ac

Installation procedure (time required: 5 seconds)

There is a page called Install Guide in the official document, but it seems that it is written for Ubuntu and CentOS, so the one for Mac is When I was looking for it, there was an item called QUICK START on the original website.

  • http://chainer.org/

What to do

$ pip install chainer

only this.

Successfully installed chainer-1.5.1 filelock-2.0.5

Great!

Try learning handwritten numbers

Samples are available in the official repositories. Just clone and run the script.

$ git clone https://github.com/pfnet/chainer.git
$ python chainer/examples/mnist/train_mnist.py

The MNIST dataset will be downloaded, trained and tested.

load MNIST dataset
Downloading train-images-idx3-ubyte.gz...
(Omitted)
epoch 1
graph generated
train mean loss=0.190947790003, accuracy=0.942850003242
test  mean loss=0.0990746175707, accuracy=0.96930000484
(Omitted)
epoch 20
train mean loss=0.0104963570454, accuracy=0.996966669559
test  mean loss=0.102703116325, accuracy=0.982000006437

The final identification accuracy seems to be 98%.

More

  • http://cvl-robot.hateblo.jp/entry/2015/06/11/223928
  • http://hi-king.hatenablog.com/entry/2015/06/11/021144
  • http://hi-king.hatenablog.com/entry/2015/06/27/194630
  • http://qiita.com/icoxfog417/items/96ecaff323434c8d677b
  • http://qiita.com/mokemokechicken/items/cc9b2c96f6e4a43c123c
  • http://d.hatena.ne.jp/shi3z/20150628/1435502562
  • http://studylog.hateblo.jp/entry/2015/07/14/000635

Caffe

A deep learning open source library implemented in C ++. Developed mainly by BVLC, a research center at the University of California, Berkeley, and available in C ++, Python, and MATLAB.

I do not know the specific release date, but since it has existed before the appearance of Chainer and TensorFlow, I think that many researches and attempts using deep learning that seem to be interesting found on the net are based on Caffe. ..

Environment construction procedure (time required: about 4 hours)

I think that the required time varies from person to person (environmental difference), but I think it will definitely take longer than TensorFlow and Chainer **. There are many dependent libraries, some of which have a long installation wait time, and you need to manually configure settings that depend on your environment, so you may be addicted to that. Also, personally, I was quite annoyed by the error that the version of numpy was different.

It seems to be addicted when GPU is involved, so I will install it in CPU mode. Also include PyCaffe.

1. Various installations

$ brew install --fresh -vd snappy leveldb gflags glog szip lmdb
$ brew tap homebrew/science
$ brew install hdf5 opencv
$ brew install --build-from-source --with-python --fresh -vd protobuf
$ brew install --build-from-source --fresh -vd boost boost-python
$ brew install openblas

* 1 It takes a long time to install opencv. </ span>

* 2 It seems that OpenBLAS does not need to be included (BLAS is included as standard on Mac), but in the procedure after this Even if I correct the path of BLAS_INCLUDE in the Makefile and then execute make, I get a fatal error:'cblas.h' file not found, so I decided to include it. </ span>

2. Clone caffe

$ git clone https://github.com/BVLC/caffe.git

3. Create Makefile.config

Copy it from the template and edit it.

$ cd caffe
$ cp Makefile.config.example Makefile.config

--Uncomment # CPU_ONLY: = 1 --Change BLAS: = atlas to BLAS: = open --Uncomment the following

# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib

--Rewrite the path of PYTHON_INCLUDE according to your environment [^ 3]

  • Before correction
PYTHON_INCLUDE := /usr/include/python2.7 \
    /usr/lib/python2.7/dist-packages/numpy/core/include
  • Revised
PYTHON_INCLUDE := /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/include/python2.7 \
    /usr/local/lib/python2.7/site-packages/numpy/core/include/

[^ 3]: Regarding the fact that / usr / include is no longer in OSX these days, it seems that you can do $ xcode-select --install, but you have to download the old command line tool. I don't want to mess with the environment around Xcode so much, so I decided to specify the include path of python directly.

4. Build & test

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

If there are no errors here, the installation is successful.

[  PASSED  ] 927 tests.

5. Install the required libraries for PyCaffe

Go to the caffe / python folder and install the required libraries for PyCaffe.

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

6. Build PyCaffe

$ cd ../
$ make pycaffe
$ make distribute

7. Set environment variables

Set environment variables for Caffe. Add the following to ~ / .bashrc and add

export PYTHONPATH={caffe/python path}:$PYTHONPATH

Reflect with source ~ / .bashrc.

8. Operation check

Try importing Caffe from the Python interpreter and it's OK if no problems occur.

$ python 
>>> import caffe

By the way, in this article, I wrote only the procedure that finally worked, but I also wrote ** a very addictive troubleshooting process ** as a separate article.

-Memo I was addicted to installing Caffe (PyCaffe) --Over & Out Afterwards

Reference page:

--http://caffe.berkeleyvision.org/install_osx.html (Official page)

  • http://qiita.com/t-hiroyoshi/items/3bba01dd11b1241f1336
  • http://qiita.com/knao124/items/c76e974cfbaabb5542e8
  • http://ichyo.jp/posts/caffe-install/

DeepDream

One layer goes up from here, and I will try some application-like things that use deep learning libraries / frameworks.

DeepDream is an OSS published by Google in July 2015.

Find a familiar object from the image, reconstruct it and output it

The artificial neural network consists of 10 to 30 layers, and the first layer inputs image information to search for "corners" and "edges", and the second and third layers continue. Is a mechanism that grasps the basic object information, and the final layer assembles the information to judge "what" the object is.

  • http://gigazine.net/news/20150707-deep-dreaming-fear/

something like.

Environment construction procedure (time required: 10 minutes)

** Caffe required ** (Installation procedure above). You can try it as long as you have Caffe.

1. Install iPython notebook [^ 4]

$ pip install ipython
$ pip install ipython[notebook]

[^ 4]: For some reason ipython: command not found, so refer to here Reinstalled on

2. Clone DeepDream

Clone the DeepDream source to the same level as the folder where Caffe is located.

$ git clone [email protected]:google/deepdream.git

3. Download the model

Download the trained caffe model from here and save it in {caffe path} / models / bvlc_googlenet /.

try

Go to the cloned deepdream folder and launch the iPython notebook.

$ cd deepdream
$ ipython trust dream.ipynb
$ ipython notebook

The browser will start up, so select dream.ipynb, press the play button at the top, and wait for a while when you reach the end ... The result image will be in the deepdream / frames / folder 0000.jpg, 0001. It will be output as jpg, ....

(Left: 0000.jpg and Right 0010.jpg) </ span>

If you want to quickly try it with your favorite image, rename it to sky1024px.jpg and put it in the same place. Or edit the part of dream.ipynb that specifies the image name.

A refreshing dining scene

Now this.

Reference article:

  • http://qiita.com/t-hiroyoshi/items/e556c4ca0e8631556584
  • http://qiita.com/icoxfog417/items/175f69d06f4e590face9

Style conversion algorithm chainer-gogh

A program released by mattya in September 2015 that allows you to convert images to your favorite style using the Deep Neural Network.

  • https://research.preferred.jp/2015/09/chainer-gogh/

Use Chainer. The original paper on the algorithm is A Neural Algorithm of Artistic Style.

Environment construction procedure (time required: 1 minute)

1. clone the source

$ git clone https://github.com/mattya/chainer-gogh.git

2. Download the model

Download the model from the URL below and place it under the cloned chainer-gogh folder.

  • https://gist.github.com/mavenlin/d802a5849de39225bcc6

Execute

Let's run the CPU easily. Just prepare the content image (input.png) and style image (style.png), place them under the chainer-gogh folder, and execute the script.

python chainer-gogh.py -m nin -i input.png -s style.png -o output_dir -g -1

(Left: input.png, Right: style.png) </ span>

(result image) </ span>

Future outlook

This time I just tried to build the environment and execute the sample already prepared, so the next step is to create your own data set using OpenCV as shown in the example of the following article, and image with Chainer or TensorFlow. I would like to try to identify.

  • http://bohemia.hatenablog.com/entry/2015/11/22/161858
  • http://bohemia.hatenablog.com/entry/2015/11/22/174603

However, collecting datasets seems to take some time, so it may be realistic to start by feeding existing datasets to Chainer and TensorFlow as shown in the article below.

  • http://rest-term.com/archives/3172/

As an iOS engineer, I also want to explore the use of mobile devices.

--iOS and Machine Learning --Over & Out Afterwards

Is it possible to prepare a trained model in advance and use it exclusively for identification, or how heavy it is if the learning function itself is tried on the mobile side (can it be done in the first place)? TensorFlow claims to be "deployable on mobile devices".

Also, in addition to images, I would like to try using it for natural language processing.

end.

Related article

-Summary of articles useful for the first step of machine learning