How to do transfer learning for MNIST dataset?
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
Transfer learning means starting from a model that has already learned useful visual features and adapting it to a new task. MNIST is small enough that training from scratch is often fine, but it is still a good dataset for learning the transfer-learning workflow in TensorFlow and Keras.
Why MNIST Needs Preprocessing First
Most pretrained image models expect larger RGB images, while MNIST images are 28 x 28 grayscale digits. Before reuse is possible, the images need to be resized and converted from one channel to three channels.
That means a transfer-learning pipeline for MNIST usually includes:
- resize from
28 x 28to the base model input size - duplicate the grayscale channel into RGB
- scale pixel values to the range expected by the pretrained model
Build A Preprocessing Pipeline
The example below uses MobileNetV2, which is a lightweight ImageNet model that works well for demonstrations.
The choice of 96 x 96 keeps the model small enough to train quickly while still matching a practical input size for pretrained vision backbones.
Freeze The Base Model
In the first stage, reuse the pretrained convolution layers as a fixed feature extractor.
The classifier head is new, but the convolution stack starts from ImageNet features. Even though handwritten digits are very different from natural photographs, low-level edge and shape features can still transfer.
Train The New Classification Head
Now train only the newly added layers.
On MNIST, this stage is often enough to reach strong accuracy quickly because the task is relatively simple.
Fine-Tune If Needed
If the frozen model plateaus and you want a small improvement, unfreeze part of the base model and continue training with a lower learning rate.
Fine-tuning too aggressively can destroy the pretrained weights, so smaller learning rates are important.
When Transfer Learning Is Overkill
MNIST is so small and well-behaved that a compact CNN trained from scratch can perform extremely well. The transfer-learning value here is mostly educational: you learn how to adapt image size, channels, freezing, and fine-tuning.
For harder image tasks with smaller labeled datasets, the same workflow becomes much more valuable.
Common Pitfalls
A common mistake is passing raw 28 x 28 x 1 MNIST images directly into an ImageNet backbone. Pretrained models expect a specific input size and channel count.
Another mistake is forgetting the model-specific preprocessing function. For MobileNetV2, preprocess_input matters because the weights were trained with a specific input normalization scheme.
It is also easy to unfreeze the whole backbone too early and train with a high learning rate. That usually harms performance instead of helping.
Summary
- Resize MNIST images and convert them from grayscale to RGB before using pretrained vision backbones.
- Freeze the base model first and train only the new classification head.
- Use the backbone's matching preprocessing function.
- Fine-tune later with a low learning rate if the frozen model plateaus.
- On MNIST, transfer learning is mainly a workflow exercise because simpler models also work very well.
Related reading
- How to do weight initialization by xavier rule in Tensorflow 2.0?
- How to do Xavier initialization on TensorFlow
- How to do zero padding in keras conv layer?
- How to downgrade to cuda 10.0 in arch linux?
- How to download datasets for sklearn? - python
- How to download graphs from tensorboard?
- How to downgrade to tensorflow-gpu version 1.12 in google colab
- How to dynamically freeze weights after compiling model in Keras?
.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.