Quest 1 โข Lesson 6
๐ Transfer Learning
Transfer learning lets you take a model trained on a large dataset (like MobileNet on ImageNet) and reuse it for a new, similar task. You only need to retrain the final layer on your own data โ saving time and computational power.
"Think of it as learning to ride a bicycle โ once you know balance, you can quickly adapt to a motorcycle. The core skills transfer."
๐ง How Transfer Learning Works
- Pre-trained model โ MobileNet (trained on 1000+ object categories).
- Feature extraction โ Use the model as a fixed feature extractor (remove the top classification layer).
- New classifier โ Add a new dense layer (or a few layers) and train it on your dataset.
- Fine-tuning (optional) โ Unfreeze some layers and continue training with a low learning rate.
MobileNet (frozen) โ Embeddings (1024 dims) โ New Dense(2) โ Softmax
transfer_learning.js
// Load pre-trained MobileNet
const mobilenet = await mobilenet.load();
// Extract embeddings for each image
const embedding = mobilenet.infer(img, true); // true = embeddings
// Build new classifier on top
const newModel = tf.sequential();
newModel.add(tf.layers.dense({units: 128, activation: 'relu', inputShape: [1024]}));
newModel.add(tf.layers.dense({units: 2, activation: 'softmax'}));
const mobilenet = await mobilenet.load();
// Extract embeddings for each image
const embedding = mobilenet.infer(img, true); // true = embeddings
// Build new classifier on top
const newModel = tf.sequential();
newModel.add(tf.layers.dense({units: 128, activation: 'relu', inputShape: [1024]}));
newModel.add(tf.layers.dense({units: 2, activation: 'softmax'}));
๐ Live Demo: Train Your Own Classifier
Upload images for two classes (e.g., cats vs dogs). Add at least 2 images per class, then train. Test with new images.
Class 0
Class 1
Load images for both classes, then click "Train".
๐ How the demo works
1. MobileNet extracts features (embeddings) from each image.
2. A new dense classifier is trained on these embeddings.
3. Training is done on the fly in your browser (no server).
4. After training, new images are classified using the trained model.
2. A new dense classifier is trained on these embeddings.
3. Training is done on the fly in your browser (no server).
4. After training, new images are classified using the trained model.
๐ Transfer Learning in Practice
- ๐จ Style transfer (e.g., Prisma app)
- ๐ผ๏ธ Custom image classifiers with small datasets
- ๐ฃ๏ธ Speech recognition (reuse acoustic models)
- ๐ Text classification (BERT fineโtuning)
- โ๏ธ Medical imaging (Xโray, MRI) with limited labelled data
โจ Challenge: Train a 3โClass Classifier
Modify the code to support 3 classes. (Hint: change the final layer to 3 units and adjust labels.)
// Change the final layer to 3 units
newModel.add(tf.layers.dense({units: 3, activation: 'softmax'}));
// Labels: [1,0,0], [0,1,0], [0,0,1] for three classes
newModel.add(tf.layers.dense({units: 3, activation: 'softmax'}));
// Labels: [1,0,0], [0,1,0], [0,0,1] for three classes
โค๏ธ Support Free Education
This course is 100% free. If it helps you, consider buying me a coffee.
โ Buy Me a Coffeeโก๏ธ Ready for more?
Next lesson: Bias & Fairness in AI โ build responsible AI.
Continue to Lesson 1.7 โ(Coming soon โ check back or buy Pro Pack for instant access)