[PYTHON] I tried to make a face diagnosis AI for a female professional golfer ②

1.First of all

By previous time, we have collected images, acquired face parts, and inflated data, which are part of data preprocessing.

This time, I actually created a model and checked the accuracy. ・ Model built by myself ・ Transfer learning (VGG16) I will write about these two.

2. Creating a file divided into training data and test data

preparation.py


from PIL import Image
import os, glob
import numpy as np
from PIL import ImageFile
import cv2
from keras.utils.np_utils import to_categorical

# IOError:image file is truncated to avoid
ImageFile.LOAD_TRUNCATED_IMAGES = True

#Creation of training data
#Create an empty list for each
img_shibuno = []
img_koiwai = []
img_hara = []
img_lists = [img_shibuno, img_koiwai, img_hara] 

#Get the path below face
in_dir = './face/*'
in_file = glob.glob(in_dir)

#Each folder (player)Perform processing for each
for num in range(len(in_file)):
    #Get the path of each image in the player's file
    in_file_name = glob.glob(in_file[num]+'/*')
    #Process each image
    for i in range(len(in_file_name)):
        #Open image
        img = Image.open(in_file_name[i])
        img = img.convert("RGB")
        #Adjust size
        img = img.resize((64,64))
        #Convert to ndarray
        data = np.asarray(img)
        # img_Add to lists
        img_lists[num].append(data)

#Combine lists with images
X_train = np.array(img_shibuno+img_koiwai+img_hara)
#0 each~Enter a value up to 2
y_train = np.array([0]*len(img_shibuno)  + [1]*len(img_koiwai) + [2]*len(img_hara))

#Creating test data
#Create an empty list for each
img_shibuno = []
img_koiwai = []
img_hara = []
img_lists = [img_shibuno, img_koiwai, img_hara] 

in_dir = './valid/*'
in_file = glob.glob(in_dir)

for num in range(len(in_file)):
    #Get the path of each image in the player's file
    in_file_name = glob.glob(in_file[num]+'/*')
    #Process each image
    for i in range(len(in_file_name)):
        #Open image
        img = Image.open(in_file_name[i])
        img = img.convert("RGB")
        #Adjust size
        img = img.resize((64,64))
        #Convert to ndarray
        data = np.asarray(img)
        # img_Add to lists
        img_lists[num].append(data)

#Combine lists with images
X_test = np.array(img_shibuno+img_koiwai+img_hara)
#0 each~Enter a value up to 2
y_test = np.array([0]*len(img_shibuno)  + [1]*len(img_koiwai) + [2]*len(img_hara))

# one-hot-Process vector
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

#Save training and validation data to a file
xy = (X_train, X_test, y_train, y_test)
np.save('./golfer.npy', xy)

This completes data preprocessing. Next, evaluate the model

3. Model creation and evaluation

Create your own model and transfer learning (vgg16) model respectively

3-1. Import module

from keras.models import Model, Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, Input
from keras.applications.vgg16 import VGG16
from keras.utils import np_utils
import keras
from keras import optimizers, models, layers
import numpy as np
import matplotlib.pyplot as plt

classes = ['shibuno', 'koiwai', 'hara']
num_classes = len(classes)
image_size = 64

3-2. Function to read data

def load_data():
    X_train, X_test, y_train, y_test = np.load('./golfer.npy', allow_pickle=True)
    #Set each pixel value of the input data to 0-Normalize in the range of 1
    X_train = X_train / 255
    X_test = X_test / 255
    return X_train, y_train, X_test, y_test

3-3. Function to learn the model

__ * Please describe only one of ① and ② below. __

① Model created by yourself
def train(X_train, y_train, X_test, y_test):
    model = Sequential()
    #X is(296, 64, 64, 3): X.shepe[1:]so(64, 64, 3)
    model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))
    model.add(Activation('relu'))
    model.add(Conv2D(32, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.1))

    model.add(Conv2D(64, (3, 3), padding='same'))
    model.add(Activation('relu'))
    model.add(Conv2D(64, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout(0.45))
    model.add(Dense(3))
    model.add(Activation('softmax'))

    model.summary()

    #Optimization algorithm RMSprop
    opt = keras.optimizers.rmsprop(lr=0.00005, decay=1e-6)

    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
    return model

Visualize the created model

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64, 64, 3)         0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 64, 64, 64)        1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 64, 64, 64)        36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 32, 32, 64)        0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 32, 32, 128)       73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 32, 32, 128)       147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 16, 16, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 16, 16, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 16, 16, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 16, 16, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 8, 8, 256)         0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 8, 8, 512)         1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 8, 8, 512)         2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 8, 8, 512)         2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 4, 4, 512)         0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 4, 4, 512)         2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 4, 4, 512)         2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 4, 4, 512)         2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 2, 2, 512)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 2048)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 256)               524544    
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 3)                 771       
=================================================================
② Transfer learning
def train(X, y, X_test, y_test):
    input_tensor = Input(shape=(64, 64, 3))
    vgg16 = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)

    #I am creating a model of the feature extraction part
    top_model = vgg16.output
    top_model = Flatten(input_shape=vgg16.output_shape[1:])(top_model)
    top_model = Dense(256, activation='sigmoid')(top_model)
    top_model = Dropout(0.5)(top_model)
    top_model = Dense(3, activation='softmax')(top_model)

    #vgg16 and top_Please concatenate models
    model = Model(inputs=vgg16.input, outputs=top_model)

    #Complete the following for statement and fix the weights up to the 15th layer
    for layer in model.layers[:15]:
        layer.trainable = False

    #Check the model structure before training
    model.summary()

    model.compile(loss='categorical_crossentropy',
                optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
                metrics=['accuracy'])
    
    return model

Visualize the created model

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64, 64, 3)         0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 64, 64, 64)        1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 64, 64, 64)        36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 32, 32, 64)        0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 32, 32, 128)       73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 32, 32, 128)       147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 16, 16, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 16, 16, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 16, 16, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 16, 16, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 8, 8, 256)         0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 8, 8, 512)         1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 8, 8, 512)         2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 8, 8, 512)         2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 4, 4, 512)         0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 4, 4, 512)         2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 4, 4, 512)         2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 4, 4, 512)         2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 2, 2, 512)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 2048)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 256)               524544    
_________________________________________________________________
dropout_1 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 6)                 1542      
=================================================================

3-4. Graphing function of correct answer rate and loss function

def compare_TV(history):
    #Set parameters
    #Correct answer rate of classification for learning data
    acc = history.history['accuracy']
    #Correct answer rate of classification for validation data
    val_acc = history.history['val_accuracy']
    #Loss function value for training data
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    #Loss function value for validation data
    epochs = range(len(acc))

    # 1)Graph of correct answer rate
    plt.plot(epochs, acc, 'bo' ,label = 'training acc')
    plt.plot(epochs, val_acc, 'b' , label= 'validation acc')
    plt.title('Training and Validation acc')
    plt.legend()

    plt.figure()

    # 2)Loss function graph
    plt.plot(epochs, loss, 'bo' ,label = 'training loss')
    plt.plot(epochs, val_loss, 'b' , label= 'validation loss')
    plt.title('Training and Validation loss')
    plt.legend()

    plt.show()

3-5. Functions that read data and train models

def main():
    #Data reading
    X_train, y_train, X_test, y_test = load_data()
    #Model learning
    model = train(X_train, y_train, X_test, y_test)

    history = model.fit(X_train, y_train, batch_size=32, epochs=70, verbose=1, validation_data=(X_test, y_test))
    
    #Evaluation and display of generalization system
    score = model.evaluate(X_test, y_test, batch_size=32, verbose=0)
    print('validation loss:{0[0]}\nvalidation accuracy:{0[1]}'.format(score))
    compare_TV(history)
    model.save('./golfer.h5')

Finally, write the main () function and execute it.

#This trains the model and generates a trained model
main()

4. Learning results

Check the accuracy rate and loss function of the learning result on the graph

① Model created by yourself

mymodel.png

__ Correct answer rate: 0.93__

② Transfer learning

転移学習.png

__ Correct answer rate: 0.95__

Since transfer learning was slightly better, we will adopt the transfer learning model.

The model has been created. Next time, I will write an application and publish it on heroku.

reference

I tried to classify Nogizaka46 by machine learning

Recommended Posts

I tried to make a face diagnosis AI for a female professional golfer ①
I tried to make a face diagnosis AI for a female professional golfer ②
I made a face diagnosis AI for a female professional golfer ③
I tried to make AI for Smash Bros.
I tried to make a Web API
I tried to make a strange quote for Jojo with LSTM
I tried to make a ○ ✕ game using TensorFlow
[Python] I tried to make a Shiritori AI that enhances vocabulary through battles
I tried to make a "fucking big literary converter"
I tried to create a reinforcement learning environment for Othello with Open AI gym
I tried to make creative art with AI! I programmed a novelty! (Paper: Creative Adversarial Network)
I tried to create a bot for PES event notification
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
[5th] I tried to make a certain authenticator-like tool with python
I tried to make a system that fetches only deleted tweets
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "date" using Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a mechanism of exclusive control with Go
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I tried to make a calculator with Tkinter so I will write it
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[Pyto] I tried to use a smartphone as a flick keyboard for PC
I tried to make a skill that Alexa will return as cold
I tried to make a url shortening service serverless with AWS CDK
I refactored "I tried to make Othello AI when programming beginners studied python"
I want to make matplotlib a dark theme
I tried to predict next year with AI
I tried to build a super-resolution method / ESPCN
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to build a super-resolution method / SRCNN ①
I tried to make a simple mail sending application with tkinter of Python
When I tried to make a VPC with AWS CDK but couldn't make it
AI beginners try to make professional student bots
I want to make a game with Python
[Patent analysis] I tried to make a patent map with Python without spending money
I tried to explain what a Python generator is for as easily as possible.
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
Continue to make stock price forecast AI for 10 hours a day 1st month
I read "How to make a hacking lab"
I tried to make a translation BOT that works on Discord using googletrans
I thought I could make a nice gitignore editor, so I tried to make something like MVP for the time being
I tried to generate a random character string
I tried to build a super-resolution method / SRCNN ③
I tried to make a dictionary function that does not distinguish between cases
I tried to create a button for Slack with Raspberry Pi + Tact Switch
I tried to build a super-resolution method / SRCNN ②
I tried to automate the face hiding work of the coordination image for wear
I tried to make a suspicious person MAP quickly using Geolonia address data
I tried to make a simple image recognition API with Fast API and Tensorflow