# Why is my CNN not training

Hi I am trying to train a CNN to differentiate between pictures of dogs and pictures of cats. It does not seem to learn anything no matter how I change the architecture. I have used the following code to create the datasets:

train_generator = tf.keras.preprocessing.image_dataset_from_directory(
G:/Dogs_cats/Train',
labels="inferred",
label_mode="binary",
class_names=None,
color_mode="rgb",
batch_size=150,
image_size=(350, 350),
shuffle=True,
seed=1)
# validation_split=0.2,
# subset='validation',
# interpolation="bilinear",
#  follow_links=False,)

validation_generator = tf.keras.preprocessing.image_dataset_from_directory(
'G:/Dogs_cats/Validation',
labels="inferred",
label_mode="binary",
class_names=None,
color_mode="rgb",
batch_size=150,
image_size=(350, 350),
shuffle=True,
seed=1)


I then created my CNN model:

model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(60, (3,3), activation='relu', input_shape=(350, 350, 3),padding='same',kernel_initializer='he_uniform'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(5, (1,1), activation='relu', padding='same',kernel_initializer='he_uniform'),
tf.keras.layers.Conv2D(60, (3,3), activation='relu',padding='same',kernel_initializer='he_uniform'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(60, (3,3), activation='relu',padding='same',kernel_initializer='he_uniform'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(412, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])

from tensorflow.keras.optimizers import Adam

model.compile(loss='binary_crossentropy',
optimizer=Adam(),
metrics=['BinaryAccuracy'])


and trained it:

history = model.fit_generator(train_generator,
epochs=450,
callbacks=[early_stopping],
verbose=1,
validation_data=validation_generator)


However the model doesn't improve at all or improve its accuracy on either the training or validation datasets.

Epoch 1/450
134/134 [==============================] - 116s 846ms/step - loss: 3993.2059 - binary_accuracy: 0.5017 - val_loss: 0.7324 - val_binary_accuracy: 0.4792
Epoch 2/450
134/134 [==============================] - 117s 845ms/step - loss: 0.8022 - binary_accuracy: 0.5158 - val_loss: 0.7285 - val_binary_accuracy: 0.4784
Epoch 3/450
134/134 [==============================] - 99s 719ms/step - loss: 0.7572 - binary_accuracy: 0.5128 - val_loss: 0.7252 - val_binary_accuracy: 0.4762
Epoch 4/450
134/134 [==============================] - 92s 666ms/step - loss: 0.7219 - binary_accuracy: 0.5165 - val_loss: 0.7219 - val_binary_accuracy: 0.4750
Epoch 5/450
134/134 [==============================] - 93s 671ms/step - loss: 0.7132 - binary_accuracy: 0.5177 - val_loss: 0.7134 - val_binary_accuracy: 0.4734
Epoch 6/450
134/134 [==============================] - 93s 677ms/step - loss: 0.7080 - binary_accuracy: 0.5160 - val_loss: 0.7009 - val_binary_accuracy: 0.4826
Epoch 7/450
134/134 [==============================] - 93s 675ms/step - loss: 0.6989 - binary_accuracy: 0.5205 - val_loss: 0.7013 - val_binary_accuracy: 0.4856
Epoch 8/450
134/134 [==============================] - 93s 676ms/step - loss: 0.6947 - binary_accuracy: 0.5162 - val_loss: 0.7015 - val_binary_accuracy: 0.4852


What can I do to make the CNN learn? I am not sure if there is a problem with the way I am loading my dataset or with the way I am running the model.

• I can know how many epoch you run? Feb 21 at 15:06

## 1 Answer

A rule of thumb, as you go deeper, number of filters increase and the size of filter remains same or increases. You don't follow both of them. This will help your network learn.

Then, consider increasing the number of filters in proper fashion if still your network is not learning.

• Please accept the answer, if it has solved your problem. Feb 25 at 13:08