I am training a ResNet50 model and I want to apply fine-tuning after the initial training. This is when I train the model without fine-tuning:
# Train initial model without fine-tuning
initial_epochs = 100
history = model.fit(train_set, validation_data = dev_set, epochs=initial_epochs,verbose=1, callbacks=callbacks)
And this is the code for fine-tuning and resuming from the last epoch:
# Train the model again for a few epochs
fine_tune_epochs = 5
total_epochs = initial_epochs + fine_tune_epochs
history_tuned = model.fit(train_set, validation_data = dev_set, initial_epoch=history.epoch[-1], epochs=total_epochs,verbose=1, callbacks=callbacks)
The problem is, I have set initial_epochs to 100 because I have early_stopping. So each model might run for ~20 up until ~40 epochs and then stop. So initial_epochs is not really 100.
This is the two training sessions in tensorboard. Is there a way to resume fine-tuned training from the epochs of the last training session? What if I just put epochs = 5 and have initial_epoch as it is?
Or can I put:
# Train the model again for a few epochs
fine_tune_epochs = 5
total_epochs = len(history.epoch) + tuned_epochs # Get total number of epochs
history_tuned = model.fit(train_set, validation_data = dev_set, initial_epoch=history.epoch[-1], epochs=total_epochs,verbose=1, callbacks=callbacks)
SOLUTION:
Running the first training session:
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_valid, y_valid), callbacks=cb)
print(history.epoch)
print(len(history.epoch))
print(history.epoch[-1])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10
9
Then:
tuned_epochs = 5
total_epochs = len(history.epoch) + tuned_epochs
history_tuned = model.fit(X_train, y_train, initial_epoch=history.epoch[-1], epochs=total_epochs, validation_data=(X_valid, y_valid), callbacks=cb)
This will tell the fit function to have initial epoch from the last epoch of the last training session and total epochs to run will be all the epochs from the last training sesssion + 5

