I'm new with keras with tensorflow backend and I'm trying to do transfer learning with pretrained net. The problem is that the accuracy on validation set is very high, around the 90% , but on test set the accuracy is very bad, less that 1%. I solved the problem using opencv to read and resize image, but I'd like to understand why with keras methods I've this problem. I paste my code below.
from keras.preprocessing.image import ImageDataGenerator from keras.applications.xception import preprocess_input import keras train_val_datagen = ImageDataGenerator( validation_split=0.25, preprocessing_function=preprocess_input) train_val_generator = train_val_datagen.flow_from_directory( # subset di allenamento directory="./image-dataset/", target_size=(299, 299), color_mode="rgb", batch_size=32, class_mode="categorical", shuffle=True, subset = 'training', seed=17) val_train_generator = train_val_datagen.flow_from_directory( # subset di validation directory="./image-dataset/", target_size=(299, 299), color_mode="rgb", batch_size=32, class_mode="categorical", shuffle=True, subset = 'validation', seed=17) final_train_generator = train_val_datagen.flow_from_directory( # set finale di allenamento con tutti i dati directory="./image-dataset/", target_size=(299, 299), color_mode="rgb", batch_size=32, class_mode="categorical", shuffle=True, seed=17)
As you can see i used Xception as pretrained-net and I choose to resize my images to adapt them to the net.
After the training I created a new Iterator for test data as follow:
test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input) test_generator = test_datagen.flow_from_directory( directory="./TEST/", target_size=(299, 299), color_mode="rgb", batch_size=1, # predico una alla volta shuffle = False, class_mode=None # non ce alcuna classe di riferimento ) test_generator.reset()
where the preprocess function is exactly the same.
The predictions are made with the following code:
predictions = model_xcpetion.predict_generator(test_generator, 6104, verbose = 1 )
where 6104 is the number of images in test folder. After this i've generated a csv with images name with relative categorical probabilities:
import pandas as pd import numpy as np df = pd.DataFrame(predictions) cols =[('probability of' + str(i)) for i in list(range(1, 30 )) ] df.columns = cols df['images'] = imNames df.to_csv('predictions_xception_all_data.csv', sep=',')
where the columns represent the labels (1 to 29) and imNames are obtained with filenames attribute of test_generator. Finally I generated the csv with the labels with highest probabilities value and i compute the accuracy obtaining the value that I wrote before.
The code that I used to solve is the same but I read and resize images with the following code:
width = 299 height = 299 dim = (width, height) images =  # for each img resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) images.append(resized)
where "img" are all the imageas read with "imread_collection" of skimage.io
Thanks in advance for your help.
EDIT1: the images resized with opencv have not been processed with the preprocessing function