Home > Software engineering >  how can i deal with overfitting in tensorflow?
how can i deal with overfitting in tensorflow?

Time:09-15

hope you're doing great. so i'm making a object classification using Deep Learning and CNN, but it seems i'm facing overfitting, and since i'm new, i don't know why it keeps overfit. can you please help me ? here's the code :

#loading data from the website called cifar
(x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()
y_train = y_train.reshape(-1,)
y_test = y_test.reshape(-1,)
classes = ["aireplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]

#resizing x_train and y_train
x_train = x_train /255.0
y_train = y_train /255.0

#cnn model
cnn = models.Sequential([
    layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    Dropout(0.5),
    layers.Flatten(),
    layers.Dense(10, activation='softmax')
])

cnn.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              metrics=['accuracy'])

#training the model
cnn.fit(x_train, y_train, epochs=10)

and here what i get :

Epoch 1/10 1563/1563 [==============================] - 6s 4ms/step - loss: 0.0054 - accuracy: 0.0999 Epoch 2/10 1563/1563 [==============================] - 6s 4ms/step - loss: 1.5368e-07 - accuracy: 0.1000 Epoch 3/10 1563/1563 [==============================] - 6s 4ms/step - loss: 6.8429e-08 - accuracy: 0.1000 Epoch 4/10 1563/1563 [==============================] - 6s 4ms/step - loss: 1.4041e-08 - accuracy: 0.1000 Epoch 5/10 1563/1563 [==============================] - 6s 4ms/step - loss: 1.8834e-09 - accuracy: 0.1000 Epoch 6/10 1563/1563 [==============================] - 6s 4ms/step - loss: 3.8385e-10 - accuracy: 0.1000 Epoch 7/10 1563/1563 [==============================] - 6s 4ms/step - loss: 3.3140e-10 - accuracy: 0.1000 Epoch 8/10 1563/1563 [==============================] - 6s 4ms/step - loss: 5.7220e-11 - accuracy: 0.1000 Epoch 9/10 1563/1563 [==============================] - 6s 4ms/step - loss: 1.2398e-10 - accuracy: 0.1000 Epoch 10/10 1563/1563 [==============================] - 6s 4ms/step - loss: 1.9550e-10 - accuracy: 0.1000

CodePudding user response:

There are really only two solutions that I know of to counter overfitting.

  1. Get more varied data to train your model on.
  2. And run fewer epochs, giving the model less time to overfit on a given set of data.

CodePudding user response:

There are lots of ways to avoid overfitting for image classification problem;

  1. You could impelemnt keras.layers.Dropout layer in your model architecture to make model not pass data from some of the nodes in hidden layers randomly.

  2. Use Transfer Learning which can cause to have a pre-trained model that have trained on lots of images and extracted more features.

  3. You could even Preprocess images and augment images depend on the dataset and case you are working on and apply on the training set images, for instance such as: Height & Width Shift, Shear Augmentation and etc.

  4. As my opinion the best architecture based on the lots of pre-trained models is that implement the convolution base of the model in this order; Convolution layer, Batch Normalization Layer, Activation Layer, Max Pool Layer

  • Related