芸能人の顔を機械学習で分類してみた（その６）

[その４](https://qiita.com/yuni/items/7edfe68de918e12af0ee)で、沢尻エリカと武井咲の画像100枚ずつを機械学習させてみたのだけれど、それぞれ30枚ずつのテストデータを使った正解率は85%で、かつ、学習済みモデルを使って目で見てテストしてみたらやっぱり全然でした(16枚正解/20枚テスト中）。

今回は、テスト結果に分類スコアを出してみます。（これまでは0/1の判定結果だけでした。）

###学習済みモデルを使って分類スコアを出す
引数に画像を与えると、顔を切り出して、切り出せたら学習モデルを適用させて判定します。

```FaceType.py
#!/usr/bin/env python
#! -*- coding: utf-8 -*-

import sys
import numpy as np
import tensorflow as tf
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

NUM_CLASSES = 2
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3
#カスケード分類器ロード
cascade_path = "/usr/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_alt.xml"

def inference(images_placeholder, keep_prob):
    """ モデルを作成する関数

    引数: 
      images_placeholder: inputs()で作成した画像のplaceholder
      keep_prob: dropout率のplace_holder

    返り値:
      cross_entropy: モデルの計算結果
    """
    def weight_variable(shape):
      initial = tf.truncated_normal(shape, stddev=0.1)
      return tf.Variable(initial)

    def bias_variable(shape):
      initial = tf.constant(0.1, shape=shape)
      return tf.Variable(initial)

    def conv2d(x, W):
      return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

    def max_pool_2x2(x):
      return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1], padding='SAME')
    
    x_image = tf.reshape(images_placeholder, [-1, 28, 28, 3])

    with tf.name_scope('conv1') as scope:
        W_conv1 = weight_variable([5, 5, 3, 32])
        b_conv1 = bias_variable([32])
        h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)

    with tf.name_scope('pool1') as scope:
        h_pool1 = max_pool_2x2(h_conv1)
    
    with tf.name_scope('conv2') as scope:
        W_conv2 = weight_variable([5, 5, 32, 64])
        b_conv2 = bias_variable([64])
        h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)

    with tf.name_scope('pool2') as scope:
        h_pool2 = max_pool_2x2(h_conv2)

    with tf.name_scope('fc1') as scope:
        W_fc1 = weight_variable([7*7*64, 1024])
        b_fc1 = bias_variable([1024])
        h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    with tf.name_scope('fc2') as scope:
        W_fc2 = weight_variable([1024, NUM_CLASSES])
        b_fc2 = bias_variable([NUM_CLASSES])

    with tf.name_scope('softmax') as scope:
        y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

    return y_conv

# 顔検出器
# detector = dlib.simple_object_detector("detector.svm")

images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS))
labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES))
keep_prob = tf.placeholder("float")

logits = inference(images_placeholder, keep_prob)
sess = tf.InteractiveSession()

saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
saver.restore(sess, "/home/yuni/tensorflow/model.ckpt")

if __name__ == '__main__':
    tf.reset_default_graph()
    test_image = []
    for i in range(1, len(sys.argv)):
        img = cv2.imread(sys.argv[i])
        img = cv2.resize(img, (28, 28))
        test_image.append(img.flatten().astype(np.float32)/255.0)
    test_image = np.asarray(test_image)

    images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS))
    labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES))
    keep_prob = tf.placeholder("float")

    logits = inference(images_placeholder, keep_prob)
    sess = tf.InteractiveSession()

    saver = tf.train.Saver()
    sess.run(tf.global_variables_initializer())
    saver.restore(sess, "/home/yuni/tensorflow/model.ckpt")

    for i in range(len(test_image)):
        result = logits.eval(feed_dict={images_placeholder: [test_image[i]],keep_prob: 1.0 })
        pred = np.argmax(logits.eval(feed_dict={ 
            images_placeholder: [test_image[i]],
            keep_prob: 1.0 })[0])
        print (result)
        print (pred)
```

###結果
[その４](https://qiita.com/yuni/items/7edfe68de918e12af0ee0)で検証してみた時に使った画像をもう一度試してみます。

#####結果１
沢尻エリカの画像は、10枚全て正解まる。

```
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/f885be6e6214433e531a36f265b89030feffcfcc38524a1f9fe50fd270cc2177.jpeg
[[9.997142e-01 2.857761e-04]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/f918d1eddf2c94592868075d45bf9368c818050cab86d56dbf8dfcf42445daaa.jpg
[[0.9980173  0.00198271]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/f9e43c4b6ec9c408891aef1cd89a6a05293933422f26afbb36692dff689dfd8b.jpg
[[9.990164e-01 9.835492e-04]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fab18876e469d224fcead1dff74ee50c2ec2f46087741725ef1c100f7bf1839f.jpg
[[9.991411e-01 8.588899e-04]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fbdac60677937487f9584c6d13b07597f5a3c757a65eacba0672f0659740848d.jpg
[[0.9844246  0.01557538]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fcbf4c42a6368963d43a659d684ee0bdd979acb3ead64e4d0317df7b0e6ba4c8.jpg
[[9.992409e-01 7.591184e-04]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fdfe7f0e994cc49a5fa32d73a5d15919206347e6c3fa0f622d217eab210bbba4.jpg
[[9.9997830e-01 2.1666094e-05]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/febb582342e4a6f4733c0bee2d61f032fe675c2d5ed73e02ac3b6d6ad788bc0e.jpg
[[0.93956244 0.06043759]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fecf271a586c97bb06105fab3babc1d9091d0303b92e8b3ad1f06202476e512a.jpg
[[0.65698296 0.343017  ]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/ffe965f8aa816fe0609e697044242e7f9b7796a02a44de3785283df2943c8a65.jpg
[[0.99510384 0.00489616]]
0
```
使った画像をみてみます。（小数第3桁切り捨て）

沢尻エリカ：99.97%、 武井咲：0.02%
![f885be6e6214433e531a36f265b89030feffcfcc38524a1f9fe50fd270cc2177.jpeg](https://qiita-image-store.s3.amazonaws.com/0/223989/b35290b0-9e35-8e17-a290-cb6a9faf1252.jpeg)
f885be6e6214433e531a36f265b89030feffcfcc38524a1f9fe50fd270cc2177.jpeg

沢尻エリカ：99.80%、 武井咲：0.00%
![f918d1eddf2c94592868075d45bf9368c818050cab86d56dbf8dfcf42445daaa.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/2780e571-1806-e479-ba23-c020010002ba.jpeg)
f918d1eddf2c94592868075d45bf9368c818050cab86d56dbf8dfcf42445daaa.jpg

沢尻エリカ：99.90%、 武井咲：0.09%
![f9e43c4b6ec9c408891aef1cd89a6a05293933422f26afbb36692dff689dfd8b.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/c2532534-ee92-bb3b-9aba-ce32f37fb065.jpeg)
f9e43c4b6ec9c408891aef1cd89a6a05293933422f26afbb36692dff689dfd8b.jpg

沢尻エリカ：99.91%、 武井咲：0.08%
![fab18876e469d224fcead1dff74ee50c2ec2f46087741725ef1c100f7bf1839f.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/859a93cb-2d82-2fda-c6a8-9998ab5aa8fd.jpeg)
fab18876e469d224fcead1dff74ee50c2ec2f46087741725ef1c100f7bf1839f.jpg

沢尻エリカ：98.44%、 武井咲：1.55%
![fbdac60677937487f9584c6d13b07597f5a3c757a65eacba0672f0659740848d.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/62b044a2-98e9-049f-2953-826bf4c065cd.jpeg)
fbdac60677937487f9584c6d13b07597f5a3c757a65eacba0672f0659740848d.jpg

沢尻エリカ：99.92%、 武井咲：0.07%
![fcbf4c42a6368963d43a659d684ee0bdd979acb3ead64e4d0317df7b0e6ba4c8.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/e9921632-cba0-4909-7bdd-9029a8c240a5.jpeg)
fcbf4c42a6368963d43a659d684ee0bdd979acb3ead64e4d0317df7b0e6ba4c8.jpg

沢尻エリカ：99.99%、 武井咲：0.00%
![fdfe7f0e994cc49a5fa32d73a5d15919206347e6c3fa0f622d217eab210bbba4.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/8fe9dd59-ec07-6b51-f846-24d3ecaf1c14.jpeg)
fdfe7f0e994cc49a5fa32d73a5d15919206347e6c3fa0f622d217eab210bbba4.jpg

沢尻エリカ：93.95%、 武井咲：0.06%
![febb582342e4a6f4733c0bee2d61f032fe675c2d5ed73e02ac3b6d6ad788bc0e.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/ca04d22a-41a8-e241-89d9-dfcee49c4399.jpeg)
febb582342e4a6f4733c0bee2d61f032fe675c2d5ed73e02ac3b6d6ad788bc0e.jpg

沢尻エリカ：65.69%、 武井咲：34.30%
![fecf271a586c97bb06105fab3babc1d9091d0303b92e8b3ad1f06202476e512a.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/cd703003-bda0-a235-f689-ceeca611c56a.jpeg)
fecf271a586c97bb06105fab3babc1d9091d0303b92e8b3ad1f06202476e512a.jpg

沢尻エリカ：99.51%、 武井咲：0.00%
![ffe965f8aa816fe0609e697044242e7f9b7796a02a44de3785283df2943c8a65.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/e788d513-1165-4b35-f08d-15ab8a1b1d8d.jpeg)
ffe965f8aa816fe0609e697044242e7f9b7796a02a44de3785283df2943c8a65.jpg

#####結果２
武井咲の画像は10枚中7枚正解でした。

``` 
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/f98c5f34aa13755a011ddd8fce4d9190c0504fbbde93f3402679758bb031f304.jpg
[[0.8705391 0.1294609]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fc41110aedf620e4546564c84317de072c778042c546325d15f36c8b86f0d88f.jpg
[[0.00231039 0.9976896 ]]
1
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fc529e533490bff6386fd84d94821ec7e9bc6d9d6dfe4e9b02611755ee6ee77a.jpg
[[0.01911758 0.98088247]]
1
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fdfdabc1999453193207a07dedbe2a5be0e9d2f8629a3ab949ae1f7b77d4f0d4.jpg
[[3.3685540e-06 9.9999666e-01]]
1
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fe06812f63527de0151c80b873d079af0e816f3585837aee92ae8117dc5b4fc9.jpg
[[0.40112364 0.5988764 ]]
1
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/febd41e6de949d68929aafe4ec2f3a3bad06331dece61270de73e8d622dc76d1.jpg
[[0.46792147 0.53207856]]
1
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/fec8bd3c8ac308acf872420db4d27f6ad6c4f9f56aec799d2685cdd19f42dc2c.jpeg
[[2.868503e-05 9.999713e-01]]
1
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/ff2f844294dd52092043d60fff85a1d8fbf6a033b299bdee6daa077f5da98c14.png
[[0.7938876 0.2061124]]
0
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/ff9b638e43b0760f1a546b75321150fc9c190535aedfd023fa5839729d1c51a3.jpeg
[[0.05063502 0.94936496]]
1
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py /home/yuni/azureVM/data/f8c84ef8beb5f0726483d2cdf51cd17bf1fe2d59e5fc1124f0c10df9af767004.jpg
[[0.61716545 0.3828345 ]]
0
```
こちらも使った画像を見てみます。

沢尻エリカ：87.05%、 武井咲：12.94%
![f98c5f34aa13755a011ddd8fce4d9190c0504fbbde93f3402679758bb031f304.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/a559c90c-f551-f232-7420-9f9cdf1c0915.jpeg)
f98c5f34aa13755a011ddd8fce4d9190c0504fbbde93f3402679758bb031f304.jpg

沢尻エリカ：0.23%、 武井咲：99.76%
![fc41110aedf620e4546564c84317de072c778042c546325d15f36c8b86f0d88f.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/726a9b61-391d-b04e-2466-8ae1d12145ac.jpeg)
fc41110aedf620e4546564c84317de072c778042c546325d15f36c8b86f0d88f.jpg

沢尻エリカ：1.91%、 武井咲：98.08%
![fc529e533490bff6386fd84d94821ec7e9bc6d9d6dfe4e9b02611755ee6ee77a.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/fd6bf569-d248-5730-f0c8-447437303668.jpeg)
fc529e533490bff6386fd84d94821ec7e9bc6d9d6dfe4e9b02611755ee6ee77a.jpg

沢尻エリカ：0.00%、 武井咲：99.99%
![fdfdabc1999453193207a07dedbe2a5be0e9d2f8629a3ab949ae1f7b77d4f0d4.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/188a3bec-873d-e0d5-d02f-ac15226d6146.jpeg)
fdfdabc1999453193207a07dedbe2a5be0e9d2f8629a3ab949ae1f7b77d4f0d4.jpg

沢尻エリカ：40.11%、 武井咲：59.88%
![fe06812f63527de0151c80b873d079af0e816f3585837aee92ae8117dc5b4fc9.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/57b2518e-0b0f-fde5-c5cd-202d9336f5d0.jpeg)
fe06812f63527de0151c80b873d079af0e816f3585837aee92ae8117dc5b4fc9.jpg

沢尻エリカ：46.79%、 武井咲：53.20%
![febd41e6de949d68929aafe4ec2f3a3bad06331dece61270de73e8d622dc76d1.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/01c2285a-54f9-0d15-b200-4e38413c77a7.jpeg)
febd41e6de949d68929aafe4ec2f3a3bad06331dece61270de73e8d622dc76d1.jpg

沢尻エリカ：0.00%、 武井咲：99.99%
![fec8bd3c8ac308acf872420db4d27f6ad6c4f9f56aec799d2685cdd19f42dc2c.jpeg](https://qiita-image-store.s3.amazonaws.com/0/223989/795dc751-1ff0-40a3-6787-d7febb618176.jpeg)
fec8bd3c8ac308acf872420db4d27f6ad6c4f9f56aec799d2685cdd19f42dc2c.jpeg

沢尻エリカ：79.38%、 武井咲：20.61%
![ff2f844294dd52092043d60fff85a1d8fbf6a033b299bdee6daa077f5da98c14.png](https://qiita-image-store.s3.amazonaws.com/0/223989/e8b66e8c-d1a0-90e9-11d7-cd83a92aa458.png)
ff2f844294dd52092043d60fff85a1d8fbf6a033b299bdee6daa077f5da98c14.png

沢尻エリカ：5.06%、 武井咲：94.93%
![ff9b638e43b0760f1a546b75321150fc9c190535aedfd023fa5839729d1c51a3.jpeg](https://qiita-image-store.s3.amazonaws.com/0/223989/dab0168d-1e0d-3340-4f36-d3a7bf6eff01.jpeg)
ff9b638e43b0760f1a546b75321150fc9c190535aedfd023fa5839729d1c51a3.jpeg

沢尻エリカ：61.71%、 武井咲：38.28%
![f8c84ef8beb5f0726483d2cdf51cd17bf1fe2d59e5fc1124f0c10df9af767004.jpg](https://qiita-image-store.s3.amazonaws.com/0/223989/edefd132-4f92-7006-978e-4afb7a4cd695.jpeg)
f8c84ef8beb5f0726483d2cdf51cd17bf1fe2d59e5fc1124f0c10df9af767004.jpg

比較的、横顔っぽいのと、笑って目が細いやつが不正解になっている感じでしょうか？

###ちなみに
今日もまた自分の顔つかってみる。

```
[yuni@machinelearningvm tensorflow]$ python3 FaceType.py ./IMG_6154.JPG
[[0.0361544  0.96384555]]
1
```
沢尻エリカ：3.61%、 武井咲：96.38%
![IMG_6154.JPG](https://qiita-image-store.s3.amazonaws.com/0/223989/e350ff2b-13ab-fbae-5d0e-5ca3d1625db2.jpeg)
IMG_6154.JPG

結果どっちでも嬉しいやつw
なんだこれw
お付き合いありがとうございました。