Why is validation accuracy significantly higher than accuracy of predicted labels on different set drawn from same distribution

Viewed 33

I'm quite new to keras and was wondering why there's a difference between the validation accuracy provided by the fit_generator and the testing accuracy once I apply the fully fitted model to a testset drawn from the same distribution. During the fitting process, the training accuracy is constantly being printed out by the fit_generator method and at the end of every epoch, the validation accuracy is printed out too. It totally makes sense to me that there's a difference between these two accuracies. But once I use the fitted model to predict labels on a different set drawn from the same distribution, the resulting accuracy is significantly lower than the validation accuracy. From my understanding of the validation accuracy, they should be more or less the same. What am I misunderstanding or doing wrong here?

from keras.preprocessing.image import ImageDataGenerator

# Add our data-augmentation parameters to ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)

# Note that the validation data should not be augmented!
val_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_dir, # This is the source directory for training images
        target_size=(299, 299),  # All images will be resized to 299x299
        batch_size=32,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

# Flow validation images in batches of 32 using val_datagen generator
validation_generator = val_datagen.flow_from_directory(
        validation_dir,
        target_size=(299, 299),
        batch_size=32,
        class_mode='binary')

totalTrain = len(list(paths.list_images(config.TRAIN_PATH)))
totalVal = len(list(paths.list_images(config.VAL_PATH)))

history = model.fit_generator(
      train_generator,
      steps_per_epoch=totalTrain // config.BS,
      epochs=8,
      validation_data=validation_generator,
      validation_steps=totalVal // config.BS)
0 Answers
Related