python – Neural network: I have a very low accuracy

I’m trying to make a face recognition project. I have 1198 images for training and 260 images for testing. When I try to train my data, the model trained just 36 images. Also I have a very low accuracy. How can I fix it please help me (used haarcascade face detector).

    import numpy as np
    import tensorflow as tf
    from tensorflow import keras
    from tensorflow.keras.models import Sequential,Model
    from tensorflow.keras.layers import ZeroPadding2D,Convolution2D,MaxPooling2D
    from tensorflow.keras.layers import 
    Dense,Dropout,Softmax,Flatten,Activation,BatchNormalization
    from tensorflow.keras.preprocessing.image import load_img,img_to_array
    from tensorflow.keras.applications.imagenet_utils import preprocess_input
    import tensorflow.keras.backend as K
    from keras.callbacks import EarlyStopping, ModelCheckpoint
    import numpy as np
    import matplotlib.pyplot as plt
    from keras.layers import Dense, Conv2D, MaxPool2D, GlobalMaxPool2D, Dropout, Flatten, 
    Activation

Define VGG_FACE_MODEL architecture

model=Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(224,224, 3)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))

Load VGG Face model weights

model.load_weights('vgg_face_weights.h5')
model.summary()

Output:

Model: "sequential"

Layer (type)                Output Shape              Param #   

================================================== ===============

 zero_padding2d (ZeroPadding  (None, 226, 226, 3)      0         
 2D)    

                                                     
                                                             
conv2d (Conv2D)             (None, 224, 224, 64)      1792      
                                                             
zero_padding2d_1 (ZeroPaddi  (None, 226, 226, 64)     0         
ng2D)                                                           
                                                             
conv2d_1 (Conv2D)           (None, 224, 224, 64)      36928     
                                                             
max_pooling2d (MaxPooling2D  (None, 112, 112, 64)     0         
)                                                               
                                                             
zero_padding2d_2 (ZeroPaddi  (None, 114, 114, 64)     0         
ng2D)                                                           
                                                             
conv2d_2 (Conv2D)           (None, 112, 112, 128)     73856     
                                                             
zero_padding2d_3 (ZeroPaddi  (None, 114, 114, 128)    0         
ng2D)                                                           
                                                             
conv2d_3 (Conv2D)           (None, 112, 112, 128)     147584    
                                                             
max_pooling2d_1 (MaxPooling  (None, 56, 56, 128)      0         
2D)                                                             
                                                             
zero_padding2d_4 (ZeroPaddi  (None, 58, 58, 128)      0         
ng2D)                                                           
                                                             
conv2d_4 (Conv2D)           (None, 56, 56, 256)       295168    
                                                             
zero_padding2d_5 (ZeroPaddi  (None, 58, 58, 256)      0         
ng2D)                                                           
                                                             
conv2d_5 (Conv2D)           (None, 56, 56, 256)       590080    
                                                             
zero_padding2d_6 (ZeroPaddi  (None, 58, 58, 256)      0         
ng2D)                                                           
                                                             
conv2d_6 (Conv2D)           (None, 56, 56, 256)       590080    
                                                             
max_pooling2d_2 (MaxPooling  (None, 28, 28, 256)      0         
2D)                                                             
                                                             
zero_padding2d_7 (ZeroPaddi  (None, 30, 30, 256)      0         
ng2D)                                                           
                                                             
conv2d_7 (Conv2D)           (None, 28, 28, 512)       1180160   
                                                             
zero_padding2d_8 (ZeroPaddi  (None, 30, 30, 512)      0         
ng2D)                                                           
                                                             
conv2d_8 (Conv2D)           (None, 28, 28, 512)       2359808   
                                                             
zero_padding2d_9 (ZeroPaddi  (None, 30, 30, 512)      0         
ng2D)                                                           
                                                             
conv2d_9 (Conv2D)           (None, 28, 28, 512)       2359808   
                                                             
max_pooling2d_3 (MaxPooling  (None, 14, 14, 512)      0         
2D)                                                             
                                                             
zero_padding2d_10 (ZeroPadd  (None, 16, 16, 512)      0         
ing2D)                                                          
                                                             
conv2d_10 (Conv2D)          (None, 14, 14, 512)       2359808   
                                                             
zero_padding2d_11 (ZeroPadd  (None, 16, 16, 512)      0         
ing2D)                                                          
                                                             
conv2d_11 (Conv2D)          (None, 14, 14, 512)       2359808   
                                                             
zero_padding2d_12 (ZeroPadd  (None, 16, 16, 512)      0         
ing2D)                                                          
                                                             
conv2d_12 (Conv2D)          (None, 14, 14, 512)       2359808   
                                                             
max_pooling2d_4 (MaxPooling  (None, 7, 7, 512)        0         
2D)                                                             
                                                             
conv2d_13 (Conv2D)          (None, 1, 1, 4096)        102764544 
                                                             
dropout (Dropout)           (None, 1, 1, 4096)        0         
                                                             
conv2d_14 (Conv2D)          (None, 1, 1, 4096)        16781312  
                                                             
dropout_1 (Dropout)         (None, 1, 1, 4096)        0         
                                                             
conv2d_15 (Conv2D)          (None, 1, 1, 2622)        10742334  
                                                             
flatten (Flatten)           (None, 2622)              0         
                                                             
activation (Activation)     (None, 2622)              0         
                                                             

================================================== ===============

Total params: 145,002,878 Trainable params: 145,002,878 Non-trainable params: 0

Remove Last Softmax layer and get model upto last flatten layer with outputs 2622 units

vgg_face=Model(inputs=model.layers[0].input,outputs=model.layers[-2].output)
 #Prepare Training Data
X_train=[]
Y_train=[]
person_folders=os.listdir(path+'/datasetkirp4/')
person_rep=dict()
for i,person in enumerate(person_folders):
person_rep[i]=person
image_names=os.listdir('datasetkirp4/'+person+"https://stackoverflow.com/")
for image_name in image_names:
img=load_img(path+'/datasetkirp4/'+person+"https://stackoverflow.com/"+image_name,target_size=(224,224))
img=img_to_array(img)
img=np.expand_dims(img,axis=0)
img=preprocess_input(img)
img_encode=vgg_face(img)
X_train.append(np.squeeze(K.eval(img_encode)).tolist())
Y_train.append(i)
X_train=np.array(X_train)
Y_train=np.array(Y_train)

SAME PROCESS FOR TESTING DATA…

# Save test and train data for later use
np.save('train_data',X_train)
np.save('train_labels',Y_train)
np.save('test_data',X_test)
np.save('test_labels',Y_test)
# Softmax regressor to classify images based on encoding 
classifier_model=Sequential()



classifier_model.add(Dense(units=100,input_dim=X_train.shape[1],kernel_initializer="glorot_uniform"))
classifier_model.add(BatchNormalization())
classifier_model.add(Activation('tanh'))
classifier_model.add(Dropout(0.3))
classifier_model.add(Dense(units=10,kernel_initializer="glorot_uniform"))
classifier_model.add(BatchNormalization())
classifier_model.add(Activation('tanh'))
classifier_model.add(Dropout(0.2))
classifier_model.add(Dense(units=260,kernel_initializer="he_uniform"))
classifier_model.add(Activation('softmax'))

classifier_model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),optimizer="nadam",metrics=['accuracy'])
history=classifier_model.fit(X_train,Y_train,epochs=100,validation_data=(X_test,Y_test))
Epoch 1/100
36/36 [==============================] - 2s 17ms/step - loss: 5.6887 - accuracy: 0.0000e+00 - 
val_loss: 5.4093 - val_accuracy: 0.0000e+00
Epoch 2/100
36/36 [==============================] - 0s 11ms/step - loss: 4.9881 - accuracy: 0.0000e+00 - 
val_loss: 4.8928 - val_accuracy: 0.0000e+00
Epoch 3/100
36/36 [==============================] - 0s 11ms/step - loss: 4.6862 - accuracy: 0.0000e+00 - 
val_loss: 4.7551 - val_accuracy: 0.0000e+00
Epoch 4/100
36/36 [==============================] - 0s 12ms/step - loss: 4.4774 - accuracy: 0.0000e+00 - 
val_loss: 4.6989 - val_accuracy: 0.0000e+00
Epoch 5/100
36/36 [==============================] - 0s 12ms/step - loss: 4.3038 - accuracy: 0.0000e+00 - 
val_loss: 4.5286 - val_accuracy: 0.0000e+00
Epoch 6/100
36/36 [==============================] - 0s 10ms/step - loss: 4.1503 - accuracy: 0.0000e+00 - 
val_loss: 4.3967 - val_accuracy: 0.0000e+00
Epoch 7/100

Epoch 99/100
36/36 [==============================] - 0s 11ms/step - loss: 1.6600 - accuracy: 0.0026 - 
val_loss: 2.9966 - val_accuracy: 0.0080
Epoch 100/100
36/36 [==============================] - 0s 11ms/step - loss: 1.6525 - accuracy: 0.0052 - 
val_loss: 3.0544 - val_accuracy: 0.0040

Leave a Comment