[英]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:我尝试了这些方法来提高准确性:
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.您可以采取以下措施来提高准确性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.