Final Assignment C2W1 Training Accuracy is not increasing

Hi Team,

Hope you are doing good.
My training accuracy is not getting near to 95% -
I am using 3 Layer and I used the Adam as optimizer - but still its not increasing.
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.001), loss=‘binary_crossentropy’, metrics=[‘accuracy’])

Graded Functions

Filename: Graded Functions
50/50Score: 50 of 50
Show grader output

Training History

Filename: Training History
25/50Score: 25 of 50
Hide grader output
Grader output
Failed test case: training accuracy under 95%. Expected: 0.95 or above, but got: 0.8589652180671692.

3 Likes

Hi @sundharazure !
I glad to see you here!
How many epochs are you using ?

1 Like

Hi,
Thanks, Hope you are doing good.
Epoch 11/15
1125/1125 [==============================] - 175s 156ms/step - loss: 0.3970 - accuracy: 0.8202 - val_loss: 0.3622 - val_accuracy: 0.8440
Epoch 12/15
1125/1125 [==============================] - 177s 157ms/step - loss: 0.3804 - accuracy: 0.8290 - val_loss: 0.3745 - val_accuracy: 0.8312
Epoch 13/15
1125/1125 [==============================] - 178s 158ms/step - loss: 0.3686 - accuracy: 0.8379 - val_loss: 0.4345 - val_accuracy: 0.8024
Epoch 14/15
1125/1125 [==============================] - 178s 158ms/step - loss: 0.3672 - accuracy: 0.8368 - val_loss: 0.3394 - val_accuracy: 0.8444
Epoch 15/15
1125/1125 [==============================] - 178s 158ms/step - loss: 0.3581 - accuracy: 0.8424 - val_loss: 0.3479 - val_accuracy: 0.8444

1 Like

The issue here was that image augmentation was being used, which resulted in the training accuracy not reaching the necessary threshold within 15 epochs. For Week 1 be sure to not use data augmentation since this will be the focus of Week 2.

1 Like

Hi can you please tell where is image augmentation being used in the code and do we need to explicitly remove it? As of now I am using 4 conv layers but still not able to go beyond 91% training accuracy.

2 Likes

Hi @Sonam_Sharma, within the train_val_generators function when instantiating the ImageDataGenerator you should only set the rescale argument to not use data augmentation. Setting additional parameters will most likely result in image augmentation being used.

Hi, I am not using data augmentation but the training accuracy is still not increasing.
Here is my code for ImageDataGenerator:

train_datagen = ImageDataGenerator(rescale = 1.0 / 255.)

My solution is adding more convolutional layers with padding = ‘same’. padding will prevent the output from getting too small.

1 Like

i can help, set batch size 90 from training and 10 for validation

5 Likes

Thanks, it works. I was stuck too but this just saved me.

1 Like

Regarding C2W1AND C2W2 training accuracy, I too am unable to get either training accuracy above the required. I got to 76% on the last try.
I have added two convolutional layers, changed the learning rate, the batch sizes and even changed one of the augmentation parameters in the Week Two network.
Please give me something to try.
As an aside, I now see how a professional can become frustrated waiting for the network to train! I’ve even had to buy GPU time to keep trying to get this thing to behave.

1 Like

A related problem is that the validation accuracy goes to 1.0 almost immediately and then fluctuates between 1.0 and 0.5. Strange!
Epoch 8/15
250/250 [==============================] - 72s 288ms/step - loss: 0.3884 - accuracy: 0.8241 - val_loss: 0.0958 - val_accuracy: 1.0000
Epoch 9/15
250/250 [==============================] - 73s 292ms/step - loss: 0.3637 - accuracy: 0.8389 - val_loss: 0.3476 - val_accuracy: 1.0000
Epoch 10/15
250/250 [==============================] - 72s 287ms/step - loss: 0.3465 - accuracy: 0.8473 - val_loss: 0.5073 - val_accuracy: 0.5000
Epoch 11/15
250/250 [==============================] - 72s 286ms/step - loss: 0.3241 - accuracy: 0.8609 - val_loss: 0.6226 - val_accuracy: 0.5000
Epoch 12/15
250/250 [==============================] - 71s 284ms/step - loss: 0.3126 - accuracy: 0.8667 - val_loss: 0.8417 - val_accuracy: 0.5000
Epoch 13/15
250/250 [==============================] - 73s
292ms/step - loss: 0.2987 - accuracy: 0.8723 - val_loss: 0.1062 - val_accuracy: 1.0000
Epoch 14/15
250/250 [==============================] - 72s 288ms/step - loss: 0.2730 - accuracy: 0.8837 - val_loss: 0.3205 - val_accuracy: 1.0000
Epoch 15/15
250/250 [==============================] - 73s 291ms/step - loss: 0.2563 - accuracy: 0.8942 - val_loss: 0.0998 - val_accuracy: 1.0000

1 Like

Thanks, that seems to be the key.
It’s not obvious, as the lesson’s notebooks always use batch sizes of 20. (I will get back to the lesson where this parameter was introduced to understand why it has impact on training accuracy).

1 Like

Ok, I found the reference :

In Coursera | Online Courses & Credentials From Top Educators. Join for Free | Coursera, we learn that
number of training images
must equal
training batch size * steps_per_epoch

Similarly,
number of validation images
must equal
validation batch size * steps_per_epoch

So, since steps_per_epoch will be the same (I suppose it is computed by model.fit, since we don’t pass it), batch size must be proportional with the number of images in the corresponding image set.

It means that the lesson’s notebook is doing something weird (not to say “wrong”), since it defines batches of 20 for both sets, whereas training set has 9 times the number of images of the validation set. Which means it will be 9 times more batches of training data. Which means, in turn, I presume, that model.fit will only call the first (1/9th) batches of training data, ignoring the other 8/9th.

3 Likes

thank you, that solved my problem too.

1 Like

This is a great explanation!

I was getting frustrated why my training was taking such a long time. I wound up reducing the batch size and not training on the entire set.

1 Like