Semi-supervised Vision Transformer

Machine Learning
Introduced by
Dosovitskiy et al. (ViT); semi-supervised extensions by multiple groups (2021–2023)
Year
2021–2022
Type
Semi-supervised deep learning for image understanding
Family
Deep learning / NLP / CV

What does it do?

Semi-supervised Vision Transformer refers to a family of approaches that apply the patch-based self-attention architecture of ViT when only a fraction of images carry labels.

Unlabeled images are used through pseudo-labeling, consistency regularization, or self-supervised pre-training, and the model is then fitted on the small labeled set. Because 'semi-supervised ViT' is not a single standardised algorithm, results and implementation details depend on which strategy is combined with which backbone.

The intuition behind the method

A Vision Transformer splits an image into fixed-size patches, treats each patch as a token, and uses self-attention so that every patch can relate to every other patch within a layer. In a semi-supervised setting most images have no label, so the model first learns from the unlabeled pool, through pseudo-labels, agreement between augmented views, or a pretext task, and then uses the small labeled set to fit the task. Self-attention does not by itself guarantee good generalisation from few labels; the benefit depends on how the representation is learned before and during fine-tuning.

History

Vision Transformers were introduced by Dosovitskiy et al. at ICLR 2021, showing that a pure transformer applied to image patches can match or exceed CNNs on large-scale image recognition. Subsequent work on scaling vision transformers (Zhai et al., 2022) studied how performance depends on model and data size. Self-supervised pre-training methods were later used as initialisation stages for ViT backbones; they are self-supervised rather than semi-supervised, but they are often combined with semi-supervised training. Pseudo-labeling schemes from semi-supervised learning have been adapted to ViT backbones in later work, and the term semi-supervised ViT refers to such combinations rather than to a single originating method.

When to use it

Choose a semi-supervised ViT when a large pool of unlabeled images exists alongside a small set of human-verified labels, and when the task is image classification or recognition in which global context across the image is useful, for example medical imaging, satellite imagery, or fine-grained species recognition. It is a more reasonable choice than a plain supervised ViT when the labeled set is small relative to the number of classes and the difficulty of the task. No universal labeled-set size applies: the point at which the method pays off depends on the number of classes, how similar the domain is to the pre-training data, and task difficulty. Compare against a supervised baseline, including a CNN, trained on the same labeled subset. Do not use it when labels are abundant enough for fully supervised training, when compute is severely constrained because ViT pre-training is expensive, or when a CNN trained on the same small labeled set performs comparably, since transformers need scale to learn good inductive biases. Do not use it where decisions must be auditable: attention maps show where the model attended but are not quantitative decision coefficients.

Limitations and considerations

  • Computationally expensive: pre-training large ViT backbones requires significant GPU resources and training time.
  • Pseudo-label quality degrades for out-of-distribution or ambiguous images, potentially reinforcing early errors.
  • ViT can underperform CNNs when training data is limited: the original ViT work reports weaker results than CNNs when pre-training data is small, and the gap narrows as pre-training data grows.
  • Hyperparameter sensitivity: confidence threshold for pseudo-labels, augmentation strength, and unsupervised loss weight all interact and require tuning.
  • Less interpretable than feature-based methods; attention maps provide intuition but not auditable decision rules.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Uses large unlabeled image collections to build representations, which can reduce the number of labeled images needed for a given task.
  • Self-attention relates all patches within one layer, so it can capture dependencies between distant image regions that convolutional filters cover only in deeper layers.
  • Publicly available self-supervised ViT checkpoints mean the pre-training stage often does not have to be run from scratch.
  • ViT performance is generally reported to grow with model size and training data scale, so additional unlabeled data can help when the backbone has the capacity to use it.
  • Pseudo-labeling and consistency losses can be added to an existing ViT training loop without changing the architecture.

Applications

  • Medical image classification and pathology detection with limited annotated scans.
  • Satellite and aerial image analysis where labeling large volumes is prohibitively expensive.
  • Fine-grained visual recognition (species, product, vehicle) with only a few labeled examples per class.
  • Industrial defect detection where defective samples are rare but normal images are plentiful.
  • Autonomous driving scene understanding using large unlabeled sensor streams.
  • Scientific image analysis in microscopy, astronomy, and remote sensing.

How it works

How it works

1

Each input image is split into a grid of fixed-size patches (e.g., 16x16 pixels). Each patch is flattened and linearly projected into an embedding vector. A learnable class token and positional embeddings are prepended and added, giving the transformer its spatial context before any attention is applied.

2

A model trained on the available labeled examples predicts labels for unlabeled images, either as soft or as hard pseudo-labels. Predictions above a confidence threshold are kept as training targets in later rounds, which gradually enlarges the effective labeled set. The threshold limits how many noisy pseudo-labels enter training, although it does not remove errors that the model is confidently wrong about.

3

Two differently augmented views of the same unlabeled image should produce the same prediction. A consistency loss is computed between the predicted class distributions for the two views; mean squared error or cross-entropy between these outputs are common choices. The loss penalises disagreement and encourages augmentation-invariant representations without requiring ground-truth labels. Implementation details of the loss, including how the targets are handled, should be stated when reporting results. The weight on this unsupervised term is a hyperparameter that should be tuned on a validation set rather than on the test set.

4

Before labeled fine-tuning, the ViT backbone is pre-trained on unlabeled images with a pretext objective. Examples include masked image modeling, contrastive or self-distillation methods, and image rotation prediction. This stage supplies the initial patch-level and global representations, and its benefit for downstream accuracy depends on the pretext method and the data.

5

The pre-trained or iteratively pseudo-labeled backbone is fine-tuned end-to-end on the small labeled set. A classification head is attached to the class token output and trained with standard cross-entropy loss. Learning rate scheduling and label smoothing are commonly applied to reduce overfitting on the limited labeled examples.

Common pitfalls

  • Using too low a confidence threshold for pseudo-labels, introducing noisy supervision that degrades accuracy.
  • Skipping a self-supervised pre-training stage and expecting pseudo-labeling alone to compensate for a weak initial backbone.
  • Evaluating only on the labeled portion of the training set rather than a genuinely held-out test set.
  • Ignoring class imbalance in the labeled set, which can bias pseudo-label generation toward majority classes.
  • Fine-tuning with a learning rate too large for the small labeled set, causing catastrophic forgetting of pre-trained representations.

Frequently asked questions

How many labeled images do I need for semi-supervised ViT to work well?

There is no universal minimum. Strong self-supervised pre-training can make very small labeled sets usable on some datasets, but the required number depends heavily on the number of classes, the similarity of your domain to the pre-training data, and task difficulty. The practical test is to compare against a fully supervised baseline, including a CNN, trained on the same labeled subset.

Should I start from a pre-trained ViT checkpoint or train from scratch?

Starting from a publicly available pre-trained ViT checkpoint (for example a ViT-B/16 model pre-trained on ImageNet with a self-supervised or supervised objective) is strongly recommended. Training from scratch requires massive unlabeled data and compute. If your domain is very different from ImageNet, domain-adaptive pre-training on your unlabeled data before fine-tuning is advisable.

What is the difference between semi-supervised ViT and self-supervised ViT?

Self-supervised ViT uses only unlabeled data throughout training, relying entirely on pretext tasks. Semi-supervised ViT combines unlabeled data (for representation learning or pseudo-labeling) with a small labeled set for task-specific supervision. Semi-supervised training directly optimises the downstream classification objective, while self-supervised training produces general features that must then be fine-tuned.

Which semi-supervised strategy works best with ViT: pseudo-labeling or consistency regularization?

Both are used in practice and are often combined, and neither is guaranteed to be better for every backbone. Pseudo-labeling relies on the model's confidence to select targets, so miscalibrated errors can be reinforced. Consistency-based methods depend on the quality of the augmentations and on the model's predictions under them. Which strategy works best for a given ViT backbone and dataset should be checked empirically on a held-out set. A self-supervised pre-training stage followed by pseudo-labeling during fine-tuning is a common combination.

Is semi-supervised ViT suitable for non-ImageNet-like image domains?

Yes, but it requires more care. For domains far from natural photographs (e.g., histopathology, X-rays, satellite images), fine-tuning a general ViT checkpoint may underperform a CNN trained from scratch on domain data. Domain-adaptive pre-training, continuing masked image modeling or contrastive pre-training on your unlabeled domain images before supervised fine-tuning, typically closes this gap.

Related methods

Sources

  1. Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., Dehghani, M., Minderer, M., Heigold, G., Gelly, S., Uszkoreit, J., & Houlsby, N. (2021). An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. International Conference on Learning Representations (ICLR 2021).

    Open publication reference
  2. Zhai, X., Kolesnikov, A., Houlsby, N., & Beyer, L. (2022). Scaling Vision Transformers. Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 12104–12113.

    Open publication reference