Skip to content
Advertisement

Tensorflow accuracy from model.predict does not match final epoch val_accuracy of model.fit

I am trying to match the accuracy of a model.predict call to the final val_accuracy of model.fit(). I am using tf dataset.

JavaScript

The dataset setup for train_ds is similar. I prefetch both…

JavaScript

Than I get the labels for the val_ds so I can use them later

JavaScript

My model

JavaScript

Compiles fine

JavaScript

Seems to fit fine

JavaScript

The last epoch output

Epoch 10: val_accuracy did not improve from 0.92291 176/176 [==============================] – 191s 1s/step – loss: 0.9876 – accuracy: 0.7318 – val_loss: 0.4650 – val_accuracy: 0.8580

Now I want to verify the val_accuracy == 0.8580 when I run model.predict()

JavaScript

Accuracy = 0.7980014275517487

I would have expected that to equal the last val accuracy, which was 0.8580, but it is off. My val_ds uses a seed so I should be getting the images in the same order when I shuffle, right? Getting ground truth labels is a pain using datasets, but I think (???) my method is correct.

I only have two classes and when I look at my predictions variable it looks like I am getting probabilities as I would expect, so I think I set up, compiled and fit my model correctly for sparse categorical cross entropy using softmax on my final layer output.

JavaScript

array([[0.42447385, 0.5755262 ], [0.2162129 , 0.7837871 ], [0.31917858, 0.6808214 ]], dtype=float32)

What am I missing?

Advertisement

Answer

What you are missing is that your validation dataset is shuffled at every iteration.

tf.keras.utils.image_dataset_from_directory has shuffle=True by default. And that shuffle method for a TensorFlow dataset has an argument reshuffle_each_iteration which is None by default. Therefore it is shuffled everytime.

The seed=38 parameter is used for tracking the samples that reserved for training and validation separately. In other words, with seed argument we can follow which samples will be used for validation dataset and vice versa.

As an example:

JavaScript

This will print:

JavaScript

Relevant source code for tf.keras.utils.image_dataset_from_directory can be found here.

If you want to match predictions with their respective labels, then you can loop over the dataset:

JavaScript

Then you can check accuracy.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement