✌(•̀ ω •́)y:

This project is implemented on Keras (back-end Tensorflow), which uses a Neural Network to classify the members of the Simpsons according to the screenshots of the cartoon, using Convolutional Neural Network (CNN), one of the most complex and difficult Neural networks at present. The dataset is made up of animated screenshots of 11 Members of the Simpsons, stored in 11 folders, each with about 1,000 images. These images have different sizes and are input into the neural network together with labels for training after normalization

Since this image recognition training is directly using images, the program can actually be used to do a lot of things (captcha recognition, machine vision in intelligent transportation, pedestrian and vehicle recognition) by simply changing the folder path and pointing to a new data set.



Directly on the code:

Importing a dependency library:



from PIL import Image
import numpy as np
import os
import glob
import re
import keras
from keras.optimizers import SGD, Adam
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as KCopy the code

* Define functions: import images from the data set, normalize and then transform them into feature matrix: take the data of the first 100 images in each folder and put them into the test set, and the rest samples are all used as training sets. *



def read_img(location):
    x_train = [] 
    y_train = [] 
    x_test = [] 
    y_test = [] 
    label_name = [] 
    dirs = os.listdir(location) 
    label = 0 
    count = 0
    for i in dirs: #loop all directory
        print(i)
        n = 0 
        label_name.append(i) #save folder name in var label_name
        x_s = 200
        y_s = 200
        for pic in glob.glob(location+'\\'+i+'\*.jpg'): 
            im = Image.open(pic) #open data
            im = im.resize((x_s, y_s), Image.ANTIALIAS)
            im = np.array(im) #store im as numpy array
            if(im.shape[0] = =200 and im.shape[1] = =200): 
                r = im[:,:,0]
                g = im[:,:,1]
                b = im[:,:,2]
                if(n<100): 
                    x_test.append([r,g,b]) #save in x_test
                    y_test.append([label]) #save in y_test
                else: #remaining data set as training data
                    x_train.append([r,g,b]) #save in x_train
                    y_train.append([label]) #save in y_train
                n = n + 1 
                count = count + 1 
        label = label + 1 #increment label
    print(label_name)
    print(dirs)
    return np.array(x_train),np.array(y_train),np.array(x_test),np.array(y_test)Copy the code

The image is normalized to 200p200p size: *

The original:

Normalized picture:

Generate training data, training labels, test data, and test labels through defined functions:



path='E:\\JLD\\desktop\\the-simpsons-characters-dataset\\simpsons_dataset'
img_rows = 200 #num of image height
img_cols = 200 #num of image width
num_class = 11 #num of classes/labels
x_train,y_train,x_test,y_test = read_img(path) Copy the code

Output result: complete traversal of 11 folders and output training label vector and test label vector:

Linear changes are made to the values of training data and test data to improve the rate of machine learning, and labels are converted into vectors to calculate loss values with cross entropy:



x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 3) 
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3) 
input_shape = (img_rows, img_cols, 3)
x_train = x_train.astype('float32') 
x_test = x_test.astype('float32') 
x_train /= 255 
x_test /= 255 
y_train = keras.utils.to_categorical(y_train, num_class) 
y_test = keras.utils.to_categorical(y_test, num_class) Copy the code

Output the dimensions of training training feature matrix, training label vector, test feature matrix and test label vector:



print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)Copy the code

Running results:

Define CNN neural network model:



model = Sequential()
model.add(Conv2D(64, kernel_size=(3.3),activation='relu',input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2.2)))

model.add(Conv2D(32, (3.3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2.2)))
model.add(Dropout(0.25))

model.add(Conv2D(32, (3.3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2.2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(num_class, activation='softmax'))Copy the code

Compiling model: Cross entropy was used as the loss value, stochastic gradient descent was used as the optimizer, and the accuracy of prediction was used to define the model.



model.compile(loss='categorical_crossentropy',
              Optimizer = SGD (lr = 0.01, decay=1e-6, Momentum = 0.9, nesterov=True),
              metrics=['accuracy'])Copy the code

The model was trained and saved once: 64 samples were processed in a batch of the model, and the iteration was performed once, and the test set data was used for verification.



model.fit(x_train, y_train, batch_size=64, epochs=1, verbose=1, validation_data=(x_test, y_test))
model.save('Simpson.h5')Copy the code

The model training is carried out in a cycle, and the training is iterated once for each cycle, and the model is saved and read for 10 times. This is written to avoid the loss of all previous training results caused by memory overflow. This statement can be run repeatedly. Machine learning, commonly known as alchemy:



for i in range(0.10):
    print('The '+str(i)+' th Iteration')
    model=load_model('Simpson.h5')
    model.fit(x_train, y_train, batch_size=64, epochs=1, verbose=1, validation_data=(x_test, y_test))
    model.save('Simpson.h5')
    K.clear_session()Copy the code

Results: The accuracy of the model in the test set is 99.09%.

To identify applications using this model, simply call the model.predict() function.