C.W.K.
Stream
Lesson 07 of 07 · published

Practical: Fine-Tuning EfficientNet

~10 min · transfer

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Everything, in one script

This is the whole track collapsed into one runnable workflow — the thing you'll copy into every new image-classification project. Read it as the recipe from lesson 6 made concrete: load a frozen EfficientNetV2S backbone, attach a small head, train the head (phase 1), then unfreeze the top layers and fine-tune at 1e-5 (phase 2). The Code section is the complete program.

The head choices worth noticing

This head is one step richer than the bare feature-extraction lesson: after GlobalAveragePooling2D it adds a Dense(128, relu) bottleneck before the final classifier. That extra layer gives the model a little room to recombine backbone features for your specific classes, and the Dropout(0.3) keeps it honest on small data. Swap the final Dense(5, softmax) to match your class count, and adjust input_shape if your backbone preset expects a different resolution.

From this template to a real project

To take this further, the only moving parts are the data pipeline (your train_ds / val_ds) and the two epoch counts. Everything else — the freeze/unfreeze dance, the two compiles, the 100× LR drop — stays exactly as written. That's the point of having a template: the transfer-learning mechanics become muscle memory and you spend your attention on the data and the metric.

Code

End-to-end transfer pipeline (feature extraction + fine-tune)·python
import keras
from keras import layers

# Load pretrained EfficientNetV2S
base = keras.applications.EfficientNetV2S(
    weights="imagenet", include_top=False,
    input_shape=(224, 224, 3),
)
base.trainable = False

# Build model
inputs = keras.Input(shape=(224, 224, 3))
x = base(inputs, training=False)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(128, activation="relu")(x)
x = layers.Dropout(0.3)(x)
outputs = layers.Dense(5, activation="softmax")(x)
model = keras.Model(inputs, outputs)

# Phase 1: Feature extraction
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(train_ds, epochs=10, validation_data=val_ds)

# Phase 2: Fine-tuning
base.trainable = True
for layer in base.layers[:-20]:
    layer.trainable = False
model.compile(optimizer=keras.optimizers.Adam(1e-5), loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(train_ds, epochs=20, validation_data=val_ds)

External links

Exercise

Pick a public dataset (Oxford Flowers, Food-101, or similar). Build the EfficientNet transfer pipeline above end to end. Achieve ≥85% validation accuracy. Save the script as your reusable transfer-learning template.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.