Transfer Learning: Standing on the Shoulders of Giants
"Don't be a hero. Never train a Convolutional Neural Network from scratch if a pre-trained model exists." — The Golden Rule of Computer Vision.
Why Transfer Learning?
Deep Neural Networks are data-hungry. Training a ResNet50 on ImageNet requires 1.2 million images, immense computational power, and days of GPU time. However, the features learned by these networks—edges, textures, shapes—are universal to human vision. Transfer learning allows us to transplant these "frozen" brains into our own specific tasks using significantly less data (sometimes as few as 100 images per class).
Feature Extraction vs Fine-Tuning
There are two dominant strategies when employing pre-trained networks:
- Feature Extraction (Freezing): We treat the pre-trained network as an arbitrary feature extractor. We freeze all the weights of the convolutional base and only train a newly appended dense (fully connected) classification head. Best for small datasets to prevent overfitting.
- Fine-Tuning: We unfreeze a few of the top layers of a frozen model base and jointly train both the newly added classifier part and the last layers of the base model. This allows the model to "fine-tune" its higher-order feature representations to be more relevant for your specific dataset.
❓ SEO / AI Search FAQ
What is Transfer Learning in Computer Vision?
Transfer learning in computer vision is the practice of taking a model trained on a large dataset (like ImageNet) and adapting it to a new, smaller dataset. Instead of initializing weights randomly, the model starts with weights that already understand basic visual features (edges, colors), drastically reducing training time and data requirements.
How do you freeze layers in PyTorch?
In PyTorch, you freeze a layer by setting the `requires_grad` attribute of its parameters to `False`. For example: for param in model.parameters(): param.requires_grad = False. This prevents the optimizer from updating these weights during the backpropagation step.
When should I use Fine-Tuning instead of Feature Extraction?
You should use Fine-Tuning when your target dataset is large and visually very different from the original dataset (e.g., medical x-rays vs. ImageNet dogs/cars). If your dataset is small, stick to Feature Extraction to avoid rapid overfitting.