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.
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
Create your own model and transfer learning (vgg16) model respectively
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
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
__ * Please describe only one of ① and ② below. __
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
=================================================================
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
=================================================================
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()
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()
Check the accuracy rate and loss function of the learning result on the graph
__ Correct answer rate: 0.93__
__ 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.
I tried to classify Nogizaka46 by machine learning
Recommended Posts