[PYTHON] Make a face recognizer using TensorFlow

Introduction

s_スクリーンショット 2016-07-27 12.37.01.png

Hi, this is Hironsan.

Face recognition is a technology that detects a person in an image and identifies the person. Face recognition can be used by incorporating it into a surveillance camera system to improve security, or by incorporating it into a robot to recognize the face of a family member.

This time, we will build a convolutional neural network using TensorFlow and make a face recognizer using an existing data set.

Target audience

You can see the theory of CNN by looking at the following.

Preparation

Install TensorFlow

Please refer to the official website for the installation of TensorFlow, which is explained carefully.

Download data

First, prepare the data set. This time, we will use the following face image dataset.

This dataset contains 10 images for each of 40 people. Each image size is 64x64 and is a grayscale image. スクリーンショット 2016-07-25 22.30.04.png

Loading images

After preparing the dataset, load the image. PyFaceRecognizer/example/input_data.py

import input_data
dataset = input_data.read_data_sets('data/olivettifaces.mat')

Here, the dataset contains training data, validation data, and test data. In addition, the image size is reduced to 32x32 at the stage of reading.

Building a convolutional neural network

Face recognition is performed using a convolutional neural network (CNN). The overall picture is as follows.

スクリーンショット 2016-07-27 13.19.06.png

The conv, pool, and fc of each layer represent the convolution layer, pooling layer, and fully connected layer, respectively. ReL in the function column represents a rectified linear function. The table of parameters is as follows.

Layer type / name patch stride Output map size function
data - - 32 x 32 x 1 -
conv1 5 x 5 1 32 x 32 x 32 ReL
pool1 2 x 2 2 16 x 16 x 32 -
conv2 5 x 5 1 16 x 16 x 64 ReL
pool2 2 x 2 2 8 x 8 x 64 -
fc3 - - 1 x 1 x 1024 ReL
fc4 - - 1 x 1 x 40 softmax

If you write it in code, it will be as follows. It's almost the same. PyFaceRecognizer/example/run.py

def inference(input_placeholder, keep_prob):
    W_conv1 = weight_variable([5, 5, 1, 32])  #The first two are patch sizes. The rest is the number of input and output channels
    b_conv1 = bias_variable([32])

    x_image = tf.reshape(input_placeholder, [-1, 32, 32, 1])  #The second and third dimensions are the width and height of the image, and the last dimension is the number of color channels.

    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)  #Folding
    h_pool1 = max_pool_2x2(h_conv1)  #max pooling

    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)
    h_pool2 = max_pool_2x2(h_conv2)

    W_fc1 = weight_variable([8 * 8 * 64, 1024])
    b_fc1 = bias_variable([1024])

    h_pool2_flat = tf.reshape(h_pool2, [-1, 8 * 8 * 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)

    W_fc2 = weight_variable([1024, 40])
    b_fc2 = bias_variable([40])

    y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)  #Output so

    return y_conv

Model training and evaluation

I wrote the model code in inference. Next, we will write the code to train the model. That is loss and training. In loss, the cross entropy is calculated, and in training, the Adam optimizer is used to update the parameters. The code is as follows.

def loss(output, supervisor_labels_placeholder):
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(supervisor_labels_placeholder * tf.log(output), reduction_indices=[1]))
    return cross_entropy


def training(loss):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
    return train_step

Face recognition is performed using the inference, loss, and training defined above. Outputs a log every 100 iterations of the training process. I set keep_peob to 1.0 so that it doesn't drop out when testing.

with tf.Session() as sess:
    output = inference(x, keep_prob)
    loss = loss(output, y_)
    training_op = training(loss)

    init = tf.initialize_all_variables()
    sess.run(init)

    for step in range(1000):
        batch = dataset.train.next_batch(40)
        sess.run(training_op, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
        if step % 100 == 0:
            print(sess.run(loss, feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}))

    correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print('test accuracy %g' % accuracy.eval(feed_dict={x: dataset.test.images, y_: dataset.test.labels, keep_prob: 1.0}))

Run

The execution result looks like the one below. You can see how the cross entropy is decreasing.

9.8893
1.68918
0.602403
0.261183
0.0490791
0.0525591
0.0133087
0.0121071
0.00673524
0.00580989

Source code

You can download the source code from the following repositories and run it.

in conclusion

I tried face recognition on an existing face dataset using a convolutional neural network. Next, I would like to try face detection and face recognition using images acquired in real time from the camera.

Reference material

Recommended Posts

Make a face recognizer using TensorFlow
I tried to make a ○ ✕ game using TensorFlow
Make a Sato Yohei discriminator using OpenCV and TensorFlow
Face detection using a cascade classifier
I made a Dir en gray face classifier using TensorFlow --(1) Introduction
I made a Dir en gray face classifier using TensorFlow-④ Face extraction
I tried playing a ○ ✕ game using TensorFlow
Face image inference using Flask and TensorFlow
Let's make a multilingual site using flask-babel
I made a Dir en gray face classifier using TensorFlow --⑩ Face classification test
I made a Dir en gray face classifier using TensorFlow --⑥ Learning program
I made a Dir en gray face classifier using TensorFlow --⑬ Playing (final)
I made a Dir en gray face classifier using TensorFlow --- ⑧ Learning execution
I made a Dir en gray face classifier using TensorFlow --⑫ Web release
I made a Dir en gray face classifier using TensorFlow --- ⑦ Learning model
I made a Dir en gray face classifier using TensorFlow --② Environment construction
A addictive story when using tensorflow on Android
A story about simple machine learning using TensorFlow
[Chat De Tornado] Make a chat using WebSocket with Tornado
Let's make a module for Python using SWIG
I made a Dir en gray face classifier using TensorFlow --⑪ Web release preparation
Make a squash game
Make a function decorator
I stumbled upon using MoviePy, so make a note
Make a distance matrix
How to make a Python package using VS Code
Try face detection in real time using a webcam
I'll make a password!
Make a Nyan button
Let's easily make a math gif using Google Colaboratory
Make a Tetris-style game!
Make a Base64 decoder
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
I made a VGG16 model using TensorFlow (on the way)
I tried to make a stopwatch using tkinter in python
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
I tried to make a simple text editor using PyQt
Let's make a LINE bot using various services [ngrok edition]
Let's make a Discord Bot.
Make a Blueqat backend ~ Part 1
Make a Blueqat backend ~ Part 2
Time measurement using a clock
I tried face recognition using Face ++
[Django] Make a pull-down menu
Make a LINE BOT (chat)
Pepper Tutorial (5): Using a Tablet
Make a bookmarklet in Python
Using a printer with Debian 10
Make a fortune with Python
Make Responder a daemon (service)
A note about TensorFlow Introduction
I tried using magenta / TensorFlow
Let's make a rock-paper-scissors game
Outline the face using Dlib (1)
Make a fire with kdeplot
A memorandum of using eigen3
Make a math drill print
I tried to make a regular expression of "amount" using Python
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python