shivahanifi / SCDD-image-segmentation-keras

Implementation of Segnet, FCN, UNet , PSPNet and other models in Keras.
https://divamgupta.com/image-segmentation/2019/06/06/deep-learning-semantic-segmentation-keras.html
MIT License
0 stars 0 forks source link

Use early stopping to find the optimal epoch numbers #10

Open shivahanifi opened 1 day ago

shivahanifi commented 1 day ago

The thesis explicitly states that both U-Net models in their study (U-Net-12 and U-Net-25) completed training within the first 65 epochs. The determination of the optimal training duration was based on the observation that the models stopped learning after this point.

Up to this point I have used 5 epochs for training. The next step would be to use stopping condition instead of fixed epoch numbers.

shivahanifi commented 1 day ago

How to implement

from tensorflow.keras.callbacks import EarlyStopping

# Define early stopping callback
early_stopping = EarlyStopping(
    monitor='val_loss',  # Metric to monitor, e.g., 'val_loss' or 'val_accuracy'
    patience=3,          # Number of epochs to wait for improvement before stopping
    restore_best_weights=True  # Restore model weights from the epoch with the best performance
)

# Train the model
history = model.fit(
    train_data,              # Your training data
    train_labels,            # Your training labels
    validation_data=(val_data, val_labels),  # Validation data for monitoring improvement
    epochs=100,              # Set a high number of epochs (e.g., 100 or more)
    callbacks=[early_stopping]  # Add the early stopping callback
)