[PYTHON] [Image classification] Facial expression analysis of dogs

1.First of all

I tried to analyze the facial expressions of animals with a focus on interest.

2. References

"Create a machine learning model to distinguish between" laughing dogs "and" angry dogs "in Keras" https://qiita.com/ariera/items/545d48c961170a784075

3. Contents

3-1. Data preprocessing

#Define a function to read an image and convert it to a matrix
from keras.preprocessing.image import load_img, img_to_array
def img_to_traindata(file, img_rows, img_cols, rgb):
    if rgb == 0:
        img = load_img(file, color_mode = "grayscale", target_size=(img_rows,img_cols)) #Read in grayscale
    else:
        img = load_img(file, color_mode = "rgb", target_size=(img_rows,img_cols)) #Read in RGB
    x = img_to_array(img)
    x = x.astype('float32')
    x /= 255
    return x

#Training data, test data generation
import glob, os

img_rows = 224 #Image size is the default size of VGG16
img_cols = 224
nb_classes = 2 #2 classes of angry and laughing
img_dirs = ["./dog_angry", "./dog_smile"] #Directory containing images of angry dogs and laughing dogs

X_train = []
Y_train = []
X_test = []
Y_test = []
for n, img_dir in enumerate(img_dirs):
    img_files = glob.glob(img_dir+"/*.jpg ")   #Read all image files in the directory
    for i, img_file in enumerate(img_files):  #directory(Character type)For all files in
        x = img_to_traindata(img_file, img_rows, img_cols, 1) #Read each image file in RGB and convert it to a matrix
        if i < 8: #Learning data from 1st to 8th sheets
            X_train.append(x) #Training data(input)Add an image-converted matrix to
            Y_train.append(n) #Training data(output)To class(Angry=0, lol=1)Add
        else:       #Test data for the 9th and subsequent sheets
            X_test.append(x) #test data(input)Add an image-converted matrix to
            Y_test.append(n) #test data(output)To class(Angry=0, lol=1)Add

import numpy as np
#Training, test data from list to numpy.Convert to ndarray
X_train = np.array(X_train, dtype='float') 
Y_train = np.array(Y_train, dtype='int')
X_test = np.array(X_test, dtype='float')
Y_test = np.array(Y_test, dtype='int')

#Categorical data(vector)Conversion to
from keras.utils import np_utils
Y_train = np_utils.to_categorical(Y_train, nb_classes)
Y_test = np_utils.to_categorical(Y_test, nb_classes)

#Save the created learning data and test data as a file
np.save('models/X_train_2class_120.npy', X_train)
np.save('models/X_test_2class_120.npy', X_test)
np.save('models/Y_train_2class_120.npy', Y_train)
np.save('models/Y_test_2class_120.npy', Y_test)

#Display the type of created data
print(X_train.shape)
print(Y_train.shape)
print(X_test.shape)


from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D

#【parameter settings】
batch_size = 20
epochs = 30

input_shape = (img_rows, img_cols, 3)
nb_filters = 32
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)

#[Model definition]
model = Sequential()
model.add(Conv2D(nb_filters, kernel_size, #Convolution layer
                        padding='valid',
                        activation='relu',
                        input_shape=input_shape))
model.add(Conv2D(nb_filters, kernel_size, activation='relu')) #Convolution layer
model.add(MaxPooling2D(pool_size=pool_size)) #Pooling layer
model.add(Conv2D(nb_filters, kernel_size, activation='relu')) #Convolution layer
model.add(MaxPooling2D(pool_size=pool_size)) #Pooling layer
model.add(Dropout(0.25)) #Drop out(Randomly disconnect between input and output to prevent overfitting)

model.add(Flatten()) #Convert a multidimensional array to a one-dimensional array
model.add(Dense(128, activation='relu'))  #Fully connected layer
model.add(Dropout(0.2))  #Drop out
model.add(Dense(nb_classes, activation='sigmoid'))  #Since it is 2 classes, the fully connected layer is sigmoid

#Compiling the model
model.compile(loss='binary_crossentropy', #Binary because it is 2 classes_crossentropy
              optimizer='adam', #Use defaults for optimization function parameters
              metrics=['accuracy'])

#[Define a callback to generate the learning result for each epoch(Save only when the accuracy is better than last time)】
from keras.callbacks import ModelCheckpoint
import os
model_checkpoint = ModelCheckpoint(
    filepath=os.path.join('models','model_2class120_{epoch:02d}.h5'),
    monitor='val_accuracy',
    mode='max',
    save_best_only=True,
    verbose=1)
print("filepath",os.path.join('models','model_.h5'))

#[Learning]
result = model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_test, Y_test),
                   callbacks=[model_checkpoint],validation_split=0.1)

3-2. Learning

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D

#【parameter settings】
batch_size = 20
epochs = 30

input_shape = (img_rows, img_cols, 3)
nb_filters = 32
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)

#[Model definition]
model = Sequential()
model.add(Conv2D(nb_filters, kernel_size, #Convolution layer
                        padding='valid',
                        activation='relu',
                        input_shape=input_shape))
model.add(Conv2D(nb_filters, kernel_size, activation='relu')) #Convolution layer
model.add(MaxPooling2D(pool_size=pool_size)) #Pooling layer
model.add(Conv2D(nb_filters, kernel_size, activation='relu')) #Convolution layer
model.add(MaxPooling2D(pool_size=pool_size)) #Pooling layer
model.add(Dropout(0.25)) #Drop out(Randomly disconnect between input and output to prevent overfitting)

model.add(Flatten()) #Convert a multidimensional array to a one-dimensional array
model.add(Dense(128, activation='relu'))  #Fully connected layer
model.add(Dropout(0.2))  #Drop out
model.add(Dense(nb_classes, activation='sigmoid'))  #Since it is 2 classes, the fully connected layer is sigmoid

#Compiling the model
model.compile(loss='binary_crossentropy', #Binary because it is 2 classes_crossentropy
              optimizer='adam', #Use defaults for optimization function parameters
              metrics=['accuracy'])

#[Define a callback to generate the learning result for each epoch(Save only when the accuracy is better than last time)】
from keras.callbacks import ModelCheckpoint
import os
model_checkpoint = ModelCheckpoint(
    filepath=os.path.join('models','model_2class120_{epoch:02d}_{val_acc:.3f}.h5'),
    monitor='val_acc',
    mode='max',
    save_best_only=True,
    verbose=1)

#[Learning]
result = model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_test, Y_test),
                   callbacks=[model_checkpoint])

3-3. Classification

from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
import matplotlib.pyplot as plt

model = load_model('models/model_2class120_04.h5')
model.summary()

def img_to_traindata(file, img_rows, img_cols, rgb):
    if rgb == 0:
        img = load_img(file, color_mode = "grayscale", target_size=(img_rows,img_cols)) #Read in grayscale
    else:
        img = load_img(file, color_mode = "rgb", target_size=(img_rows,img_cols)) #Read in RGB
    x = img_to_array(img)
    x = x.astype('float32')
    x /= 255
    return x

import numpy as np
img_rows = 224 #Image size is the default size of VGG16
img_cols = 224

##Image loading
filename = "dog_smile/n02085936_37.jpg "
x = img_to_traindata(filename, img_rows, img_cols, 1) # img_to_The traindata function is defined when generating training data
x = np.expand_dims(x, axis=0)

##Determine which class
preds = model.predict(x)
pred_class = np.argmax(preds[0])
print("Identification result:", pred_class)
print("probability:", preds[0])

from keras import backend as K
import cv2

#Take the final output of the model
model_output = model.output[:, pred_class]

#Take out the last convolution layer
last_conv_output = model.get_layer('conv2d_3').output #'block5_conv3').output

#Gradient of the output of the final convolution layer with respect to the model final output
grads = K.gradients(model_output, last_conv_output)[0]
# model.When you enter input, last_conv_Define a function to output output and grads
gradient_function = K.function([model.input], [last_conv_output, grads]) 

#Find the gradient of the loaded image
output, grads_val = gradient_function([x])
output, grads_val = output[0], grads_val[0]

#Average weights and multiply by layer output to create heatmap
weights = np.mean(grads_val, axis=(0, 1))
heatmap = np.dot(output, weights)

heatmap = cv2.resize(heatmap, (img_rows, img_cols), cv2.INTER_LINEAR)
heatmap = np.maximum(heatmap, 0) 
heatmap = heatmap / heatmap.max()

heatmap = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)  #Color the heatmap
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)  #Convert color to RGB

img = plt.imread(filename, cv2.IMREAD_UNCHANGED)
print(img.shape)  # (330, 440, 4)

fig, ax = plt.subplots()
ax.imshow(img)

plt.show()

4. Classification result

2020-04-26 21:12:46.904489: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fded852e7f0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-04-26 21:12:46.904528: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 222, 222, 32)      896       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 220, 220, 32)      9248      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 110, 110, 32)      0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 108, 108, 32)      9248      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 54, 54, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 54, 54, 32)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 93312)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               11944064  
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 258       
=================================================================
Total params: 11,963,714
Trainable params: 11,963,714
Non-trainable params: 0
_________________________________________________________________
Identification result: 1
probability:[0.27252397 0.6845933 ]
(375, 500, 3)

5. Future issues

It is necessary to narrow down the problems to improve the accuracy.

Recommended Posts

[Image classification] Facial expression analysis of dogs
[PyTorch] Image classification of CIFAR-10
Clash of Clans and image analysis (3)
Analysis of X-ray microtomography image by Python
[PyTorch] Image classification of CIFAR-10
Supervised learning 1 Basics of supervised learning (classification)
Practice typical methods of statistics (1)
Multi-class, multi-label classification of images with pytorch
Machine learning algorithm (implementation of multi-class classification)
[Image classification] Facial expression analysis of dogs
Judge Yosakoi Naruko by image classification of Tensorflow.
Image of closure
[OpenCV / Python] I tried image analysis of cells with OpenCV
Story of image analysis of PDF file and data extraction
Basics of regression analysis