Vision Transformer-based Adversarial Domain Adaptation
Yahan Li Yuan Wu



: A Processing-Near-Memory Architecture for 
Multi-Scheme Fully Homomorphic Encryption
    [
    April 29, 2024
======================================================================================




Unsupervised domain adaptation (UDA) aims to transfer knowledge from a labeled source domain to an unlabeled target domain. The most recent UDA methods always resort to adversarial training to yield state-of-the-art results and a dominant number of existing UDA methods employ convolutional neural networks (CNNs) as feature extractors to learn domain invariant features. Vision transformer (ViT) has attracted tremendous attention since its emergence and has been widely used in various computer vision tasks, such as image classification, object detection, and semantic segmentation, yet its potential in adversarial domain adaptation has never been investigated. In this paper, we fill this gap by employing the ViT as the feature extractor in adversarial domain adaptation. Moreover, we empirically demonstrate that ViT can be a plug-and-play component in adversarial domain adaptation, which means directly replacing the CNN-based feature extractor in existing UDA methods with the ViT-based feature extractor can easily obtain performance improvement. The code is available at <https://github.com/LluckyYH/VT-ADA>.





Domain Adaptation, adversarial training, vision transformer




§ INTRODUCTION



Deep neural networks (DNNs) have delivered remarkable achievements across diverse domains, spanning computer vision (CV), natural language processing (NLP), and speech recognition <cit.>. However, these impressive strides are intrinsically tethered to copious volumes of annotated data <cit.>. Collecting such labeled data is perennially laborious and resource-intensive, a challenge that underscores the quest for strategies to extrapolate knowledge effectively from label-rich domains to label-scarce ones <cit.>. Unfortunately, conventional supervised machine learning presupposes training and test data be drawn from identical data distributions, a notion often at odds with real-world scenarios due to domain shifts.

The predicament of domain shift, a formidable challenge, has spawned the discipline of domain adaptation, endeavoring to transmute knowledge from source domains rich in labels to target domains bereft of them. In machine learning, Unsupervised Domain Adaptation (UDA) shoulders the more onerous task of transposing knowledge from labeled source domains to unlabeled target domains <cit.>. Early UDA techniques entail reweighting source instances based on their pertinence to the target domain via human-engineered features <cit.>. The most recent UDA methodologies traverse a different trajectory by projecting data points from both source and target domains into a shared latent space. They strive to diminish the chasm between the data distributions of source and target domains while nurturing features that are both transferrable and discriminative <cit.>.

The quest to minimize domain discrepancy unfolds along two strategies. One avenue leverages metrics like maximum mean discrepancy (MMD) <cit.> or correlation distances <cit.>. Alternatively, adversarial training, as pioneered by <cit.>, and refined by <cit.>, deploys a captivating two-player minimax game. Here, a domain discriminator locks horns with a feature extractor, with the former striving to demarcate source features from target ones, and the latter endeavoring to obfuscate the discriminator's task <cit.>.

Most extant UDA approaches lean heavily upon Convolutional Neural Networks (CNNs) <cit.>. However, the recent emergence of the Vision Transformer (ViT) <cit.> has galvanized the computer vision landscape. In stark contrast to CNNs, which glean insights from local image patches, ViT partitions an image into non-overlapping patches and harnesses self-attention to model long-range dependencies among these patches <cit.>. This raises a compelling inquiry: Can ViT, which has demonstrated its mettle in diverse computer vision domains <cit.>, be harnessed to augment adversarial domain adaptation (ADA)?

Our research embarks upon addressing this very question. We introduce the Vision Transformer-based Adversarial Domain Adaptation (VT-ADA) approach by replacing the CNN-based feature extractor employed in ADA methods with ViT. Notably, our empirical investigations underscore that this substitution is not only facile but also yields tangible enhancements in learning domain-invariant features. We conduct extensive experiments across three UDA benchmarks, wherein the observations unequivocally affirm the efficacy of VT-ADA in augmenting the transferability and discriminability of domain-invariant features. Remarkably, our variant of VT-ADA, which integrates ViT into CDAN <cit.> emerges as a potent contender against state-of-the-art ADA methods.



§ RELATED WORKS





 §.§ Unsupervised Domain Adaptation



Unsupervised domain adaptation (UDA) addresses the domain shift challenge by training a model to transition from a labeled source domain to an unlabeled target domain <cit.>. Predominantly, UDA methodologies utilize CNN-based feature extractors and adversarial training to cultivate domain-invariant features that exhibit robust generalization across disparate domains <cit.>. Domain Adversarial Neural Networks (DANNs) integrate a gradient reversal layer and adversarial training within the UDA framework <cit.>, while Adversarial Discriminative Domain Adaptation (ADDA) employs asymmetric feature extractors to facilitate domain alignment <cit.>. Additionally, the Cross-Domain Transformer (CDTrans) leverages cross-attention mechanisms to derive common representations shared between the source and target domains <cit.>.






 §.§ Vision Transformer



The Transformer architecture, initially designed to tackle challenges in NLP, incorporates mechanisms such as word embedding, position embedding, and self-attention <cit.>. The adaptation of this architecture for visual tasks led to the development of the ViT, which has revolutionized the field of CV by treating images as sequences of non-overlapping patches <cit.>. This approach departs from the traditional CNNs that rely on domain-specific inductive biases, embracing instead the advantages of extensive pre-training and global context modeling. ViT has proven its capability, matching or even surpassing CNNs in tasks like image classification and setting the stage for its broader adoption as a replacement for CNNs across various CV challenges. The growing dominance of ViT-based models in the field is evident <cit.>. In this paper, we explore the potential of ViT as a plug-and-play component within existing ADA frameworks, aiming to enhance the discriminative power and transferability of domain-invariant features.



 §.§ Self-Attention


The self-attention mechanism is a pivotal component in ViT <cit.>. Specifically, an image ℐ∈ℝ^H× W× C is transformed into a sequence of flattened 2D patches ∈ℝ^N× (P^2· C), where (H, W) represents the height and weight dimensions of the original image, C denotes the number of channels, (P, P) specifies the resolution of each image patch, and N=HW/P^2 is the number of resulting patches. For the self-attention, these patches are initially projected into three vectors: queries 𝒬∈ℝ^N× d_k, keys 𝒦∈ℝ^N× d_k, and values 𝒱∈ℝ^N× d_v, with d_k and d_v denoting their respective dimensions. The self-attention output is derived as a weighted sum of the values, where each weight is determined by a compatibility function between a query and its corresponding key. The N patches serve as the inputs for the self-attention module, and the process can be formulated as:


    SA(𝒬,𝒦,𝒱)=softmax(𝒬𝒦^T/√(d_k))𝒱


The self-attention module accentuates the interdependencies among patches within the input image. According to eq.<ref>, multi-head self-attention is employed within the ViT to effectively model long-range dependencies. This configuration is elucidated as follows:


    MSA(𝒬,𝒦,𝒱)=Concat(head_1,...,head_k)𝒲^O


where head_i=SA(𝒬𝒲_i^𝒬,𝒦𝒲_i^𝒦,𝒱𝒲_i^𝒱), 𝒲_i^𝒬, 𝒲_i^𝒦, 𝒲_i^𝒱 are projections of different heads, 𝒲^O is another mapping function.



§ THE APPROACH



In this paper, we consider UDA tasks in the following setting: there exist abundant labeled data in the source domain, D^s={(_i^s,y_i^s)}_i=1^n_s with _i^s∈𝒳 and y_i^s∈𝒴, and a set of unlabeled instances in the target domain, D^t={_i^t}_i=1^n_t with _i^t∈𝒳. The data in the two domains are drawn from different distributions 𝒮 and 𝒯, but share the same label space. The main objective of UDA is to learn a model h:𝒳→𝒴 that has a good capacity of generalizing on both the source and target domains, indicating that h could yield good classification results on both D^s and D^t.




 §.§ Adversarial Domain Adaptation



The central premise of ADA is to cultivate domain-invariant features that generalize across various domains. Originating with Domain Adversarial Neural Networks (DANNs) <cit.>, adversarial learning has become prevalent for cultivating feature representations that mitigate domain disparities in UDA. Taking DANN as a prototype, it consists of three main components: a feature extractor F, a task-specific classifier C, and a binary domain discriminator D. The feature extractor F:𝒳→ℝ^m transforms an input instance  from the input space 𝒳 into a shared latent space F()∈ℝ^m. Historically, ADA methods predominantly employ CNN architecture for the feature extractor <cit.>. The classifier C:ℝ^m→𝒴 translates a feature vector from the shared latent space into the label space 𝒴. The domain discriminator D:ℝ^m→[0,1] differentiates source features (domain index 0) from target features (domain index 1) within the latent space. Through adversarial training, F is optimized to confound D, enabling DANN to acquire features transferable across domains. Concurrently, F and C are trained to minimize the classification error on labeled source data, thereby enhancing the discriminative power of the features across categories. DANN is formally described as follows:


    min_F,Cmax_Dℒ_c(F,C)+λ_dℒ_d(F,D)



    ℒ_c(F,C)=𝔼_(^s,y^s)∼𝒮ℓ(C(F(^s)),y^s)



    ℒ_d(F,D)=   𝔼_^s∼𝒮log[D(F(^s))] 
    
        +   𝔼_^t∼𝒯log[1-D(F(^t))]


Where ℓ(·,·) is the standard cross-entropy loss, and λ_d is a trade-off hyperparameter.

Conditional domain adversarial network (CDAN) exploits the classifier predictions that are believed to convey rich discriminative information to conditional adversarial training <cit.>. Specifically, it conditions the domain discriminator D by using the pseudo-labels provided by the classifier through a multilinear mapping, the conditional adversarial loss can be formulated as:


    ℒ_d(F,D)=   𝔼_^s∼𝒮log[D[F(^s)),C(F(^s)]] 
    
        +   𝔼_^t∼𝒯log[1-D[F(^t),C(F(^t))]]


where [·,·] represents the concatenation of two vectors.



 §.§ Vision Transformer-based Adversarial Domain Adaptation


To evaluate the potential of ViT in ADA, we introduce a Vision Transformer-based Adversarial Domain Adaptation (VT-ADA) approach that utilizes the standard ViT architecture <cit.> as the backbone of the feature extractor. Historically, CNN-based methods have prevailed in UDA research, achieving notable successes as highlighted by <cit.>. In our VT-ADA methodology, we substitute the conventional CNN-based feature extractor in ADA with the vanilla ViT to assess its efficacy. More specifically, we employ ViT as a feature extractor in DANN to learn domain-invariant features. To further explore the feasibility of ViT as a plug-and-play component in ADA, we also implement a variant of VT-ADA within the CDAN framework, thereby broadening the scope of our investigation into the adaptability of ViT in adversarial settings.






§ EXPERIMENTS
















 §.§ Datasets



Office-31 is a benchmark domain adaptation dataset containing images belonging to 31 classes from three domains: Amazon (A) with 2,817 images, which contains images downloaded from amazon.com, Webcam (W) with 795 images and DSLR (D) with 498 images, which contain images taken by web camera and DSLR camera with different settings, respectively. We evaluate all methods on six domain adaptation tasks: A → W, D → W, W → D, A → D, D → A, and W → A.

ImageCLEF is a benchmark dataset for ImageCLEF 2014 domain adaptation challenges, which contains 12 categories shared by three domains: Caltech-256 (C), ImageNet ILSVRC 2012 (I), and Pascal VOC 2012 (P). Each domain contains 600 images and 50 images for each category. The three domains in this dataset are of the same size, which is a good complementation of the Office-31 dataset where different domains are of different sizes. We build six domain adaptation tasks: I → P, P → I, I → C, C → I, C → P, and P → C.

Office-Home is a more challenging dataset for UDA, consisting of around 15500 images from 65 classes in office and home settings, forming four extremely distinct domains: Artistic images (Ar), Clip Art (Cl), Product images (Pr), and Real-World images (Rw). We evaluate our methods on all twelve transfer tasks: Ar → Cl, Ar → Pr, Ar → Rw, Cl → Ar, Cl → Pr, Cl → Rw, Pr → Ar, Pr → Cl, Pr → Rw, Rw → Ar, Rw → Cl, and Rw → Pr.



 §.§ Implementation Details



In this study, all experiments were conducted using the PyTorch framework. We employed the ViT-Base with a 16 × 16 input patch size (ViT-B/16) <cit.>, which was pre-trained on ImageNet-21K, as our feature extractor to learn domain-invariant features. The ViT-B/16 configuration comprises 12 transformer layers. For training, we utilized mini-batch stochastic gradient descent with a momentum of 0.9, applying a learning rate annealing strategy inspired by <cit.>. The learning rate was dynamically adjusted according to the formula η_p=η_0/(1+θ p)^β, where p represents the progression of training epochs normalized between [0,1]. The initial learning rate η_0 was set to 0.01, with θ=10 and β=0.75, parameters fine-tuned to ensure optimal convergence and minimal error in the source domain. Furthermore, the domain adaptation parameter λ_d was gradually increased from 0 to 1, using the schedule 1-exp(-δ p)/1+exp(-δ p) where δ=10, to effectively balance the influence of domain adaptation during training.



 §.§ Comparison Methods



We enhance the domain adversarial neural network (DANN) <cit.> and conditional adversarial domain adaptation (CDAN) <cit.> frameworks by integrating the ViT-B/16 as the feature extractor. Our approach is evaluated against several state-of-the-art (SOTA) methods, including DANN <cit.>, CDAN, Conditional Adversarial Domain Adaptation with Entropy Conditioning (CDAN+E) <cit.>, Adversarial Discriminative Domain Adaptation (ADDA) <cit.>, Enhanced Transport Distance (ETD) <cit.>, and Cross-Domain Transformer (CDTrans) <cit.>, to underscore the comparative effectiveness of our proposed method.



 §.§ Results



The results for Office-31 are detailed in Table <ref>, with baseline results sourced from original publications where applicable. These findings demonstrate that our VT-ADA significantly enhances the accuracy of both DANN and CDAN, delivering competitive performance relative to leading UDA methodologies. Specifically, VT-ADA boosts DANN by 8.8% and CDAN by 4.9%. Further, results for ImageCLEF and Office-Home are presented in Tables <ref> and <ref>, respectively. Notably, our VT-ADA(CDAN) variant outperforms other methodologies on these platforms. On ImageCLEF, VT-ADA enhances DANN by 1.7% and CDAN by 3.6%, while on Office-Home, the increases are 16.9% for DANN and 15.2% for CDAN, indicating substantial gains on these more rigorous benchmarks. Moreover, VT-ADA(CDAN) dominates in 4 of 6 domains on ImageCLEF and 11 of 12 domains on Office-Home, surpassing the second-place CDTrans by 4.3% on the latter, confirming the robustness of our method in challenging environments.





Visualization Analysis:  We employed the t-SNE technique to visualize the representations derived from DANN, CDAN, CDAN+E, and VT-ADA for task A → W of Office-31 dataset. The visualizations are displayed in Fig.1. Analysis of Fig.1 reveals that the feature alignments between source and target domains using DANN are suboptimal, with CDAN also demonstrating inadequate alignment. Conversely, CDAN+E shows improved alignment and enhanced classification performance. Most notably, the results from VT-ADA(CDAN) are markedly superior to those achieved with prior methods, underscoring the efficacy of employing ViT to learn more discriminative and transferable features within the context of UDA.




Convergence Analysis: We testify the convergence speed of DANN, CDAN, CDAN+E, and VT-ADA(CDAN), with the prediction accuracy on task A → W shown in Fig.2. It can be noteworthy that VT-ADA(CDAN) enjoys faster convergence speed among these methods.










§ CONCLUSION



In this study, we propose a vision transformer-based adversarial domain adaptation (VT-ADA) method. This method utilizes ViT as a feature extractor to learn domain-invariant features. We empirically demonstrate that the ViT is an easy plug-and-play module that can be applied in existing ADA methods and bring advances. Additionally, our experiments on three UDA benchmarks and extensive analysis show that ViT has the ability to boost both the transferability and discriminability of the learned domain-invariant features and enhance the convergence speed of ADA methods.





IEEEbib


