2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

芸能人の顔を機械学習で分類してみた(その6)

Last updated at Posted at 2018-04-09

その4で、沢尻エリカと武井咲の画像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)

###結果
その4で検証してみた時に使った画像をもう一度試してみます。

#####結果1
沢尻エリカの画像は、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
f885be6e6214433e531a36f265b89030feffcfcc38524a1f9fe50fd270cc2177.jpeg

沢尻エリカ:99.80%、 武井咲:0.00%
f918d1eddf2c94592868075d45bf9368c818050cab86d56dbf8dfcf42445daaa.jpg
f918d1eddf2c94592868075d45bf9368c818050cab86d56dbf8dfcf42445daaa.jpg

沢尻エリカ:99.90%、 武井咲:0.09%
f9e43c4b6ec9c408891aef1cd89a6a05293933422f26afbb36692dff689dfd8b.jpg
f9e43c4b6ec9c408891aef1cd89a6a05293933422f26afbb36692dff689dfd8b.jpg

沢尻エリカ:99.91%、 武井咲:0.08%
fab18876e469d224fcead1dff74ee50c2ec2f46087741725ef1c100f7bf1839f.jpg
fab18876e469d224fcead1dff74ee50c2ec2f46087741725ef1c100f7bf1839f.jpg

沢尻エリカ:98.44%、 武井咲:1.55%
fbdac60677937487f9584c6d13b07597f5a3c757a65eacba0672f0659740848d.jpg
fbdac60677937487f9584c6d13b07597f5a3c757a65eacba0672f0659740848d.jpg

沢尻エリカ:99.92%、 武井咲:0.07%
fcbf4c42a6368963d43a659d684ee0bdd979acb3ead64e4d0317df7b0e6ba4c8.jpg
fcbf4c42a6368963d43a659d684ee0bdd979acb3ead64e4d0317df7b0e6ba4c8.jpg

沢尻エリカ:99.99%、 武井咲:0.00%
fdfe7f0e994cc49a5fa32d73a5d15919206347e6c3fa0f622d217eab210bbba4.jpg
fdfe7f0e994cc49a5fa32d73a5d15919206347e6c3fa0f622d217eab210bbba4.jpg

沢尻エリカ:93.95%、 武井咲:0.06%
febb582342e4a6f4733c0bee2d61f032fe675c2d5ed73e02ac3b6d6ad788bc0e.jpg
febb582342e4a6f4733c0bee2d61f032fe675c2d5ed73e02ac3b6d6ad788bc0e.jpg

沢尻エリカ:65.69%、 武井咲:34.30%
fecf271a586c97bb06105fab3babc1d9091d0303b92e8b3ad1f06202476e512a.jpg
fecf271a586c97bb06105fab3babc1d9091d0303b92e8b3ad1f06202476e512a.jpg

沢尻エリカ:99.51%、 武井咲:0.00%
ffe965f8aa816fe0609e697044242e7f9b7796a02a44de3785283df2943c8a65.jpg
ffe965f8aa816fe0609e697044242e7f9b7796a02a44de3785283df2943c8a65.jpg

#####結果2
武井咲の画像は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
f98c5f34aa13755a011ddd8fce4d9190c0504fbbde93f3402679758bb031f304.jpg

沢尻エリカ:0.23%、 武井咲:99.76%
fc41110aedf620e4546564c84317de072c778042c546325d15f36c8b86f0d88f.jpg
fc41110aedf620e4546564c84317de072c778042c546325d15f36c8b86f0d88f.jpg

沢尻エリカ:1.91%、 武井咲:98.08%
fc529e533490bff6386fd84d94821ec7e9bc6d9d6dfe4e9b02611755ee6ee77a.jpg
fc529e533490bff6386fd84d94821ec7e9bc6d9d6dfe4e9b02611755ee6ee77a.jpg

沢尻エリカ:0.00%、 武井咲:99.99%
fdfdabc1999453193207a07dedbe2a5be0e9d2f8629a3ab949ae1f7b77d4f0d4.jpg
fdfdabc1999453193207a07dedbe2a5be0e9d2f8629a3ab949ae1f7b77d4f0d4.jpg

沢尻エリカ:40.11%、 武井咲:59.88%
fe06812f63527de0151c80b873d079af0e816f3585837aee92ae8117dc5b4fc9.jpg
fe06812f63527de0151c80b873d079af0e816f3585837aee92ae8117dc5b4fc9.jpg

沢尻エリカ:46.79%、 武井咲:53.20%
febd41e6de949d68929aafe4ec2f3a3bad06331dece61270de73e8d622dc76d1.jpg
febd41e6de949d68929aafe4ec2f3a3bad06331dece61270de73e8d622dc76d1.jpg

沢尻エリカ:0.00%、 武井咲:99.99%
fec8bd3c8ac308acf872420db4d27f6ad6c4f9f56aec799d2685cdd19f42dc2c.jpeg
fec8bd3c8ac308acf872420db4d27f6ad6c4f9f56aec799d2685cdd19f42dc2c.jpeg

沢尻エリカ:79.38%、 武井咲:20.61%
ff2f844294dd52092043d60fff85a1d8fbf6a033b299bdee6daa077f5da98c14.png
ff2f844294dd52092043d60fff85a1d8fbf6a033b299bdee6daa077f5da98c14.png

沢尻エリカ:5.06%、 武井咲:94.93%
ff9b638e43b0760f1a546b75321150fc9c190535aedfd023fa5839729d1c51a3.jpeg
ff9b638e43b0760f1a546b75321150fc9c190535aedfd023fa5839729d1c51a3.jpeg

沢尻エリカ:61.71%、 武井咲:38.28%
f8c84ef8beb5f0726483d2cdf51cd17bf1fe2d59e5fc1124f0c10df9af767004.jpg
f8c84ef8beb5f0726483d2cdf51cd17bf1fe2d59e5fc1124f0c10df9af767004.jpg

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

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

[yuni@machinelearningvm tensorflow]$ python3 FaceType.py ./IMG_6154.JPG
[[0.0361544  0.96384555]]
1

沢尻エリカ:3.61%、 武井咲:96.38%
IMG_6154.JPG
IMG_6154.JPG

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

2
4
0

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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?