Supervised pre-training, typically using large labeled datasets like ImageNet, has been a standard practice for initializing models for transfer learning. However, creating such massive labeled datasets is expensive and time-consuming. Furthermore, models pre-trained on specific datasets might carry biases or learn features not perfectly suited for extremely different target domains. Self-Supervised Learning (SSL) emerges as a compelling alternative, enabling models to learn rich visual representations directly from unlabeled data.
The core idea behind SSL is to create a "pretext" task where the supervision signal is derived from the data itself, rather than from human-provided labels. By solving this pretext task, the model is forced to learn meaningful semantics, patterns, and structures within the visual data. The features learned during this self-supervised pre-training phase often transfer remarkably well to downstream tasks like classification, detection, or segmentation, sometimes even outperforming supervised pre-training, especially when labeled data for the downstream task is scarce.
Designing Pretext Tasks
The effectiveness of SSL depends on the design of the pretext task. The task should be challenging enough to necessitate learning high-level semantic features, yet solvable using only the input data. Several families of pretext tasks have proven successful in computer vision:
Contrastive Learning
Contrastive methods are currently among the most popular and effective SSL approaches. The fundamental principle is to learn representations that pull augmented versions ("views") of the same image closer together in an embedding space, while pushing representations of different images farther apart.
Imagine taking an image and creating two different distorted versions of it (e.g., through cropping, color jittering, rotation). These form a "positive pair". Any view from a different image forms a "negative pair". The model, typically a CNN encoder, processes these views to generate feature vectors (embeddings). A contrastive loss function, like NT-Xent (Normalized Temperature-scaled Cross Entropy), then encourages the embeddings of positive pairs to have high similarity (e.g., high cosine similarity) and embeddings of negative pairs to have low similarity.
Popular contrastive learning frameworks include:
- SimCLR (Simple Framework for Contrastive Learning of Visual Representations): Uses a large batch size to provide many negative examples directly within the batch. It employs a projection head (a small MLP) after the encoder during pre-training, which is discarded for downstream tasks. Strong data augmentation is a significant component.
- MoCo (Momentum Contrast): Addresses the need for many negative examples without requiring massive batch sizes. It maintains a dynamic dictionary (queue) of encoded keys from previous batches, using a momentum-updated encoder to ensure consistency.
- BYOL (Bootstrap Your Own Latent): An interesting approach that learns by predicting the output of a target network (a momentum-updated version of the online network) for a different augmented view of the same image. It achieves strong results without explicit negative pairs, relying instead on the interaction between the online and target networks.
Overview of contrastive self-supervised learning. Augmented views of the same image (A1, A2) produce representations (z_A1, z_A2) that are pulled together, while representations from different images (z_A1, z_B1) are pushed apart.
Masked Image Modeling (MIM)
Inspired by the success of Masked Language Modeling (MLM) in NLP (like BERT), Masked Image Modeling techniques apply a similar concept to vision. The idea is to randomly mask a significant portion of an input image and train the model to predict the content of the masked regions.
- BEiT (Bidirectional Encoder representation from Image Transformers): Masks patches of an image and predicts discrete visual tokens corresponding to the original masked patches. These visual tokens are obtained from a pre-trained discrete Variational Autoencoder (dVAE).
- MAE (Masked Autoencoders): Uses an asymmetric encoder-decoder architecture. The encoder (often a Vision Transformer) operates only on the visible patches (a small fraction, e.g., 25%). A lightweight decoder then reconstructs the full image pixels from the encoded representation of visible patches and mask tokens. This asymmetry makes pre-training very efficient.
By learning to reconstruct or predict masked parts, the model must understand context, object shapes, and textures from the surrounding visible portions, leading to powerful representations.
Other Pretext Tasks
While contrastive learning and MIM are dominant, other approaches exist:
- Predicting Rotation: Train a model to predict the rotation angle (e.g., 0, 90, 180, 270 degrees) applied to an input image.
- Jigsaw Puzzles: Divide an image into patches, shuffle them, and train the model to predict the correct permutation.
- Clustering-based (e.g., DeepCluster): Periodically cluster the features produced by the network and use the cluster assignments as pseudo-labels to update the network weights.
Utilizing SSL Pre-trained Models
Once the model is pre-trained using a pretext task on a large unlabeled dataset, the learned encoder serves as an excellent feature extractor. The typical workflow mirrors supervised transfer learning:
- Pre-train: Train a model (e.g., a ResNet or ViT encoder) on a large unlabeled dataset using an SSL method like SimCLR or MAE.
- Adapt: Take the pre-trained encoder (discarding any SSL-specific heads like the projection head in SimCLR or the decoder in MAE).
- Fine-tune: Add a task-specific head (e.g., a linear classifier for image classification, detection heads for object detection) and fine-tune the entire model or parts of it on a (potentially small) labeled dataset for the target task. Alternatively, use the frozen encoder as a feature extractor.
Advantages
Advantages:
- Reduces Label Dependency: Uses substantial amounts of easily accessible unlabeled data.
- Improved Generalization: Can learn more general and potentially less biased representations compared to supervised pre-training on specific datasets.
- State-of-the-Art Performance: SSL pre-training often matches or exceeds supervised pre-training performance on various downstream benchmarks, especially in low-data regimes.
- Foundation for Adaptation: Provides a strong starting point for the domain adaptation and few-shot learning techniques discussed earlier in this chapter.
Key Points:
- Computational Cost: SSL pre-training can be computationally intensive, often requiring significant GPU resources and time, though methods like MAE offer improved efficiency.
- Hyperparameter Sensitivity: Performance can be sensitive to the choice of pretext task, data augmentations, optimizer settings, and other hyperparameters.
- Negative Transfer: While rare with modern methods, there's always a possibility that features learned via a specific pretext task might not perfectly align with a particular downstream task.
Self-supervised learning represents a significant advancement in training deep learning models for vision. By cleverly defining pretext tasks that extract supervisory signals from the data itself, SSL allows us to use unlabeled data to build powerful, general-purpose visual encoders, providing a foundation for tackling diverse computer vision challenges through transfer learning and adaptation.