TensorFlow をインストールして、Pepper の画像を認識させてみた


## はじめに

* 先日、[TensorFlow勉強会（２）](http://connpass.com/event/24265/)に行ってきたのですが、画像認識の話のところで、Pepper はどう認識されるのだろう？と思い、試してみました。

## インストール

### TensorFlow のインストール

```bash
$ brew install bazel swig
$ sudo easy_install -U six
$ sudo easy_install -U numpy
$ sudo easy_install wheel
$ sudo easy_install ipython

$ git clone -b v0.6.0 --recurse-submodules https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
$ ./configure
$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
$ sudo pip install /tmp/tensorflow_pkg/tensorflow-0.6.0-py2-none-any.whl
```

### インストール後の動作確認

```bash
$ python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant("Hello, TensorFlow!")
>>> sess = tf.Session()
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
>>> print(sess.run(hello))
Hello, TensorFlow!

>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
```

## 画像認識を試す

### パンダ (サンプル)
```bash
$ python classify_image.py
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.89233)
indri, indris, Indri indri, Indri brevicaudatus (score = 0.00859)
lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00264)
custard apple (score = 0.00141)
earthstar (score = 0.00107)
```

### Pepper その1

![pepper1ss.jpg](https://qiita-image-store.s3.amazonaws.com/0/20418/b004ff8d-0716-31cb-5543-a40c91e14a8b.jpeg "pepper1ss.jpg")

```bash
$ python classify_image.py --image_file pepper1ss.jpg
soap dispenser (score = 0.26181)
sewing machine (score = 0.08305)
digital clock (score = 0.07441)
hand blower, blow dryer, blow drier, hair dryer, hair drier (score = 0.04593)
piggy bank, penny bank (score = 0.02755)
```

- [soap dispenser](https://www.google.co.jp/search?q=soap+dispenser&client=safari&rls=en&source=lnms&tbm=isch&sa=X&ved=0ahUKEwi9wsL_3PXKAhUjHaYKHbmZCzAQ_AUIBygB&biw=1280&bih=695)

### Pepper その2

![pepper3s.jpg](https://qiita-image-store.s3.amazonaws.com/0/20418/2da0a3a5-a31c-a3da-40d5-ceb7200e21d5.jpeg "pepper3s.jpg")

```bash
$ python classify_image.py --image_file pepper3s.jpg
switch, electric switch, electrical switch (score = 0.51433)
soap dispenser (score = 0.11694)
stopwatch, stop watch (score = 0.11000)
hook, claw (score = 0.02720)
corkscrew, bottle screw (score = 0.01832)
```

- [electrical switch](https://www.google.co.jp/search?q=electrical+switch&client=safari&rls=en&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiLyYO23fXKAhVLG5QKHU3LAUYQ_AUIBygB&biw=1280&bih=695)

### Pepper その3

![pepper4s.jpg](https://qiita-image-store.s3.amazonaws.com/0/20418/1b4165e6-0079-cc54-b4c5-f9da8c6e2ec7.jpeg "pepper4s.jpg")

```bash
$ python classify_image.py --image_file pepper4s.jpg
soap dispenser (score = 0.13773)
mask (score = 0.13569)
joystick (score = 0.09575)
piggy bank, penny bank (score = 0.07548)
pencil sharpener (score = 0.02219)
```

### Nao

![nao1s.jpg](https://qiita-image-store.s3.amazonaws.com/0/20418/49cacb7a-c130-97d5-83c1-09b1b94f9f30.jpeg "nao1s.jpg")

```bash
$ python classify_image.py --image_file nao1s.jpg
toyshop (score = 0.09573)
knee pad (score = 0.04186)
digital watch (score = 0.04072)
pill bottle (score = 0.03983)
soap dispenser (score = 0.02415)
```

### Romeo

![romeo2.jpg](https://qiita-image-store.s3.amazonaws.com/0/20418/5e01cea1-b039-5b71-2f15-b712add215bf.jpeg "romeo2.jpg")

```bash
$ python classify_image.py --image_file romeo2.jpg
breastplate, aegis, egis (score = 0.25803)
knee pad (score = 0.08682)
toyshop (score = 0.07168)
comic book (score = 0.06568)
cuirass (score = 0.04174)
```

### Pepper その4

![pepper-07s.jpg](https://qiita-image-store.s3.amazonaws.com/0/20418/b07b6dd7-c4d3-cca1-e531-969146643f7d.jpeg "pepper-07s.jpg")

```bash
$ python classify_image.py --image_file pepper-07s.jpg
bell pepper (score = 0.98801)
pencil sharpener (score = 0.00038)
cucumber, cuke (score = 0.00017)
stove (score = 0.00017)
Granny Smith (score = 0.00016)
```

これはすばらしい。

## 感想

* ImageNet では分類されていないだろうから、変な答えが出ることを期待していたのだけど、あまり面白くはならなかったな。
* まずはこれを学習させてみる、というのは手頃な課題になるか。