[Python] EfficientNetb0 validation loss stagnan not decreases, validation accuracy increases...

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    can u help to figure this out, using efficientnetb0 with my dataset train 1515, validation 326, test 327, have 6 classes, the validation on the first epoch decrease but in nearly last epochs validation loss and accuracy stuck

    from tensorflow.keras.preprocessing.image import ImageDataGenerator
    # Menggunakan ImageDataGenerator untuk preprocessing
    train_datagen_1 = ImageDataGenerator(
    preprocessing_function=keras.applications.efficientnet.preprocess_input, # Normalisasi gambar
    rotation_range=30, # Rotasi gambar hingga 40 derajat
    width_shift_range=0.1, # Geser horizontal
    height_shift_range=0.1, # Geser vertikal
    shear_range=0.1, # Distorsi gambar
    zoom_range=0.1, # Zoom in/out
    brightness_range=[0.8, 1.2],
    fill_mode='reflect'
    )

    val_test_datagen_1 = ImageDataGenerator(
    preprocessing_function=keras.applications.efficientnet.preprocess_input,
    # Normalisasi gambar tanpa augmentasi untuk validasi dan test
    )

    # Memuat dataset dari folder dengan preprocessing
    train_generator_1 = train_datagen_1.flow_from_directory(
    os.path.join(output_dir, 'train'),
    batch_size=batch_size_1,
    target_size=(224, 224),
    class_mode='categorical', # Klasifikasi multi-kelas
    shuffle=True
    )

    val_generator_1 = val_test_datagen_1.flow_from_directory(
    os.path.join(output_dir, 'val'),
    batch_size=batch_size_1,
    target_size=(224, 224),
    class_mode='categorical',
    shuffle=True
    )

    test_generator_1 = val_test_datagen_1.flow_from_directory(
    os.path.join(output_dir, 'test'),
    batch_size=batch_size_1,
    class_mode='categorical',
    target_size=(224, 224),
    shuffle=False # Do not shuffle test data
    )


    from tensorflow.keras.applications import EfficientNetB0
    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Conv2D, GlobalMaxPooling2D, Dense, Dropout, BatchNormalization
    from tensorflow.keras import regularizers

    # Jumlah kelas dalam dataset
    num_classes_1 = train_generator_1.num_classes

    # Load pre-trained EfficientNetB0 model with ImageNet weights (transfer learning)
    base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

    x=GlobalMaxPooling2D()(base_model.output)
    x=Dense(512, activation='relu', bias_regularizer=regularizers.L2(1e-3))(x)
    x = Dropout(0.3)(x)
    x=Dense(256, activation='relu', bias_regularizer=regularizers.L2(1e-3))(x)
    x = Dropout(0.4)(x)
    x=Dense(128, activation='relu', bias_regularizer=regularizers.L2(1e-3))(x)
    x = Dropout(0.4)(x)
    predictions = Dense(num_classes_1, activation='softmax')(x)

    # Define the model
    model_1 = Model(inputs=base_model.input, outputs=predictions)

    # Compile the model
    model_1.compile(optimizer='Adamax', loss='categorical_crossentropy', metrics=['accuracy'])

    # Model summary
    model_1.summary()


    Epoch 1/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 34s 357ms/step - accuracy: 0.2407 - loss: 1.8402 - val_accuracy: 0.4699 - val_loss: 1.4752
    Epoch 2/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 34s 354ms/step - accuracy: 0.3408 - loss: 1.6115 - val_accuracy: 0.5394 - val_loss: 1.2970
    Epoch 3/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 33s 347ms/step - accuracy: 0.4078 - loss: 1.4846 - val_accuracy: 0.5532 - val_loss: 1.1945
    Epoch 4/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 34s 360ms/step - accuracy: 0.4714 - loss: 1.3449 - val_accuracy: 0.5718 - val_loss: 1.1228
    Epoch 5/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 41s 361ms/step - accuracy: 0.5302 - loss: 1.2129 - val_accuracy: 0.6181 - val_loss: 0.9750
    Epoch 6/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 33s 346ms/step - accuracy: 0.5883 - loss: 1.0862 - val_accuracy: 0.6620 - val_loss: 0.8812
    Epoch 7/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 41s 345ms/step - accuracy: 0.6415 - loss: 0.9847 - val_accuracy: 0.6551 - val_loss: 0.8544
    Epoch 8/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 41s 346ms/step - accuracy: 0.7070 - loss: 0.8461 - val_accuracy: 0.7199 - val_loss: 0.7719
    Epoch 9/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 41s 432ms/step - accuracy: 0.7443 - loss: 0.7486 - val_accuracy: 0.7338 - val_loss: 0.7709
    Epoch 10/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 34s 359ms/step - accuracy: 0.7685 - loss: 0.7007 - val_accuracy: 0.7569 - val_loss: 0.6712
    Epoch 11/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 41s 361ms/step - accuracy: 0.7830 - loss: 0.6317 - val_accuracy: 0.7801 - val_loss: 0.6585
    Epoch 12/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 33s 349ms/step - accuracy: 0.8102 - loss: 0.5673 - val_accuracy: 0.7731 - val_loss: 0.7297
    Epoch 13/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 33s 349ms/step - accuracy: 0.8379 - loss: 0.5060 - val_accuracy: 0.7593 - val_loss: 0.6847
    Epoch 14/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 41s 350ms/step - accuracy: 0.8537 - loss: 0.4300 - val_accuracy: 0.7593 - val_loss: 0.6988
    Epoch 15/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 33s 350ms/step - accuracy: 0.8716 - loss: 0.3819 - val_accuracy: 0.7870 - val_loss: 0.6517
    Epoch 16/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 33s 349ms/step - accuracy: 0.8931 - loss: 0.3353 - val_accuracy: 0.7824 - val_loss: 0.7737
    Epoch 17/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 41s 348ms/step - accuracy: 0.8804 - loss: 0.3973 - val_accuracy: 0.8009 - val_loss: 0.7213
    Epoch 18/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 82s 785ms/step - accuracy: 0.8933 - loss: 0.3527 - val_accuracy: 0.7940 - val_loss: 0.6943
    Epoch 19/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 34s 360ms/step - accuracy: 0.9056 - loss: 0.3201 - val_accuracy: 0.8218 - val_loss: 0.6387
    Epoch 20/20
    95/95 ━━━━━━━━━━━━━━━━━━━━ 34s 359ms/step - accuracy: 0.9130 - loss: 0.2675 - val_accuracy: 0.7986 - val_loss: 0.7243


    i already try to decrease validation loss but it still hard can u guys help me

    Continue reading...

Compartilhe esta Página