2

Background

While fine tuning a classification model in Keras, it printed val_acc: 0.8456. This code was used for fine-tuning.

After fine-tuning, manually loading the trained model and predicting the valuation set, a much lower accuracy of 0.28 was received.

The following code was used for valuation:

model = load_model(MODEL_PATH)
...
img = kimage.load_img(img_path, target_size=target_size)
x = kimage.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = vgg19.preprocess_input(x)
pred = model.predict(x)

Question

What might be the cause for the big discrepancy in accuracy 0.85 != 0.28?

Michael
  • 3,206
  • 5
  • 26
  • 44

2 Answers2

2

You're using different preprocessing for training and testing. Specifically,

rescale = 1./255

is used for training, but

x = vgg19.preprocess_input(x)

is used for testing.

What imagenet_utils.preprocess_input() does is subtracting the mean (computed on ImageNet, as suggested by the name):

    # Zero-center by mean pixel
    x[:, :, :, 0] -= 103.939
    x[:, :, :, 1] -= 116.779
    x[:, :, :, 2] -= 123.68

So it's fairly different from the preprocessing applied on your training data.

Yu-Yang
  • 14,539
  • 2
  • 55
  • 62
1

Using the same ImageDataGenerator

My ImageDataGenerator was:

train_datagen = ImageDataGenerator(
    rescale=1. / 255, ...)

Was able to reproduce its preprocessing as follows:

img = load_img(image_path, target_size=target_size)
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x *= rescale_factor

score = model.predict(x)
Michael
  • 3,206
  • 5
  • 26
  • 44