Keras: Accuracy Drops While Finetuning Inception - python

I am having trouble fine tuning an Inception model with Keras.
I have managed to use tutorials and documentation to generate a model of fully connected top layers that classifies my dataset into their proper categories with an accuracy over 99% using bottleneck features from Inception.
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
from keras import applications
# dimensions of our images.
img_width, img_height = 150, 150
#paths for saving weights and finding datasets
top_model_weights_path = 'Inception_fc_model_v0.h5'
train_data_dir = '../data/train2'
validation_data_dir = '../data/train2'
#training related parameters?
inclusive_images = 1424
nb_train_samples = 1424
nb_validation_samples = 1424
epochs = 50
batch_size = 16
def save_bottlebeck_features():
datagen = ImageDataGenerator(rescale=1. / 255)
# build bottleneck features
model = applications.inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=(img_width,img_height,3))
generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=False)
bottleneck_features_train = model.predict_generator(
generator, nb_train_samples // batch_size)
np.save('bottleneck_features_train', bottleneck_features_train)
generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=False)
bottleneck_features_validation = model.predict_generator(
generator, nb_validation_samples // batch_size)
np.save('bottleneck_features_validation', bottleneck_features_validation)
def train_top_model():
train_data = np.load('bottleneck_features_train.npy')
train_labels = np.array(range(inclusive_images))
validation_data = np.load('bottleneck_features_validation.npy')
validation_labels = np.array(range(inclusive_images))
print('base size ', train_data.shape[1:])
model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(1000, activation='relu'))
model.add(Dense(inclusive_images, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='Adam',
metrics=['accuracy'])
proceed = True
#model.load_weights(top_model_weights_path)
while proceed:
history = model.fit(train_data, train_labels,
epochs=epochs,
batch_size=batch_size)#,
#validation_data=(validation_data, validation_labels), verbose=1)
if history.history['acc'][-1] > .99:
proceed = False
model.save_weights(top_model_weights_path)
save_bottlebeck_features()
train_top_model()
Epoch 50/50
1424/1424 [==============================] - 17s 12ms/step - loss: 0.0398 - acc: 0.9909
I have also been able to stack this model on top of inception to create my full model and use that full model to successfully classify my training set.
from keras import Model
from keras import optimizers
from keras.callbacks import EarlyStopping
img_width, img_height = 150, 150
top_model_weights_path = 'Inception_fc_model_v0.h5'
train_data_dir = '../data/train2'
validation_data_dir = '../data/train2'
#how many inclusive examples do we have?
inclusive_images = 1424
nb_train_samples = 1424
nb_validation_samples = 1424
epochs = 50
batch_size = 16
# build the complete network for evaluation
base_model = applications.inception_v3.InceptionV3(weights='imagenet', include_top=False, input_shape=(img_width,img_height,3))
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(1000, activation='relu'))
top_model.add(Dense(inclusive_images, activation='softmax'))
top_model.load_weights(top_model_weights_path)
#combine base and top model
fullModel = Model(input= base_model.input, output= top_model(base_model.output))
#predict with the full training dataset
results = fullModel.predict_generator(ImageDataGenerator(rescale=1. / 255).flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=False))
inspection of the results from processing on this full model match the accuracy of the bottleneck generated fully connected model.
import matplotlib.pyplot as plt
import operator
#retrieve what the softmax based class assignments would be from results
resultMaxClassIDs = [ max(enumerate(result), key=operator.itemgetter(1))[0] for result in results]
#resultMaxClassIDs should be equal to range(inclusive_images) so we subtract the two and plot the log of the absolute value
#looking for spikes that indicate the values aren't equal
plt.plot([np.log(np.abs(x)+10) for x in (np.array(resultMaxClassIDs) - np.array(range(inclusive_images)))])
Here is the problem:
When I take this full model and attempt to train it, Accuracy drops to 0 even though validation remains above 99%.
model2 = fullModel
for layer in model2.layers[:-2]:
layer.trainable = False
# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
#model.compile(loss='binary_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), metrics=['accuracy'])
model2.compile(loss='categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
train_datagen = ImageDataGenerator(rescale=1. / 255)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
callback = [EarlyStopping(monitor='acc', min_delta=0, patience=3, verbose=0, mode='auto', baseline=None)]
# fine-tune the model
model2.fit_generator(
#train_generator,
validation_generator,
steps_per_epoch=nb_train_samples//batch_size,
validation_steps = nb_validation_samples//batch_size,
epochs=epochs,
validation_data=validation_generator)
Epoch 1/50
89/89 [==============================] - 388s 4s/step - loss: 13.5787 - acc: 0.0000e+00 - val_loss: 0.0353 - val_acc: 0.9937
and it gets worse as things progress
Epoch 21/50
89/89 [==============================] - 372s 4s/step - loss: 7.3850 - acc: 0.0035 - val_loss: 0.5813 - val_acc: 0.8272
The only thing I could think of is that somehow the training labels are getting improperly assigned on this last train, but I've successfully done this with similar code using VGG16 before.
I have searched over the code trying to find a discrepancy to explain why a model making accurate predictions over 99% of the time drops its training accuracy while maintaining validation accuracy during fine tuning, but I can't figure it out. Any help would be appreciated.
Information about the code and environment:
Things that are going to stand out as weird, but are meant to be that way:
There is only 1 image per class. This NN is intended to classify
objects whose environmental and orientation conditions are
controlled. Their is only one acceptable image for each class
corresponding to the correct environmental and rotational situation.
The test and validation set are the same. This NN is only ever
designed to be used on the classes it is being trained on. The images
it will process will be carbon copies of the class examples. It is my
intent to overfit the model to these classes
I am using:
Windows 10
Python 3.5.6 under Anaconda client 1.6.14
Keras 2.2.2
Tensorflow 1.10.0 as the backend
CUDA 9.0
CuDNN 8.0
I have checked out:
Keras accuracy discrepancy in fine-tuned model
VGG16 Keras fine tuning: low accuracy
Keras: model accuracy drops after reaching 99 percent accuracy and loss 0.01
Keras inception v3 retraining and finetuning error
How to find which version of TensorFlow is installed in my system?
but they appear unrelated.

Note: Since your problem is a bit strange and difficult to debug without having your trained model and dataset, this answer is just a (best) guess after considering many things that may have could go wrong. Please provide your feedback and I will delete this answer if it does not work.
Since the inception_V3 contains BatchNormalization layers, maybe the problem is due to (somehow ambiguous or unexpected) behavior of this layer when you set trainable parameter to False (1, 2, 3, 4).
Now, let's see if this is the root of the problem: as suggested by #fchollet, set the learning phase when defining the model for fine-tuning:
from keras import backend as K
K.set_learning_phase(0)
base_model = applications.inception_v3.InceptionV3(weights='imagenet', include_top=False, input_shape=(img_width,img_height,3))
for layer in base_model.layers:
layer.trainable = False
K.set_learning_phase(1)
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(1000, activation='relu'))
top_model.add(Dense(inclusive_images, activation='softmax'))
top_model.load_weights(top_model_weights_path)
#combine base and top model
fullModel = Model(input= base_model.input, output= top_model(base_model.output))
fullModel.compile(loss='categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
#####################################################################
# Here, define the generators and then fit the model same as before #
#####################################################################
Side Note: This is not causing any problem in your case, but keep in mind that when you use top_model(base_model.output) the whole Sequential model (i.e. top_model) is stored as one layer of fullModel. You can verify this by either using fullModel.summary() or print(fullModel.layers[-1]). Hence when you used:
for layer in model2.layers[:-2]:
layer.trainable = False
you are actually not freezing the last layer of base_model as well. However, since it is a Concatenate layer, and therefore does not have trainable parameters, no problem occurs and it would behave as you intended.

Like the previous reply, I'll try to share some thoughts to see whether it helps.
There are a couple of things that called my attention (and maybe are worth reviewing). Note: some of them should have given you issues with the separate models as well.
Correct if I'm wrong, but it seems you used sparse_categorical_crossentropy for the first training while you used categorical_crossentropy for the second one. Is it correct? Because I believe they assume labels differently (sparse assumes integers and the other assumes one-hot).
Have you tried to set the layers you added in the end as trainable = True? I know that you have already set the others to trainable = False, but maybe that's something worth checking too.
It seems the data generator is not making use of the default preprocessing function used in Inception v3, which uses a per-mean channel.
Have you tried any experiment using Functional instead of Sequential API?
I hope that helps.

Related

Keras model returns high validation accuracy while training, but accuracy is very low while evaluating

I am trying to train a simple MobileNetV3Small under keras.applications as shown below
base_model = keras.applications.MobileNetV3Small(
input_shape= INPUT_SHAPE,
alpha=.125,
include_top=False,
classes=1,
dropout_rate = 0.2,
weights=None)
x = keras.layers.Flatten()(base_model.output)
preds = keras.layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs=base_model.input, outputs=preds)
model.compile(loss="binary_crossentropy",
optimizer='RMSprop',
metrics=["binary_accuracy"])
train_datagen = ImageDataGenerator(
rescale=1.0 / 255,
rotation_range=40,
horizontal_flip=True,
vertical_flip=True,
)
train_generator = train_datagen.flow_from_directory(
os.path.join(DATA_ROOT, 'train'),
target_size=(56,56),
batch_size=128,
class_mode="binary",
)
validation_datagen = ImageDataGenerator(rescale=1.0 / 255)
validation_generator = validation_datagen.flow_from_directory(
os.path.join(DATA_ROOT, 'val'),
target_size=(56,56),
batch_size=128,
class_mode="binary",
)
model_checkpoint_callback = keras.callbacks.ModelCheckpoint(
filepath=SAVE_DIR,
save_weights_only=True,
monitor='val_binary_accuracy',
mode='max',
save_best_only=True)
es_callback = keras.callbacks.EarlyStopping(patience=10)
model.fit(train_generator,
epochs=100,
validation_data=validation_generator,
callbacks=[model_checkpoint_callback, es_callback],
shuffle=True)
When I train the model I got validation accuracy around 0.94. But when I call model.evaluate on the exact same validation data, the accuracy becomes 0.48. When I call model.predict with any data it outputs constant value 0.51...
There is nothing wrong with learning rate, optimizer or metrics. What could be wrong here?
EDIT:
After training when I run
pred_results = model.evaluate(validation_generator)
print(pred_results)
it gives me the output for 1 epoch trained network:
6/6 [==============================] - 1s 100ms/step - loss: 0.6935 -
binary_accuracy: 0.8461
However, when I save and load the model with either model.save() or tf.keras.models.save_model(). The output becomes something like this:
6/6 [==============================] - 2s 100ms/step - loss: 0.6935 -
binary_accuracy: 0.5028 [0.6935192346572876, 0.5027709603309631]
and output of the model.predict(validation_generator) is:
[[0.5080832] [0.5080832] [0.5080832] [0.5080832]
.
.
. [0.5080832] [0.5080832]]
What I've tried so far:
Used tf.keras.utils.image_dataset_from_directory() instead of ImageDataGenerator
Fixed tensorflow and numpy seeds globally.
Found similar problem in another SO post, and decreased momentum parameter of MobileNet BatchNormalization layers one by one.
for layer in model.layers[0].layers:
if type(layer) is tf.keras.layers.BatchNormalization:
layer.momentum = 0.9
First two moves do not have an effect, the after applying the third step, I get no longer same predictions for any input. However, evaluate() and predict() still have different accuracy values.
Have you tried setting shuffle = False in validation_datagen.flow_from_directory()? It's a little misleading but the .flow_from_directory() method shuffles by default, which is problematic when generating your validation dataset. This is shuffling your validation data when you try to call .predict. Whereas in your training loop, the .fit method implicitly DOESN'T shuffle the validation set.
The reason I think this is the issue, is because you state that calling .predict() on the validation set nets you ~.5 accuracy, and you're also running a binary classification (sigmoid output with binary cross entropy loss), which makes perfect sense IF you're (mistakenly) shuffling your validation data. Untrained binary classifiers on balanced datasets will usually do around 50% accuracy (.5 for 0, .5 for 1) since it's just guessing at that point.
Source: I've built and trained a lot of image classification models before, and this happened to me a lot.
It might be worth trying model.save_weights('directory') and then rebuilding your model (i think here that is re-running the base_model = ... code) through model.load_weights('directory'). That is what i do in my own models, and when i then do that, the accuracy/loss stay the exact same before and after saving and loading.
If you run pred_results = model.evaluate(validation_generator)
after you fit the model, the loaded weights at this moment are the ones updated on last training epoch.
What you have to do is after model.fit is loading the weights saved from
model_checkpoint_callback with something like
model = model.load_weights(SAVE_DIR)` # and then .evaluate
pred_results = model.evaluate(validation_generator)
print(pred_results)

Python- Mask Detection Nerual Network using Keras [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
So this is my first real try on neural networks with keras. I've been trying to make a classifier which decides wether someone is wearing a mask. Below i provided my model. I achived a training accuracy of about 87 percent and a validation accuracy of 85 percent. With own images it performs mostly well too. I created my own dataset with about 600 images for each of the two classes. However I have some questions.
Is it correct? Any suggested improvements? As I said this is my first neural network project i dont really know if i made anything wrong(which I most likely did)
How would i implemet this to predict on camera feed and not only images. Would I just get the frames from for example opencv and predict those? Any review and/or help is highly appreciated
This is my model:
from keras.layers import Dense, Input, Dropout, GlobalAveragePooling2D, Flatten, Conv2D, BatchNormalization, Activation, MaxPooling2D
from keras.models import Model, Sequential
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
from sklearn.model_selection import train_test_split
NAME = "mask-detection"
tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))
train_datagen = ImageDataGenerator(rescale=1 / 255)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(100, 100),
batch_size=32,
class_mode='categorical',
color_mode = "grayscale"
)
label_map = (train_generator.class_indices)
validation_generator = test_datagen.flow_from_directory(
'data/test',
target_size=(100, 100),
batch_size=32,
class_mode='categorical',
color_mode = "grayscale"
)
model=Sequential()
model.add(Conv2D(100,(3,3),padding='same', input_shape=(100,100,1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(100,(3,3), padding='same',))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(50,activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(
optimizer=RMSprop(lr=0.0001),
loss="binary_crossentropy",
metrics=["accuracy"])
model.fit(train_generator, epochs=20, callbacks=[tensorboard])
model.evaluate(validation_generator)
model.save('saved_model/model')
model.summary()
print(label_map)
And with this i predict the imgs:
import numpy as np
from keras.preprocessing import image
model = tf.keras.models.load_model('saved_model/model')
predictions = ["Mask", "No Mask"]
def predict_mask(img):
x = image.load_img(img, color_mode="grayscale", target_size=(100, 100))
x = image.img_to_array(x)
x = np.expand_dims(x, axis= 0)
x = np.array(x).astype("float32")/255
x = x.reshape([1, 100, 100, 1])
classes = model.predict(x)
return predictions[np.argmax(classes)]
img = "test.png"
print(predict_mask(img))
There are a number of things you can do. If you want to use your existing model then I recommend you use the keras callbacks ReduceLROnPlateau and ModelCheckpoint. The first enables you to use an adjustable learning rate. Set it up to monitor validation loss. A typical use is shown in the code below where if the validation loss fails to reduce on an epoch the learning rate is reduced by 50% .This allows you to use a larger learning rate initially and have it reduce automatically in later epochs. The second enables you to save the model with the lowest validation loss. Typical application is shown in the code below. Documentation for these callbacks is here. After training load this model to do predictions. If you want to get better results I recommend you try transfer learning. Many models are available with documentation here. I prefer to use the MobileNet model because it has only 4 million trainable parameters versus say 140 million for VGG16 and is about as accurate in most cases. Code below shows typical use.
rlrp=tf.keras.callbacks.ReduceLROnPlateau(
monitor='val_loss', factor=0.5, patience=1, verbose=0, mode='auto',
min_delta=0.0001, cooldown=0, min_lr=0)
checkpoint_filepath = '/tmp/checkpoint'
mcp=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_filepath,
monitor='val_loss', verbose=0, save_best_only=True,
save_weights_only=False, mode='auto', save_freq='epoch', options=None)
callbacks=[rlrp,mcp]
mobile = tf.keras.applications.mobilenet.MobileNet( include_top=False,
input_shape=(image_size,image_size,3),
pooling='max', weights='imagenet',
alpha=1, depth_multiplier=1,dropout=.5)
x=mobile.layers[-1].output
x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
predictions=Dense (2, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=predictions)
for layer in model.layers:
layer.trainable=True
model.compile(Adamax(lr=.05), loss='categorical_crossentropy', metrics=['accuracy'])
data=model.fit(x=train_generator, epochs=20, verbose=1,
callbacks=callbacks, validation_data=validation_generator, shuffle=True)

Vgg16 for gender detection (male,female)

We have used vgg16 and freeze top layers and retrain the last 4 layers on gender dataset 12k male and 12k female. It gives very low accuracy especially for male. We are using the IMDB dataset. On female test data it gives female as output but on male it gives same output.
vgg_conv=VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
Freeze the layers except the last 4 layers
for layer in vgg_conv.layers[:-4]:
layer.trainable = False
Create the model
model = models.Sequential()
Add the vgg convolutional base model
model.add(vgg_conv)
Add new layers
model.add(layers.Flatten())
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5)) model.add(layers.Dense(2, activation='softmax'))
nTrain=16850 nTest=6667
train_datagen = image.ImageDataGenerator(rescale=1./255)
test_datagen = image.ImageDataGenerator(rescale=1./255)
batch_size = 12 batch_size1 = 12
train_generator = train_datagen.flow_from_directory(train_dir, target_size=(224, 224), batch_size=batch_size, class_mode='categorical', shuffle=False)
test_generator = test_datagen.flow_from_directory(test_dir, target_size=(224, 224), batch_size=batch_size1, class_mode='categorical', shuffle=False)
model.compile(optimizer=optimizers.RMSprop(lr=1e-6), loss='categorical_crossentropy', metrics=['acc'])
history = model.fit_generator( train_generator, steps_per_epoch=train_generator.samples/train_generator.batch_size, epochs=3, validation_data=test_generator, validation_steps=test_generator.samples/test_generator.batch_size, verbose=1)
model.save('gender.h5')
Testing Code:
model=load_model('age.h5')
img=load_img('9358807_1980-12-28_2010.jpg', target_size=(224,224))
img=img_to_array(img)
img=img.reshape((1,img.shape[0],img.shape[1],img.shape[2]))
img=preprocess_input(img)
yhat=model.predict(img)
print(yhat.size)
label=decode_predictions(yhat)
label=label[0][0]
print('%s(%.2f%%)'% (label[1],label[2]*100))
Firstly, you are saving the model as gender.h5 and during testing you are loading the model age.h5. Probably you have added different code for the testing here.
Coming to improving the accuracy of the program -
Most importantly is that you are using loss = 'categorical_crossentropy', change it to loss = 'binary_crossentropy' in model.compile as you have just 2 classes. So your
model.compile(optimizer="adam",loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy']) will look like this.
Also change class_mode='categorical' to class_mode='binary' in flow_from_directory.
As categorical_crossentropy goes hand in hand with softmax activation in the last layer, and if you change the loss to binary_crossentropy the last activation should also be changed to sigmoid. So last layer should be Dense(1, activation='sigmoid').
You have added 2 Dense layers of 4096, this will add 4096 * 4096 = ‭16,777,216‬ weights to be learnt by the model. Reduce them may be to 1026 and 512 respectively.
You have added Dropout layer of 0.5, that is to keep the 50% of neurons off during the epoch. That is huge number. Better is to drop off the Dropout layer and use to only if your model is overfitting.
Set batch_size = 1. As you have very less input let every epoch have same number of steps as input records.
Use Data Augmentation technique like horizontal_flip, vertical_flip, shear_range, zoom_range of ImageDataGenerator to generate the new batches of training and validation images during every epoch.
Train your model for large number of epoch. You are just training for epoch=3, that is too less for learning the weights. Train for epoch=50 and later trim the number.
Hope this answers your question. Happy Learning.

I am getting a very high loss in training (& testing) my auto-encoder

I am getting a very high loss (170+). I am making an auto-encoder with 3 hidden layers and using SGD as my optimiser. I have used cross_entropy as my loss function. Also initially, the accuracy I am getting is pretty good (about 0.88) but it decreases after almost every epoch.
Here is my code:
encoding_dim=8
i=Input(shape=(60,))
encoded=Dense(30,activation='sigmoid')(i)
encoded1=Dense(15,activation='sigmoid')(encoded)
encoded2=Dense(8,activation='relu')(encoded1)
#encoded=Dense(encoding_dim,activation='sigmoid')(encoded2)
decoded=Dense(15,activation='sigmoid')(encoded2)
decoded2 =Dense(30,activation='sigmoid')(decoded)
decoded3 =Dense(60,activation='sigmoid')(decoded2)
autoencoder = Model(i, decoded3)
ec = Model(i,encoded)
encoded_input=Input(shape=(encoding_dim,))
decoder_layer=autoencoder.layers[-3](encoded_input)
decoder_layer=autoencoder.layers[-2](decoder_layer)
decoder_layer=autoencoder.layers[-1](decoder_layer)
decoder = Model(encoded_input, decoder_layer)
from keras.optimizers import SGD
opt = SGD(lr=0.06)
#model.compile(loss = "categorical_crossentropy", optimizer = opt)
autoencoder.compile(loss = "categorical_crossentropy", optimizer = opt,metrics=['accuracy'])
autoencoder.fit(X_Train, X_Train,
epochs=200,
batch_size=200,
shuffle=True,
validation_data=(X_Test, X_Test))
#encoded_out= ec.predict(X_Test)
#decoded_out=decoder.predict(encoded_out)
At least in principle, sigmoid should only be used for your last decoding layer (here decoded3) - see the examples in Building Autoencoders in Keras. So, change all your other activations to relu.
Also, accuracy does not make sense in autoencoders - just remove it from your model compilation and focus on the loss.

Face recognition with keras

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')
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.

Categories

Resources