简体   繁体   English

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

[英]How should i increase accuracy of face recognition using cnn?

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)

With this architecture i'm getting 80% accuracy.通过这种架构,我获得了 80% 的准确率。 I tried these methods to increase accuracy:我尝试了这些方法来提高准确性:

  1. Batch normalization批量归一化
  2. Weight Decay(L2 regularizer)权重衰减(L2 正则化器)
  3. Dropout辍学

But all of them failed to give expected result.但是他们都没有给出预期的结果。

Batch normalization gives error:批量标准化给出错误:

/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>

On the other hand Dropout and regularizer reduces the accuracy instead!!另一方面,Dropout 和正则化器反而降低了准确性!!

First you have import the BatchNormalization layer from keras.layers.BatchNormalization.首先,您从 keras.layers.BatchNormalization 导入 BatchNormalization 层。 You can take the following measures to increase the accuracy.您可以采取以下措施来提高准确性。

  1. Decrease the batch size.减小批量大小。
  2. Decrease the learning rate to a smaller number like 0.001 or 0.0001.将学习率降低到较小的数字,例如 0.001 或 0.0001。
  3. Slightly increase the dropout rates to 0.5 or 0.6.将丢失率略微增加到 0.5 或 0.6。
  4. Use the LeakyReLU activation with alpha ( negative slope ) 0.2 or 0.01.使用 alpha(负斜率)0.2 或 0.01 的 LeakyReLU 激活。
  5. Try using AdaGrad or Nadam optimizers.尝试使用 AdaGrad 或 Nadam 优化器。
  6. Add two Conv2d layers and then add the pooling layer.添加两个 Conv2d 层,然后添加池化层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM