Train multi-class image classifier in Keras
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
Training a multi-class image classifier in Keras is mostly about keeping the data pipeline, label encoding, and model output consistent. The network architecture matters, but many training failures come from mismatched class labels, missing normalization, or weak validation setup rather than from the CNN itself. A solid baseline should be simple, reproducible, and easy to improve later with transfer learning.
Organize the Dataset Correctly
Keras works well with a directory-per-class layout:
This lets you use the built-in dataset loader:
By default, labels are integer class IDs. That choice affects the correct loss and metric configuration later.
Normalize and Prepare the Input Pipeline
Images should be normalized before training. A simple Rescaling layer is enough for a baseline model.
If the dataset fits in memory, cache() can speed things up further. If not, skip it and keep the pipeline streaming.
Build a Simple Baseline CNN
Start with a small model you can reason about before jumping to more complex transfer-learning setups.
The final dense layer size must equal the number of classes, and softmax pairs naturally with multi-class classification.
Add Data Augmentation Carefully
Augmentation can improve generalization, but keep it realistic for the domain.
You can insert it near the model input:
For some tasks, such as medical imaging or OCR, careless augmentation can damage label meaning. Use domain judgment.
Train with Callbacks
Callbacks help stop overtraining and preserve the best model.
That gives a reliable training loop without much extra complexity.
Evaluate Beyond Accuracy
Validation accuracy is helpful, but you should also inspect actual predictions and class-specific errors.
For imbalanced datasets, add confusion matrices or per-class precision and recall. A model can show acceptable overall accuracy while failing badly on one class.
Upgrade Path: Transfer Learning
If the baseline underperforms, use a pretrained backbone such as EfficientNet or MobileNet. That usually improves results on small or medium datasets.
Then place your classifier head on top. This is often a better starting point than trying to deepen a small custom CNN endlessly.
Common Pitfalls
The most common mistake is mismatching labels, loss, and output layer. Integer labels with categorical_crossentropy, or one-hot labels with sparse_categorical_crossentropy, will produce wrong training behavior.
Another issue is forgetting normalization or using inconsistent preprocessing between training and inference.
Developers also often rely on training accuracy alone. If validation metrics are ignored, the model can look good while overfitting badly.
Summary
- Keep class directories, label encoding, and final output size consistent.
- Normalize images and build a reproducible
tf.datapipeline. - Start with a simple baseline CNN before moving to transfer learning.
- Use callbacks to stop early and preserve the best weights.
- Inspect validation behavior and per-class errors, not just headline accuracy.
Related reading
- Train Stacked Autoencoder Correctly
- Train Stacked Autoencoder Correctly
- Train Tensorflow Object Detection on own dataset
- Training a fully convolutional neural network with inputs of variable size takes unreasonably long time in Keras/TensorFlow
- Train TensorFlow language model with NCE or sampled softmax
- Train Tensorflow Object Detection on own dataset
- Trained models for tensorflow ocr
- Training time of Tensorflow Object Detection API on MSCOCO
.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.