如何使用cnn提高人脸识别的准确性?

时间:2018-12-17 12:27:28

标签: keras deep-learning computer-vision conv-neural-network

from tensorflow.python.keras.applications.inception_v3 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from keras.layers.normalization import BatchNormalization
from keras import regularizers
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'


data_gen = ImageDataGenerator(preprocessing_function=preprocess_input, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)

train_gen = data_gen.flow_from_directory('/home/bg22/PycharmProjects/KUNAL/dataset/training_set', target_size=(64,64), batch_size=16, class_mode='categorical')
test_gen = data_gen.flow_from_directory('/home/bg22/PycharmProjects/KUNAL/dataset/test_set', target_size=(64,64), batch_size=16, class_mode='categorical')

model = Sequential()

model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(64, 64, 3)))
#model.add(BatchNormalization())
#model.add(Dropout(0.5))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
#model.add(BatchNormalization())
#model.add(Dropout(0.5))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Conv2D(16, kernel_size=(3, 3), activation='relu'))
#model.add(BatchNormalization())
#model.add(Dropout(0.5))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Conv2D(16, kernel_size=(3, 3), activation='relu'))
#model.add(BatchNormalization())
#model.add(Dropout(0.5))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())

#model.add(Dense(16, activation='relu', kernel_regularizer=regularizers.l2(0.01)))
model.add(Dense(2, activation='softmax'))

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

model.fit_generator(train_gen, epochs= 10,  validation_data=test_gen)

使用这种架构,我获得了80%的准确性。 我尝试了以下方法来提高准确性:

  1. 批量归一化
  2. 体重衰减(L2调节器)
  3. 退出

但是所有人都未能给出预期的结果。

批量归一化给出错误:

/usr/bin/python3.5 "/home/bg22/PycharmProjects/KUNAL/Face Recognition.py"

Using TensorFlow backend.

Found 8000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.

Traceback (most recent call last):

  File "/home/bg22/PycharmProjects/KUNAL/Face Recognition.py", line 20, in <module>

   model.add(BatchNormalization())

  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/checkpointable/base.py", line 364, in _method_wrapper

   method(self, *args, **kwargs)

  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine/sequential.py", line 130, in add

   'Found: ' + str(layer))

TypeError: The added layer must be an instance of class Layer. Found: keras.layers.normalization.BatchNormalization object at 0x7fddf526e9e8>

另一方面,辍学和正则化会降低准确性!

1 个答案:

答案 0 :(得分:0)

首先,您已从keras.layers.BatchNormalization导入BatchNormalization层。您可以采取以下措施来提高准确性。

  1. 减小批次大小。
  2. 将学习率降低到较小的数字,例如0.001或0.0001。
  3. 将辍学率略微提高到0.5或0.6。
  4. 使用LeakyReLU激活的alpha(负斜率)为0.2或0.01。
  5. 尝试使用AdaGrad或Nadam优化器。
  6. 添加两个Conv2d层,然后添加池化层。