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%.Я попробовал эти методы для повышения точности:
- нормализация партии
- снижение веса (регулятора L2)
- выпадение
но всеони не дали ожидаемого результата.
Нормализация партии дает ошибку:
/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>
С другой стороны, выпадение и регуляризатор вместо этого снижает точность !!