Fine-Tuning a Pretrained Model

Deep Learning with Keras and TensorFlow 10 min min read Updated: Nov 16, 2026 Advanced
Fine-Tuning a Pretrained Model
Advanced Topic 7 of 9

What is Fine-Tuning?

After feature extraction, your new classifier head is trained. Fine-tuning goes one step further: you unfreeze the last part of the pretrained base and train it together with your head, using a very small learning rate. This adapts the high-level features to your own images.

The Two-Phase Recipe

  1. Phase 1: freeze the base and train only the head until it converges.
  2. Phase 2: unfreeze the top layers of the base, recompile with a low learning rate and train a few more epochs.
Why not unfreeze from the start? The new head starts with random weights and produces large random gradients. These would damage the carefully learned weights of the base. First train the head, then fine-tune.

Phase 2 in Code

python # base_model and model come from the previous lesson base_model.trainable = True print("Layers in base:", len(base_model.layers)) fine_tune_at = 100 # keep layers before 100 frozen for layer in base_model.layers[:fine_tune_at]: layer.trainable = False model.compile( optimizer=keras.optimizers.Adam(1e-5), # 100x smaller than before loss="binary_crossentropy", metrics=["accuracy"] ) history_fine = model.fit(train_ds, validation_data=val_ds, epochs=5)
  • Always recompile after changing trainable
  • Use a learning rate about 10 to 100 times smaller than in phase 1
  • Early layers learn general features (edges), so keep them frozen. Later layers are more specific, so those are the ones to adapt

BatchNormalization Layers

When calling the base model, we used training=False. This keeps the BatchNormalization layers in inference mode, so their statistics are not destroyed while fine-tuning. Keras handles this correctly when you set base_model.trainable = True and call the model with training=False as shown.

Compare the Results

python print("Before fine-tuning:", max(history.history["val_accuracy"])) print("After fine-tuning: ", max(history_fine.history["val_accuracy"]))
SettingTypical choice
Layers to unfreezeTop 20 to 30 percent of the base
Learning rate1e-5 to 1e-4
Epochs3 to 10
CallbacksEarlyStopping with restore_best_weights
Warning: If validation accuracy drops after unfreezing, your learning rate is too high or you unfroze too many layers. Go back to the saved model from phase 1.

Conclusion

Fine-tuning gives the last few percent of accuracy. Be gentle: unfreeze only the top layers and use a tiny learning rate.

Quick Summary: Train the head first, then unfreeze the top layers, recompile with a learning rate like 1e-5 and train a few epochs.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators