Train accuracy is great, but test accuracy is bad

Viewed 655

I used ImageDataGenerator.flow_from_directory() to train and test my model. When it is training, the accuracy was up to 90%.
But when I test my model, the accuracy was just the average(50%).

This is how I use ImageDataGenerator:

datagen = ImageDataGenerator(rescale=1. / 255)

train_data = datagen.flow_from_directory(img_path + '\\train', target_size=(224, 224),
                                     classes=['NORMAL', 'PNEUMONIA'],
                                     batch_size=10)

test_data = datagen.flow_from_directory(img_path + '\\test', target_size=(224, 224),
                                    classes=['NORMAL', 'PNEUMONIA'],
                                    batch_size=10)

This is how I train and test my model:

model.fit_generator(train_data, steps_per_epoch=32, epochs=100)

test_info = model.evaluate_generator(test_data, steps=10)

print(test_info)

I tried to replace test_data with train_data when test the model. But the result was identical.

1 Answers

It looks like your model is over fitting on the training data. You can try some methods to prevent it like:

  • early stopping
  • Regularization( dropout, L1/L2 regularization)

  • cross-validation

  • Increasing dataset(if possible)

  • Reduce architecture complexity

Related