Why is this keras network not "learning"?

I am trying to build a convolutional neural network to classify cats and dogs(a very basic problem because I want to learn). One of the approaches that I'm trying is to have 2 output neurons to check the class(instead of using only 1 and make 0 --> cats and 1--> dogs for example) but for some reason the network is not learning, could someone help me?

This is the model:

from keras.models import Sequential
from keras.layers import Input, Dropout, Flatten, Conv2D, MaxPooling2D, Dense, Activation
from keras.optimizers import RMSprop,Adam
from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping
from keras.utils import np_utils

optimizer = Adam(lr=1e-4)
objective = 'categorical_crossentropy'


def classifier():
    
    model = Sequential()
    
    model.add(Conv2D(64, 3, padding='same',input_shape=train.shape[1:],activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))

    model.add(Conv2D(256, 3, padding='same',activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
    
    model.add(Conv2D(256, 3, padding='same',activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
    
    model.add(Conv2D(256, 3, padding='same',activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
    

    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))

    model.add(Dense(2))
    model.add(Activation('softmax'))
    
    print("Compiling model...")
    model.compile(loss=objective, optimizer=optimizer, metrics=['accuracy'])
    return model

print("Creating model:")
model = classifier()

this is the main loop

from keras.models import Sequential
from keras.layers import Input, Dropout, Flatten, Conv2D, MaxPooling2D, Dense, Activation
from keras.optimizers import RMSprop
from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping
from keras.utils import np_utils

epochs = 5000
batch_size = 16

class LossHistory(Callback):
    def on_train_begin(self, logs={}):
        self.losses = []
        self.val_losses = []
        
    def on_epoch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
        self.val_losses.append(logs.get('val_loss'))

early_stopping = EarlyStopping(monitor='val_loss', patience=4, verbose=1, mode='min')        
       

def run():
    
    history = LossHistory()
    print("running model...")
    model.fit(train, labels, batch_size=batch_size, epochs=epochs,
              validation_split=0.10, verbose=2, shuffle=True, callbacks=[history, early_stopping])
    
    print("making predictions on test set...")
    predictions = model.predict(test, verbose=0)
    return predictions, history

predictions, history = run()

loss = history.losses
val_loss = history.val_losses

and here is one example of the input labels:

array([[1, 0],
       [0, 1],
       [1, 0],
       ..., 
       [0, 1],
       [0, 1],
       [0, 1]])

PS: don't bother about the input format, because using the same input it works for a binary classifier.

Upvotes: 3

Views: 654

Answers (1)

Mitiku
Mitiku

Reputation: 5412

Your rate argument of dropout layer is too large. Dropout layers are used as regularization technique for deep learning neural networks, and to overcome overfitting. Your rate argument specifies how much percentage to drop from previous layer's activation while training. 0.5 rate means dropping 50% of the previous layer's activation. Though some times this large percentage of rate argument is feasible, some times it hinders the learning rate of neural networks. So you should be careful while choosing the rate argument of dropout layer.

Upvotes: 2

Related Questions