TensorFlow
Inception Model
Image Classification
Pre-trained Models
Machine Learning

TensorFlow Adding Class to Pre-trained Inception Model Outputting Full Image Hierarchy

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

If you want a pretrained Inception model to recognize a new class, the normal solution is transfer learning: keep the convolutional base, replace the classification head, and train a new output layer for your target labels. What you usually cannot do cleanly is bolt one extra class onto the old ImageNet softmax and expect TensorFlow to preserve some built-in "full hierarchy" of original classes plus your custom class automatically.

Understand What The Pretrained Head Actually Does

A pretrained Inception model such as InceptionV3 with include_top=True ends in a classifier trained on the original ImageNet label set. Those logits correspond to the training classes used for that model.

That means the final layer is not a generic expandable taxonomy. It is a fixed classifier head.

If your goal is custom classification, the standard TensorFlow guidance is to remove the top layer and attach a new one for your own classes.

Replace The Top Layer

Here is the standard Keras pattern.

python
1import tensorflow as tf
2from tensorflow import keras
3from tensorflow.keras import layers
4
5base_model = keras.applications.InceptionV3(
6    include_top=False,
7    weights="imagenet",
8    input_shape=(299, 299, 3)
9)
10base_model.trainable = False
11
12inputs = keras.Input(shape=(299, 299, 3))
13x = keras.applications.inception_v3.preprocess_input(inputs)
14x = base_model(x, training=False)
15x = layers.GlobalAveragePooling2D()(x)
16x = layers.Dropout(0.2)(x)
17outputs = layers.Dense(5, activation="softmax", name="custom_classes")(x)
18model = keras.Model(inputs, outputs)
19
20model.compile(
21    optimizer="adam",
22    loss="sparse_categorical_crossentropy",
23    metrics=["accuracy"]
24)

Here, the new output layer has 5 classes because the task is now defined by your dataset, not by ImageNet's original classifier.

If You Need Original Classes Too

This is where people often get stuck. If you need the original ImageNet predictions and your new label at the same time, you have to define what that means operationally.

Common choices are:

  • keep two separate models, one for ImageNet labels and one for your custom labels
  • build a new classifier head that predicts a new combined label space and retrain it accordingly
  • use the pretrained model only as a feature extractor and train your own downstream classifier

There is no free way to append a new label to the existing softmax without providing training data and defining how the new output relates to the old outputs.

About The "Full Image Hierarchy"

ImageNet labels are related to WordNet concepts, but the neural network's output is still just a vector of class scores. The model does not automatically reason over a symbolic hierarchy during ordinary inference.

So if you want hierarchical output, you usually implement it in post-processing or in a custom label structure. It is not something that appears automatically just because the original dataset had a taxonomy behind it.

Fine-Tune Later If Needed

After training the new head, you can optionally unfreeze part of the backbone and fine-tune with a low learning rate.

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

This can improve accuracy when the new image domain is meaningfully different from ImageNet.

Common Pitfalls

A common mistake is keeping include_top=True and trying to add one more class after the existing 1000-way softmax. That usually does not reflect a coherent training objective.

Another mistake is assuming the ImageNet hierarchy is built directly into inference output semantics. The model predicts class scores, not a rich symbolic tree.

It is also easy to forget preprocessing. Inception models expect a particular input size and preprocessing function, so custom data must follow the same conventions.

Summary

  • To add custom classes to Inception, remove the pretrained top layer and train a new classifier head.
  • The original ImageNet softmax is a fixed classifier, not a plug-and-play expandable taxonomy.
  • If you need both original and custom labels, define an explicit multi-model or combined-label strategy.
  • Hierarchical output usually has to be implemented outside the raw softmax layer.
  • Fine-tuning the backbone later can help once the new head is working.

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.