Inception-ResNet-v2
machine learning
neural networks
transfer learning
deep learning

Retraining the last layer of Inception-ResNet-v2

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Retraining only the last layer of Inception-ResNet-v2 is a standard transfer-learning workflow when you have a pretrained ImageNet model and a new classification problem with different classes. The idea is to keep the convolutional backbone fixed, replace the classifier head, and train only that final part on your dataset.

Why Retrain Only the Last Layer

The earlier layers of Inception-ResNet-v2 learn broadly useful visual features such as edges, textures, shapes, and part-level patterns. If your new dataset is not huge, reusing those features is usually better than training the full network from scratch.

Retraining only the classifier head gives you:

  • much faster training
  • lower GPU memory pressure
  • less risk of overfitting on a small dataset

It is usually the first transfer-learning step before considering deeper fine-tuning.

Building the Model in Keras

With tf.keras.applications.InceptionResNetV2, the common pattern is to load the pretrained base without the top classification layer, freeze it, and add a new dense output layer for your class count.

python
1import tensorflow as tf
2
3num_classes = 5
4
5base_model = tf.keras.applications.InceptionResNetV2(
6    include_top=False,
7    weights="imagenet",
8    input_shape=(299, 299, 3),
9)
10base_model.trainable = False
11
12inputs = tf.keras.Input(shape=(299, 299, 3))
13x = tf.keras.applications.inception_resnet_v2.preprocess_input(inputs)
14x = base_model(x, training=False)
15x = tf.keras.layers.GlobalAveragePooling2D()(x)
16outputs = tf.keras.layers.Dense(num_classes, activation="softmax")(x)
17
18model = tf.keras.Model(inputs, outputs)
19model.compile(
20    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
21    loss="sparse_categorical_crossentropy",
22    metrics=["accuracy"],
23)

The include_top=False setting removes the original ImageNet classifier, and the new dense layer becomes the only trainable classification head.

Input Size and Preprocessing Matter

Inception-ResNet-v2 expects images resized to 299 x 299, and you should use the matching preprocessing function. Skipping the model-specific preprocessing or feeding the wrong input size can hurt accuracy enough that the transfer-learning setup appears broken.

If your dataset pipeline is built with tf.data, the preprocessing step can be integrated cleanly:

python
1def preprocess(image, label):
2    image = tf.image.resize(image, (299, 299))
3    image = tf.cast(image, tf.float32)
4    return image, label

The actual normalization still happens in the model through preprocess_input in the earlier example.

Training the New Head

Once the base is frozen, training updates only the last layer's weights.

python
1history = model.fit(
2    train_ds,
3    validation_data=val_ds,
4    epochs=10,
5)

If the dataset is small, data augmentation is often more valuable than trying to unfreeze deeper layers immediately.

When to Fine-Tune More Than the Last Layer

Retraining only the final layer is a strong baseline, but it may plateau if your target dataset is visually very different from ImageNet. In that case, unfreezing a small portion of the upper backbone can help.

python
1base_model.trainable = True
2
3for layer in base_model.layers[:-50]:
4    layer.trainable = False
5
6model.compile(
7    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),
8    loss="sparse_categorical_crossentropy",
9    metrics=["accuracy"],
10)

The key is to lower the learning rate when fine-tuning deeper layers. Otherwise, you can quickly damage the pretrained weights.

Common Pitfalls

The biggest pitfall is forgetting to freeze the base model before the first training phase. If everything is trainable immediately, training becomes slower and the pretrained representation can drift too aggressively.

Another issue is skipping the model-specific preprocessing. Inception-style models are sensitive to input scaling conventions.

Developers also sometimes replace only the very last dense layer but keep a mismatch between the label format and the loss function. For example, one-hot labels and integer labels need different loss choices.

Finally, when you later unfreeze layers for fine-tuning, always recompile the model. Keras needs a fresh compiled training graph after trainable flags change.

Summary

  • Retraining the last layer of Inception-ResNet-v2 is a standard transfer-learning baseline.
  • Load the pretrained backbone with include_top=False, freeze it, and add a new classifier head.
  • Use the correct 299 x 299 input size and the matching preprocessing function.
  • Start with only the last layer trainable, then fine-tune upper layers later if needed.
  • Lower the learning rate significantly before unfreezing part of the backbone.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.