Reputation: 717
Below is my face recognition model. I get several issues while training my training data on it. My dataset contains images of me. When I train it, validation accuracy is 100%. And also its prediction is bad. What can I do to solve this problem?
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32,(3,3),activation='relu',
input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Dropout(0.5))
model.add(layers.Conv2D(64,(3,3),activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128,(3,3),activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Dropout(0.5))
model.add(layers.Conv2D(128,(3,3),activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(512,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
print(model.summary())
from keras import optimizers
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size = (150,150),
batch_size=20)
validation_generator = val_datagen.flow_from_directory(
validation_dir,
target_size = (150,150),
batch_size=20)
history = model.fit_generator(
train_generator,
steps_per_epoch = 100,
epochs = 3,
validation_data = validation_generator,
validation_steps = 50)
model.save('/home/monojit/Desktop/me3.h5')
Upvotes: 0
Views: 1371
Reputation: 510
How large is the dataset that is being used? A small dataset might be the problem, or your model architecture if the model is not generalizing well. Also, you might look at image augmentation using the ImageDataGenerator
, see the blog post that i'll link to in a bit.
If the purpose of this project is to get as high accuracy as possible, without explicitly learning how CNN and their different layers work, then i would suggest the following. As you are working with images, you might not want to re-invent the wheel. Go on and take a pre-trained convolutional neural network, then train that on your imaged. This will yield you way higher accuracy with less epochs, than an untrained network. A great blog post can be found here Keras Cats vs dogs. Now this tutorial is cats vs dogs. But you can use (almost, depending on your input images) the exact same code for your problem.
Upvotes: 1