[Python] My CNN model is overfitted, and I don't know how to fix it

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am making a CNN model to recognize skin diseases. I have made three iterations of the model, one basic, one with Augmentation and one with Imbalance.All the models work, but I have noticed that they suffer from overfitting, that is, their training accurancy is very high, but their testing accurancy never really measures up. I have added layers to the model, I have taken layers, I have added a Dropout layer, I have messed with batchsize, imagesize, epochs.

    The training folder has 2239 images over 9 classes, while testing as 118 images over 9 classes.

    This is the code I am using for the basic model:

    img_height = 224
    img_width = 224
    batch_size1 = 24
    batch_size2 = 16
    batch_size3 = 32
    batch_size4 = 40
    epoch = 30
    num_classes=0

    training1 = tf.keras.utils.image_dataset_from_directory(
    train_data_dir,
    labels="inferred",
    label_mode="int",
    color_mode="rgb",
    batch_size= batch_size1,
    image_size=(img_height, img_width),
    shuffle=True,
    seed=1256,
    validation_split=None,
    subset=None,
    interpolation="bilinear",
    follow_links=False,
    crop_to_aspect_ratio=False,
    pad_to_aspect_ratio=False,
    data_format=None,
    verbose=True
    )

    testing1 = tf.keras.utils.image_dataset_from_directory(
    test_data_dir,
    labels="inferred",
    label_mode="int",
    color_mode="rgb",
    batch_size=batch_size1,
    image_size=(img_height, img_width),
    shuffle=True,
    seed=1256,
    validation_split=None,
    subset=None,
    interpolation="bilinear",
    follow_links=False,
    crop_to_aspect_ratio=False,
    pad_to_aspect_ratio=False,
    data_format=None,
    verbose=True
    )

    train_classes = training1.class_names
    train_classes_index = len(train_classes)
    test_classes = testing1.class_names
    test_classes_index = len(test_classes)

    num_classes = train_classes_index
    class_names = training1.class_names

    class_list = []

    for image_batch, labels_batch in training1:
    class_list.append(class_names[labels_batch[0]])

    AUTOTUNE = tf.data.AUTOTUNE

    training1 = training1.cache().prefetch(buffer_size=AUTOTUNE)
    testing1 = testing1.cache().prefetch(buffer_size=AUTOTUNE)

    normalization_layer = Rescaling(1./255)

    normalized_ds1 = training1.map(lambda x, y: (normalization_layer(x), to_categorical(y,9)))
    testing1 = testing1.map(lambda x, y: (normalization_layer(x), to_categorical(y,9)))

    model1 = Sequential([
    Conv2D(32, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Dropout(0.25),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(num_classes, activation='softmax')
    ])

    model1.build(input_shape=(batch_size1, img_height, img_width, 3))

    model1.compile(optimizer='adam',
    loss= 'categorical_crossentropy',
    metrics=['accuracy'])

    history1 = model1.fit(normalized_ds1, batch_size=batch_size1, epochs=epoch,validation_data=testing1)

    acc = history1.history['accuracy']
    val_acc = history1.history['val_accuracy']

    loss = history1.history['loss']
    val_loss = history1.history['val_loss']

    epochs_range = range(epoch)

    plt.figure(figsize=(8, 8))
    plt.subplot(1, 2, 1)
    plt.plot(epochs_range, acc, label='Training Accuracy')
    plt.plot(epochs_range, val_acc, label='Validation Accuracy')
    plt.legend(loc='lower right')
    plt.title('Training and Validation Accuracy')

    plt.subplot(1, 2, 2)
    plt.plot(epochs_range, loss, label='Training Loss')
    plt.plot(epochs_range, val_loss, label='Validation Loss')
    plt.legend(loc='upper right')
    plt.title('Training and Validation Loss')
    plt.show()


    This is the graph of the training and testing accuracy and loss of the model

    I want any advice to fix this problem.

    Continue reading...

Compartilhe esta Página