0

I have a dataset with 5 classes, and below there is the number of images in each class:

Class1: 6427 Images

Class2: 12678 Images

Class3: 9936 Images

Class 4: 26077 Images

Class 5: 1635 Images

I run my first CNN model on this dataset and my model was overfitted as can be seen below: Confusion Matrix

enter image description here

The overfitting is obvious in these models, also the high sensitivity on label4. I have tried different ways like Augmentation and etc to fix this and finally I fixed it by using VGG16 model. But I still have the class imbalance, but the strange thing is that the class imbalance now is shifted to label 1 as can be seen below. enter image description here enter image description here

In order to fix the class imbalance I choose equal number of each class for example I choose 1600 images from each class and I ran my model on that but again I have high sensitivity on label1. I do not know what is the problem. I would like to mention that when I add Dropout layer I receive the result below for the confusion matrix which is very bad: enter image description here

I add my first model which I get overfitting and high sensitivity on label 4 below:

os.chdir('.')

train_path = './train'
test_path = './test'
valid_path = './val'

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_path, target_size=(224,224), classes=['label1', 'label2','label3','label4','label5'], batch_size=32)

test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_path, target_size=(224,224), classes=['label1', 'label2','label3','label4','label5'], batch_size=32)

valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=valid_path, target_size=(224,224), classes=['label1', 'label2','label3','label4','label5'], batch_size=32)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', padding= 'same', input_shape=(224,224,3)))

model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=2))

model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3),activation= 'relu', padding='same'))

model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=2))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(5, activation='softmax'))

print(model.summary())


model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])



history = model.fit(x=train_batches, validation_data= valid_batches, epochs=10)
predictions = model.predict(x=test_batches, verbose=0)
amiroruji72
  • 17
  • 2
  • 6
  • What is the range of the loaded pixel values in the images ? Are the images normalized into smaller ranges somewhere in preprocessing? In addition, the size of the images is 224*224, with two small blocks, which means that the Faltten() layer will still have large number of generated features. – ALI HAIDAR Mar 01 '21 at 05:47
  • The size of images are 370*370. In the preprocessing I have used ImageDataGenerator with VGG16 preprocess input. But I didn't get your point about Flatten layer. Should I do sth about it? – amiroruji72 Mar 01 '21 at 06:10
  • Going back to the first step, the imaging data should be normalized into smaller ranges for the algorithm to be able to perform and learn. For e.g., the original pixel values might be between 0-255, you need to normalized the values of pixels to smaller ranges. – ALI HAIDAR Mar 01 '21 at 06:51
  • I tried it but again I have high sensitivity on label4 – amiroruji72 Mar 01 '21 at 10:06

0 Answers0