1
$\begingroup$

i am using tensorflow=2.15.0 and keras associated with it I have made a cnn network to identify a total of 2294 images into 10 different classes or, data is divided as 229 images are contained in each folder and the images are sorted or numbered according to the timestamp. size of image is 300(width),900(height),1(grayscale)

I have read almost all of the posts regarding this in stackoverflow and other online forums but I do not get the answer for my case

I have tried every possible thing except crossvalidation using k-fold because I cannot figure out how to apply it in my case as the former code before the creation of model is little bit different as what is taught in the example.

 # Train-Test Split
X_train, X_test, Y_train, Y_test = train_test_split(all_images, all_labels_one_hot, test_size=0.3, random_state=99,shuffle=True)
print('X_train.shape:', X_train.shape)
print('X_test.shape:', X_test.shape)
print('Y_train.shape:', Y_train.shape)
print('Y_test.shape:', Y_test.shape)

 # Output
X_train.shape: (1605, 900, 300)
X_test.shape: (689, 900, 300)
Y_train.shape: (1605, 10)
Y_test.shape: (689, 10)

# Define the model
model = keras.Sequential([
    keras.layers.Conv2D(filters=32, kernel_size=3, activation=keras.layers.LeakyReLU(alpha=0.01), kernel_initializer='he_uniform', padding = 'same', input_shape=img_shape, name='conv_01'),
    keras.layers.BatchNormalization(),
    keras.layers.AveragePooling2D(pool_size=2,strides=2, name='pool_01'),

    keras.layers.Conv2D(filters=64, kernel_size=3,activation=keras.layers.LeakyReLU(alpha=0.01),kernel_initializer='he_uniform', padding='same', name='conv_02'),
    keras.layers.BatchNormalization(), 
    keras.layers.AveragePooling2D(pool_size=2,strides=2, name='pool_02'),  
    
    keras.layers.Conv2D(filters=64, kernel_size=3,activation=keras.layers.LeakyReLU(alpha=0.01),kernel_initializer='he_uniform', padding='same', name='conv_03'),
    keras.layers.BatchNormalization(),   
    keras.layers.AveragePooling2D(pool_size=2, strides=2,name='pool_03'),

    keras.layers.Conv2D(filters=128, kernel_size=3,activation=keras.layers.LeakyReLU(alpha=0.01), kernel_initializer='he_uniform',padding='same', name='conv_04'),
    keras.layers.BatchNormalization(),   
    keras.layers.AveragePooling2D(pool_size=2, strides=2,name='pool_05'),  

 keras.layers.Flatten(name='flatten_01'),
    keras.layers.Dropout(0.3, name='dropout_01'),

    keras.layers.Dense(64,activation=keras.layers.LeakyReLU(alpha=0.01),kernel_regularizer=keras.regularizers.l2(l2=0.01),name='dense-03'),
    keras.layers.BatchNormalization(),
    keras.layers.Dropout(0.2, name='dropout_04'),

    keras.layers.Dense(128,activation=keras.layers.LeakyReLU(alpha=0.01),kernel_regularizer=keras.regularizers.l2(l2=0.01),name='dense-01'),
    keras.layers.BatchNormalization(),
    keras.layers.Dropout(0.2, name='dropout_02'),

    keras.layers.Dense(128,activation=keras.layers.LeakyReLU(alpha=0.01),kernel_regularizer=keras.regularizers.l2(l2=0.01),name='dense-02'),
    keras.layers.BatchNormalization(),
    keras.layers.Dropout(0.2, name='dropout_03'),

keras.layers.Dense(10, activation='softmax',name='fc_layer'),
])

#Compile the model
history = model.fit(X_train, Y_train, batch_size=16, epochs=50, validation_split=0.35,shuffle=True,callbacks=([lr_scheduler]))

optimizer used is RMSprop and loss is categorical cross entropy. Could anyone please suggest how can I avoid overfitting?

I have tried dataaugmentation, applied regularization, changing the hyperparameters but no effect. I have tried with the 2 and 3 conv2D layers with the change in filters but no improvement. Could someone please help?

in addition to the question, I have changed the architecture so initially 3 conv2D -> 32/64/128 followed by one hidden layer(128) and the last output layer of 10 neurons so I am getting validation accuracy of 89% and training accuracy of 99%

and again i have changed the architecture so 3 conv2D -> 32/64/128 followed by two hidden layers(64/64) then one output layer of 10 neurons and the validation accuracy obtained is 85% and training accuracy obtained is 99%

I have put the image for the above run.

plot

$\endgroup$
5
  • $\begingroup$ Welcome! As your question is currently written, it's not clear why you think your model is overfitting. Please add a plot or something else to show why you think your model is overfitting. $\endgroup$
    – m13op22
    Commented Mar 4 at 16:18
  • $\begingroup$ If you provide more of the code or context as requested by @m13op22, I would be more than happy to take a look. Training run output would certainly be helpful. $\endgroup$ Commented Mar 4 at 20:34
  • $\begingroup$ could you please have a look and suggest something then? @m13op22 $\endgroup$ Commented Mar 6 at 11:12
  • $\begingroup$ Also, Mr. @Daniel Curtis - you can suggest something by looking at the plot $\endgroup$ Commented Mar 6 at 11:13
  • $\begingroup$ What are the proportions of classes in the training and test sets? Are they the same? If not, and it makes sense for your problem, add the stratify=all_labels_one_hot parameter to train_test_split to ensure the class proportions are the same in both the train and test sets. $\endgroup$
    – m13op22
    Commented Mar 6 at 15:12

1 Answer 1

1
$\begingroup$

My first inclination is that your dataset size is relatively small, and you have many hidden layers, which increases the likelihood of your model overgeneralizing on the training data, leading to overfitting. Your training graphs of the loss and accuracy for train/test data confirm this.

Thoughts:

  • Reduce Hidden Layers in size and quantity and compare the results of each run.
  • Try reducing the Conv2D layers and sizes if the above does not work.
  • Experiment with further data augmentation. Random rotations, flips, zooming, etc.
  • Ensemble; however, this is a bit more complex of an approach.
  • Experiment with different L2/L1 regulization techniques.
$\endgroup$
3
  • 2
    $\begingroup$ thankyou @Daniel Curtis for the reply. I will try with hidden and conv2D layers although I have already reduced the dense and conv2d layers. I have tried with data augmentation but no effect as such. I found L2 to be more effective in my case and now i have kept the penalization parameter l2 as 0.05 but if i increase it to say 0.09 then validation accuracy drops down. $\endgroup$ Commented Mar 7 at 14:55
  • $\begingroup$ Optimizing the dataset and approach to the problem can also yield significant results. I was working on a problem earlier today, and this method was the solution. Also, given the analysis your Conv2D layers are performing, a given kernel size is frequently more optimal for a particular task. All are parts of the giant puzzle, and to me, that is why this field is a lot of fun. $\endgroup$ Commented Mar 9 at 6:54
  • $\begingroup$ thankyou @Daniel Curtis for the reply. Doing some change and waiting for a long period of time to get the result because unfortunately tensorflow is not so good with gpu memory allocation and it fills the 8 gb gpu :( $\endgroup$ Commented Mar 9 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.