Cat and Dog Image Classifier val_accuracy stays the same

Tell us what’s happening:
While training the model val_accuracy always stays at 0.5 while val_loss, loss and accuracy grow. I searched some possible Solutions but none of them change the problem.

Your code so far

base_model = tf.keras.applications.MobileNetV2(input_shape=(IMG_HEIGHT, IMG_WIDTH, 3), include_top=False, weights="imagenet")
base_model.trainable = False

model = Sequential([
    base_model,
    tf.keras.layers.GlobalAveragePooling2D(),
    Dense(1)
])

model.summary()
model.compile(optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.0001), loss = tf.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy'])

history = model.fit(train_data_gen, steps_per_epoch=total_train//batch_size, epochs=epochs, validation_data=val_data_gen, validation_steps=total_val//batch_size)
Epoch 1/15
15/15 [==============================] - 17s 1s/step - loss: 0.6744 - accuracy: 0.5887 - val_loss: 0.8840 - val_accuracy: 0.5000
Epoch 2/15
15/15 [==============================] - 16s 1s/step - loss: 0.6594 - accuracy: 0.6100 - val_loss: 0.8880 - val_accuracy: 0.5000
Epoch 3/15
15/15 [==============================] - 16s 1s/step - loss: 0.6456 - accuracy: 0.6218 - val_loss: 0.8851 - val_accuracy: 0.5000
Epoch 4/15
15/15 [==============================] - 16s 1s/step - loss: 0.6349 - accuracy: 0.6213 - val_loss: 0.8839 - val_accuracy: 0.5000
Epoch 5/15
15/15 [==============================] - 16s 1s/step - loss: 0.6273 - accuracy: 0.6298 - val_loss: 0.8965 - val_accuracy: 0.5000
Epoch 6/15
15/15 [==============================] - 16s 1s/step - loss: 0.6125 - accuracy: 0.6469 - val_loss: 0.9007 - val_accuracy: 0.5000
Epoch 7/15
15/15 [==============================] - 16s 1s/step - loss: 0.6037 - accuracy: 0.6287 - val_loss: 0.9031 - val_accuracy: 0.5000
Epoch 8/15
15/15 [==============================] - 16s 1s/step - loss: 0.6046 - accuracy: 0.6453 - val_loss: 0.9073 - val_accuracy: 0.5000
Epoch 9/15
15/15 [==============================] - 16s 1s/step - loss: 0.5900 - accuracy: 0.6592 - val_loss: 0.9126 - val_accuracy: 0.5000
Epoch 10/15
15/15 [==============================] - 16s 1s/step - loss: 0.5577 - accuracy: 0.6790 - val_loss: 0.9146 - val_accuracy: 0.5000
Epoch 11/15
15/15 [==============================] - 16s 1s/step - loss: 0.5597 - accuracy: 0.6843 - val_loss: 0.9205 - val_accuracy: 0.5000
Epoch 12/15
15/15 [==============================] - 16s 1s/step - loss: 0.5453 - accuracy: 0.6790 - val_loss: 0.9286 - val_accuracy: 0.5000
Epoch 13/15
15/15 [==============================] - 16s 1s/step - loss: 0.5401 - accuracy: 0.6886 - val_loss: 0.9292 - val_accuracy: 0.5000
Epoch 14/15
15/15 [==============================] - 16s 1s/step - loss: 0.5431 - accuracy: 0.7041 - val_loss: 0.9327 - val_accuracy: 0.5000
Epoch 15/15
15/15 [==============================] - 16s 1s/step - loss: 0.5400 - accuracy: 0.6923 - val_loss: 0.9406 - val_accuracy: 0.5000

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Cat and Dog Image Classifier

Link to the challenge:

Welcome to the forums @vkaramanis.

Your statistics are very similar to ones I generated from a different model and it passed the classification test at ~70%. Are you passing the test at the end? Once I passed the test, I was ready to move on, and did.

After reading a lot more on this, I still don’t know the answer. Your loss and accuracy are trending in the correct directions. Everything I saw on this topic was varied opinions on using sigmoid or softmax activation on the final dense layer or a failure to shuffle the validation images (should be the default if using tensorflow.keras.preprocessing.image.ImageDataGenerator.flow_from_directory()) or some other similar image processing/loading problem. I think I would investigate the image loading/processing of the validation images first and see what happens.

The other consideration is that you are using a pre-defined and trained base model. With pre-trained weights, I would not expect much change in the validation data anyway.

Good luck.

Hi Jeremy,

I passed the test, i just found peculiar this problem and as you said i could not find any solution on that. i tried changing the activation function, batch size, learning rate. none of that solve the issue.
I don not think is the pretrained model as this happen to you too.
Maybe is something wrong with the dataset or just a bug.

Thanks for your reply! :slight_smile: