Unsupervised Domain Adaptation for Visual Recognition
Module: Computer Vision | Difficulty: Premium
Domain Discrepancy
CORAL Loss
where are covariance matrices of source and target features.
DANN (Domain-Adversarial Training)
where GRL is the gradient reversal layer.
| Model | Office-Home | DomainNet | VisDA-C | Approach |
|---|---|---|---|---|
| DANN | 65.8% | 52.3% | 73.2% | Adversarial |
| CDAN | 70.2% | 57.1% | 80.1% | Conditional |
| DPL | 73.5% | 60.2% | 83.5% | Pseudo-label |
| DAFormer | 78.2% | 65.8% | 88.2% | Transformer |
import torch
import torch.nn as nn
import torch.nn.functional as F
class GradientReversalLayer(torch.autograd.Function):
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
return -ctx.alpha * grad_output, None
class DomainAdversarialNetwork(nn.Module):
def __init__(self, feature_extractor, classifier,
domain_discriminator, alpha=1.0):
super().__init__()
self.feature_extractor = feature_extractor
self.classifier = classifier
self.domain_discriminator = domain_discriminator
self.alpha = alpha
def forward(self, x, return_domain=False):
features = self.feature_extractor(x)
class_output = self.classifier(features)
reversed_features = GradientReversalLayer.apply(
features, self.alpha)
domain_output = self.domain_discriminator(reversed_features)
if return_domain:
return class_output, domain_output
return class_output
class SelfTrainingDA:
def __init__(self, model, threshold=0.95):
self.model = model
self.threshold = threshold
def generate_pseudo_labels(self, target_data):
self.model.eval()
pseudo_labels = []
with torch.no_grad():
logits = self.model(target_data)
probs = F.softmax(logits, dim=1)
max_probs, labels = probs.max(dim=1)
mask = max_probs > self.threshold
pseudo_labels.append((target_data[mask], labels[mask]))
return pseudo_labels
Research Insight: Domain adaptation has evolved from adversarial methods (DANN) to self-training approaches (DPL, DAFormer) that use pseudo-labeling on target data. The key insight is that high-confidence pseudo-labels provide effective supervision for adapting to the target domain. DAFormer demonstrated that transformer-based architectures are more robust to domain shift than CNNs, likely due to their global receptive field and data efficiency. The combination of masked image modeling with domain adaptation achieves state-of-the-art results by learning domain-invariant features.