0

I have a problem with my model. After training and saving it, I loaded my model and tried to predict images classes. Just to test it, I tryed with the same images that I used to train the model.

from tensorflow.keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
import csv

model = load_model('newmodel.h5')

#training and validation dataset
train_dir = "ROIClassifier/data/train/"
train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data
train_dataset = train_image_generator.flow_from_directory(directory=train_dir,
                                                          target_size= (128,128),
                                                          classes=['tumor', 'stroma'],
                                                          class_mode='binary',
                                                          )

validation_image_generator = ImageDataGenerator(rescale=1./255,
                                                )  # Generator for our training data
test_dir = "ROIClassifier/data/validation/"
validation_dataset = validation_image_generator.flow_from_directory(directory=test_dir,
                                                                    target_size= (128,128),
                                                                    classes=['tumor', 'stroma'],
                                                                    class_mode='binary')

results = model.predict_classes(train_dataset,  batch_size=None)
evaluation = model.evaluate(train_dataset)
print(train_dataset.class_indices)
# name of csv file
filename = "results.csv"
# field names
fields = ['ID', 'Class', 'Prediction']

with open(filename, 'w', newline='') as csvfile:
    # creating a csv writer object
    csvwriter = csv.writer(csvfile)
    # writing the fields
    csvwriter.writerow(fields)
    a = 0
    for i in range(0, 293):
        if train_dataset.classes[i]== int(*results[i]):
            a += 1
        csvwriter.writerow([i, train_dataset.classes[i], *results[i]])

I load the model, generate the dataset, predict the classes and evaluate the model. Finally, I save a csv (to make some statistics with SPSS).

Model evaluation:

1/10 [==>...........................] - ETA: 2s - loss: 0.6935 - accuracy: 0.6562
 2/10 [=====>........................] - ETA: 1s - loss: 0.6839 - accuracy: 0.6875
 3/10 [========>.....................] - ETA: 1s - loss: 0.6657 - accuracy: 0.7188
 4/10 [===========>..................] - ETA: 1s - loss: 0.6388 - accuracy: 0.7812
 5/10 [==============>...............] - ETA: 0s - loss: 0.6361 - accuracy: 0.7812
 6/10 [=================>............] - ETA: 0s - loss: 0.6315 - accuracy: 0.7865
 7/10 [====================>.........] - ETA: 0s - loss: 0.6202 - accuracy: 0.7991
 8/10 [=======================>......] - ETA: 0s - loss: 0.6241 - accuracy: 0.7891
 9/10 [==========================>...] - ETA: 0s - loss: 0.6256 - accuracy: 0.7951
10/10 [==============================] - 1s 147ms/step - loss: 0.6205 - accuracy: 0.7959

But only 50 percent of prediction matches the actual class. Why?

FrancecoMartino
  • 409
  • 2
  • 5
  • I was not able to find in your code the step in which you store the model, you can see how it can be done in this thread https://stackoverflow.com/questions/42763094/how-to-save-final-model-using-keras. This needs to be done carefully, otherwise you might not save the final weights and when you load the model it will be loaded as untrained. – Preto Feb 23 '20 at 23:31
  • I used "model.save('newmodel.h5') just after model fitting. Actually I wrote history = model.fit(...), could this be the problem? – FrancecoMartino Feb 24 '20 at 09:23
  • I found the problem: I had to had "shuffle = off" in flow_from_directory – FrancecoMartino Feb 24 '20 at 22:30

0 Answers0