Inception v3
TensorFlow
transfer learning
model retraining
machine learning

How do you add new categories and training to a pretrained Inception v3 model in TensorFlow?

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

Adding new categories to a pretrained Inception v3 model is a standard transfer-learning task. Instead of training a deep image classifier from scratch, you reuse the convolutional backbone learned on ImageNet and replace the final classification head. This gives faster training, lower data requirements, and usually better results for small or medium custom datasets.

Load Inception v3 Without the Original Classifier

The pretrained top layer predicts ImageNet classes, so the first step is removing it and attaching a new head for your categories.

python
1import tensorflow as tf
2
3num_classes = 5
4
5base_model = tf.keras.applications.InceptionV3(
6    weights="imagenet",
7    include_top=False,
8    input_shape=(299, 299, 3),
9)
10
11base_model.trainable = False
12
13inputs = tf.keras.Input(shape=(299, 299, 3))
14x = tf.keras.applications.inception_v3.preprocess_input(inputs)
15x = base_model(x, training=False)
16x = tf.keras.layers.GlobalAveragePooling2D()(x)
17x = tf.keras.layers.Dropout(0.2)(x)
18outputs = tf.keras.layers.Dense(num_classes, activation="softmax")(x)
19
20model = tf.keras.Model(inputs, outputs)
21model.summary()

This builds a new classifier while keeping pretrained feature extraction frozen.

Prepare the Dataset Correctly

Transfer learning works best when input size, label structure, and preprocessing are consistent.

python
1import tensorflow as tf
2
3batch_size = 32
4img_size = (299, 299)
5
6train_ds = tf.keras.utils.image_dataset_from_directory(
7    "data/train",
8    image_size=img_size,
9    batch_size=batch_size,
10)
11
12val_ds = tf.keras.utils.image_dataset_from_directory(
13    "data/val",
14    image_size=img_size,
15    batch_size=batch_size,
16)
17
18train_ds = train_ds.prefetch(tf.data.AUTOTUNE)
19val_ds = val_ds.prefetch(tf.data.AUTOTUNE)

Keep class folder order stable and record train_ds.class_names, because Android apps, APIs, or reports often depend on the same label ordering later.

Train the New Classification Head First

Freeze the base model initially and train only the new head. This stabilizes optimization and prevents early destruction of pretrained features.

python
1model.compile(
2    optimizer=tf.keras.optimizers.Adam(1e-3),
3    loss="sparse_categorical_crossentropy",
4    metrics=["accuracy"],
5)
6
7history = model.fit(
8    train_ds,
9    validation_data=val_ds,
10    epochs=5,
11)

If you have very little data, add augmentation before the base model to improve generalization.

python
1augment = tf.keras.Sequential([
2    tf.keras.layers.RandomFlip("horizontal"),
3    tf.keras.layers.RandomRotation(0.05),
4    tf.keras.layers.RandomZoom(0.1),
5])

Then apply augment(inputs) before preprocess_input.

Fine-Tune the Upper Layers

Once the new head learns the task, unfreeze part of the pretrained model and continue with a lower learning rate.

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(1e-5),
8    loss="sparse_categorical_crossentropy",
9    metrics=["accuracy"],
10)
11
12fine_tune_history = model.fit(
13    train_ds,
14    validation_data=val_ds,
15    epochs=5,
16)

This allows the upper visual features to adapt to your categories without destabilizing the entire network.

Save the Model and Label Mapping

After training, save the model and the label list together.

python
1model.save("inception_v3_custom.keras")
2
3with open("labels.txt", "w", encoding="utf-8") as f:
4    for name in train_ds.class_names:
5        f.write(name + "\n")

Saving the label mapping is essential. If label order changes later, predictions can become semantically wrong even when accuracy seems fine.

Practical Training Guidance

Transfer learning quality depends on:

  • dataset cleanliness
  • label balance
  • enough validation data
  • matching preprocessing in training and inference

Inception v3 expects 299 by 299 RGB images and its specific preprocessing function. If you skip that or use another model’s normalization, training can look normal but underperform badly.

Also monitor validation loss, not just training accuracy. A pretrained backbone can overfit small datasets quickly once fine-tuning begins.

Common Pitfalls

One common mistake is leaving the original ImageNet classifier attached instead of replacing it. Another is fine-tuning the entire model immediately with a high learning rate, which often destroys pretrained features. Developers also forget to use preprocess_input for Inception v3 specifically. Label-order drift between training and inference is another frequent production bug. Finally, people often evaluate only accuracy and miss class imbalance problems that require precision and recall checks.

Summary

  • Load Inception v3 without the original top classifier.
  • Add a new dense head sized for your custom categories.
  • Freeze the base model first and train the new head.
  • Fine-tune upper layers later with a much smaller learning rate.
  • Keep preprocessing and label mapping identical across environments.
  • Save both the trained model and the class-name order for deployment.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
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