Edit tensorflow inceptionV3 retraining-example.py for multiple classificiations
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
If you are using the old TensorFlow InceptionV3 retraining example, the first thing to clarify is what “multiple classifications” means. For most people it means multi-class classification, where each image belongs to one class out of many. In that case, the script usually needs little or no structural editing because it already builds an output layer sized to the number of label folders it finds.
Multi-Class Retraining With the Original Script
The classic retrain.py or similarly named example works by scanning a directory tree and treating each subdirectory as one label. If your dataset has cats, dogs, and birds, you do not hardcode three outputs by hand. You place images into three folders and the script creates the final classifier layer with three logits.
A typical dataset layout looks like this:
Then you run the retraining command against that root directory:
The important detail is that the script infers the class count from the folders under --image_dir. If you add a fourth folder named horses, the model output layer becomes four-way automatically. For the common one-image-one-label case, that is the only change you need.
Under the hood, the script usually does three things:
- It loads the pre-trained InceptionV3 graph.
- It caches bottleneck features for each training image.
- It trains a new final layer whose width matches the number of labels.
That means you should focus more on dataset organization than on editing the TensorFlow graph manually.
When You Actually Need Code Changes
You do need code changes if one image can belong to several labels at once. That is a multi-label problem, not a multi-class one. A photo might be both outdoor and sunset, for example. The old retraining example is generally built for a single target label per image and ends with a softmax style classifier.
For multi-label training, you need three conceptual changes:
- Encode labels as multi-hot vectors instead of one label index.
- Use a sigmoid output per label instead of softmax.
- Train with binary cross-entropy instead of categorical cross-entropy.
In modern TensorFlow, it is often simpler to move to Keras rather than heavily patch the old example script. A small example looks like this:
With that model, a target such as [1, 0, 1, 0, 0] means the image belongs to labels one and three at the same time. That cannot be represented correctly by the original one-label retraining flow.
What to Edit in an Older TensorFlow Script
If you must keep the older script, inspect the parts that define the final layer and loss. In many versions, you will find a placeholder or tensor for the ground-truth labels, then a fully connected layer on top of the bottleneck tensor, and finally a cross-entropy calculation.
For a true multi-label setup, the edits usually include:
- changing the label placeholder shape from one class index to a full label vector
- replacing the softmax loss path with sigmoid cross-entropy
- updating evaluation logic so predictions can return several labels above a threshold
The prediction stage changes as well. In a softmax model, you often take the single highest score. In a sigmoid model, you pick every label whose probability is above a threshold such as 0.5:
Common Pitfalls
The most common mistake is confusing multi-class with multi-label. If each image has exactly one class, the old retraining example already supports more than two classes through the folder layout alone.
Another common issue is inconsistent directory naming. The script treats folder names as labels, so Dog and dog may become separate classes depending on your workflow and operating system conventions.
Class imbalance also hurts transfer learning. If one folder has thousands of images and another has a few dozen, the final layer may look accurate overall while performing badly on the small classes. Balance the dataset or use class weighting.
Finally, many online answers refer to TensorFlow 1.x example scripts that are now dated. If you are starting new work, prefer a Keras-based pipeline because it is easier to inspect, easier to debug, and much easier to adapt when the target problem changes.
Summary
- For ordinary multi-class retraining, you usually do not edit
retraining-example.pymuch at all. - The number of output classes is normally inferred from the label subdirectories in your image folder.
- Real code changes are needed when one image can have several labels at once.
- Multi-label training requires sigmoid outputs, multi-hot targets, and binary cross-entropy.
- For new projects, a small Keras model on top of InceptionV3 is usually cleaner than patching the old example script.
Related reading
- Edit tensorflow inceptionV3 retraining-example.py for multiple classificiations
- Efficient element-wise multiplication of a matrix and a vector in TensorFlow
- Efficiently Finding Closest Word In TensorFlow Embedding
- Efficiently grab gradients from TensorFlow?
- Efficiently implementing erode/dilate
- Ellipse Detection using Hough Transform
- Effective queries in machine learning
- Effects of randomizing the order of inputs to a neural network
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.