Help about using Keras and Tensorflow Correctly

Hi anyone has any guidance on this issue

Just a context I’m using tensorflow on google colab to do classification on MRI images of brain tumours with 4 classes and dataset size 480

glioma, meningioma, no tumor and pituitary

My dataset is loaded into using
ImageDataGenerator from keras.preprocessing and using my custom preprocessing code
And then loaded using datagen.flow_from_directory with a batch size of 32

And currently using keras.applications.DenseNet201 to build this

Currently doing hyperparameter tuning with keras_tuner, in which I managed to retrieve a val accuracy of 82%

But when I use the same validation_generator to plot a confusion matrix retrieved using tuner.get_best_models(1)[0] and then performing a .predict using that model, my confusion matrix is all over the place, i further tested using train_generator and a perfect confusion matrix is to be expected but it is also all over thr place, am I doing something wrong? Or any guidance on this matter?

Hi @Sam_Li ,

May be check for Class Imbalance, If your dataset has a significant class imbalance ,the model may be biased toward the majorty class. This can lead to high overal acuracy but poor performance on minoriy classes, which will be reflected in the confusion matrix.

With a relatively small dataset size of 480 samples and four classes, your model is likely to overfit. To mitigate this issue and improve the model’s performance, you should consider increasing the size of your dataset and ensuring that the classes are well-balanced.

Thanks.